views:

41

answers:

1

I define the \quan macro to consistently typeset quantities (copied below) in my preamble, and it works fine…except in figure captions where pdflatex complains that I've got an undefined control sequence and crashes.

How can I modify the macro to make it work in the caption environment?

Thanks!

\let\unitsep=\,     % Inserted at spaces in second arg, separates units (A s)
\let\numunitsep=\;  % Separator between number and unit (12.3 V)
\makeatletter
\def\quan#1{\ifmmode \let\@shiftmath=\relax \else \let\@shiftmath=$\fi
\@shiftmath \uppercase{\def\@quanarg{#1}}%
\expandafter\@quannum\@quanarg E\@noexp\@end \futurelet\@nextchar\@quanunit}
\def\@quannum#1E#2\@end{\@quanmant#1\@end \ifx\@noexp#2 \else \@quanexp#2\fi}
\def\@quanmant#1{\ifx#1\@end \let\@next=\relax \else
\if,#1\mathord,\else #1\fi \let\@next=\@quanmant \fi \@next}
\def\@quanexp#1E\@noexp{\times 10^{#1}}
\def\@quanunit{\if[\@nextchar
\def\@next[##1]{\numunitsep\mathrm{##1}\endgroup\@shiftmath}%
\begingroup\@quanspace \else \let\@next=\@shiftmath \fi \@next}
{\catcode`\ =\active\gdef\@quanspace{\catcode`\ =\active\let =\unitsep}}
\makeatother
A: 

It's a bit of a non-answer, but it might be useful to use siunitx for this purpose instead.

As for why things might go wrong within captions, recall that captions are written to the .lof or .lot files, so you have to worry about the fragility or otherwise of the macros. The root of that problem is that macros are expanded before being written out, and that means that when they finally are executed -- when they're read in from the file on the next run -- they don't necessarily behave as one might expect (I've been caught confusingly by this before). (I'm not saying that this is definitely your problem, but your mention of a failure when in a moving argument means that this is where I'd look first).

Norman Gray