[texhax] macro expansion trouble

Neal H. Walfield neal at walfield.org
Sat Aug 4 16:09:31 CEST 2012


Hello,

I have written a program that does some data processing and then
generates a plot using pgfplots.  I want to include the plot from a
LaTeX document, but I don't want to embed parts of my document in my
data analysis program, such as, how wide the plot should be.

I decided to add a hook (in this case, called \Arg) to the generated
plot, which I could set from my document:

  \begin{axis}[\Arg]
    \addplot[only marks, mark=o] coordinates {
      (3.000000, 1469.000000)
       (4.000000, 461.000000)
    };
  \end{axis}

And then define a macro that sets the hook variable and \input's the
plot commands:

  \newcommand{\includepgfplot}[2][]{{\def\Arg{#1}\input{#2}}}

In my document, I use the macro as follows:

  \begin{tikzpicture}
  \includepgfplot[width=0.5\textwidth]{/tmp/plot}
  \end{tikzpicture}

When I process this with LaTeX, I get the following error:

  ! Missing \endcsname inserted.
  <to be read again> 
                     \textwidth 
  l.1 \begin{axis}[\Arg]
                        
  ? 

The generated pdf is actually correct (when I use \nonstopmode to
ignore errors), but I want to figure out what the problem is.  The
following is a minimal working example:

  \documentclass{article}
  \usepackage{pgfplots} 
  
  \newcommand{\includepgfplot}[2][]{{\def\Arg{#1}\input{#2}}}
  
  \begin{document}
  
  \begin{tikzpicture}
  \includepgfplot[width=0.5\textwidth]{/tmp/plot}
  \end{tikzpicture}
  
  \end{document}

To process the example, use pdflatex and save the above plot data
(from \begin{axis} to \end{axis}) as /tmp/plot.tex.

Any ideas what I am doing wrong?

Thanks,

Neal


More information about the texhax mailing list