Insert values from a file in a latex document

淺唱寂寞╮ 提交于 2019-12-06 09:21:31

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.

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