5

I'd like to have an environment to make augmented matrices with multiple columns on the right hand side. In this question I found a nice environment that makes an augmented matrix with one column on the right hand side.

The code in that question is:

\newenvironment{amatrix}[1]{%
  \left(\begin{array}{@{}*{#1}{c}|c@{}}
}{%
  \end{array}\right)
}
Peter
  • 335
  • 2
  • 9

2 Answers2

4

Your solution is good, but I propose a more intuitive syntax:

\documentclass{article}
\usepackage{amsmath}

\NewDocumentEnvironment{amatrix}{>{\SplitArgument{1}{|}}m} {\left(\makeamatrix#1} {\end{array}\right)} \NewDocumentCommand{\makeamatrix}{mm}{% \IfNoValueTF{#2} {\begin{array}{@{}{#1}{c}@{}}} {\begin{array}{@{}{#1}{c}|*{#2}{c}@{}}}% }

\begin{document}

[ \begin{amatrix}{4} a & b & c & d \ v & w & x & y \end{amatrix} \quad \begin{amatrix}{3|1} a & b & c & d \ v & w & x & y \end{amatrix} \quad \begin{amatrix}{2|2} a & b & c & d \ v & w & x & y \end{amatrix} ]

\end{document}

enter image description here

You may need \usepackage{xparse} if you are running LaTeX prior to 2020-10-01.

egreg
  • 1,121,712
3

I adapted the above code to

\newenvironment{amatrix}[2][1]{%
  \left(\begin{array}{@{}*{#2}{c}|*{#1}{c}@{}}
}{%
  \end{array}\right)
}

which can then be used as

\begin{amatrix}[2]{2}
    a & b & c & d \\
    v & w & x & y
\end{amatrix}

The first argument is optional, and it indicates the number of columns to the right of the vertical line. The default value is 1. The second argument is mandatory and indicates the number of columns to the left of the vertical line.

Peter
  • 335
  • 2
  • 9