위 첨자 및/또는 아래 첨자를 미리 검색하여 이를 "흡수"하고 인수를 이동하는 매크로를 정의합니다.

위 첨자 및/또는 아래 첨자를 미리 검색하여 이를 "흡수"하고 인수를 이동하는 매크로를 정의합니다.

정의에 따라 이미 위 첨자 및/또는 아래 첨자가 있는 수학 매크로가 많이 있습니다. 즉 다음과 같습니다.

\newcommand*{\mymathsym}{x^{\text{foo}}_{\text{bar}}}

텍스트 본문에서 이러한 기호에는 추가 위/아래 첨자가 필요한 경우가 많습니다. 즉, 작성자는 기본 기호를 {}-중괄호 쌍에 넣어야 한다는 것을 기억해야 합니다. 그렇지 않으면 이중 위/아래 첨자 오류가 발생합니다.

\begin{equation}
  {\mymathsym}^{\text{extra}}
\end{equation}

추가 위 첨자는 보조 위 첨자가 되며 약간 높고 작게 설정됩니다. 추가 위 첨자 여기에는 두 가지 단점이 있습니다. a) 특수 적용 분야에서는 개념적 관점에서 두 위 첨자가 동일한 계층 구조 수준에 있습니다. 즉, 두 위 첨자는 실제로 "foo, extra" 목록으로 인쇄되어야 하며 반대 순서인 "extra, foo"도 똑같이 좋습니다. b) 주 위 첨자와 아래 첨자의 길이가 매우 불균형한 경우 보조 위 첨자는 멀리 떨어져 설정됩니다. 예:

\newcommand*{\mymathsymlong}{x^{\text{foo}}_{\text{very long foobar}}}

그리고

\begin{equation}
{\mymathsymlong}^{\text{extra}}
\end{equation}

수확량 멀리 떨어진 위 첨자

해결 방법으로 현재 선택적 인수를 취하고 해당 인수를 내부 위 첨자에 추가하는 다음 정의를 사용합니다.

\newcommand*{\mymathsymext}[1][]{x^{\text{foo}\if!#1!\else, #1\fi}_{\text{very long foobar}}}

\if!#1!(주의 : 인수가 a 로 확장되면 실패하기 때문에 조건이 빈 인수를 테스트하는 올바른 방법이 아니라는 것을 알고 있습니다 !. 하지만 매크로가 무엇을 하는지는 알 수 있을 것 같습니다.)

다음과 같이 사용됩니다.

\begin{equation}
\mymathsymext \qquad\text{vs.}\qquad \mymathsymext[\text{extra}]
\end{equation}

그러나 여기에 이미지 설명을 입력하세요 여기에는 두 가지 주요 단점이 있습니다. a) \newcommand단일 선택적 인수만 지원합니다. 따라서 디자인 타임에 추가 위 첨자나 추가 아래 첨자가 결국 필요할지 여부를 결정해야 합니다. 둘 다 지원할 수는 없습니다. b) 사용자는 추가 위/아래 첨자를 배치하기 위한 특이한 구문을 기억해야 합니다.

질문:

\mymathsymsuper매크로 를 어떻게 정의합니까 ?

  • ^뒤에 위 첨자 문자 <tok> 및/또는 아래 첨자 문자 _<tok>가 각각 뒤에 추가 토큰 <tok>이 오면 미리 스캔합니다.
  • "그들을 흡수"하고,
  • <tok>을 쉼표로 구분된 내부 위/아래 첨자의 끝으로 이동합니까?

전체 MWE:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\newcommand*{\mymathsym}{x^{\text{foo}}_{\text{bar}}}

\newcommand*{\mymathsymlong}{x^{\text{foo}}_{\text{very long foobar}}}

\newcommand*{\mymathsymext}[1][]{x^{\text{foo}\if!#1!\else, #1\fi}_{\text{very long foobar}}}

\begin{document}

Here, the author must know that \verb#\mymathsym# has already a super- and subscript and must remember to put the main symbol into a pair of \{\}-braces, otherwise a double sup-/subscript error occurs.
The extra superscript becomes a secondary superscript and it set slightly higher and smaller:
\begin{equation}
{\mymathsym}^{\text{extra}}
\end{equation}

If the primary sup- and subscript are very unbalanced in their length, the secondary subscript is set very far apart:
\begin{equation}
{\mymathsymlong}^{\text{extra}}
\end{equation}

This extended macro takes an optional argument and ``absorbs'' the extra superscript into the primary superscript:
\begin{equation}
\mymathsymext \qquad\text{vs.}\qquad \mymathsymext[\text{extra}]
\end{equation}
Still, the author must remember this ``unusual'' syntax and it only supports either an extra super- or subscript, bot not both.

\paragraph{Question:}
How does one define a macro \verb#\mymathsymsuper# that
\begin{itemize}
  \item scans ahead if it followed by a superscript character $\verb!^!\langle \mathit{token}_\text{sup}\rangle$ and/or subscript character $\verb!_!\langle \mathit{token}_\text{sub}\rangle$ each followed by an    additional token $\mathit{token}_\text{sup}$ and $\mathit{token}_\text{sub}$ resp.
  \item ``absorbs them'', and
  \item moves $\mathit{token}_\text{sup}$ and/or $\mathit{token}_\text{sub}$ to end of its internal sup-/subscript separated by a comma?
\end{itemize}

\end{document}

답변1

다음을 사용하면 매우 쉽습니다 xparse.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\mymathsym}{e{^_}}{%
  x^{\mathrm{foo}\IfValueT{#1}{,#1}}_{\mathrm{bar}\IfValueT{#2}{,#2}}%
}

\begin{document}

\begin{gather}
\mymathsym \\
\mymathsym^{\mathrm{extrasup}} \\
\mymathsym_{\mathrm{extrasub}} \\
\mymathsym^{\mathrm{extrasup}}_{\mathrm{extrasub}} \\
\mymathsym_{\mathrm{extrasub}}^{\mathrm{extrasup}}   
\end{gather}

\end{document}

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

e{^_}매크로를 사용하면 ^또는 _(순서 상관없이)를 미리 살펴보고 #1위 첨자, #2아래 첨자에 할당합니다. 존재 여부를 테스트할 수 있습니다 \IfValueT(또는 \IfValueTF아래 첨자/위 첨자가 없는 상태에서 일부 작업을 수행하려는 경우).

관련 정보