从 csv 文件中读取字符串以用作图形元数据

从 csv 文件中读取字符串以用作图形元数据

我遇到了以下问题,我使用 pgfplots(table) 从生成的数据中生成我的 latex 文档中的图表和表格。我想从元数据文件中读取其他信息。原因是数据取决于生成它的架构,我想将其写入标题中。因此,我想将标题以及其他元信息(如轴标签)存储在每个图表/表格的 csv 文件中,并在生成文档时读取它。我发现 datatool、xstring 和其他一些可能有用的包,但无法找到实现我尝试做的事情的方法。

提前感谢你的帮助!

答案1

您可以从文本文件中读取行,方法是先read使用 创建对象\newread,使用 打开文件\openin,然后使用 将各行读入宏中\read。然后,您可以在图表或标题中重复使用文件的内容:

\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}

相关内容