Graphics, Figures & TablesTikZ: How can I use a matrix to define matrix elements?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

TikZ: How can I use a matrix to define matrix elements?

Post by topsquark »

This is a bit crazy. Here's my code:

Code: Select all

\usetikzlibrary{matrix}
\begin{tikzpicture}
     \def\entry{{{First,Second},{Third,Fourth}}}
     \matrix[matrix of nodes] {
          {\entry[0][0]} & {\entry[0][1]} \\
          {\entry[1][0]} & {\entry[1][1]} \\
     };
\end{tikzpicture}
I'm trying to get the output
First Second
Third Fourth

but it prints out the whole matrix entry then [0][1] at each box.

Bah! This is simple. What am I doing wrong?

Thanks!

-Dan

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

TikZ: How can I use a matrix to define matrix elements?

Post by Bartman »

Read section "94.2 Syntax for Mathematical Expressions: Operators" on how to evaluate the array-like structure of your \entry command's definition.

Code: Select all

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
  \newcommand{\print}[1]{\pgfmathparse{#1}\pgfmathresult}
  \newcommand{\entry}{{{"First","Second"},{"Third","Fourth"}}}
  \matrix[
    matrix of nodes,
    execute at begin cell=\print
  ] {
    {\entry[0][0]} & {\entry[0][1]} \\
    {\entry[1][0]} & {\entry[1][1]} \\
  };
\end{tikzpicture}
\end{document}
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

TikZ: How can I use a matrix to define matrix elements?

Post by topsquark »

Bartman wrote:Read section "94.2 Syntax for Mathematical Expressions: Operators" on how to evaluate the array-like structure of your \entry command's definition.

Code: Select all

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
  \newcommand{\print}[1]{\pgfmathparse{#1}\pgfmathresult}
  \newcommand{\entry}{{{"First","Second"},{"Third","Fourth"}}}
  \matrix[
    matrix of nodes,
    execute at begin cell=\print
  ] {
    {\entry[0][0]} & {\entry[0][1]} \\
    {\entry[1][0]} & {\entry[1][1]} \\
  };
\end{tikzpicture}
\end{document}
Thank you! A bit less obvious than I thought it would be, but obvious enough once I saw what the problem was.

Yes, I finally figured out today that the contents of {..} is considered text, no matter that a variable {\x} might look like a number. I haven't really spent much time with the pgf codes. My loss, apparently.

I have two more questions if you don't mind.
1) I can make this work with \def\entry{{{..}}}. Is there an advantage in using \newcommand?

2) "execute at begin cell" is a neat trick. I'll have to remember that. I would like you to check my understanding of how that works. It tells the matrix operation to run \print on, say, the \entry[0][0] element and returns the number to the 0,0 position in the matrix, inside the { }, where it can be processed properly. This avoids the problem of trying to do something like {\print(\entry[0][0])}, which just repeats my initial mistake and shouldn't work. (I apologize for the likely bad terminology, clearly I'm a noob programmer.)

Thanks for the help!

-Dan
Post Reply