
為了確保只能在數學模式下使用的新命令的正確使用,我引入了一個mathcommand
,它通過 來檢查數學模式\ifmmode
。
現在我想用這樣一個節標題中的數學命令,儘管在數學模式下使用該指令,但仍會出現數學環境警告:
\documentclass{scrbook}
% provide mathcommand environment
\newcommand{\assuremath}{%
\relax\ifmmode\else\message{LaTeX Warning: Mathmode command used outside of math mode on input line \the\inputlineno}\fi
}
\newcommand{\newmathcommand}[2]{\newcommand{#1}{#2\assuremath}}
\newcommand{\renewmathcommand}[2]{\renewcommand{#1}{#2\assuremath}}
% define some mathcommand
\newmathcommand{\someMathCommand}{e^x}
\begin{document}
% use mathcommand in a caption
\section{I love $\someMathCommand$}
\end{document}
我不熟悉章節標題中數學模式的機制。我想這就是問題發生的原因...
答案1
事情是這樣的。
節標題是一個移動參數,這意味著它儲存在記憶體中,並.aux
在下一頁發生時寫出到檔案中。
執行寫入操作時,TeX 擴充宏沒有執行命令;而且它是不是當時處於數學模式(處於不實際上)。這意味著條件為假並被\message{...}
寫入.aux
檔案。
你透過說解決問題
\DeclareRobustCommand{\assuremath}{%
\relax\ifmmode\else\message{LaTeX Warning: Mathmode command used outside of math mode on input line \the\inputlineno}\fi
}