1

I use a \arr macro as a shortcut for bmatrix environments. In addition, I use a hacked version of bmatrix (see: What's the best way make an “augmented” coefficient matrix?) so that it's easier to typeset augmented matrices.

In theory, \arr{SOMETHING} should be equivalent to \begin{bmatrix}SOMETHING\end{bmatrix}, and most of the time it is. For instance,

\[
\arr   {  2 &  5 }
\arr   {  1 \\ 2 },
\arr   {  1 &  3 }
\arr   {  1 \\ 2 }
\]

produces

enter image description here

However, when I put the lines above inside another \arr, in order to obtain a nested matrix, it blows up

\[
\arr{
\arr   {  2 &  5 }
\arr   {  1 \\ 2 } \\
\arr   {  1 &  3 }
\arr   {  1 \\ 2 }
}
\]

enter image description here

with an error that says "incomplete \ifdim", whereas

\begin{bmatrix}
  \begin{bmatrix}
    2 & 5
  \end{bmatrix}
  \begin{bmatrix}
    1 \\ 2
  \end{bmatrix}
  \\
  \begin{bmatrix}
    1 & 3
  \end{bmatrix}
  \begin{bmatrix}
    1 \\ 2
  \end{bmatrix}
\end{bmatrix}

works

enter image description here

Why?

Below follows the code

\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}

\makeatletter

% https://tex.stackexchange.com/questions/2233/
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols r]{%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{#1}}

\newcommand{\arr}[2][\@empty]{%
  \setbox\@tempboxa\hbox{#1}
  \ifdim\wd\@tempboxa=0pt
  \begin{bmatrix}#2\end{bmatrix}
  \else
  \begin{bmatrix}[#1]#2\end{bmatrix}
  \fi
}

\makeatother


\begin{document}

\[
\arr   {  2 &  5 }
\arr   {  1 \\ 2 },
\arr   {  1 &  3 }
\arr   {  1 \\ 2 }
\]
% \[
% \arr{
% \arr   {  2 &  5 }
% \arr   {  1 \\ 2 } \\
% \arr   {  1 &  3 }
% \arr   {  1 \\ 2 }
% }
% \]
% \[
% \begin{bmatrix}
%   \begin{bmatrix}
%     2 & 5
%   \end{bmatrix}
%   \begin{bmatrix}
%     1 \\ 2
%   \end{bmatrix}
%   \\
%   \begin{bmatrix}
%     1 & 3
%   \end{bmatrix}
%   \begin{bmatrix}
%     1 \\ 2
%   \end{bmatrix}
% \end{bmatrix}
% \]
\end{document}

EDIT: unsimplified definition of \arr follows:

\newcommand{\arr}{\@ifstar\arr@star\arr@nostar}
\newcommand{\arr@nostar}[2][\@empty]{%
  \setbox\@tempboxa\hbox{#1}\ifdim\wd\@tempboxa=0pt
  \begin{bmatrix}#2\end{bmatrix}
  \else \begin{bmatrix}[#1]#2\end{bmatrix} \fi
}
\newcommand{\arr@star}[2][\@empty]{%
  \setbox\@tempboxa\hbox{#1}\ifdim\wd\@tempboxa=0pt
  \begin{matrix}#2\end{matrix}
  \else \begin{matrix}[#1]#2\end{matrix} \fi
}
  • 1
    Why not \newcommand{\arr}[2][c]{\begin{bmatrix}[#1]#2\end{bmatrix}} – David Carlisle Jan 23 '15 at 11:48
  • @DavidCarlisle I think it's because I have a starred version of \arr that uses no brackets. Here I simplified by renaming \arr@nostar to \arr, but in reality is \arr is a "dispatcher" macro. – Ernest A Jan 23 '15 at 11:52
2

\setbox is a rather error prone way to test if an argument is empty, but here I don't think you need the test at all:

\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}

\makeatletter

% http://tex.stackexchange.com/questions/2233/
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols r]{%
  \hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{#1}}


\newcommand{\arr}{\@ifstar\arr@star\arr@nostar}

\newcommand{\arr@star}[2][*\c@MaxMatrixCols r]{%
  \begin{matrix}[#1]#2\end{matrix}%
}

\newcommand{\arr@nostar}[2][*\c@MaxMatrixCols r]{%
  \begin{bmatrix}[#1]#2\end{bmatrix}%
}



\makeatother


\begin{document}

\[
\arr   {  2 &  5 }
\arr   {  1 \\ 2 },
\arr   {  1 &  3 }
\arr   {  1 \\ 2 }
\]
 \[
 \arr{
 \arr   {  2 &  5 }
 \arr   {  1 \\ 2 } \\
 \arr   {  1 &  3 }
 \arr   {  1 \\ 2 }
 }
 \]
% \[
% \begin{bmatrix}
%   \begin{bmatrix}
%     2 & 5
%   \end{bmatrix}
%   \begin{bmatrix}
%     1 \\ 2
%   \end{bmatrix}
%   \\
%   \begin{bmatrix}
%     1 & 3
%   \end{bmatrix}
%   \begin{bmatrix}
%     1 \\ 2
%   \end{bmatrix}
% \end{bmatrix}
% \]
\end{document}
  • Correct. I don't know why I put the test in the first place. – Ernest A Jan 23 '15 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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