2

I got the following problem, I use pgfplots(table) to generate plots and tables in my latex document from generated data. I would like to read additional information from a metadata file. The reason for that, is that the data depends on the architecture it was generated on, which I would like to write into the caption. Therefore I would like to store a caption, as well as other metainfo (like axis labels) in a csv file for each plot/table and read it when generating my document. I found datatool, xstring and some other packages which might be useful but could not figure out a way to achieve what I try to do.

Thanks in advance for any help!

  • Do you want to store the meta info in the same file as your data, or in a separate file? Are you completely flexible about the formatting of the data? – Jake Mar 11 '13 at 12:49
  • @Jake I'm completely flexible about formatting the data since I'm writing the script generating the data myself. Of course I'm flexible using one or multiple files but I would prefer one for the data and one for the meta data. – soriak Mar 11 '13 at 13:00
5

You can read lines from a text file by first creating a read object using \newread, opening the file using \openin and then reading the individual lines into macros using \read. Then you can reuse the contents of the file in the plots or the caption:

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

\begin{filecontents*}{meta1.txt}
The first of a series of plots.
X1
Y1
\end{filecontents*}
\begin{filecontents*}{meta2.txt}
The second of a series of plots.
X2
Y2
\end{filecontents*}
\newread\myread   
\newcommand{\readmetadata}[1]{
    \openin\myread=#1
    \read\myread to \plotcaption
    \read\myread to \plotxlabel
    \read\myread to \plotylabel
    \closein\myread
}


\begin{document}

\readmetadata{meta1.txt}
\begin{figure}\centering
\begin{tikzpicture}[trim axis left]
\begin{axis}[
    xlabel=\plotxlabel,
    ylabel=\plotylabel
]
\addplot {rnd};
\end{axis}
\end{tikzpicture}
\caption{\protect\plotcaption}
\end{figure}

\readmetadata{meta2.txt}
\begin{figure}\centering
\begin{tikzpicture}[trim axis left]
\begin{axis}[
    xlabel=\plotxlabel,
    ylabel=\plotylabel
]
\addplot {rnd};
\end{axis}
\end{tikzpicture}
\caption{\protect\plotcaption}
\end{figure}
\end{document}
  • wow that's a very nice solution! Thanks that completes my automated tool-chain for updating my results in my document. Very helpful! Thanks again! – soriak Mar 11 '13 at 14:28
  • found out that your solution requires one latex read per included file (had no reason until now to think about it, but yesterday I had no room for a new read). I had a short look and by moving \newread\myread before the command it requires only one read. Please don't consider this to be criticism, I only wanted to share my experience. Thanks again for your help! – soriak Mar 23 '13 at 15:19
  • Please, put the \newread outside the definition of \readmetadata: the input stream can be reused, provided you close it after usage. So also a \closein instruction should appear in the definition. – egreg Apr 20 '13 at 23:38
  • @egreg: Like so? – Jake Apr 21 '13 at 11:52
  • @Jake Yes, that's the idea. – egreg Apr 21 '13 at 12:14

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.