4

I am defining a new command and a flag with:

\newcommand{\setActorsStyle}[1][1]{
  \ifnum#1=1
     \setActorsStyleColor{}
  \else
     \setActorsStyleBW{}
  \fi
}

\newcommand{\flag}{0}

Calling the command as following with

\setActorsStyle[\flag{}]{}

results in compilation error Missing = inserted for \ifnum as if the parameter given to \ifnum is not an integer. What am I missing here ?

  • Note you should have a % at the end of every line that ends with { or } (but not 1) otherwise use of this command will generate white space in the printed output. – David Carlisle Sep 2 '14 at 13:20
  • The {} trick should be used only when you want to ensure that following spaces are not gobbled, so usually for commands that do typesetting. – egreg Sep 2 '14 at 13:38
7

You shouldn't have the empty braces after \flag.

\documentclass{article}
\newcommand{\setActorsStyle}[1][1]{
  \ifnum#1=1
     \typeout{ONE!}
     %\setActorsStyleColor{}
  \else
     \typeout{NOT ONE!}
     %\setActorsStyleBW{}
  \fi
}

\newcommand{\flag}{0}

\setActorsStyle[\flag]{}

Here I have disabled the two macros whose definition I do not have, and used \typeout to display a message in the terminal, to show that the modified code works.

The problem with the original version is that #1 expands to the number 0 followed by an empty group. This combination is not a number, so it breaks the syntax for \ifnum. You could get away with \flag{} if you did

\ifnum1=#1

because then the expansion would yield \ifnum1=0{} which is ok.

  • Thanks ! Can you please explain why in this particular case I shouldn't use the empty braces ? – Manuel Selva Sep 2 '14 at 13:06
  • @ManuelSelva with the braces it is \ifnum0{}=1 which is a syntax error. – David Carlisle Sep 2 '14 at 13:18
  • @ManuelSelva --- I have edited my answer. – Ian Thompson Sep 2 '14 at 13:24
  • With \ifnum#1=1 the space token caused by the end-of-line is ignored. With \ifnum1=#1 it wouldn't when #1 is \flag{}. To cope with this you should have \ifnum 1=#1\relax – egreg Sep 2 '14 at 13:42

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.