[Tex/LaTex] How to move the equation numbers to bottom lines

equations

enter image description here

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{mathtools}
\begin{document}
\abovedisplayskip=0pt\relax
\begin{align}
\begin{multlined}[b][7cm]
\framebox[5cm]{Top LHS}\\
\framebox[5cm]{Middle LHS}\\
\framebox[5cm]{Bottom LHS}
\end{multlined}
    &= \begin{multlined}[t][7cm]
            \framebox[5cm]{Top RHS 1}\\
            \framebox[5cm]{Middle RHS 1}\\
            \framebox[5cm]{Bottom RHS 1}
        \end{multlined}\\
    &= \begin{multlined}[t][7cm]
            \framebox[5cm]{Top RHS 2}\\
            \framebox[5cm]{Middle RHS 2}\\
            \framebox[5cm]{Bottom RHS 2}
        \end{multlined}
\end{align}
\end{document}

Best Answer

Usually the baseline of the equation number is aligned with the base line of the equation. The multlined with option t is just a box with a large depth.

You want to move the equation number down to the "bottom line", but this is not well defined:

  • An equation can have several multlined environments with different bottom base lines.
  • In the question the lines are boxed. Thus the baseline or depth of the equation number can be aligned with the bottom rule or maximum depth of the equation or the base line of the deepest element.
  • The bottom line could be the bottom line of \substack, used as subscript of an integral/sum operator.

Therefore I think, it depends on the equation, where the best place for the equation number is. The following example defines \tagvpos that sets a position label to mark the vertical position, where the equation tag should go:

  • It is implemented for the measuring environments of package amsmath (e.g. gather, align, not equation).
  • It uses package zref-savepos for the position label that uses a feature of pdfTeX that is available in both modes PDF and DVI and that is also supported by XeTeX and LuaTeX.
  • Package amsmath has the feature that it moves the equation tag down, if the equation is too wide. In the latter case, the equation number is not additionally moved via \tagvpos.

Example file:

\documentclass{article}
\usepackage[
  a4paper,
  hmargin=48mm,
]{geometry}
\usepackage{mathtools}

\usepackage{zref-savepos}
\makeatletter
\@ifdefinable\saved@make@display@tag{%
  \@ifdefinable\saved@maketag@@@{%
    \@ifdefinable\tagvpos@label{%
      \newif\iftagvpos@shifttag@
      \g@addto@macro{\shifttag@true}{%
        \global\tagvpos@shifttag@true
      }%
      \newcommand*{\tagvpos@reset}{%
        \global\let\tagvpos@label\relax
        \global\tagvpos@shifttag@false
      }%
      \let\saved@make@display@tag\make@display@tag
      \def\make@display@tag{%
        \ifmeasuring@
          \tagvpos@reset
        \else
          \let\saved@maketag@@@\maketag@@@
          \def\maketag@@@##1{%
            \ifx\tagvpos@label\relax
              \tagvpos@reset
              \expandafter\@firstoftwo
            \else
              \zref@refused{\tagvpos@label a}%
              \zref@refused{\tagvpos@label b}%
              \begingroup
                \let\@auxout=\@mainaux
                \zsaveposy{\tagvpos@label b}%
              \endgroup
              \ifx\tag@shifts\@empty
              \else
                \if1\shift@tag\row@\relax
                  \global\tagvpos@shifttag@true
                \fi
              \fi
              \iftagvpos@shifttag@
                \tagvpos@reset
                \expandafter\expandafter\expandafter\@firstoftwo
              \else
                \expandafter\expandafter\expandafter\@secondoftwo
              \fi
            \fi
            {%
              \saved@maketag@@@{##1}%
            }{%
              \raisebox{%
                \dimexpr\zposy{\tagvpos@label a}sp-\zposy{\tagvpos@label b}sp\relax
              }{\saved@maketag@@@{##1}}%
              \tagvpos@reset
            }%
          }%
        \fi
        \saved@make@display@tag
      }%
      \begingroup
        \let\@addtoreset\ltx@gobbletwo
        \newcounter{tagvpos}
      \endgroup
      \renewcommand*{\thetagvpos}{tagvpos\number\value{tagvpos}}
      \newcommand*{\tagvpos}{%
        \ifmeasuring@
        \else
          \stepcounter{tagvpos}%
          \xdef\tagvpos@label{\thetagvpos}%
          \begingroup
            \let\@auxout=\@mainaux
            \zsaveposy{\tagvpos@label a}%
          \endgroup
        \fi
      }%
    }%
  }%
}
\makeatother

% Testing

\usepackage{color}
\newcommand*{\tagmark}{%
  \begingroup
    \sbox0{$\vcenter{}$}%
    \raisebox{-\ht0}[0pt][0pt]{\textcolor{red}{$\,\mapsto$}}%
  \endgroup
}

\begin{document}

\begin{gather}
  \text{environment \texttt{gather}}\nonumber
  \\
  \hspace{.75\linewidth}1 = 1
  \\
  \hspace{.75\linewidth}0.5 = \frac{1}{2\tagmark\tagvpos}\\
  \tag{a}
  \label{a}
  \hspace{.75\linewidth}\ref{a} = \frac{\eqref{a}\tagmark\tagvpos}{1}
  \\
  \hspace{\linewidth}
  \llap{%
    $\dots$
    \begin{tabular}[t]{@{}r@{}}
       very long\\equation\tagmark\tagvpos
    \end{tabular}%
  }
\end{gather}

\begin{align}
\text{environment \texttt{align}} &+ \text{\texttt{multlined}}\nonumber\\
\begin{multlined}[b][5cm]
\framebox[3cm]{Top LHS}\\
\framebox[3cm]{Middle LHS}\\
\framebox[3cm]{Bottom LHS}
\end{multlined}
    &= \begin{multlined}[t][5cm]
            \framebox[3cm]{Top RHS 1}\\
            \framebox[3cm]{Middle RHS 1}\\
            \framebox[3cm]{Bottom RHS 1\tagmark\tagvpos}
        \end{multlined}\\
    &= \begin{multlined}[t][5cm]
            \framebox[3cm]{Top RHS 2}\\
            \framebox[3cm]{Middle RHS 2}\\
            \framebox[3cm]{Bottom RHS 2\tagmark\tagvpos}
        \end{multlined}
\end{align}

\begin{align}
\hspace{\linewidth}
  \llap{%
    $\dots$
    \begin{tabular}[t]{@{}r@{}}
       very long\\equation\tagmark\tagvpos
    \end{tabular}%
  }
\end{align}
\end{document}

Result

Remarks:

  • The internal position labels are automatically named using a counter. The labels are written to the main .aux file only to keep the uniqueness of the label names, if \include files are involved (\let\@auxout\@mainaux). Also the counter for the label names is not reset in .aux files for \include files (this is the purpose of the local \let\@addtoreset\@gobbletwo right before \newcounter).

  • Shift detection for too wide equations: Environment gather uses a switch, whereas align stores the shift state in a list for all rows.