Passing command-line arguments to LaTeX document
Solution 1:
That is, can I pass latex some command-line arguments so that I can choose which style to use based on that argument?
Yes. Three options:
One
In your source file, write
\providecommand{\comment}[1]{\emph{#1}}% fallback definition
and then compile the LaTeX document ("myfile.tex") as
pdflatex (whatever options you need) "\newcommand\comment[1]{\textbf{#1}}\input{myfile}"
Two
Alternatively,
pdflatex "\let\ifmyflag\iftrue\input{myfile}"
and then have in the source
\ifcsname ifmyflag\endcsname\else
\expandafter\let\csname ifmyflag\expandafter\endcsname
\csname iffalse\endcsname
\fi
...
\ifmyflag
\newcommand\comment[1]{\emph{#1}}
\else
\newcommand\comment[1]{\textbf{#1}}
\fi
Three
Or even
pdflatex "\def\myflag{}\input{myfile}"
with
\ifdefined\myflag
\newcommand\comment[1]{\emph{#1}}
\else
\newcommand\comment[1]{\textbf{#1}}
\fi
which is probably the shortest, albeit slightly fragile because you never know when a package might define \myflag
behind your back.
Solution 2:
You should use Will's approaches when you need fairly flexible one-off options, like say changing the position line on your resume. If otoh you are producing the same selection of options over & over, then you should consider avoiding command line arguments, or working them into a build script or makefile.
I'll give two techniques for avoiding command line arguments :
Trick 1: If you're producing a fixed array of documents that must remain accessible, like your two styles example, then I'd recommend simply implementing Will's latex code inside another tex file, i.e. thesis.tex contains a \providecommand\comment[1]{\emph{#1}}
and thesis-ugly.tex consists of \newcommand\comment[1]{\textbf{#1}} \input thesis.tex
.
You must of course rerun tools like bibtex when using this technique, unless you symlink the intermediary files, ala ln -s thesis.aux thesis-ugly.aux
and ln -s thesis.bbl thesis-ugly.bbl
.
Trick 2: I found trick 1 awkward for changing document papersizes, so I wrote the following perl script, called simply papersize. The command papersize A4 teaching.tex
modifies teaching.tex in place, and symlinks teaching.pdf to teaching-A4.pdf, so that running pdflatex teaching
creates teaching-A4.pdf, but does not disturb the pre-existing teaching-letter.pdf and does not require rerunning bibtex teaching
. It does obviously require rerunning pdflatex twice for documents with internal references.
#!/usr/bin/perl -i~ -n
BEGIN {
die "Usage: papersize letter/A4/etc. [filename]\n" if ($#ARGV < 0);
$SIZE = shift @ARGV; @files=@ARGV;
$FLAG = "% paper size :: ";
}
if (/$FLAG(\w+)/) {
if ($1 eq $SIZE) {
s/^\% //;
} else {
s/^([^\%])/\% \1/;
}
}
print $_;
END {
foreach (@files) {
if (s/\.tex//) {
$l = "$_-$SIZE.pdf"; $_ .= ".pdf";
unlink($_) if (-l $_);
symlink($l,$_) if (! -e $_);
} }
}
You must add the special comments % paper size :: ...
to every file line that should be changed when you change the paper size.
\documentclass[letterpaper,11pt]{article} % paper size :: letter
% \documentclass[a4paper,11pt]{article} % paper size :: A4
\usepackage[text={6.5in,8.8in}]{geometry} % paper size :: letter
% \usepackage[text={16.4cm,24.5cm}]{geometry} % paper size :: A4
You could obviously work papersize into a build script or makefile too or modify the above script for .dvi files.. or generalize the script to other modifications.
Solution 3:
This may not be the optimal solution for the situation in question (also a very old post), but I thought I'd present an alternative solution which was useful for me. It is simply to define the two commands in two different files, link a third file to the desired choice, and \input that third file. For example, define:
% in main.tex
\input{options}
% ...
.
% in option1.tex
\newcommand{\comment}[1]{\textbf{#1}}
.
% in option2.tex
\newcommand{\comment}[1]{\emph{#1}}
Then to compile with option1, simply run:
> ln -s option1.tex options.tex && pdflatex main.tex
I prefer this because I am more comfortable with bash than obscure latex, have no problem with a proliferation of files (these can easily be organized in subdirectories), and have scripts compile my document anyway so this additional step comes at little cost.