[Tex/LaTex] Setting package option from the command line

compilingoptional argumentstcolorbox

I want to compile a document that uses the tcolorbox package in two different ways:

  • with the option /tcb/lowerbox set to visible, so that the lower part of the tcolorbox environment is typeset
  • with the option /tcb/lowerbox set to ignored, so that the lower part of the tcolorbox environment is not used.

At the beginning of the document there is the command

\tcbset{lowerbox=ignored}

which should be commented for the first case only.

Is it possible to control this behaviour from the command line when compiling the document, so that I do not have to edit the source explicitly in order to have the two versions of the document?

Here is a minimal working example. Let's say it is saved as test.tex.

\documentclass{standalone}

\usepackage{tcolorbox}

%\tcbset{lowerbox=ignored}

\begin{document}

\begin{tcolorbox}
  Upper part.
  \tcblower
  Lower part.
\end{tcolorbox}

\end{document}

Best Answer

You can add code to be executed when LaTeX scans \begin{document}. So long as \tcbset{lowerbox=ignored} is executed before \begin{document}, calling

pdflatex '\AtBeginDocument{\tcbset{lowerbox=visible}}\input{<filename>}'

for processing <filename>.tex should do. Read on if it doesn't.


Not every setting can be passed this way; for instance, if package foo.sty sets its options at \begin{document}, say calling \fooset{baz=true}, adding

\AtBeginDocument{\fooset{baz=false}}

to the command line would do nothing, because the order in which the calls are executed would be

\AtBeginDocument{\fooset{baz=false}}
...
\usepackage{foo}

which becomes

\fooset{baz=false}% <-- from the command line
\fooset{baz=true}% <-- from \usepackage{foo}

A way out is adding a layer, using \AtEndPreamble (requires \usepackage{etoolbox} in the document):

pdflatex '\AtEndPreamble{\AtBeginDocument{\fooset{baz=false}}}\input{<filename>}'
Related Question