Insert values from a file in a latex document

这一生的挚爱 提交于 2019-12-08 00:33:40

问题


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.


回答1:


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}



回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!