4

I have a strange problem with pgfplots and using a macro to return a string to a file to be included.

MWE Code:

\documentclass[a4paper,10pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\newenvironment{type}[1]{%
    \newcommand\getFile[1]{%
        directory1/#1/##1}
}{}

\begin{document}
\begin{type}{directory2}
    File: 
    \begin{tikzpicture}[baseline]
        \begin{groupplot}[
                group style={
                    group size=2 by 1,
                },
            ]
            \nextgroupplot
            \addplot graphics[
                xmin=0,
                xmax=100,
                ymin=0,
                ymax=100,
            ] {directory1/directory2/test_1.eps};
            \nextgroupplot
            \addplot graphics[
                xmin=0,
                xmax=100,
                ymin=0,
                ymax=100,
            ] {\getFile{test_1.eps}};
        \end{groupplot}    
    \end{tikzpicture}
\end{type}
\end{document}

Error:

LaTeX Error: File `directory1/directory2/test_1.eps' not found.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.33         \end{groupplot}

Comment out the second plot and the first plot works fine, so the file is there, the directories can be found and pgfplots can include the file fine. Include he second with the macro returning the correct file (it says so in the error) and it can't find it.

1 Answers1

4

The problem appears to be an expansion issue in the interaction of pgfplots and \includegraphics.

It can be reduced to the following minimal working example:

\documentclass{standalone}
\usepackage{graphics}

\newenvironment{type}[1]{%
    \newcommand\getFile[1]{%
        directory1/#1/##1}
}{}

\begin{document}
\begin{type}{directory2}
    File: 
    \def\TEMP{\getFile{P.pdf}}
    \includegraphics{\TEMP}
\end{type}
\end{document}

This is precisely what pgfplots feeds to \includegraphics -- and it fails. Apparently, \includegraphics fails to expand the macros correctly. I digged into it and realized that it handles one level of expansion, but not two.

However, it handles the expansion when it automatically appends file extensions -- the following example works well:

\documentclass{standalone}
\usepackage{graphics}

\newenvironment{type}[1]{%
    \newcommand\getFile[1]{%
        directory1/#1/##1}
}{}

\begin{document}
\begin{type}{directory2}
    File: 
    \def\TEMP{\getFile{P}}
    \includegraphics{\TEMP}
\end{type}
\end{document}

Thus, the solution for you is: use \getFile{test_1} instead of \getFile{test_1.eps}.

\documentclass[a4paper,10pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\newenvironment{type}[1]{%
    \newcommand\getFile[1]{%
        directory1/#1/##1}
}{}

\begin{document}
\begin{type}{directory2}
    File: 
    \begin{tikzpicture}[baseline]
        \begin{groupplot}[
                group style={
                    group size=2 by 1,
                },
            ]
            \nextgroupplot
            \addplot graphics[
                xmin=0,
                xmax=100,
                ymin=0,
                ymax=100,
            ] {directory1/directory2/test_1.eps};
            \nextgroupplot
            \addplot graphics[
                xmin=0,
                xmax=100,
                ymin=0,
                ymax=100,
            ] {\getFile{test_1}};
        \end{groupplot}    
    \end{tikzpicture}
\end{type}
\end{document}

I will modify the codebase of pgfplots such that it eliminates the unnecessary temp variable.

  • I don't understand: you're adding a level: if two steps of expansion aren't performed, neither will three. Perhaps you're thinking to \edef\TEMP{\getFile{P}}. – egreg Nov 17 '15 at 20:54
  • @egreg I agree with your computation regarding the "2 vs 3 argument". I have been somewhat imprecise in my answer: the point why \def\TEMP{\getFile{P}} works although \def\TEMP{\getFile{P.pdf}} does not is because the first one runs through an entirely different code segment of \includegraphics (namely through the "I try to append a suitable extension" loop). This "different code segment" supports an arbitrary number of expansion steps and happens to work together with the OP's minimal working example if the extension is omitted. – Christian Feuersänger Nov 17 '15 at 21:04
  • I have modified pgfplots such that it does no longer add the unnecessary level of expansion (will become part of 1.13). – Christian Feuersänger Nov 17 '15 at 21:04
  • Since the question is about \addplot graphics, it would be better to add the example also for that situation. – egreg Nov 17 '15 at 21:06
  • Interesting. Is there a way to force LaTeX to expand the argument to \addplot graphics before expanding the macro itself? I tried to add a ridiculous number of \expandafters, but it did not work. – jarauh Nov 18 '15 at 18:24
  • Yes, there is a way (using \edef, for example). However, I would recommend to use my workaround rather than expansion control. If you want to learn more about expansion control, you may want to read http://tex.stackexchange.com/questions/12668/where-do-i-start-latex-programming/27589#27589 – Christian Feuersänger Nov 18 '15 at 19:40