2

Is it possible to define a new matrix environment with a parameter function at the end?

This is the enviorment I am trying to use

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

So that I can type this simple matrix.

\begin{amatrix}{3}{r_1\leftrightarrow r_3}
-R_1    &   R_2 &   0   &   E_2-E_1 \\
0       &   R_2 &   R_3 &   E_2     \\
1       &   1   &   -1  &   0       \\
\end{amatrix}

and get this result.

What the data should look like?

I know that I can define the environment and the command separately as

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

\newcommand{\arr}{$\stackrel{#2}{\rightarrow}$}

then I just need to type

\begin{amatrix}{3}
-R_1    &   R_2 &   0   &   E_2-E_1 \\
0       &   R_2 &   R_3 &   E_2     \\
1       &   1   &   -1  &   0       \\
\end{amatrix}
\arr{r_1\leftrightarrow r_3}

but is it possible to combined this into one environment, and how would I do this?

JFaris
  • 35
  • 3
  • 1
    welcome to tex.sx. if you provide a small compilable example that can be experimented with, i'm sure there is someone here who would be delighted to experiment. – barbara beeton Dec 11 '17 at 20:49
  • I'm not sure this idea is good: the arrow belongs logically after the matrix and connects two of them. – egreg Dec 11 '17 at 21:54
  • @egreg I am still new to LaTeX and I'm trying to learn the program. The document I am typing is showing the steps of solving a system of linear equation using a matrix. I was thinking about combined the matrix and the arrow into one environment to simplify the coding, since I am using this same setup so many times. – JFaris Dec 12 '17 at 16:23

2 Answers2

3

The fundamental problem you're running into here is that the \end{<env>} definition of a \newenvironment{<env>} that takes arguments does not have access to these arguments by default (see Why can't the end code of an environment contain an argument?). There are ways around it though...

Here is a suggested interface using xparse. It allows you to specify an optional second argument for the amatrix construction (since you might not need it at every iteration):

enter image description here

\documentclass{article}

\usepackage{mathtools,xparse}

\NewDocumentEnvironment{amatrix}{ m o }
  {
    \left[\begin{array}{ @{} *{#1}{c} | c @{} }
  }{
    \end{array}\right]
    \IfValueT{#2}
      {\xrightarrow{#2}}
  }

\begin{document}

\[
  \begin{amatrix}{3}[r_1 \leftrightarrow r_3]
  -R_1    &   R_2 &   0   &   E_2-E_1 \\
  0       &   R_2 &   R_3 &   E_2     \\
  1       &   1   &   -1  &   0
  \end{amatrix}
  \begin{amatrix}{3}
  -R_1    &   R_2 &   0   &   E_2-E_1 \\
  0       &   R_2 &   R_3 &   E_2     \\
  1       &   1   &   -1  &   0
  \end{amatrix}
\]

\end{document}
Werner
  • 603,163
  • The results look arguably nicer if you replace \stackrel{#2}{\rightarrow} by \xrightarrow{#2} (requires mathtools). –  Dec 11 '17 at 21:00
  • @marmot: Awesome suggestion. – Werner Dec 11 '17 at 21:12
  • @Werner Thank you for your information. I am still new to LaTeX and I was not able to get this idea to work. I just have two questions about your solution. 1) Since this new environment is a matrix would it be a good idea to add '$' to the beginning and end of the environment? Or is there something wrong with this idea? 2) Why did you add '[' and ']' just before and after the matrix? – JFaris Dec 12 '17 at 16:44
  • @JFaris: Adding $...$ inside your definition necessarily puts the environment content inside math mode. That means you can't use it inside another math environment (like \[...\] or $...$). For this there is \ensuremath{...}, but then the amatrix environment definition will have to be changed. I usually leave such switches (to math mode) out of the definition, since it encourages you to code relative to the context - insert math mode switches where they're needed so the syntax/context is clear. \[...\] puts its contents in display math mode without equation numbering. – Werner Dec 12 '17 at 17:13
  • @Werner Thank you for clarifying this for me. It makes sense to leave math mode out of the environment. – JFaris Dec 12 '17 at 17:22
0

I've used Werner's nice example and added a few (possibly desirable) options.

enter image description here

\documentclass{article}

\usepackage{mathtools,xparse}

\NewDocumentEnvironment{amatrix}{ m d() o d() o D(){r} }
  {
    \left[\begin{array}{ @{} *{#1}{c} | c @{} }
  }{
    \end{array}\right]
    \IfValueT{#3}
      {\xrightarrow{\IfValueT{#2}{#2 \cdot} #6_{#3} 
    \IfValueT{#5}      
      { \IfValueTF{#4}{#4 \cdot}{\leftrightarrow} #6_{#5} } } }
  }

\begin{document}

\[
  \begin{amatrix}{3}[1][3]
  -R_1    &   R_2 &   0   &   E_2-E_1 \\
  0       &   R_2 &   R_3 &   E_2     \\
  1       &   1   &   -1  &   0
  \end{amatrix}
  \begin{amatrix}{3}
  1       &   1   &   -1  &   0       \\
  0       &   R_2 &   R_3 &   E_2     \\
  -R_1    &   R_2 &   0   &   E_2-E_1
  \end{amatrix}
\]

\[
  \begin{amatrix}{3}(2)[1](+3)[3]
  1       &   1   &   -1  &   0       \\
  0       &   R_2 &   R_3 &   E_2     \\
  -R_1    &   R_2 &   0   &   E_2-E_1
  \end{amatrix}
  \begin{amatrix}{3}
  1       &    1   &   -1  &   0       \\
  0       &    R_2 &   R_3 &   E_2     \\
  2-3R_1  & 2+3R_2 &   -2  &  3E_2-3E_1
  \end{amatrix}
\]

\[
  \begin{amatrix}{3}(4)[1]
  -R_1    &   R_2 &   0   &   E_2-E_1 \\
  0       &   R_2 &   R_3 &   E_2     \\
  1       &   1   &   -1  &   0
  \end{amatrix}
  \begin{amatrix}{3}
 -4R_1    &  4R_2 &   0   &   4E_2-4E_1 \\
  0       &   R_2 &   R_3 &   E_2     \\
  1       &   1   &   -1  &   0
  \end{amatrix}
\]

\end{document}
  • With your introductions I find that it unfortunately also removes some of the context from the row operations. \begin{amatrix}{3}{2r_1+3r_3} is far clearer than \begin{amatrix}{3}(2)[1](+3)[3]. – Werner Dec 12 '17 at 06:30
  • @Werner I completely agree with you, it was just a gimmick. –  Dec 12 '17 at 10:39