Вы находитесь на странице: 1из 4

15/11/2014

LaTeX conditional expression - TeX - LaTeX Stack Exchange


sign up

TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related
typesetting systems. It's 100% free, no registration required.

log in

tour

Take the 2-minute tour

help

LaTeX conditional expression


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 ?
{conditionals}

edited Jan 6 at 9:01

asked Nov 24 '10 at 13:00


Cedric H.
1,153

12

20

possible duplicate of How do I conditionally execute something based on a documentclass option?


Waldir Leoncio Oct 10 at 19:34

4 Answers
There are many ways.

Plain LaTeX
Perhaps the most canonical is:
\newif\ifpaper

Then either
\papertrue % or
\paperfalse

And to use it:


\ifpaper
% using paper
\else
% electronic
\fi

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.

etoolbox
A more user-friendly and modern approach is taken by etoolbox , where you'd write instead
\newtoggle{paper}

which is set with either


\toggletrue{paper}
\togglefalse{paper}

And to use it:


\iftoggle{paper}{%
% 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

http://tex.stackexchange.com/questions/5894/latex-conditional-expression

1/4

15/11/2014

LaTeX conditional expression - TeX - LaTeX Stack Exchange

(and others):
\ifboolexpr { togl {paper} or test {\ifdef{\foo}} } {true} {false}

expl3
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.
edited Jul 8 '11 at 0:21

answered Nov 24 '10 at


13:08
Will Robertson
38.5k

Thanks ! Just perfect.

117

166

Cedric H. Nov 24 '10 at 13:53

12

+1 for not mentioning the obsolete ifthen package. Philipp Nov 27 '10 at 10:24

"troubles with nesting" - you might link to your excellent digression/answer. Charles Stewart Nov 27
'10 at 11:05

your answer is very useful for me. xport Dec 9 '10 at 7:24
@Philipp: Wahts the problem with ifthen I use it often an think its syntax is easier that that one
shown by Will with etoolbox . Tobi Jul 7 '11 at 15:16

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.
%%%my-varient-for-different-output.tex
%%%This is the file that you actually call with pdftex/lualatex/etc.
\documentclass{minimal}
%\usepackage{xifthen}
\usepackage[foo]{optional}
%\input{themain-file.tex}
%%%EOF my-varient-for-different-output.tex
%%%%START OF themain-file.tex
\usepackage{boolexpr}
\makeatletter
%Check if optional is loaded. Otherwise, you don't
%want to define these commands.
\@ifpackageloaded{optional}{%
%%Don't do anything here because you've got it covered.
}{%
%%%What are the defaults if no optional class was loaded?
\usepackage[bar]{optional}
}
%%%Let's make an ifthenelse version of the \opt command that's
%%%provided by the optional package

http://tex.stackexchange.com/questions/5894/latex-conditional-expression

2/4

15/11/2014

LaTeX conditional expression - TeX - LaTeX Stack Exchange

\newcommand\ifopt[3]{%
\ifboolexpr{%
0 = \pdfstrcmp{true}{%
\opt{#1}{true}%
}%
}{#2}{#3}%
}%
%%%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.
\newcommand*\switchopt[1]{%
\switch[\pdfstrcmp{\opt{#1}{#1}}]%
}
%%%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...
%%%
\newcommand*\caseopt[1]{%
\pdfstrcmp{#1}{\opt{#1}{#1}}% = 0%
}
\makeatother
\begin{document}
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.
\switchopt{bar}
\case{{foo}} Foo stuff here!
\case{{bar}} Bar stuff here!
\case{{third}} Some other thingy-dingy!
\otherwise Nothing I know of was set.
\endswitch%
\switch
\case{\caseopt{foo}} Foo stuff here!
\case{\caseopt{bar}} Bar stuff here!
\otherwise Nothing I know of was set.
\endswitch%
\end{document}

Although the chosen answer is probably better, this may lead to more ideas.
edited Nov 27 '10 at 5:42

answered Nov 27 '10 at


5:31
Andrew Starks
1,084

17

ifthen can be your friend alsohttp://www.ifi.uio.no/it/latex-links/ifthen.pdf


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.
answered Nov 24 '10 at
14:46
flip
161

How is etoolbox low level? Charles Stewart Nov 27 '10 at 11:06


I just meant the dive into straight TeX, but yes, etoolbox is decidedly not. flip Nov 27 '10 at 14:47

You can at the command line do something like \def\MYFLAG{} and then test if \MYFLAG is
undefined in your document (or an included style file).

http://tex.stackexchange.com/questions/5894/latex-conditional-expression

3/4

15/11/2014

LaTeX conditional expression - TeX - LaTeX Stack Exchange

This allows you to run a document in different ways without having to change it, something
like command line options to a program.
Not exactly the question asked, but strongly related to the use case you give.
edited Dec 12 '11 at 18:15
Joseph Wright
120k

310

605

answered Jul 7 '11 at 14:58


Jonathan Fine
1,201

11

How to test if \MYFLAG is undefined? Xi Jli Feb 1 '13 at 1:11


@XiJli: I normally use \ifdefined\MYFLAG ... \else ... \fi . Peter Grill Mar 24 at 22:46

http://tex.stackexchange.com/questions/5894/latex-conditional-expression

4/4

Вам также может понравиться