I've been using matrices in Latex for while but have found them somewhat tedious to write. Is it possible to create a new command \nmatrix (for instance) in latex that allows matrices to be written in the following form:


And would yield the following matrix:

Example Matrix

I know you could define something like

\newcommand{\nmatrix}[4]{$ \begin{bmatrix} #1 & #2 \\ #3 & #4\\\end{bmatrix} $}

however this takes effort to write and ideally I'd like to be able to write any m by m matrix using the command \nmatrix by changing the number of inputs. Eg \nmatrix{1,2,3;4,5,6} would then yield a 2 by 3 matrix.


1 Answer 1


Tailor made for tabstackengine, which already has such a macro (and others).

\setstackEOL{;}% row separator
\setstackTAB{,}% column separator
\setstacktabbedgap{1ex}% inter-column gap
\setstackgap{L}{1.0\normalbaselineskip}% inter-row baselineskip

enter image description here

An optional argument exists to set the column alignment as l, c, or r.

It also has a \fixTABwidth{<T or F>} macro to set all columns to the same width (or not), shown before and after:

enter image description here

  • Thanks for this answer, I was wondering though, is there any way to do this with round brackets instead of square brackets? Thanks! May 26, 2017 at 12:26
  • 1
    @SamDavenport Sure. \parenMatrixstack. There is also \braceMatrixstack, \vertMatrixstack, and simply \Matrixstack. See tabstackengine documentation at ctan.org/pkg/tabstackengine May 26, 2017 at 13:38

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