align*環境にあるかどうか確認する

align*環境にあるかどうか確認する

TeX が 環境にあるか通常の数式環境にあるかで、異なるスタイルの行列を使用したいと思いますalign*。数式モード ( \ifmmode) かどうかを確認する解決策はすでに見つかりましたが、 にあるかどうかを確認する解決策はありませんでしたalign。これまでのところ、私のコードは次のようになります。

\newcommand\cvec[1]{
    \relax\ifmmode\begin{smallmatrix}#1\end{smallmatrix}\else\begin{pmatrix}#1\end{pmatrix}\fi}

それとも、これを行うための別の簡単なコマンドがありますか?

答え1

私はそのようなアプローチは避けます。インライン数式内の行列は、行smallmatrix数が 2 行を超えるとすぐにベースライン間の等距離が損なわれるため、極力控えめに使用する必要があります。

パッケージにはと がamsmath用意されているので、次の方法で目的を達成できるかもしれません。\ifinalign@\ifingather@

\makeatletter
\newcommand\cvec[1]{%
  \relax
  \ifinalign@
    \expandafter\@firstoftwo
  \else
    \ifingather@
      \expandafter\expandafter\expandafter\@firstoftwo
    \else
      \expandafter\expandafter\expandafter\@secondoftwo
    \fi
  \fi
  {\begin{pmatrix}#1\end{pmatrix}}%
  {\left(\begin{smallmatrix}#1\end{smallmatrix}\right)}%
}
\makeatother

ただし、マクロはまたは\cvecでは期待どおりに動作しません。 およびは(すべての場合で の有無にかかわらず) の代替としてではなく、複数行表示の場合にのみ使用することに注意してください。equationmultlinealigngatherequation*

\cvecこれらすべてのケースで が正しく動作することを保証する唯一の正しい方法は、を使用することです\mathchoice

\newcommand{\cvec}[1]{%
  \mathchoice{\begin{pmatrix}#1\end{pmatrix}}
    {\left(\begin{smallmatrix}#1\end{smallmatrix}\right)}
    {\text{$\left(\begin{smallmatrix}#1\end{smallmatrix}\right)$}}
    {\text{$\left(\begin{smallmatrix}#1\end{smallmatrix}\right)$}}
}

完全な例

\documentclass{article}
\usepackage{amsmath}
\newcommand{\cvec}[1]{%
  \mathchoice{\begin{pmatrix}#1\end{pmatrix}}
    {\left(\begin{smallmatrix}#1\end{smallmatrix}\right)}
    {\text{$\left(\begin{smallmatrix}#1\end{smallmatrix}\right)$}}
    {\text{$\left(\begin{smallmatrix}#1\end{smallmatrix}\right)$}}
}

\begin{document}
$\cvec{a\\b}$
\begin{align}
\cvec{a\\b}
\end{align}
\begin{gather}
\cvec{a\\b}
\end{gather}
\begin{equation}
\cvec{a\\b}
\end{equation}
\begin{multline}
x\\\cvec{a\\b}
\end{multline}
\end{document}

ここに画像の説明を入力してください

上記の定義を試してみると、式 3 と 4 の出力が になることがわかりますsmallmatrix

*私の提案は、アスタリスクを簡単に追加または削除できるように、-variant を使用してマクロを定義することです。

\makeatletter
\newcommand{\cvec}{\@ifstar{\thomas@scvec}{\thomas@cvec}}
\newcommand{\thomas@scvec}[1]{%
  \text{$\left(\begin{smallmatrix}#1\end{smallmatrix}\right)$}}
\newcommand{\thomas@cvec}[1]{\begin{pmatrix}#1\end{pmatrix}}
\makeatother

あるいはxparse

\usepackage{xparse}
\NewDocumentCommand{\cvec}{ s m }{%
 \IfBooleanTF{#1}
   {\text{$\left(\begin{smallmatrix}#1\end{smallmatrix}\right)$}}
   {\begin{pmatrix}#1\end{pmatrix}}%
}

\cvec*インライン モードと表示モードで使用します。上付き/下付き文字で使用する予定がない場合は\cvec省略できます。\text\cvec*

答え2

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\makeatletter
\begin{align}
  \ifinalign@ true \else false \fi
\end{align}

\[
  \ifinalign@ true \else false \fi
\]
\end{document}

関連情報