文本中的引用表值

文本中的引用表值

我希望能够在我的文本中引用表格值(这是因为我经常更新表格,然后在文本中列出具体值)。以下是我将使用的示例表:

% Example Table
\documentclass{minimal}
\begin{filecontents*}{scientists.csv}
name,surname,age
Albert,Einstein,133
Marie,Curie,145
Thomas,Edison,165
\end{filecontents*}

% Read in Table


  \documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstabletypeset[
    col sep=comma,
    string type,
    columns/name/.style={column name=Name, column type={|l}},
    columns/surname/.style={column name=Surname, column type={|l}},
    columns/age/.style={column name=Age, column type={|c|}},
    every head row/.style={before row=\hline,after row=\hline},
    every last row/.style={after row=\hline},
    ]{scientists.csv}
\end{document}

我可能希望能够通过引用他/她的名字来引用文中给定科学家的年龄(即:)

Albert Einstein is \ref{albert} years old.  

理想情况下,这仍将使用 pgfplotstable,因为这是我目前在许多表格中读取的方式。谢谢,

答案1

另一种无需修改数据的替代方法是通过\pgfplotstablegetelem...\pgfplotsretval对。请注意,行索引从 0 开始,而不是从 1 开始。

\documentclass{article}
\usepackage{pgfplotstable}
\begin{filecontents*}{scientists.csv}
name,surname,age
Albert,Einstein,133
Marie,Curie,145
Thomas,Edison,165
\end{filecontents*}

\pgfplotstableread[col sep=comma]{scientists.csv}\mytable
\def\getcell#1#2#3{
\pgfplotstablegetelem{#1}{#2}\of{#3}\pgfplotsretval%
}
\begin{document}
\pgfplotstabletypeset[
    string type,
    columns/name/.style={column name=Name, column type={|l}},
    columns/surname/.style={column name=Surname, column type={|l}},
    columns/age/.style={column name=Age, column type={|c|}},
    every head row/.style={before row=\hline,after row=\hline},
    every last row/.style={after row=\hline},
    ]\mytable

\bigskip

\getcell{0}{name}{\mytable} \getcell{0}{surname}{\mytable} is \getcell{0}{age}{\mytable} 
years old. \getcell{1}{name}{\mytable} \getcell{1}{surname}{\mytable} is 
\getcell{1}{age}{\mytable} years old. But \getcell{2}{name}{\mytable} 
\getcell{2}{surname}{\mytable} is still older, he is \getcell{2}{age}{\mytable} years old.

\end{document}

在此处输入图片描述

答案2

如果可以选择适当准备 csv 文件,请尝试一下(这里我将文件内容和表格捆绑在一个 latex 源中)

\documentclass{article}

\makeatletter
\def\printandsetlabel#1#2#3{#2\setcounter{#1}{#2}%
 \protected@edef\@currentlabel
 {\csname p@#1\endcsname\csname the#1\endcsname}%
 \label{#3}}
\makeatother

\newcounter{age}
\newcommand*{\age}[2]{\printandsetlabel{age}{#1}{#2}}

\begin{filecontents*}{scientists.csv}
name,surname,age
Albert,Einstein,\age{133}{albert}
Marie,Curie,\age{145}{marie}
Thomas,Edison,\age{165}{thomas}
\end{filecontents*}

% Read in Table

\usepackage{pgfplotstable}
\pgfplotsset{compat=1.7}

\begin{document}\thispagestyle{empty}\hsize8cm
\pgfplotstabletypeset[
    col sep=comma,
    string type,
    columns/name/.style={column name=Name, column type={|l}},
    columns/surname/.style={column name=Surname, column type={|l}},
    columns/age/.style={column name=Age, column type={|c|}},
    every head row/.style={before row=\hline,after row=\hline},
    every last row/.style={after row=\hline},
    ]{scientists.csv}

\bigskip
Albert Einstein is \ref{albert} years old and Marie Curie is \ref{marie} years
old. But Thomas Edison is still older, he is \ref{thomas} years old.
\end{document}

代码结果

相关内容