Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to typeset an article. In the preamble, I defined these two things:

\newcommand{\leftexp}[2]{{\vphantom{#2}}^{#1}{#2}}

\newcommand{\hr}{\leftexp{*}{\mathbb{R}}}

However, when I try to use $\hr$ in a section, as in:

\section{Constructing $\hr$}

I get an error:

Incomplete `\iffalse`; all text was ignored after line 1.

Any idea what could be causing this? Everything works just fine if I don't have the $\hr$ in the section (e.g. It typesets just fine if I have \section{Constructing $\mathbb{R}$}, say).

share|improve this question
2  
Since the argument of \section "moves", protecting it seems to solve your problem: \newcommand{\hr}{\protect\leftexp{*}{\mathbb{R}}}, or just using \protect\hr in your sectional header. –  Werner Feb 1 '12 at 23:19

2 Answers 2

up vote 9 down vote accepted

It'll be a fragile command, and \section is a moving argument, so use \protect or declare it with \DeclareRobustCommand

enter image description here

\documentclass{article}

\usepackage{amsmath,amssymb}

\newcommand{\leftexp}[2]{{\vphantom{#2}}^{#1}{#2}}

\newcommand{\hr}{\leftexp{*}{\mathbb{R}}}

\DeclareRobustCommand{\leftexpB}[2]{{\vphantom{#2}}^{#1}{#2}}
\DeclareRobustCommand{\hrB}{\leftexpB{*}{\mathbb{R}}}

\begin{document}

\tableofcontents

\section{Constructing $\protect\hr$}

aa

\section{Constructing $\hrB$}

aa


\end{document} 
share|improve this answer
1  
Thanks a lot. I'm just learning these things, so may I ask what exactly is a "moving argument" and a "fragile command"? Alternatively, what does \protect do? –  FPP Feb 1 '12 at 23:22
5  
Ah, the honest answer is a "moving argument" is any argument in which things go wrong sometimes. A fragile command is a command that goes wrong in a moving argument, and \protect normally does nothing but in each such command it is locally defined to do whatever necessary to fix the problem. This case is typical, the heading is typeset as the section head, but also perhaps written to the toc file and/or used at a different font size in a page head. \protect holds the command unexpanded so it is written to the toc file unexpanded and only expanded when the toc is read back in. –  David Carlisle Feb 1 '12 at 23:26
1  

You got the (in)famous "fragile command in moving argument" problem. The stuff in \section is written into table of contents, which breaks things.

The following works:

\newcommand{\leftexp}[2]{{\protect\vphantom{#2}}^{#1}{#2}}
\newcommand{\hr}{\leftexp{*}{\mathbb{R}}}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.