# Defining a good tab-able macro for matrices.

I want to declare some macros in my preamble which allow me to create 2 x 2 matrices more quickly, and in particular, I want to be able to tab through the four entry positions. I feel like this is super basic and I am missing an obvious solution. Ideally I would like the command to implement smallmatrix and pmatrix.

EDIT: I apologize for the confusion. What I mean was something like \newcommand{\norm}[1]{\left|\left|#1\right|\right|} where the command takes an argument and using tab-completion in TexWorks or enter-completion in Kile will put your cursor in the argument field automatically. I hope this clarification helps!

-
Please clarify what you mean by "tab through the four entry positions"? Perhaps you are using a smart editor (like emacs auctex) that examines your preamble and does some kind of intelligent autocompletion? –  Lev Bishop Aug 17 '10 at 3:44
@Lev, sorry I have added a bit to my post for clarification. –  BBischof Aug 17 '10 at 16:25

For the matrix macro, I've been using the following, although anything from other commenters will do:

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


Then \mat{a}{b}{c}{d} produces the matrix with rows (a, b) and (c, d). Insert this in the preamble.

Then you want to enable the autocompletion of this new command. Here is how to do this for TeXworks.

First, locate the file tw-latex.txt in the completion folder TeXworks resource directory; in Ubuntu, it is ~/.TeXworks/completion, on other systems, refer to the manual. Edit it as administrator (it's read-only by default on Linux).

Somewhere in the file (in alphabetical order, ideally), add the following line:

\mat{#INS#}{•}{•}{•}#RET#


This will add the Tab and Ctrl+Tab autocompletion for your new \mat command.

Alternatively, in TeXworks you can define an autocompletion macro like this:

\mat:=\left(\begin{array}{cc}#INS# & • \\• & • \\ \end{array}\right)#RET#


This will auto-expand \mat to the whole construction. This method has an advantage of working in all TeX documents without you having to define the \mat command in the preamble, or having to include the style file.

However, it is ugly, TeXworks specific, and needs autocomplete (whereas with the \mat defined via \newcommand, you can type it on your own efficiently). Hence this method is probably not preferred in this case, but it still may be useful for other constructions.

To modify autocompletion rules to your liking, refer to this section of the manual.

-

Do you mean just shortcuts? Here's one possibility defining commands instead of environments:

\documentclass{article}
\usepackage{amsmath}
\newcommand*{\pmat}[1]{\begin{pmatrix}#1\end{pmatrix}}
\newcommand*{\smat}[1]{\begin{smallmatrix}#1\end{smallmatrix}}
\begin{document}
$\pmat{a & b \\ c & d} \smat{e & f \\ g & h}$
\end{document}

-
I would prefer the commands to take 4 arguments. –  BBischof Aug 17 '10 at 16:39
Though, one argument is easier to type and in my opinion more readable. With the braces it's harder to see where the row ends, all arguments look the same, rows cannot be distinguished. If complex matrix entries would have braces too, mistakes could happen easily. Errors because of missing or too much braces happen often and aren't always easy to locate. –  Stefan Kottwitz Aug 17 '10 at 16:52

I'm not sure what you mean by tabbing through the four entries, but the following is a slight modification to what Stefan Kottwitz gave.

\documentclass{article}
\usepackage{amsmath}
\newcommand*\pmat[4]{\begin{pmatrix}#1&#2\\#3&#4\end{pmatrix}}
\newcommand*\smat[4]{\begin{smallmatrix}#1&#2\\#3&#4\end{smallmatrix}}
\begin{document}
$\pmat{a}{b}{c}{d} \smat{e}{f}{g}{h}$
\end{document}

-
I think this is exactly what I want! –  BBischof Aug 17 '10 at 16:40
then you should mark it as the accepted answer –  Will Robertson Aug 18 '10 at 10:14