\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는 매크로를 확장합니다.없이명령 실행; 게다가 그것은~ 아니다당시 수학 모드에 있었습니다(현재아니요모드). 이는 조건이 거짓이고 파일 \message{...}에 기록된다는 의미입니다 .aux.

다음과 같이 문제를 해결합니다.

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

관련 정보