I feel really stupid for asking this, but how do you form more complex if conditionals in TeX? I'm looking for something like:

\ifnum\x=1 OR \ifnum\x=14
    {do this}
    {do that}

I don't want to have to resort to copy-pasting the entire condition just to change the expression when the body is the same.


There are a number of approaches. Assuming you are looking for a purely primitive-based on, then something like

\ifnum\ifnum\x=1 1\else\ifnum\x=14 1\else0\fi\fi
   =1 %
   <do this>
   <do that>

Thus you use 'secondary' conditionals to convert the original problem into a simple TRUE/FALSE situation, where the 'outer' \ifnum is simply testing for 0 or 1. (This works as TeX keeps expanding until it finds a number when considering the outer \ifnum.)

It's important to get the number-termination correct when using this approach. In the example, the spaces after \x=1 and \x=14 are required to get the correct outcome. With a bit more imagination, you can make more complex constructs using the same approach (for example, you can having combined OR and AND conditions in this way.)

An alternative method if the logic gets complex would be to include the 'payload' as separate macros:

\ifnum\x=1 %
  \ifnum\x=14 %
\def\myfirstcase{do this}
\def\mysecondcase{do that}

This is what you often see with larger 'to do' blocks. The \expandafter use is 'good practice' but may not be needed depending on the exact nature of the code to insert.

  • 8
    Yes, TeX is Turing-complete, but... – raphink Sep 29 '11 at 12:27
  • 6
    @Raphink There are alternative, logic-based, approaches. However, the question says tex-core, so I went with a pure primitive approach. (I'd personally use LaTeX3's \bool_if:nTF.) – Joseph Wright Sep 29 '11 at 12:28

  \ifboolexpr{ test {\ifnumcomp{#1}{=}{1}} or test {\ifnumcomp{#1}{=}{14}} }
    {do this}
    {do that}}

\mytest{1} \mytest{14} \mytest{0}

There is also the xifthen package that provides for "composite" tests.

  • I like this answer, because the longer a document gets, the more likely it's going to use the etoolbox package (either directly or via some other package). So chances are this is available without extra packages. – Matthew Leingang Apr 27 '18 at 16:40

The package xintexpr implements boolean logic on arithmetic expressions. We can use therein the \pdfstrcmp utility (if the engine makes it available) to compare strings.





\xintifboolexpr { \x = 1 || \x = 14 }

\xintifboolexpr { even(\x) && \x < 24 }

\xintifboolexpr { \x = floor(sqrt(\x))^2 }

% \pdfstrcmp {text1}{text2} evaluates to 0 if text1 and text2 are equal
% to -1 if text1 comes first in lexicographic order, to +1 else
% To test if the strings are equal we thus use not(\pdfstrcmp {text1}{text2})
% (or we can use the ! as synonym of the "not" function)

\xintifboolexpr { 1=1 && (2=3 || 4<= 4 || not(\pdfstrcmp {abc}{def})) && !(2=4)}

\xintifboolexpr {\pdfstrcmp {abc}{def} = -1}


Compiled with PDFLaTeX:

enter image description here

The package can also be used with Plain TeX.

As seen above && is AND and || is OR. It is also possible to write 'and' and respectively 'or' (quotes mandatory).


LaTeX3 provides testing of boolean expressions that allows for using infix notation && (for AND), || (for OR) and ! (for negation). In addition to this, parentheses can be used to isolate sub-expressions.





\bool_if:nTF { \int_compare_p:n {\x = 1} || \int_compare_p:n {\x = 14} }
  { True }
  { False }


The following (more complex) example was taken from section VI.3 Boolean expressions of the LaTeX3 interfaces documentation:

\int_compare_p:n { 1 = 1 } &&
    \int_compare_p:n { 2 = 3 } ||
    \int_compare_p:n { 4 <= 4 } ||
    \str_if_eq_p:nn { abc } { def }
  ) &&
! \int_compare_p:n { 2 = 4 }

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.