3

So I'm trying to make my own command to make matrices less painful

My current command is:

\newcommand{\mat}[2]{
\left(\begin{array}{#1}{#2}\end{array}\right)}

with the intention to use it as:

\mat{cc}{1&0\\0&1}

producing the same thing as

\left(
\begin{array}{cc}
1 & 0 \\
0 & 1
\end{array}\right)

But I keep getting the message "Missing } inserted." I think it doesn't like the \\ in the command argument.

Any suggestions?

Edit: Fixed it. Removed the brace group from the macro

  • 1
    See the tabstackengine package. It does this already. Either \tabularCenterstack or \parenMatrixstack should do the trick. See http://tex.stackexchange.com/questions/139771/writing-a-table-with-equally-spaced-columns-based-on-the-widest-column/139946#139946, and http://tex.stackexchange.com/questions/148669/can-i-tab-inside-of-align-environment/148684#148684 – Steven B. Segletes Mar 05 '14 at 02:07
  • Thanks, but I'm also just curious as to why this isn't working – sicklybeans Mar 05 '14 at 02:10
  • This may very well not be the problem, but I really don't think you should wrap #2 in a brace group. (And welcome to TeX.SX!) I don't think \\ likes being in a brace group. – Sean Allred Mar 05 '14 at 02:34
  • thank you. Getting rid of the brace group fixed the problem! – sicklybeans Mar 05 '14 at 02:36
  • Glad to help—don't forget you can accept the answer soon :) That's the way we publicly say 'thank you' on the SE network, and it's a good way of telling everyone that your issue is resolved. – Sean Allred Mar 05 '14 at 02:49
  • 1
    A general view of matrix construction is available in Where is the \matrix command? – Werner Mar 05 '14 at 17:07

2 Answers2

8

Remove the spurious braces in your definition:

\documentclass{article}
\newcommand{\mat}[2]{
  \left(
  \begin{array}{#1}
    #2
  \end{array}
  \right)
}
\begin{document}
\[ \mat{c}{2\\3} \]
\end{document}

Why is this necessary?

Brace groups are kind of a special thing in TeX (take a look at TeX by Topic for detailed information; run texdoc texbytopic in your terminal). When you put something in a brace group, you are asking TeX to process it as a unit. The problem is, with array there are many things that need to be dealt with as individual units inside, such as rows. (The real reason actually has to do with things like 'vertical mode' and 'horizontal mode', but the above explanation will hold for many other things.)

Sean Allred
  • 27,421
  • But this approach still will not work with alignment tabs (&) though, or am I missing something? – Steven B. Segletes Mar 05 '14 at 03:06
  • @StevenB.Segletes it does work. – azetina Mar 05 '14 at 03:22
  • @StevenB.Segletes The real problem is that TeX is switching from horizontal mode into vertical mode when it encounters the break—this can't really be done in a brace group to the best of my understanding. The alignment character doesn't switch modes, so it's alright. – Sean Allred Mar 05 '14 at 05:58
1

The tabstackengine package does this already, as I mentioned in my comment. The first form gives the parens with the stack, but all columns must be of the same alignment. The second form needs the parens added separately, but allows each column to have its own alignment.

The examples below are not pretty, but are merely meant to demonstrate these alignment variations.

\documentclass{article}
\usepackage{tabstackengine}
\parskip 1ex
\begin{document}
\setstacktabbedgap{1ex}
$\parenMatrixstack[r]{1 & 23 \\34 & 1}$\par
\setstacktabulargap{1ex}
$\left(\tabularCenterstack{lc}{1 & 23 \\34 & 1} \right)$
\end{document} 

enter image description here