Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to use different styles of matrices if TeX is in an align* environment or in a normal math environment. I already found a solution for checking if it is in math mode (\ifmmode) but not something to check if it is in an align. So far my code would look like this:

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

Or is there another simple command to do this?

share|improve this question
1  
In any math formula \ifmmode is true, so the false branch in your conditional will never be followed. Unfortunately, distinguishing between inline formulas and displayed ones requires \mathchoice. Don't use align* as a replacement for equation* (or \[...\]). –  egreg Jan 2 at 22:54
add comment

2 Answers

up vote 6 down vote accepted

I'd avoid such an approach. Matrices in inline formulas should be used very sparingly, because as soon as a smallmatrix has more than two rows, it will spoil the equidistance between baselines.

The amsmath package provides \ifinalign@ and \ifingather@, so your aim might be accomplished by

\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

but the \cvec macro will not work as expected in equation or multline. Note that align and gather should not be used as substitutes for equation (with or without * in all cases), but only for multiline displays.

The only correct way to ensure correct working of \cvec in all these cases is using \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)$}}
}

Full example

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

enter image description here

Try with the definition above and you'll see that in equations 3 and 4 the output would be with a smallmatrix.

My suggestion is to define a macro with a *-variant, so the asterisk can easily been added or dropped.

\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

Alternatively, with xparse,

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

You'll use \cvec* for the inline mode and \cvec for the display mode. You can leave out \text if you don't plan to use \cvec* in sub/superscripts.

share|improve this answer
add comment
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\makeatletter
\begin{align}
  \ifinalign@ true \else false \fi
\end{align}

\[
  \ifinalign@ true \else false \fi
\]
\end{document}
share|improve this answer
    
nice, except that it would be helpful to point out that any references that require \makeatletter are best done only in command definitions. having the @ "live" in the body of a document can cause confusion, if not actual problems. –  barbara beeton Jan 3 at 14:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.