I am trying to create an environment in which I can place several georeferenced images into a PGFPlots axis.

I have working code that reads in the specified image, reads in the world file (a text file that specifies the bounding box of the image in real-world coordinates) and then saves everything in a pgfplotstable. All of this works as expected.

The problem occurs when I try to draw the images. The \foreach loop I use reads the table row-by-row, assigning the graphics limits defined in the table, and then it is supposed to include the graphics file listed in the first column of the table. What happens is that the last image in the table is being used for each step in the loop and earlier images are not drawn.

What I am trying to get is something like this: enter image description here

What I end up with is this: enter image description here

The three images are positioned correctly - its that the wrong image file is used for all of the steps prior to the last one. I've tried used \let\imageName\pgfplotsretval in the \ExtractRow macro but get an undefined control sequence error when compiling. The only way I can get it to compile is with \xdef\imageName\pgfplotsretval.

MWE below:







\pgfplotstableread[col sep=comma,header=false]{


\foreach \i in {0,...,\RowCount}{
    \addplot graphics[xmin=\xmin,ymin=\ymin,xmax=\xmax,ymax=\ymax]{\imageName};

image1: image1.png

image2: image2.png

image3: image3.png

1 Answer 1


There might be some expanding issue regarding \foreach and \addplot. In my humble opinion, \addplot will read the file name without expanding it. So the three images being included are not image1, image2, and image3, instead, they are \imageName, \imageName, and \imageName. When PGFPLOTS tries to include the images, the three \imageName tokens will all expanded to the latest value: image3, which leads to the result.

To get over this kind of issue, PGF-family usually define a \pgf@marshal so that one can use a single \expandafter to jump over it. In this case, you may do the following:

    \foreach \i in {0,...,\RowCount}{
        \def\pgfplotsmarshal{\addplot graphics[xmin=\xmin,ymin=\ymin,xmax=\xmax,ymax=\ymax]}
  • pgfplots has \pgfplotsinvokeforeach for this purpose.
    – percusse
    Jan 22, 2016 at 8:02
  • @percusse Sounds interesting. Throughout all packages there is only one occurrence of this macro: where it is defined. Anyway, what does invoke mean in this case?
    – Symbol 1
    Jan 22, 2016 at 14:50
  • Pgfplots gathers all the plots to decide the axis size and ticks and lots of other stuff. Hence they are not expanded right away and stored as you mention. Invoke means that whatever the loop variable is it will be replaced with its original meaning and then stored. It should be in the pgfplotsutils file somewhere.
    – percusse
    Jan 22, 2016 at 14:55
  • @percusse I had tried using a \pgfplotsinvokeforeach for this look previously and could not get it to work.
    – andy
    Jan 22, 2016 at 15:00
  • Changing the loop to \pgfplotsinvokeforeach {0,...,\RowCount}{ \ExtractRow\GeoTable{#1} \addplot graphics[xmin=\xmin,ymin=\ymin,xmax=\xmax,ymax=\ymax]{\imageName}; } compiles but gives me the same result of the last image drawn three times.I think I understand that \pgfplotsinvokeforeach is replacing the index at each step and not the macros defined in \ExtractRow. Any idea how I can get this to work since I need to replace five values:\xmin,\xmax, etc. and not just \imageName in each loop step?
    – andy
    Jan 22, 2016 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .