8

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}
  • 4
    pdflatex '\AtBeginDocument{\tcbset{lowerbox=visible}}\input{file}' might do. Can you add a short example to play with? – egreg Oct 22 '13 at 22:45
  • @egreg Your suggestion works. Are you going to turn your comment into an answer? – Romildo Oct 23 '13 at 1:10
6

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>}'
  • 1
    Usually I compile my documents with latexmk with the command latexmk -pvc -pdf <filename>. How can the method described in the answer be adapted to be used with latexmk, if possible at all? – Romildo Oct 23 '13 at 8:14
  • The last command given in this answer does not work because of the requirement on the etoolbox package. How can this requirement be inserted in the given command? – Romildo Oct 23 '13 at 12:01
  • You can add \RequirePackage{etoolbox} before \AtEndPreamble; for latexmk you have to use the -pdflatex="..." option, check the doc. – egreg Oct 23 '13 at 12:04
  • Thanks for the solution. This is the full command I used with Miktex 2.9 on Windows OS: pdflatex --job-name=My_LaTeX_Filename_ArXiV "\RequirePackage{etoolbox}\AtEndPreamble{\AtBeginDocument{\pagestyle{fancy}}}\input{My_LaTeX_Filename}" – imriss Jan 12 '15 at 19:36
5

You can avoid the \AtBeginDocument problems with this approach:

\ifdefined\ignorelowerbox\tcbset{lowerbox=ignored}\fi

in the preamble. Then

pdflatex '\def\ignorelowerbox{true}\input{<filename>}'

if you want to ignore and

pdflatex '\input{<filename>}'

if not. This method is completely general (uses the e-TeX ifdefined for conciseness).

One could devise other schemes in this style unfortunately LaTeX has put the @ in quite a few useful things like \@namedef or \@firstoftwo hence this is more complicated as one would have to set the catcode etc... to use them.

  • and \csname ... \endcsname is too long to type in the command line. – user4686 Oct 23 '13 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.