2

I have a tikz code using values stored in a 1D array, here is a MWE :

\documentclass{standalone}
\usepackage{tikz}

\def \myArray {{0,1,2,3}}

\begin{document}
    \begin{tikzpicture}

        \pgfmathsetmacro\x{\myArray[0]}
        \draw (0,0) node {\x};

    \end{tikzpicture}
\end{document}

So this one works OK for my complex code, but I could really improve it using 2D arrays and loops. Here is a Minimum Not Working Example of what it could look like :

\documentclass{standalone}
\usepackage{tikz}

\def \myArray {{0,1}{2,3}}

\begin{document}
    \begin{tikzpicture}

        \pgfmathsetmacro\x{\myArray[1][1]}
        \draw (0,0) node {\x};

    \end{tikzpicture}
\end{document}

with this code, I'm expecting to have the value "3" displayed (2nd line, 2nd column).

Is there a way to do 2D array storage and indexing with latex/tikz ?

4

If you are keen on a change of syntax, with expl3 it can be easily done:

\documentclass{standalone}
\usepackage{tikz,xparse}

\ExplSyntaxOn
\NewDocumentCommand{\definearray}{mm}
 {
  \seq_gclear_new:c { g_thomas_array_#1_seq }
  \seq_set_split:cnn { g_thomas_array_#1_seq } { ; } { #2 }
 }
\DeclareExpandableDocumentCommand{\retrieve}{mmm}
 {
  \clist_item:fn { \seq_item:cn { g_thomas_array_#1_seq } { #2 + 1 } } { #3 + 1 }
 }
\cs_generate_variant:Nn \seq_set_split:Nnn { c }
\cs_generate_variant:Nn \clist_item:nn { f }
\ExplSyntaxOff

\definearray{myarray}{0,1;2,3}

\begin{document}

\begin{tikzpicture}
\draw (0,0) node {\retrieve{myarray}{1}{1}};
\end{tikzpicture}

\end{document}

The two +1 are for starting indexing at 0, as usual for PGF arrays (expl3 sequences and comma separated lists start at 1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.