align* 환경에 있는지 확인하세요.

align* 환경에 있는지 확인하세요.

align*TeX가 환경이나 일반 수학 환경에 있는 경우 다양한 스타일의 행렬을 사용하고 싶습니다 . 이미 수학 모드( \ifmmode) 에 있는지 확인하는 솔루션을 찾았지만 align. 지금까지 내 코드는 다음과 같습니다.

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

아니면 이를 수행하는 또 다른 간단한 명령이 있습니까?

답변1

나는 그런 접근 방식을 피하고 싶습니다. 인라인 수식의 행렬은 매우 자제해서 사용해야 합니다. smallmatrix2개 이상의 행이 있으면 기준선 사이의 등거리가 손상되기 때문입니다.

패키지 는 및 를 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}

관련 정보