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 would like to do something like this

\newcommand{\pmat}[1][2][3][4]{\begin{pmatrix} #1 & #2 \\ #3 & #4\end{pmatrix}}

But this produces the error

ERROR: LaTeX Error: Missing \begin{document}.
...                                              
l.42 \newcommand{\pmat}[1][2][3
                               ][4]{\begin{pmatrix} #1 & #2 \\ #3 & #4\end{p..

How do I create a newcommand \pmat such that \pmat{1}{2}{3}{4} produces \begin{pmatrix} 1 & 2 \\ 3 & 4\end{pmatrix}?

Thanks, Thomas

share|improve this question

3 Answers 3

up vote 11 down vote accepted

It's because the proper syntax is

\newcommand{\pmat}[4]{\begin{pmatrix} #1 & #2 \\ #3 & #4\end{pmatrix}}

The argument between the brackets is the number of arguments, not the argument's name.

share|improve this answer
    
Thanks! I had the feeling that I was just missing something elementary –  Thomas Jun 9 '11 at 16:32
\documentclass{article}
\usepackage{amsmath}
\newcommand\pmat[4]{\ensuremath{\begin{pmatrix} #1 & #2 \\ #3 & #4\end{pmatrix}}}

\begin{document}
\pmat{1}{2}{3}{4}
\end{document}
share|improve this answer
    
Thank you for the answer! –  Thomas Jun 9 '11 at 16:32
    
Is it a best practice to use \ensuremath? –  xport Jun 9 '11 at 18:10

The syntax for \newcommand (without optional argument) is

\newcommand{\<name>}[<number of arguments>]{<definition>}

so you can use

\newcommand{\pmat}[4]{%
  \begin{pmatrix} #1 & #2 \\ #3 & #4\end{pmatrix}
}
share|improve this answer
    
Thank you for the answer! –  Thomas Jun 9 '11 at 16:32

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.