1

I want to quickly swap out different values in a tex file - like for a batch print so far I'm using multiple .tex files and swap out the \input examplefile.tex in my header.

examplefile.tex:

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


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

and in my mainfile

\input example.txt

Display my Var \Cat1Val1
...

It works perfect but I dislike using multiple .tex files which are hard to update. I would like to use a csv and get the values out of a specific row like:

% 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
  • Have you checked the csvsimple package? It probably has what you need. – Alain Merigot Jan 15 '20 at 18:35
2

You can set your variables in a CSV that is readable using datatool. Then you can define a macro (like \printval below) that reads a value based on some input.

enter image description here

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

While LaTeX is not a database tool, this approach is flexible enough even for large documents.

Your Answer

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