# Aligning equation at the \middle| of a \left ... \middle ... \right triple

I'm typesetting a matrix inversion procedure by means of Gaussian elimination. This typically involves writing a big matrix with a bar in the middle, which separates the original matrix from the to-be inverted matrix. I would like to typeset this so that the different rows are aligned precisely at the bar in the middle.

A MWE of my current code is the following, using the physics package to typeset the matrices (but the amsmath could certainly manage it just as fine with a few extra lines of code) and \left(, \middle|, and \right) to typeset the parentheses surrounding the matrix and the bar in the middle.

\documentclass{article}
\usepackage{amsmath}
\usepackage{physics}
\begin{document}
\begin{align}
&\left(\mqty{a & b \\ c & d}\middle|\mqty{1 & 0 \\ 0 & 1}\right), \\
&\left(\mqty{1 & \frac{b}{a} \\ c & d}\middle|\mqty{\frac{1}{a} & 0 \\ 0 & 1}\right), \\
&\left(\mqty{1 & \frac{b}{a} \\ 0 & \frac{ad - bc}{a}}\middle|\mqty{\frac{1}{a} & 0 \\ - \frac{c}{a} & 1}\right), \\
&\left(\mqty{1 & \frac{b}{a} \\ 0 & 1}\middle|\mqty{\frac{1}{a} & 0 \\ - \frac{c}{ad - bc} & \frac{a}{ad - bc}}\right), \\
&\left(\mqty{1 & 0 \\ 0 & 1}\middle|\mqty{\frac{1}{a} + \frac{bc}{a(ad - bc)} & - \frac{b}{ad - bc} \\ - \frac{c}{ad - bc} & \frac{a}{ad - bc}}\right), \\
&\left(\mqty{1 & 0 \\ 0 & 1}\middle|\mqty{\frac{ad - bc + bc}{a(ad - bc)} & - \frac{b}{ad - bc} \\ - \frac{c}{ad - bc} & \frac{a}{ad - bc}}\right), \\
&\left(\mqty{1 & 0 \\ 0 & 1}\middle|\mqty{\frac{d}{ad - bc} & - \frac{b}{ad - bc} \\ - \frac{c}{ad - bc} & \frac{a}{ad - bc}}\right).
\end{align}
\end{document}


Here is the current output:

Note that while the MWE inverts a 2x2 matrix, my actual calculation is more involved and I would like a solution that works for a matrix of arbitrary size.

If you don't mind using LuaLaTeX, then luamathalign can help. \AlignHere instead of & can be used inside \left\right blocks:

\documentclass{article}
\usepackage{amsmath}
\usepackage{physics}
\usepackage{luamathalign}
\begin{document}
\begin{align}
\left(\mqty{a & b \\ c & d}\AlignHere\middle|\mqty{1 & 0 \\ 0 & 1}\right), \\
\left(\mqty{1 & \frac{b}{a} \\ c & d}\AlignHere\middle|\mqty{\frac{1}{a} & 0 \\ 0 & 1}\right), \\
\left(\mqty{1 & \frac{b}{a} \\ 0 & \frac{ad - bc}{a}}\AlignHere\middle|\mqty{\frac{1}{a} & 0 \\ - \frac{c}{a} & 1}\right), \\
\left(\mqty{1 & \frac{b}{a} \\ 0 & 1}\AlignHere\middle|\mqty{\frac{1}{a} & 0 \\ - \frac{c}{ad - bc} & \frac{a}{ad - bc}}\right), \\
\left(\mqty{1 & 0 \\ 0 & 1}\AlignHere\middle|\mqty{\frac{1}{a} + \frac{bc}{a(ad - bc)} & - \frac{b}{ad - bc} \\ - \frac{c}{ad - bc} & \frac{a}{ad - bc}}\right), \\
\left(\mqty{1 & 0 \\ 0 & 1}\AlignHere\middle|\mqty{\frac{ad - bc + bc}{a(ad - bc)} & - \frac{b}{ad - bc} \\ - \frac{c}{ad - bc} & \frac{a}{ad - bc}}\right), \\
\left(\mqty{1 & 0 \\ 0 & 1}\AlignHere\middle|\mqty{\frac{d}{ad - bc} & - \frac{b}{ad - bc} \\ - \frac{c}{ad - bc} & \frac{a}{ad - bc}}\right).
\end{align}
\end{document}


The result: