LaTeX:从 cvs 行中获取变量

LaTeX:从 cvs 行中获取变量

我想快速交换 tex 文件中的不同值 - 就像批量打印一样,到目前为止,我正在使用多个 .tex 文件并交换\input examplefile.tex标题中的值。

示例文件.tex:

\newcommand\CombineValue1{\Cat1Val1 \\ \Cat1Val2}
\newcommand\CombineValue2{\Cat3Val1 ~ \Cat1Val3}


\newcommand\Cat1Val1{Somevalue}
\newcommand\Cat1Val2{Somevalue b}
\newcommand\Cat2Val1{Somevalue c}
\newcommand\Cat3Val1{Morevalues}
....

在我的主文件中

\input example.txt

Display my Var \Cat1Val1
...

它运行完美,但我不喜欢使用难以更新的多个 .tex 文件。我想使用 csv 并从特定行中获取值,例如:

% Should be the name written in column 1; like a 'key = values' table/dict.
% The numeric value of the row would be fine for a start as well
% Defined in header
\userow{my.csv}{keyname of the row} 

%and then used in the document like:
\InsertColum{2} to get the value in the 2nd colum of the specific row from my.csv

答案1

您可以在 CSV 中设置变量,可以使用以下方式读取datatool。然后您可以定义一个宏(如下\printval所示),根据某些输入读取一个值。

在此处输入图片描述

\documentclass{article}

\usepackage{datatool}

% To make this into a complete, minimal example, create the following files.
% In your use-case, you would create these in Excel/outside of LaTeX, most likely.
\begin{filecontents*}[overwrite]{variable_list1.csv}
variable,value
cat1val1,1SomeValue1
cat1val2,1SomeValue2
cat2val1,1SomeValue3
cat3val2,1SomeValue4
\end{filecontents*}
\begin{filecontents*}[overwrite]{variable_list2.csv}
variable,value
cat1val1,2SomeValue1
cat1val2,2SomeValue2
cat2val1,2SomeValue3
cat3val2,2SomeValue4
\end{filecontents*}

\newcommand{\printval}[1]{%
  \DTLfetch
    {variablelist}% <db name>
    {variable}% <column 1 name>
    {#1}% <column 1 value>
    {value}% <column 2 name>
}

\begin{document}

\DTLloaddb
  {variablelist}% <db name>
  {variable_list1.csv}% <filename>

See \printval{cat1val2} and \printval{cat1val1}, together with \printval{cat3val2} and \printval{cat2val1}.

\DTLdeletedb{variablelist}% Clear database so it can be overwritten (just for this example)
\DTLloaddb
  {variablelist}% <db name>
  {variable_list2.csv}% <filename>

See \printval{cat1val2} and \printval{cat1val1}, together with \printval{cat3val2} and \printval{cat2val1}.

\end{document}

虽然 LaTeX 不是数据库工具,但这种方法即使对于大型文档也足够灵活。

相关内容