2

I was trying to create an environment for an augmented matrix and came across this answer, which gave me this code

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

used as

\begin{amatrix}{2}
   1 & 2 & 3 \\  a & b & c
 \end{amatrix}

It works great, but it drives me nuts that the argument is one fewer than the total number of columns in the augmented matrix. I tried changing the #1 to #1-1, but that does not seem to work. The closest thing I could find was the calc package, but that only works in certain circumstances, and this does not seem to be one of them.

So my question is: is there a way to either redefine the amatrix environment so that the argument matches the total number of columns in the augmented matrix? An even better solution would be one where I don't have to put in an argument at all, but I don't want to be greedy here.

2 Answers2

3

An even better solution would be one where I don't have to put in an argument at all, but I don't want to be greedy here.

Be greedy! Here is a solution with tabularray package (and no argument at all):

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}

\NewDocumentEnvironment{amatrix}{+b}{ \begin{+pmatrix}[ colsep=4pt, rowsep=0pt, vline{Y}={-}{solid}, column{1}={leftsep=1pt}, column{Z}={rightsep=1pt} ] #1 \end{+pmatrix}% }{}

\begin{document}

\[
\begin{amatrix}
    1 & 2 & 3 \\
    a & b & c 
\end{amatrix}
\]

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

\end{document}

enter image description here

(Thanks to samcarter for her suggestion).

CarLaTeX
  • 62,716
2

This addresses your requirement of specifying the number of columns. \numexpr#1-1 removes 1 from the input:

enter image description here

\documentclass{article}

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

\begin{document}

[ \begin{amatrix}{3} 1 & 2 & 3 \ a & b & c \end{amatrix} ]

\end{document}

Werner
  • 603,163