# Issue with \\ in custom command

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

-
See the tabstackengine package. It does this already. Either \tabularCenterstack or \parenMatrixstack should do the trick. See tex.stackexchange.com/questions/139771/…, and tex.stackexchange.com/questions/148669/… –  Steven B. Segletes Mar 5 '14 at 2:07
Thanks, but I'm also just curious as to why this isn't working –  sicklybeans Mar 5 '14 at 2: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 5 '14 at 2:34
thank you. Getting rid of the brace group fixed the problem! –  sicklybeans Mar 5 '14 at 2:36
A general view of matrix construction is available in Where is the \matrix command? –  Werner Mar 5 '14 at 17:07

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.)

-
But this approach still will not work with alignment tabs (&) though, or am I missing something? –  Steven B. Segletes Mar 5 '14 at 3:06
@StevenB.Segletes it does work. –  azetina Mar 5 '14 at 3: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 5 '14 at 5:58

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}


-
If possible though, I'd like to be able to do it using vanilla latex. Making my own macros is about getting better at latex as much as it is about anything else –  sicklybeans Mar 5 '14 at 2:22
@sicklybeans Understood. Best wishes. Welcome to the site. –  Steven B. Segletes Mar 5 '14 at 2:25