8

I am attempting to edit an if statement that I was using in an environment I created. Previously, the code was

\if #1\empty
\else
\textbf{#1.}\hspace{0.5em}%
\fi

I want to change it to something like

\if #1\empty \textbf{}\hspace{0.5em}
\else
\textbf{#1.}\hspace{0.5em}%
\fi

where I could put what I want in the \textbf{}. But whatever I put in there, I get \Omega printed in front of it. When left empty like above, it simply prints \Omega. If #1 is not empty, there is no \Omega.

My code

\documentclass[12pt,letterpaper]{article}
\newenvironment{question}[3][]{
\noindent\ignorespaces
\if #1\empty \textbf{}\hspace{0.5em}
\else
\textbf{#1.}\hspace{0.5em}%
\fi
}{}
\begin{document}
\begin{question}{0}{8}\end{question}

\begin{question}[1]{0}{8}\end{question}
\end{document}

Any advice on getting rid of this capital omega is very welcome.

9

\if expands the following tokens until it finds two expandable tokens. If #1 is empty, then \empty is expanded to nothing, then \textbf, there it finds these tokens. Expansions:

\empty ->

\empty vanishes, because its definition text is empty.

\textbf -> \protect \textbf␣

In the context here, \protect has the meaning of \relax, thus it is the first non-expandable token.

\textbf␣ #1 -> \ifmmode ...\else \hmode@bgroup ... \fi

We don't have math mode, thus the \hmode@bgroup is expanded:

\hmode@bgroup -> \leavevmode \bgroup
\leavevmode -> \unhbox \voidb@x

Now we the second non-expandable token is \unhbox. Because both tokens are command tokens, the result of \if is true and the remaining tokens are not removed. \voidb@x is executed, now as standalone token, not as argument for \unhbox. It's a character constant encoding the box number, which was intended to be used with \unhbox.

\voidb@x is number 65, defined as \char"A. In the current font this gives the uppercase omega.

Also the test fails, if #1 starts with two equal non-expandable tokens, e.g. 11.

The following example puts #1 in the macro \QuestionFirstParam and compares it via \ifx with \empty, because \ifx does not expand the following tokens, instead it compares the next two tokens:

\documentclass[12pt,letterpaper]{article}
\newenvironment{question}[3][]{%
  \noindent
  \def\QuestionFirstParam{#1}%
  \ifx\QuestionFirstParam\empty
    \leavevmode
    % \textbf{}%
    % \hspace{0.5em}%
  \else
    \textbf{#1.}\hspace{0.5em}%
  \fi
  arg2 = #2, arg3 = #3%
}{}
\begin{document}
\begin{question}{0}{8}\end{question}

\begin{question}[1]{0}{8}\end{question}
\end{document}

Result

5
  • Can you explain little bit why the original code prints that omega?
    – Pouya
    Sep 21 '14 at 18:57
  • Of course, \textbf{} does nothing apart making TeX spinning its wheels for a while.
    – egreg
    Sep 21 '14 at 19:10
  • @egreg I haven't removed it, because starting a line with \hbox{.5em} does not look too good either. Perhaps it's due to the minimizing part of the MWE. Sep 21 '14 at 19:14
  • \hspace{.5em} does start a paragraph.
    – egreg
    Sep 21 '14 at 19:15
  • Thank you, not only did you give a solution, but an in depth explanation, so that I now will not make a similar mistake. Much appreciated.
    – AThomack
    Sep 21 '14 at 19:30

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.