\ifmmode セクションのキャプションをチェック

\ifmmode セクションのキャプションをチェック

数式モードでのみ使用される新しいコマンドの正しい使用を保証するために、 を導入しました。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はマクロを展開します。それなしコマンドを実行する。さらに、ない当時は数学モード(いいえモードです。つまり、条件は false であり、ファイル\message{...}に書き込まれます.aux

問題を解決するには

\DeclareRobustCommand{\assuremath}{%
  \relax\ifmmode\else\message{LaTeX Warning: Mathmode command used outside of math mode on input line \the\inputlineno}\fi
}

関連情報