2

I'm trying to draw some figures with externally generated data. I'd like to draw a simple line inside a tikzpicture using pgfplots like so:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\centering
\begin{tikzpicture}[domain=0:10]
    \begin{axis}
        \addplot[blue] (x, 5 * x + 7);
    \end{axis}
\end{tikzpicture}
\end{document}

but I'd like to define the plotted equation in a separate file. This does not work:

\addplot[blue] (x, \input{math.txt});

(with math.txt containing simply 5 * x + 7). Neither does this:

\addplot[blue] (x, \protected\input{math.txt});

or this:

\addplot[blue] \input{math.txt}

(with math.txt containing (x, 5 * x + 7);).

In all cases, I get an error like

! Argument of \pgfplotsforeachungrouped@ has an extra }.

which isn't particularly instructive to me in this context.

Does anybody know how to accomplish this short of wrapping the whole thing in Perl or something? Apparently the input command isn't quite as low level as I thought.

In case it matters, I'm on a Windows 7 machine running MiKTeX 2.9.

1

You can use the approach from How do I create a macro which reads the content of a file when the macro is defined? to store the file contents in a macro, which can then be used in the \addplot command:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{catchfile}

\begin{filecontents*}{math.txt}
5 * x + 7
\end{filecontents*}


\begin{document}

\CatchFileDef{\foo}{math.txt}{}

\centering
\begin{tikzpicture}[domain=0:10]
    \begin{axis}
        \addplot[blue] (x, \foo);
    \end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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