3

I was trying some programming in LaTeX and found that \ifx was predefined, since I get an error from this:

\documentclass[10pt]{book}%
\newif\ifx
\xtrue
\begin{document}
\end{document}

Then I found there is a way to check if a command exist from this answer:

how-can-i-check-in-latex-or-plain-tex-whether-a-command-exists-by-name

Use it to check for \ifx:

\documentclass[10pt]{book}%
\newcommand{\checkfor}[1]{%
  \ifcsname#1\endcsname%
    ... command '#1' exists ...%
  \else%
    ... command '#1' does not exist ...%
  \fi%
}
\begin{document}
\checkfor{ifx}\par
\end{document}

Compiling the above says indeed that command \ifx exists.

But how would one get a list of all the \ifXXXXXs that are out there? I need to use lots of \if in my code to customize my build and need to make lots of new boolean variables. If there is a list out there, I can paste it on my wall next to my computer and just look at it before I use a new command instead of getting an error or having to check using code each time.

  • 2
    egreg has listed the primitives but every package you load may define some more. Are you sure you need to define any at all (well you never need to define any new if constructs) – David Carlisle May 18 '16 at 22:26
  • 1
    you can define your propre command that add a prefix to ensure that the new condition is not defined. – touhami May 18 '16 at 23:16
7

The \newif command doesn't check whether the conditional is already existing; the assumption is that a programmer using \newif knows what she/he is doing.

If you want an interface like \newcommand, load ifthen and use \newboolean; in this case

\newboolean{x}

would tell you

! LaTeX Error: Command \ifx already defined.
               Or name \end... illegal, see p.192 of the manual.

The same with etoolbox and \newbool.

You find the list of primitive conditionals in the TeXbook or in TeX by Topic. Add to them the e-TeX conditionals

\ifdefined
\ifcsname
\iffontchar

In the manual for e-TeX (texdoc etex) you find the complete list

enter image description here

Already loaded package might have added to the list.

Some engines also add to the list; for instance pdftex adds

\ifincsname
\ifpdfprimitive
\ifpdfabsnum
\ifpdfabsdim

The conditional \ifpdfprimitive is \ifprimitive in XeTeX. Similarly, LuaTeX has \ifprimitive, \ifabsnum and \ifabsdim. Both engines share \ifincsname.

  • Don't forget about the pdfTeX conditionals (and their renamed counterparts in Xe/LuaTeX). – Henri Menke May 18 '16 at 21:28
  • @HenriMenke Yes, you're right – egreg May 18 '16 at 21:29
  • 1
    Also it might be worth mentioning the boolean type of l3prg (\bool_new:N does name checking). – Henri Menke May 18 '16 at 21:31

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.