5

I'm trying to write a macro in which one of the arguments is a double backslash, to obtain a column matrix, but I'm unable to escape the \ character. Here's a simple example that throws an error. It also throws an error if I try \myBmatrix{z_1 \\ z_2} or \myBmatrix{z_1 \\\\\\ z_2}

(The actual macro I'm trying to write is more complex but I thought I'd start with this silly one)

\documentclass{amsart}
\def\myBmatrix#1{
    \begin{bmatrix}
    {{#1}}
    \end{bmatrix}
    }
\begin{document}
\begin{align}
\myBmatrix{z_1 \\\\ z_2}
\end{align}
\end{document}
1
  • What's the error? That is, what is the exact error message?
    – cfr
    Sep 10, 2016 at 2:36

2 Answers 2

3

You probably want (some of your lines are commented and replaced by my versions):

\documentclass{amsart}
\def\myBmatrix#1{
    \begin{bmatrix}
%    {{#1}}
    #1
    \end{bmatrix}
    }
\begin{document}
\begin{align}
%\myBmatrix{z_1 \\\\ z_2}
\myBmatrix{z_1 \\ z_2}
\end{align}
\end{document}

enter image description here

5
  • What is changing to \gdef supposed to do?
    – cfr
    Sep 10, 2016 at 3:02
  • @cfr You are right. This time it is unneeded. Corrected. Sep 10, 2016 at 3:05
  • \def works fine.
    – cfr
    Sep 10, 2016 at 3:08
  • Thanks @Przemysław Scherwentke. Could you explain why the curly brackets create a problem?
    – Leo Simon
    Sep 10, 2016 at 15:15
  • @LeoSimon A cell inside arraay-like environments is a group. Hence, using curly braces, you may obtain an unbalanced group. This is only general sketch of the problem: I haven't analysed matrix environment yet. Sep 10, 2016 at 16:30
4

\def will work if you just remove the excess of curly brackets and additional backslashes. However, \newcommand* would be better since this is for LaTeX.

\documentclass{amsart}
\newcommand*\myBmatrix[1]{%
  \begin{bmatrix}
    #1
  \end{bmatrix}%
}
\begin{document}
\begin{align}
  \myBmatrix{z_1 \\ z_2}
\end{align}
\end{document}

Remember that a backslash doesn't escape a backslash in TeX. If \\ doesn't work, \\\\ isn't likely to be better ;).

output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .