If a have a latex document, how can I read some key-value pairs from a file an insert them into the document?
Something like this:
Latex code:
customer ID: ${customerID}
Text file:
customerID=123456
And the resulting .pdf
file should contain the customer ID.
We can always write a perl script to expand them...
defs.txt:
customerID=123456
customerTel=22530000000
doc.tex:
\documentclass{article}
\begin{document}
latex
customer ID: ${customerID}
and ${address}
costum telphone ID: ${customerTel}
\end{document}
perl script tex-defs:
#!/usr/bin/perl -n
BEGIN{$tex=0;}
if(not $tex and /^(\w+)=(.*)/) { $v{$1}=$2 }
if(/\\documentclass\{/ ) { $tex=1 }
if($tex) { s/\$\{(\w+)\}/$v{$1} || "???$1"/ge; print }
testing (after chmod...):
$ tex-defs defs.txt doc.tex
\documentclass{article}
\begin{document}
latex
customer ID: 123456
and ???address
costum telphone ID: 22530000000
\end{document}
If you have some data (for instance your customerID) in a file that is saved to say 'data/foo.dat', then you could use that value in your .tex-file like this:
This is the customerID: \input{data/foo.dat}.
Then the pdf would show
This is the customerID: 123456.
The command \input just inserts whatever is inside the file you provide. I'm not quite sure about how to get the key-value pairs working, but maybe you could make it so that the values you need are stored in files with the "key" as the filename. If you place these files in some subfolder it would not clutter your working directory, and may be just as good a place as any to store your data.
来源:https://stackoverflow.com/questions/29078107/insert-values-from-a-file-in-a-latex-document