[Tex/LaTex] Defining a good tab-able macro for matrices.

macrosmatrices

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.

Thanks in advance!

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!

Best Answer

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.

Related Question