[Tex/LaTex] Use array in loop in tikz

arraysloopstikz-pgf

In general: I need to retrieve values from an array to submit those values as indices to a function.

More specific: I want to create figures, which contain subfigures. These subfigures are loaded from mysubfigures.pdf. The order of the subfigures is saved in a seperate list ggg. I made a minimal example below.

This is the part that doesn't work and where I need your help: page=\ggg(\subfigure)

I want it to be like page=64 when \subfigureis1, page=61 when \subfigureis2, and so on.

(I have looked at some similar questions on stackexchange but I don't understand the answers.)

\documentclass[class=minimal,border=0pt,tikz]{standalone}
\usepackage{tikz}
\usepackage{arrayjob}

\begin{document}

\newarray\ggg
\readarray{ggg}{64&61&41}
\ggg(1)

    \foreach \subfig in {1,2,3}
    {
        %This is Figure \subfig
        \includegraphics[page=\ggg(\subfig)]{./mysubfigures.pdf};
    }

\end{document}

Best Answer

The problem here is that arrayjob does some nasty stuff to get the value out of the array and this is not compatible with how \includegraphics handles its arguments.

There are two solutions:

1. expand and then use

\documentclass[class=minimal,border=0pt,tikz]{standalone}
\usepackage{tikz}
\usepackage{arrayjob}

\begin{document}

    \newarray\ggg
    \readarray{ggg}{64&61&41}
    \ggg(1)

    \foreach \subfig in {1,2,3}
    {
        %This is Figure \subfig
        \checkggg(\subfig)
        \includegraphics[page=\cachedata]{mysubfigures};
    }
\end{document}

Here \checkARRAYNAME(INDEX) will save the value in the macro \cachedata so we can use it in the arguments normally since that will not need special treatment to rewrite to the value.

2. use pgfmath's arrays

Since you are using tikz the package pgfmath is already loaded and it is able to understand arrays. Simply do:

\def\ggg{{64,61,41}} % <- your array is just a list    

    \foreach \i in {0,1,2} % 0-based indexes
    {
        %This is Figure \i
        \pgfmathsetmacro{\pg}{\ggg[\i]} % <- array notation ARRAY[INDEX]
        \includegraphics[page=\pg]{mysubfigures}
    }

A third way

Unless you really need arrays somewhere else in your document, you do not need them here as you can just exploit the excellent \foreach syntax:

\foreach[count=\i] \pg in {64,61,41} {
    Subfigure~\i, Page~\pg:
    \includegraphics[page=\pg]{mysubfigures}
}
Related Question