% Copyright 2021 Karl Wette
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Karl Wette.

\ProvidesFile{linenoamsmathdemo.tex}
             [2021/09/30 Make amsmath work with lineno]

\documentclass{ltxdoc}

\ifdefined\AddToHook
  \newcommand{\loadorder}{\textsf{lineno} is loaded first, and then patches \textsf{amsmath} using \texttt{\textbackslash AddToHook}}
  \usepackage[mathlines]{lineno}
  \usepackage[leqno]{amsmath}
\else
  \newcommand{\loadorder}{\textsf{amsmath} is loaded first, and then patched directly by \textsf{lineno}}
  \usepackage[leqno]{amsmath}
  \usepackage[mathlines]{lineno}
\fi
\usepackage{lipsum}
\usepackage{hyperref}

\begin{document}

\title{Make \textsf{amsmath}\footnote{\url{https://ctan.org/pkg/amsmath}}~ work with \textsf{lineno}}
\author{Karl Wette}

\maketitle

This document demonstrates patches to the \textsf{amsmath} package to work with
the \textsf{lineno} package. The code I've used is largely based on the posts
here\footnote{\url{https://tex.stackexchange.com/a/461192}} and
here\footnote{\url{https://tex.stackexchange.com/a/443201}}; credit is due to
their author(s). In addition I've made a few refinements to handle some corner
cases.

\section*{Demonstration}

This section demonstrates that, with this package, line numbers are correctly
formatted when using \textsf{amsmath} math environments. \loadorder.

\newcounter{lipsumparagraph}
\newcommand{\nextlipsum}{\stepcounter{lipsumparagraph}\lipsum[\thelipsumparagraph][1-4]}
\newcommand{\crs}{\qquad\qquad\texttt{[\textbackslash\textbackslash{}*]}\\*}

\linenumbers

\subsection*{Normal text}

\nextlipsum

\subsection*{\texttt{equation}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{equation}
  E = m c^2 \,.
\end{equation}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{equation}
  E = m c^2 \,.
\end{equation}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{equation*}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{equation*}
  E = m c^2 \,.
\end{equation*}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{equation*}
  E = m c^2 \,.
\end{equation*}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{\textbackslash[\ldots\textbackslash]}}

\subsection*{With line numbers in equations}
\nextlipsum
\[
  E = m c^2 \,.
\]
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\[
  E = m c^2 \,.
\]
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{multline}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{multline}
  \frac{1}{1 + x} = 1 - x + x^{2} - x^{3} + x^{4} - x^{5} + x^{6} - x^{7} + x^{8} - x^{9} + \mathcal{O}(x^{10}) \,.
\end{multline}
\nextlipsum
\begin{multline}
  \frac{1}{1 + x} = 1 - x + x^{2} - x^{3} + x^{4} - x^{5} + x^{6} - x^{7} + x^{8} - x^{9} \\
  + x^{10} - x^{11} + x^{12} - x^{13} + x^{14} - x^{15} + x^{16} - x^{17} + x^{18} - x^{19} + \mathcal{O}(x^{20}) \,.
\end{multline}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{multline}
  \frac{1}{1 + x} = 1 - x + x^{2} - x^{3} + x^{4} - x^{5} + x^{6} - x^{7} + x^{8} - x^{9} + \mathcal{O}(x^{10}) \,.
\end{multline}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{multline}
  \frac{1}{1 + x} = 1 - x + x^{2} - x^{3} + x^{4} - x^{5} + x^{6} - x^{7} + x^{8} - x^{9} \\
  + x^{10} - x^{11} + x^{12} - x^{13} + x^{14} - x^{15} + x^{16} - x^{17} + x^{18} - x^{19} + \mathcal{O}(x^{20}) \,.
\end{multline}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{multline*}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{multline*}
  \frac{1}{1 + x} = 1 - x + x^{2} - x^{3} + x^{4} - x^{5} + x^{6} - x^{7} + x^{8} - x^{9} + \mathcal{O}(x^{10}) \,.
\end{multline*}
\nextlipsum
\begin{multline*}
  \frac{1}{1 + x} = 1 - x + x^{2} - x^{3} + x^{4} - x^{5} + x^{6} - x^{7} + x^{8} - x^{9} \crs
  + x^{10} - x^{11} + x^{12} - x^{13} + x^{14} - x^{15} + x^{16} - x^{17} + x^{18} - x^{19} + \mathcal{O}(x^{20}) \,.
\end{multline*}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{multline*}
  \frac{1}{1 + x} = 1 - x + x^{2} - x^{3} + x^{4} - x^{5} + x^{6} - x^{7} + x^{8} - x^{9} + \mathcal{O}(x^{10}) \,.
\end{multline*}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{multline*}
  \frac{1}{1 + x} = 1 - x + x^{2} - x^{3} + x^{4} - x^{5} + x^{6} - x^{7} + x^{8} - x^{9} \\
  + x^{10} - x^{11} + x^{12} - x^{13} + x^{14} - x^{15} + x^{16} - x^{17} + x^{18} - x^{19} + \mathcal{O}(x^{20}) \,.
\end{multline*}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{gather}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{gather}
  E = m c^2 \,.
\end{gather}
\nextlipsum
\begin{gather}
  E = m c^2 \,, \\
  E^2 = p^2 c^2 + m_0^2 c^4 \,.
\end{gather}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{gather}
  E = m c^2 \,.
\end{gather}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{gather}
  E = m c^2 \,, \\
  E^2 = p^2 c^2 + m_0^2 c^4 \,.
\end{gather}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{gather*}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{gather*}
  E = m c^2 \,.
\end{gather*}
\nextlipsum
\begin{gather*}
  E = m c^2 \,, \crs
  E^2 = p^2 c^2 + m_0^2 c^4 \,.
\end{gather*}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{gather*}
  E = m c^2 \,.
\end{gather*}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{gather*}
  E = m c^2 \,, \\
  E^2 = p^2 c^2 + m_0^2 c^4 \,.
\end{gather*}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{align}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{align}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{align}
\nextlipsum
\begin{align}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \\
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{align}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{align}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{align}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{align}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \\
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{align}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{align*}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{align*}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{align*}
\nextlipsum
\begin{align*}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \crs
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{align*}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{align*}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{align*}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{align*}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \\
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{align*}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{alignat}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{alignat}{2}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{alignat}
\nextlipsum
\begin{alignat}{2}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \\
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{alignat}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{alignat}{2}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{alignat}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{alignat}{2}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \\
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{alignat}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{alignat*}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{alignat*}{2}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{alignat*}
\nextlipsum
\begin{alignat*}{2}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \crs
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{alignat*}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{alignat*}{2}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{alignat*}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{alignat*}{2}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \\
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{alignat*}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{flalign}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{flalign}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{flalign}
\nextlipsum
\begin{flalign}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \\
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{flalign}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{flalign}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{flalign}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{flalign}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \\
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{flalign}
\end{linenomath*}
\nextlipsum

\subsection*{\texttt{flalign*}}

\subsection*{With line numbers in equations}
\nextlipsum
\begin{flalign*}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{flalign*}
\nextlipsum
\begin{flalign*}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \crs
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{flalign*}
\nextlipsum

\subsection*{Without line numbers in equations}
\nextlipsum
\begin{linenomath*}
\begin{flalign*}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,.
\end{flalign*}
\end{linenomath*}
\nextlipsum
\begin{linenomath*}
\begin{flalign*}
  \nabla \cdot \vec E = 0 \,, &\quad \nabla \times \vec E = - \frac{\partial \vec B}{\partial t} \,, \\
  \nabla \cdot \vec B = 0 \,, &\quad \nabla \times \vec B = \frac{1}{c^2} \frac{\partial \vec E}{\partial t} \,.
\end{flalign*}
\end{linenomath*}
\nextlipsum

\nolinenumbers

\end{document}

% Local Variables:
% mode: doctex
% TeX-master: t
% End:
