Search code examples
pdfpdf-generationlatexpdflatex

Insert values from a file in a latex document


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.


Solution

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