Jump to content

TeX/if

From Wikibooks, open books for an open world
< TeX

Synopsis

[edit | edit source]
\if <token-1><token-2> 
  <tex-code-1> 
[\else <tex-code-2>] 
\fi

Description

[edit | edit source]

The \if command denotes the start of an if-then-else control structure. The forms <token-1> and <token-2> must expand to tokens. <token-1> and <token-2> can be either a character or a control sequence. If <token-1> and <token-2> both expand to the same character code then <tex-code-1> is processed; otherwise it is ignored. If the \else section is included and <token-1> and <token-2> expand to different character codes, then <tex-code-2> is processed; otherwise it is ignored.

A small LaTeX example

[edit | edit source]

Here is a small LaTeX example, demonstrating the use of \if.

\documentclass{minimal}
\begin{document}


\ifx\mycmd\undefined
undefed
\else
  \if\mycmd1
  defed, 1
  \else
  defed
  \fi
\fi


\def\mycmd{1}

\ifx\mycmd\undefined
undefed
\else
  \if\mycmd1
  defed, 1
  \else
  defed
  \fi
\fi


\def\mycmd{0}

\ifx\mycmd\undefined
undefed
\else
  \if\mycmd1
  defed, 1
  \else
  defed
  \fi
\fi


\end{document}

This example generates the output:

undefed
defed, 1
defed