tags:

views:

969

answers:

3

Sometimes, I define new commands such as the following.

\newcommand{\comment}[1]{\textbf{#1}}
%\necommand{\comment}[1]{\emph{#1}}

The above commands enable me to change the style of parts of my code all at once. If I want to generate both of the possible styles, I have to compile my LaTeX document two times each time modifying the source code to enable the desired style.

Is there a way to avoid the source code modification in such cases? That is, can I pass latex some command-line arguments so that I can choose which style to use based on that argument?

A: 

I do not think so as latex does not know about command-line arguments.

So you need another layer, e.g. a script catching your argument and then doing textual substitution in the document. Similar to how configure modifies Makefile.in into Makefile.

You can probably do that with any decent scripting language as they all have command-line modules.

Dirk Eddelbuettel
+6  A: 

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.

Will Robertson
Can you do similar tricks with "latex" or these are specific to "pdflatex"?
reprogrammer
Should work fine with latex, too.
godbyk
A: 

To provide my dissertation in both the required, ugly, tree wasting format, and a compact prettier version, I used ifthen an a kludge of make and sed that rewrote a bit of the header.

I think Will's approaches are all nicer.

dmckee