0

I use Beamer presentations in workshops where I set questions, have students work on the answers, and then display some sample answers. I give the students a version of the presentation without the answers, so they can read back through the presentation as needed.

It seems the usual way of doing such conditional processing in LaTeX would be to define a command to mark up an answer and then provide two definitions of the command, one that includes the answers and another that omits the answers. The usual way of switching between these definitions is to use distinct top-level source files.

I would prefer to avoid creating an extra top-level source file for each presentation and instead control the formatting behaviour using command line arguments, an environment variable, or such like. Then I could write a script to format the presentation twice, once with the answers and once without answers.

Looking through the arguments of pdflatex, I don't see a way of passing a flag that could be used in the LaTeX source to choose between the definitions.

Related questions and solutions:

glyn
  • 113

2 Answers2

4

The command line is taken as Tex source code, unless it consists purely of letters in which case it is taken as a filename with an implicit \input

so you can do

pdflatex '\def\opta{1} \def\optb{yes} \input{file}'

and in the body of file.tex test the values of \opta and \optb. You may need different quote characters, or use \\ in place of \ depending on the commandline shell that you use.

David Carlisle
  • 757,742
0

When on my system I save the following example as test.tex and on the shell (via chdir or cd or whatever) change the active directory to the directory where test.tex is stored, and then compile test.tex by calling latexmk from the shell/command-line via

latexmk -cd -gg -pdflatex="lualatex --shell-escape %O %S" -pdf test.tex

(maybe on your platform you need --enable-write18 instead of --shell-escape)

, then during the first lualatex-run initiated by latexmk, where aux-files don't exist in the beginning, another latexmk-run is initiated where the -jobname-option is used for attaching the phrase _WithAnswers to the current expansion of \jobname.

In any case it is checked whether the expansion of the \jobname-primitive contains the phrase _WithAnswers.

If so, the switch \ifWithAnswers is set to "true".

If not so, the switch \ifWithAnswers is set to "false".

As a result you have one latexmk-run on test.tex which exactly once initiates another latexmk-run on test.tex with -jobname="test_WithAnswers".

In the end you get test.pdf which is without answers and test_WithAnswers.pdf which is with answers.

%=======================================================================================================
\ExplSyntaxOn
\cs_new:Npn \CheckWhetherJobnameContainsPhrase #1 {
  \exp_args:Nne \use:nn {\str_if_in:nnTF} {{\jobname} {#1}}
}
\ExplSyntaxOff

\newif\ifWithAnswers

\CheckWhetherJobnameContainsPhrase{_WithAnswers}{% %---------------------------------------------------------------------------- % Stuff in case the document is to be created with answers %............................................................................ \WithAnswerstrue }{% %---------------------------------------------------------------------------- % Stuff in case the document is to be created without answers %............................................................................ % - Let's call another instance of latexmk for creating the document-variant % with answers: %............................................................................ % (Let's do things when the.aux-files are already read.) \AtBeginDocument{% \immediate\write\csname @mainaux\endcsname{\string\providecommand\string\DoOnlyInFirstLaTeXRun[1]{}}% \providecommand\DoOnlyInFirstLaTeXRun[1]{#1}% \DoOnlyInFirstLaTeXRun{% \RequirePackage{shellesc}% more recent releases of LuaTeX don't provide % \write18 any more. This package makes sure % under such LuaTeX \write18 is emulated by % calls to \directlua. \ShellEscape{% latexmk -cd -jobname="\jobname\string_WithAnswers" -pdflatex="lualatex \csname @percentchar\endcsname O \csname @percentchar\endcsname S" -pdf \jobname.tex }% }% }% %............................................................................ \WithAnswersfalse } %=======================================================================================================

\documentclass{article}

\begin{document}

This is a text.

\ifWithAnswers It comes with answers. \else It comes without answers. \fi

\end{document}

Ulrich Diez
  • 28,770