Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
2  
Why can't you just write \foreach \subfigure in {64,61,41} and use page=\subfigure? –  Torbjørn T. Jun 14 at 16:52
    
Side note: Using \subfigure as a loop "variable" might be dangerous. –  Jubobs Jun 14 at 17:06
    
Just edited the MWE to remove typos and wrong tikzpicture env –  Bordaigorl Jun 14 at 18:58
    
What about avoiding using arrays and using lists instead? \foreach[count=\i] \pg in {64,61,41} {Subfigure~\i, Page~\pg: \includegraphics[page=\pg]{mysubfigures} } If you elaborate on why you want to use arrays we could give you more help –  Bordaigorl Jun 14 at 19:04

1 Answer 1

up vote 5 down vote accepted

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}
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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