[Tex/LaTex] Augmented matrix with fractions

fractionsmath-modematrices

I need to typeset some augmented matrices, but some of them contain fractions.
I also need to do that with gmatrix, as I need its features.

The following code is used to create the line in between the entries:

\newcommand{\mline}{%
  \hspace{-\arraycolsep}%
  \strut\vrule
  \hspace{-\arraycolsep}%
}

With that I can create something like this:

Example: Augmented matrix without fractions

Code:

\begin{align} 
    \begin{gmatrix}[p] 
        1 & 2 & 3 & \mline & 40 \\
        2 & 3 & 4 & \mline & 500 \\
        3 & 4 & 5 & \mline & 6000
    \end{gmatrix} 
\end{align}

But when it contains fractions, it looks more like this:

Example: Augmented matrix with fractions

Code:

\begin{align} 
    \begin{gmatrix}[p] 
        1 & 2 & \frac{1}{3} & \mline & 40 \\
        2 & 3 & \frac{1}{4} & \mline & 500 \\
        3 & 4 & 5 & \mline & \frac{1}{6}
    \end{gmatrix} 
\end{align}

The line gets kind of dashed which does not really work. It also looks unaesthetic that the fractions almost touch.

I hope you are able to help me.

Best Answer

I can offer you a manual fix:

\documentclass{article}
\usepackage{amsmath}
\usepackage{gauss}

% patch gauss macros for doing their work in `align'
% and other amsmath environments; see
% http://tex.stackexchange.com/questions/146532/
\usepackage{etoolbox}
\makeatletter
\patchcmd\g@matrix
 {\vbox\bgroup}
 {\vbox\bgroup\normalbaselines}% restore the standard baselineskip
 {}{}
\makeatother


\newcommand{\mline}[1][0pt]{%
  \hspace{-\arraycolsep}%
  \ifdim#1>0pt
    \dimen0=\ht\strutbox \dimen2=\dimen0
    \advance\dimen0 #1\relax
    \ht\strutbox=\dimen0
  \fi
  \smash{\strut\vrule} % the `\vrule` is as high and deep as a strut
  % since assignments to \ht\strutbox are global, we restore the height
  \ifdim#1>0pt
    \ht\strutbox=\dimen2
  \fi
  \hspace{-\arraycolsep}%
}

\begin{document}
\[
\begin{gmatrix}[p]
1 & 2 & \mline & 3 \\
4 & 5 & \mline & 6 \\
7 & 8 & \mline[2pt] & \frac{1}{6}
\rowops
 \swap{0}{1}
 \mult{0}{\cdot 7}
 \add[5]{1}{2}
\end{gmatrix}
\]
\end{document}

enter image description here

For conflicting fractions, use a new \gfrac macro

\documentclass{article}
\usepackage{amsmath}
\usepackage{gauss}

% patch gauss macros for doing their work in `align'
% and other amsmath environments; see
% http://tex.stackexchange.com/questions/146532/
\usepackage{etoolbox}
\makeatletter
\patchcmd\g@matrix
 {\vbox\bgroup}
 {\vbox\bgroup\normalbaselines}% restore the standard baselineskip
 {}{}
\makeatother

\newcommand{\gfrac}[2]{\frac{\smash[b]{\mathstrut}#1}{\smash[t]{\mathstrut}#2}}

\newcommand{\BAR}[1][0pt]{%
  \hspace{-\arraycolsep}%
  \ifdim#1>0pt
    \dimen0=\ht\strutbox \dimen2=\dimen0
    \advance\dimen0 #1\relax
    \ht\strutbox=\dimen0
  \fi
  \smash{\strut\vrule} % the `\vrule` is as high and deep as a strut
  % since assignments to \ht\strutbox are global, we restore the height
  \ifdim#1>0pt
    \ht\strutbox=\dimen2
  \fi
  \hspace{-\arraycolsep}%
}

\begin{document}
\[
\begin{gmatrix}[p]
1 & \gfrac{1}{3} & \BAR & 3 \\
4 & \gfrac{1}{4} & \BAR[4pt] & 6 \\
7 & 8 & \BAR[2pt] & \frac{1}{6}
\rowops
 \swap{0}{1}
 \mult{0}{\cdot 7}
 \add[5]{1}{2}
\end{gmatrix}
\]
\end{document}

enter image description here

However, my advice is to use the slashed form for fractions.

Related Question