Comprobar si está en un entorno align*

Comprobar si está en un entorno align*

Quiero usar diferentes estilos de matrices si TeX está en un align*entorno o en un entorno matemático normal. Ya encontré una solución para verificar si está en modo matemático ( \ifmmode), pero no algo para verificar si está en modo align. Hasta ahora mi código se vería así:

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

¿O hay otro comando sencillo para hacer esto?

Respuesta1

Yo evitaría tal enfoque. Las matrices en fórmulas en línea deben usarse con mucha moderación, porque tan pronto como a smallmatrixtiene más de dos filas, arruinará la equidistancia entre las líneas de base.

El amsmathpaquete proporciona \ifinalign@y \ifingather@, por lo que su objetivo podría lograrse si

\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

pero la \cvecmacro no funcionará como se esperaba en equationo multline. Tenga en cuenta que aligny gatherno debe usarse como sustituto de equation(con o sin *en todos los casos), sino solo para pantallas multilínea.

La única forma correcta de garantizar el correcto funcionamiento \cvecen todos estos casos es utilizar \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)$}}
}

Ejemplo completo

\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}

ingrese la descripción de la imagen aquí

Pruebe con la definición anterior y verá que en las ecuaciones 3 y 4 el resultado sería con a smallmatrix.

Mi sugerencia es definir una macro con una *variante -, para que el asterisco se pueda agregar o eliminar fácilmente.

\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

Alternativamente, con xparse,

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

Lo usará \cvec*para el modo en línea y \cvecpara el modo de visualización. Puede omitirlo \textsi no planea usarlo \cvec*en subíndices/superíndices.

Respuesta2

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

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

información relacionada