조건부 매크로

조건부 매크로

목표:표시된 수학 모드인지 여부에 따라 다른 명령을 대체하는 매크로를 갖고 싶습니다. 특히 텍스트 줄에서는 고정 크기의 꺾쇠 괄호를 생성해야 \langle ... \rangle하지만 표시되는 수학에서는 가변 크기의 괄호를 생성해야 합니다 \left<...\right>.

동기 부여:수학 표현식은 표시되는 표현식과 표시되지 않는 표현식 사이에서 자주 이동할 수 있으므로 매크로는 범용적이어야 합니다(교과서를 작성하고 편집하고 재편집하는 등). 표시된 수학에서 표현식 코드를 이동할 때마다 표현식 코드를 변경해야 한다면 오타가 발생할 수 있습니다. 위의 꺾쇠 괄호가 있는 예는 물리학 문헌의 평균화에 대한 표준 표기법입니다.

첫 번째 시도아래 MWE에 있습니다. 인라인 수학에서는 작동하지만 디스플레이 모드에서는 올바르게 대체하지 못합니다. LaTeX는 각각 $$또는 \[및 의 사용법에 따라 "Missing \right. Inserted" 또는 "Bad Math Environment Delimiter"로 반응합니다 . \]배치 모드에서 컴파일하면 왼쪽 괄호의 크기는 정확하지만 오른쪽 괄호는 잘못된 출력이 생성됩니다. 이 결과는 \def 또는 \newcommand의 사용에 의존하지 않습니다. 대괄호에 대해서도 동일하게 테스트되었습니다 [...].

시스템:Mac OS 12.7.2., LaTeX: <pdfTeX, 버전 3.141592653-2.6-1.40.24(TeX Live 2022)(사전 로드된 형식=latex) 제한됨\write18 활성화됨. 확장 모드 진입 (./mwe.tex LaTeX2e <2021-11-15> 패치 레벨 1 L3 프로그래밍 레이어 <2022-02-24>>

이제 MWE:

\documentclass{article}
\def \la{\ifinner \langle \else \left< \fi }
\def \ra{\ifinner \rangle \else \right> \fi }
\begin{document}
The macros work in the text line: $\la \int \Omega_n^2 dt \ra$, 
but do not in the displayed math. Obviously, the 
false text is not expanded properly, but why?  
\[
\la \int \Omega_n^2 dt \ra 
\]
\end{document}

답변1

이 접근 방식을 사용하지 않는 것이 좋습니다. 일반적으로 "올바른 일"을 수행하지 않습니다.

어쨌든 고객은 항상 옳기 때문에 amsmath \if@display조건을 활용할 수 있습니다.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\la}{\if@display\left\fi\langle}
\newcommand{\ra}{\if@display\right\fi\rangle}
\makeatother

\begin{document}

The macros work in the text line: $\la \int \Omega_n^2 \, dt \ra$,
and also in displayed math
\[
\la \int \Omega_n^2 \, dt \ra
\]
\begin{align}
I=\la \int \Omega_n^2 \, dt \ra
\end{align}
\begin{gather}
I=\la \int \Omega_n^2 \, dt \ra
\end{gather}

\end{document}

여기에 이미지 설명을 입력하세요

답변2

\ifinnerTeX 상태의 낮은 수준 측면을 테스트하지만 기본적으로 문서 수준 요소와 관련하여 tex에서 유용한 작업을 수행하지 않습니다.

여기에서 \la테스트합니다.진실인라인 수학 및거짓in 이지만 not을 통해 디스플레이 스타일을 입력하는 것과 같은 디스플레이 환경 \[에서는 true로 테스트됩니다.amsmathgatheralign
$\displaystyle display math$
$$ display math$$

하지만 시험은 언제나진실\ra표현식이 이미 내부에 있거나 시작 시 false인 경우 실행 \ifinner되어 \la왼쪽 -오른쪽 쌍 내의 모든 수학이 내부 모드에 있으므로 true로 \left< 설정됩니다 . \ifinner따라서 일치를 실행하지 마십시오 \right.

또한 \left..\right스트레칭이 필요하지 않은 경우에도 수평 간격에 영향을 미치므로 피해야 합니다.

mathtools왼쪽에서 오른쪽으로 가로 간격을 수정하는 보다 강력한 메커니즘이 제공됩니다 .

여기에 이미지 설명을 입력하세요

\documentclass{article}
\def\test{\relax\ifinner T\else F\fi}

\usepackage{mathtools}

\DeclarePairedDelimiter\ang\langle\rangle

\begin{document}

a $\test  \ang{\int \Omega_n^2 dt}  \ang[\big]{\int \Omega_n^2 dt}$

\[
\test \ang{\int \Omega_n^2 dt}  \ang*{\int \Omega_n^2 dt}  \ang[\Big]{\int \Omega_n^2 dt}
\]

\begin{equation}
  \test \ang{\int \Omega_n^2 dt}  \ang*{\int \Omega_n^2 dt} \ang[\Big]{\int \Omega_n^2 dt}
\end{equation}

\begin{gather}
  \test \ang{\int \Omega_n^2 dt}  \ang*{\int \Omega_n^2 dt} \ang[\Big]{\int \Omega_n^2 dt}
\end{gather}
\end{document}

답변3

\leftTeX는 수학 모드에서 -delimiter를 만나면 조판 모드를 "inner"로 전환합니다 .

\ifinner따라서 적절한 시기에 확장하는 것이 문제인 것 같습니다 .

\expanded추가 조판 전에 -tests를 평가 하는 데 사용할 수 \ifinner있으며 이에 따라 조판 모드가 전환됩니다.

\documentclass{article}
\newcommand*\la{\ifinner\else\expandafter\left\fi\langle}%
\newcommand*\ra{\ifinner\else\expandafter\right\fi\rangle}%
\begin{document}
The macros work in the text line: $\la \int \Omega_n^2 dt \ra$, 
and also do work in the displayed math.
\[%
\expanded{\la \unexpanded{\int \Omega_n^2 dt} \ra}
\]%
\end{document}

\la어떤 상황에서는 이러한 방식으로 및 \ra및 따라서 \langle/ \left\langle\rangle/가 완전히 확장되는 상황이 \right\rangle문제가 될 수 있습니다.

매크로를 사용하여 다음을 래핑할 수도 있습니다 \unexpanded.

\documentclass{article}
\newcommand\inangle[1]{%
  \expanded{%
    \ifinner\else\unexpanded{\left}\fi\unexpanded{\langle}%
    \unexpanded{#1}%
    \ifinner\else\unexpanded{\right}\fi\unexpanded{\rangle}%
  }%
}%
\begin{document}
The macros work in the text line: $\inangle{\int \Omega_n^2 dt}$, 
and also do work in the displayed math.
\[\inangle{\int \Omega_n^2 dt}\]
\end{document}

여기에 이미지 설명을 입력하세요

경고:

이미 언급했듯이그의 답변에서 David Carlisle, 어떤 경우에도 이러한 접근 방식은 실제로 항목이 디스플레이 모드에서 조판되지 않고 를 통해 선택된 디스플레이 스타일을 사용하여 인라인 모드에서 조판되는 LaTeX 환경에서는 실패합니다 \displaystyle.

관련 정보