Passing Command Line Arguments to a LaTeX Document
Some time ago, someone asked me if I knew a way to pass command line arguments to LaTeX documents. My first response was that this ought to be impossible; being a compiler, LaTeX cannot in any way alter it’s input files. But command line parameters can alter the output without touching the input of course.
This has kept me thinking for a while, and at the end I came up with some kind of a solution.
Let’s take a simple LaTeX file:
\documentclass{article}
\begin{document}
\someinput{}
\end{document}
and call it test.tex.
We will define the output of \someinput{} as a command line parameter at LaTeX invocation, as follows:
echo '\\newcommand{\\someinput}{hello world} \\input test' | latex
Result: test.dvi containing the text “hello world”. Admitted, it’s not the easiest solution, but it does make LaTeX useful inside simple batch jobs!
For more advanced LaTeX templating, I advise using Perl and Text::Template.