% \iffalse meta-comment
%
% linenoamsmath.dtx
% 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.
%
% This work consists of the files linenoamsmath.dtx and
% linenoamsmath.ins and the derived file linenoamsmath.sty.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{linenoamsmath.dtx}
%</driver>
%<*package>
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{linenoamsmath}
    [2021/09/29 v1.0 Make the amsmath package work with the lineno package]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage[leqno]{amsmath}
\usepackage[mathlines]{linenoamsmath}[2021/09/29]
\usepackage{lipsum}
\usepackage{hyperref}
\RecordChanges
\begin{document}
\DocInput{linenoamsmath.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{74}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \changes{v1.0}{2021/09/29}{Initial version}
%
% \GetFileInfo{linenoamsmath.sty}
%
% \title{The \textsf{linenoamsmath} package\thanks{This document
%   corresponds to \textsf{linenoamsmath}~\fileversion, dated \filedate.}}
% \author{Karl Wette}
%
% \maketitle
%
% This package patches the
% \textsf{amsmath}\footnote{\url{https://ctan.org/pkg/amsmath}} package to work
% with the \textsf{lineno}\footnote{\url{https://ctan.org/pkg/lineno}}
% package. The code I've used is largely based on the posts at
% \url{https://tex.stackexchange.com/a/461192} and
% \url{https://tex.stackexchange.com/a/443201}; credit is due to their author.
% In addition I've made a few refinements to handle some corner cases.
%
% \section{Usage}
%
% Just include the package; it will automatically include \textsf{amsmath} and \textsf{lineno}:
% \begin{verse}
%   |\usepackage{linenoamsmath}| \\
%   ... \\
%   |\linenumbers| \\
%   ... Text with line numbers ... \\
%   |\begin{align}| \\
%     ... Equations without line numbers ... \\
%   |\end{align}|
% \end{verse}
% All options to this package are passed through to \textsf{lineno},
% so e.g.\ if you want to add line numbers to equations you can specity the
% |[mathlines]| option:
% \begin{verse}
%   |\usepackage[mathlines]{linenoamsmath}| \\
%   ... \\
%   |\linenumbers| \\
%   ... Text with line numbers ... \\
%   |\begin{align}| \\
%     ... Equations with line numbers ... \\
%   |\end{align}|
% \end{verse}
% If you need to pass options to \textsf{amsmath}, you can include it manually first:
% \begin{verse}
% |\usepackage[...]{amsmath}| \\
% |\usepackage[mathlines]{linenoamsmath}|
% \end{verse}
%
% \section{Demonstration}
%
% This section demonstrates that, with this package, line numbers are correctly
% formatted when using \textsf{amsmath} math environments.
%
% \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
%
% \StopEventually{}
%
% \section{Implementation}
%
% All options to this package are passed through to \textsf{lineno}.
%    \begin{macrocode}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{lineno}}
\ProcessOptions\relax
%    \end{macrocode}
% Include \textsf{amsmath} and \textsf{lineno}.
%    \begin{macrocode}
\RequirePackage{amsmath}
\RequirePackage{lineno}
%    \end{macrocode}
% Needs \textsf{etoolbox} for patching macros.
%    \begin{macrocode}
\RequirePackage{etoolbox}
%    \end{macrocode}
% Patch math environments:
% \begin{itemize}
%
% \item Setting |\postdisplaypenalty=0| removes extra line numbers from
%   \textsf{amsmath} math environments.
%
% \item Patching |\math@cr@| to add |\linenoamsmath@ams@eqpen| to |\@eqpen|,
%   then setting |\linenoamsmath@ams@eqpen| to |\interdisplaylinepenalty|,
%   preserves line numbers when equations are separated using the |\\*| line
%   break instead of |\\|.
%
% \end{itemize}
%    \begin{macrocode}
\newcommand*\linenoamsmath@patch[1]{%
  \cspreto{#1}{\linenomath}%
  \cspreto{#1*}{\linenomath}%
  \csappto{end#1}{\endlinenomath}%
  \csappto{end#1*}{\endlinenomath}%
}
\newcount\linenoamsmath@ams@eqpen
\cspreto{math@cr@}{\global\@eqpen\numexpr\@eqpen+\linenoamsmath@ams@eqpen\relax}
\newcommand*\linenoamsmath@patch@ams[1]{%
  \cspreto{#1}{%
    \linenomath%
    \postdisplaypenalty=0%
    \global\linenoamsmath@ams@eqpen\interdisplaylinepenalty%
  }%
  \cspreto{#1*}{%
    \linenomath%
    \postdisplaypenalty=0%
    \global\linenoamsmath@ams@eqpen\interdisplaylinepenalty%
  }%
  \csappto{end#1}{%
    \global\linenoamsmath@ams@eqpen\z@%
    \endlinenomath%
  }%
  \csappto{end#1*}{%
    \global\linenoamsmath@ams@eqpen\z@%
    \endlinenomath%
  }%
}
\linenoamsmath@patch{equation}
\linenoamsmath@patch@ams{multline}
\linenoamsmath@patch@ams{gather}
\linenoamsmath@patch@ams{align}
\linenoamsmath@patch@ams{alignat}
\linenoamsmath@patch@ams{flalign}
%    \end{macrocode}
% Patch |\mmeasure@| which is used internally by |multline|:
% \begin{itemize}
%
% \item Setting |\interdisplaylinepenalty=0| removes an extra line number from
%   the beginning of a multi-line |multline| where line numbers in equations are
%   turned on.
%
% \item Adding |\\| to the end of the argument of |\mmeasure@| removes an extra
%   line number from the beginning of a single-line |multline| where line
%   numbers in equations are turned off.
%
% \item Need to set |\linenoamsmath@ams@eqpen| to zero while in this macro to
%   avoid creating extra line numbers.
%
% \end{itemize}
%    \begin{macrocode}
\let\linenoamsmath@ams@mmeasure\mmeasure@
\def\mmeasure@#1{%
  \global\linenoamsmath@ams@eqpen\z@%
  \begingroup%
  \interdisplaylinepenalty=0%
  \linenoamsmath@ams@mmeasure{#1\\}%
  \endgroup%
  \global\linenoamsmath@ams@eqpen\interdisplaylinepenalty%
}
%    \end{macrocode}
%
% \PrintChanges
% \Finale
\endinput
% \endinput
% Local Variables:
% mode: doctex
% TeX-master: t
% End:
