上付き文字や下付き文字を先にスキャンし、それらを「吸収」して引数を移動するマクロを定義します。

上付き文字や下付き文字を先にスキャンし、それらを「吸収」して引数を移動するマクロを定義します。

定義上、上付き文字や下付き文字がすでにある数学マクロがたくさんあります。例えば、

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

本文では、これらの記号には追加の添え字/下付き文字が必要になることがよくあります。つまり、著者はメインの記号を 2 つの {} 括弧で囲むことを忘れないようにする必要があります。そうしないと、添え字/下付き文字が 2 つ重になるエラーが発生します。

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

追加の上付き文字は二次上付き文字になり、わずかに高く小さく設定されます。 追加の上付き文字 これには 2 つの欠点があります。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!(注:引数が に展開されると失敗するため、条件は空の引数をテストする正しい方法ではないことはわかっています!。ただし、マクロが何をするのかは理解できたと思います。)

用途は

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

そして 、次のようになります。ここに画像の説明を入力してください ただし、これには 2 つの大きな欠点があります。a) \newcommand1 つのオプション引数のみをサポートします。したがって、設計時に、追加の上付き文字または追加の下付き文字のどちらかが最終的に必要になるかどうかを決定する必要があります。両方をサポートすることはできません。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存在しない上付き/下付き文字で何らかのアクションを実行したい場合は、 を使用します)。

関連情報