为什么这个用于显示带有单位的数量的LaTeX宏在图形标题中出现中断?

为什么这个用于显示带有单位的数量的LaTeX宏在图形标题中出现中断?,latex,macros,caption,Latex,Macros,Caption,我在序言中定义了(复制到下面),它工作得很好…除了图中的标题,pdflatex抱怨我有一个未定义的控制序列和崩溃 如何修改宏以使其在标题环境中工作 谢谢 \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{\ifm

我在序言中定义了(复制到下面),它工作得很好…除了图中的标题,pdflatex抱怨我有一个未定义的控制序列和崩溃

如何修改宏以使其在标题环境中工作

谢谢

\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

这有点不确定,但使用
siunitx
可能会有用

至于为什么字幕中会出现问题,请记住字幕是写入
.lof
.lot
文件的,因此您必须担心宏的脆弱性。这个问题的根源是宏在被写出来之前是被扩展的,这意味着当它们最终被执行时——当它们在下一次运行时从文件中读入时——它们的行为不一定像人们所期望的那样(我以前对此感到困惑)。(我并不是说这肯定是你的问题,但你在一场激烈的争论中提到失败意味着这是我首先考虑的问题)