I would like to be able to set a flag at the beginning of the main flag (say output) that tells if I want to produce a paper document or an electronic document.

Then I should check this flag to define or not empty pages, hypperref, oneside or twosides, etc.

How could I do that ?


4 Answers 4


There are many ways.

Plain LaTeX

Perhaps the most canonical is:


Then either

\papertrue % or

These set \ifpaper to be equal to \iftrue and \iffalse respectively. Therefore to use it:

  % using paper
  % electronic

However, using these primitive conditionals can lead to unexpected problems if you're not entirely familiar with the way TeX processes them, and the following solutions are recommended.


A more user-friendly and modern approach is taken by etoolbox, where you'd write instead


which is set with either


And to use it:

  % using paper
  % electronic

Why do I say this is more friendly? You'll never run into troubles with nesting, for which LaTeX's \newif conditions can sometimes be a bit of a pain to deal with.

etoolbox also supports boolean expressions such as

\ifboolexpr { togl {paper} and togl {pdf} } {true} {false}

which can also be combined with various testing functions also provided by that package (and others):

\ifboolexpr { togl {paper} or test {\ifdef{\foo}} } {true} {false}


Finally, for package writers (not really document authors): expl3 as part of LaTeX3 provides booleans that can be used in all sorts of interesting ways. For basic use, they look like this:

\bool_new:N \l_tmpa_bool

\bool_set_true:N \l_tmpa_bool
\bool_set_false:N \l_tmpa_bool

\bool_if:NTF \l_tmpa_bool {true} {false}
\bool_if:NT \l_tmpa_bool {true}
\bool_if:NF \l_tmpa_bool {false}

You can use boolean logic as follows:

\bool_if:nTF { ( \l_tmpa_bool || \l_tmpb_bool ) && !( \l_tmpc_bool ) } {true} {false}

Everything is expandable and lazy evaluation is used to avoid performing tests that don't need to be checked.

You can also combine boolean variables above with conditional functions such as \cs_if_eq_p:NN (‘are two control sequences equal?’) which can be tested in exactly the same way:

\bool_if:nTF { \l_tmpa_bool && \cs_if_eq_p:NN \foo \bar } {true} {false}

The overlap between etoolbox and expl3 here is rather pronounced; I think of the difference between the two as the separation between document authors (for etoolbox) and package writers (for expl3). But it's purely a matter of taste, when it comes down to it.

  • 33
    +1 for not mentioning the obsolete ifthen package.
    – Philipp
    Commented Nov 27, 2010 at 10:24
  • 1
    "troubles with nesting" - you might link to your excellent digression/answer. Commented Nov 27, 2010 at 11:05
  • 3
    @Philipp: Waht’s the problem with ifthen I use it often an think it’s syntax is easier that that one shown by Will with etoolbox.
    – Tobi
    Commented Jul 7, 2011 at 15:16
  • 1
    @Tobi: a partial answer is that this natural syntax makes it much harder to parse reliably, and it turns out to be impossible (or at least rather tough) to parse it "expandably". This may lead to trouble when used inside moving arguments, for instance. It is probably not the only reason, though I can't think of them right away. Commented Jul 7, 2011 at 17:54
  • 1
    @WillRobertson It is also perhaps worth mentioning that the default setting for \newtoggle is false. I say that because I just looked it up after reading your answer. Thanks for this useful answer! Commented Jul 27, 2015 at 11:35

Another approach is with the optional package, which I use for many purposes, along with boolexpr, which has a nice \switch and \case command.

Here's a minimal to illustrate.

%%%This is the file that you actually call with pdftex/lualatex/etc.
%%%EOF my-varient-for-different-output.tex

%%%%START OF themain-file.tex

%Check if optional is loaded. Otherwise, you don't
%want to define these commands.
%%Don't do anything here because you've got it covered.
%%%What are the defaults if no optional class was loaded?
%%%Let's make an ifthenelse version of the \opt command that's 
%%%provided by the optional package
        0 = \pdfstrcmp{true}{%
%%%This command is a wrapper around the \switch command that's
%%%provided by boolexpr. It lets you quickly create a switch
%%%that checks if an option is set.
%%%I'm sure there is a way to embed the \switch 
%%%command in here, but I'm not that smart. Is
%%%there some application of \noexpand?
%%%Anyway, this is more common to use than \switchopt
%%%You'll use \switch (with no optional argument)
%%%and \case[\caseopt{myoption}] Do stuff...
    \pdfstrcmp{#1}{\opt{#1}{#1}}% = 0%


    I'm writing some text but \opt{foo}{don't want this 
    unless foo is there.} If I want an else as part of 
    the deal, then \ifopt{bar}{bar must be there}{bar 
    is out to lunch} and it's time to go.

    Nesting ifopt commands can be tedious. Best to use
     a case if we've got a ton of definitions\ldots
    %%%NOTICE THE {{ and }}. They are required when using
    %%% an optional argument in \switch.
        \case{{foo}} Foo stuff here!
        \case{{bar}} Bar stuff here!
        \case{{third}} Some other thingy-dingy!
        \otherwise Nothing I know of was set.

        \case{\caseopt{foo}} Foo stuff here!
        \case{\caseopt{bar}} Bar stuff here!
        \otherwise Nothing I know of was set.

Although the chosen answer is probably better, this may lead to more ideas.


ifthen can be your friend also


but Will R's solutions are more low level and possibly closer to what you want.

I use ifthen with my CV, since I have to have an internal version as well as an external version. Not to mention that it makes having a 'long' and 'short' version.

  • How is etoolbox low level? Commented Nov 27, 2010 at 11:06
  • I just meant the dive into straight TeX, but yes, etoolbox is decidedly not.
    – flip
    Commented Nov 27, 2010 at 14:47
  • 2
    By some users, the ifthen package is considered obsolete: See tex.stackexchange.com/questions/13866/…
    – koppor
    Commented May 29, 2016 at 5:26

At the command-line, you can do \def\MYFLAG{} and then test if \MYFLAG is defined in your document (or an included style file) with \ifdefined\MYFLAG ... \else ... \fi.

This allows you to run a document in different ways without modifying it, like command-line options for a program.

Not exactly the question asked, but strongly related to the use case you give.

  • 4
    How to test if \MYFLAG is undefined?
    – Lenik
    Commented Feb 1, 2013 at 1:11
  • 2
    @XièJìléi: I normally use \ifdefined\MYFLAG ... \else ... \fi. Commented Mar 24, 2014 at 22:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .