% \iffalse meta-comment
%
% Copyright (C) 2021 by Jinwen XU 
% -------------------------------
% 
% This file may be distributed and/or modified under the conditions of the LaTeX
% Project Public License, either version 1.3c 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
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{mindflow.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{mindflow}
%<*package>
    [2021/09/12 mindflow environment]
%</package>
%
%<*driver>
\documentclass{article}
\usepackage{doc}
\usepackage[a4paper,top=1.2in,bottom=1.2in,left=1.6in,right=1.2in]{geometry}
\usepackage{titlesec}
\titleformat{\section}{\large\scshape\centering}{\thesection.}{.5em}{}
\titleformat{\subsection}{\large\itshape}{\thesubsection}{.5em}{}
\usepackage{titling}
\setlength{\droptitle}{-.7in}
\usepackage{enumitem}
\setlist{noitemsep}
\usepackage{mathpazo,newpxtext}
\useosf
\usepackage[symbol]{footmisc}
\usepackage{microtype}
\PassOptionsToPackage{mathlines}{lineno}
\usepackage[linenumber,rightmarker]{mindflow}
\usepackage{parskip}
\usepackage{listings}
\definecolor{maintheme}{RGB}{50,100,150}
\definecolor{forestgreen}{RGB}{21,122,81}
\definecolor{lightergray}{gray}{0.99}
\lstset{language=[LaTeX]TeX,
    keywordstyle=\color{maintheme},
    basicstyle=\ttfamily,
    commentstyle=\color{forestgreen}\ttfamily,
    stringstyle=\rmfamily,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{lightergray},
    flexiblecolumns=true,
    escapeinside={(*}{*)},
    % numbers=left,
    % numberstyle=\scriptsize, stepnumber=1, numbersep=5pt,
    % firstnumber=last,
} 
\providecommand{\meta}[1]{$\langle${\normalfont\itshape#1}$\rangle$}
\lstset{moretexcs=%
    {linenumbers,nolinenumbers,mindflowLeft,mindflowRight,mindflowTextFont,mindflowNumFont,mindflowMarkerFont,mindflowLineHeight,setlength,colorlet,color,
    RequirePackage,SetupKeyvalOptions,DeclareBoolOption,ProcessKeyvalOptions,PassOptionsToPackage,
    PackageWarning,MessageBreak,
    if@mindflow@twocolumn,@mindflow@incolumntrue,if@mindflow@linenumber,@mindflow@leftlinenumbertrue,if@mindflow@on,if@mindflow@off,@mindflow@offfalse,if@mindflow@incolumn,if@mindflow@leftlinenumber,if@mindflow@rightlinenumber,if@mindflow@leftmarker,if@mindflow@rightmarker,if@mindflow@nonbreakable,
    mindflow@parindent,mindflow@parskip,
    ifLNturnsON,LNturnsONtrue,LNturnsONfalse,ifLineNumbers,
    mindflowset,
    mindflow,endmindflow,comment,endcomment,
    mindflowON,endmindflowON,mindflowOFF,endmindflowOFF,
    mfSepLine,makeLineNumber,LineNumber,
    mindflow@makeLineNumber,LocallyStopLineNumbers,ResumeLineNumbers,
    ifdefined,linenomathpatch,cspreto,csappto,patchcmd,linenomathpatchAMS,linenomathAMS,linenomathWithnumbers,linenomath,endlinenomath,linenomathNonumbers,linenopenalty,
    define@key,setkeys,
    nowidow,
    }
}
\lstnewenvironment{code*}% 
{\setkeys{lst}{columns=fullflexible,keepspaces=true}}{}
\lstnewenvironment{code}% 
{\setkeys{lst}{columns=fullflexible,keepspaces=true,numbers=left,numberstyle=\scriptsize, stepnumber=1, numbersep=5pt,lastline=253}}{}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{mindflow.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \GetFileInfo{mindflow.dtx}
%
%
% \title{{\normalfont\textsf{mindflow}} -- write your ideas in a clear way}
% \author{\scshape Jinwen Xu}
% \date{\filedate}
%
% \maketitle
%
% \section{Introduction}
%
% The \textsf{mindflow} package provides you a way to write your ideas 
% and annotations clearly. For example, with the options
% \verb|linenumber| and \verb|rightmarker|, you will get something like:
% \begin{mindflow}
%     What to write next:
%     \begin{itemize}
%         \item usage: the initialization, options and configuration;
%         \item an example;
%         \item some word on the line numbering feature;
%         \item some internal macros;
%         \item the complete code.
%     \end{itemize}
% \end{mindflow}
% This example only shows the default effect. You can customize it further according to the instructions in the next section.
%
% \section{The usage}
%
% \subsection{How to load it\,?}
% Simply load the package with 
% \iffalse
%<*example>
% \fi
\begin{code*}
  \usepackage{mindflow}
\end{code*}
% \iffalse
%</example>
% \fi
%
% By default, the environment has no line numbers or markers. You can use the
% following options (they can be specified directly, like \verb|off|, or as a
% boolean optional, like \verb|off=false|):
%
% \begin{tabular}{ll}
%     \verb|linenumber| & Same as \verb|leftlinenumber|\\
%     \verb|leftlinenumber| & Enable left line numbers\\
%     \verb|rightlinenumber| & Enable right line numbers\\
%     \verb|leftmarker| & Enable left marker, by default it is a ``\verb|*|''\\
%     \verb|rightmarker| & Enable right marker, by default it is a ``\verb|*|''\\
%     \verb|incolumn| or \verb|twocolumn| & The separation line would fit in the column \\&(\emph{automatically applied in two-column documents})\\
%     \verb|off|, \verb|on| & Turn the \verb|mindflow| environments \emph{off} or \emph{on}
% \end{tabular}
%
% Additionally, there is an option \verb|nonbreakble|, which enables the \textsf{tcolorbox} version, permits a background color, but is not breakable and doesn't support line numbering quite well.
%
% \subsection{How to use it\,?}
% \DescribeEnv{\{mindflow\}}
% You can then use the \verb|mindflow| environment as
% \iffalse
%<*example>
% \fi
\begin{code*}
  \begin{mindflow}
      (*{\normalfont\meta{text}}*)
  \end{mindflow}
\end{code*}
% \iffalse
%</example>
% \fi
%
% \subsection{Further configuration}
%
% \DescribeMacro{\mindflowset}
% You can use \lstinline|\mindflowset{|\meta{configuration}\lstinline|}| to
% configure the \texttt{mindflow} environment. The available keys are listed at the top of the next page.
%
% \begin{center}
% \begin{tabular}{ll|ll}
%	  \hline
%     \verb|left| & The left marker & \verb|right| & The right marker\\
%     \verb|textcolor| & Color of the text & \verb|textfont| & Font of the text\\
%     \verb|numcolor| & Color of line numbers & \verb|numfont| & Font of line numbers\\
%     \verb|markercolor| & Color of the markers & \verb|markerfont| & Font of the marker\\
%     \verb|linecolor| & Color of the separation line & \verb|lineheight| & Height of the separation line\\
%	  \hline
% \end{tabular}
%\end{center}
%
% Additionally, all the options mentioned in the previous section (except for \verb|nonbreakble|) can be set with \lstinline|\mindflowset|.%
% \footnote{If the package option \texttt{nonbreakble} is enabled, then there is also a key \texttt{backgroundcolor}, similar to \texttt{textcolor}, for configuring the background color of the \texttt{mindflow} environments.}
%
% \section{An example}
% With the following settings:
% \iffalse
%<*example>
% \fi
\begin{code*}
\mindflowset{
    leftlinenumber, rightlinenumber, leftmarker, rightmarker,
    linecolor   = green!50!black,
    textcolor   = blue!50!cyan,
    numcolor    = red!50!orange,
    markercolor = orange,
    textfont    = \footnotesize\sffamily\itshape,
    numfont     = \small\footnotesize\ttfamily,
    markerfont  = \footnotesize\sffamily,
    left        = \hspace{1em}\(\succ\),
    right       = \(\prec\)\hspace{1em},
    lineheight  = 1pt
}
\end{code*}
% \iffalse
%</example>
% \fi
% \vspace{-.5\baselineskip}
% One will have something like this:
% \begingroup
% \makeatletter
% \@mindflow@leftmarkertrue
% \@mindflow@rightlinenumbertrue
% \makeatother
% \colorlet{mindflowLine}{green!50!black}
% \colorlet{mindflowText}{blue!50!cyan}
% \colorlet{mindflowNum}{red!50!orange}
% \colorlet{mindflowMarker}{orange}
% \renewcommand{\mindflowTextFont}{\footnotesize\sffamily\itshape}
% \renewcommand{\mindflowNumFont}{\small\footnotesize\ttfamily}
% \renewcommand{\mindflowMarkerFont}{\footnotesize\sffamily}
% \renewcommand{\mindflowLeft}{\hspace{1em}\(\succ\)}
% \renewcommand{\mindflowRight}{\(\prec\)\hspace{1em}}
% \setlength{\mindflowLineHeight}{1pt}
% \begin{mindflow}
%     Hello, here is some text without a meaning. This text should show what a printed text will look like at this place. If you read this text, you will get no information. Really? Is there no information? Is there a difference between this text and some nonsense like “Huardest gefburn”? Kjift – not at all! A blind text like this gives you information about the selected font, how the letters are written and an impression of the look. 
% \end{mindflow}
% \endgroup
%
% \section{About the line numbers}
% \linenumbers
%
% You can also add line numbers to the other part of your document in the usual
% way. For example, line numbers have been turned on for this paragraph with
% \lstinline|\linenumbers|.
%
% \begin{mindflow}
%   Line numbers within the \textsf{mindflow} environments are independent from
%   those of the main text.
% \end{mindflow}
% (\emph{I recommend that you put an empty line before and after the mindflow
% environments, i.e.~make the \emph{\textsf{mindflow}} environment a paragraph
% of its own. This way, your paragraph structure won't get changed when you use
% the} \verb|off|\emph{ option.})
%
% \nolinenumbers
%
% The line numbering feature is provided by the package \textsf{lineno}. For
% more details, please refer to its documentation. Notice that, by default,
% displayed equations are not numbered. You can add the following line before
% \lstinline|\usepackage{mindflow}|:
% \iffalse
%<*example>
% \fi
\begin{code*}
  \PassOptionsToPackage{mathlines}{lineno}
\end{code*}
% \iffalse
%</example>
% \fi
% \vspace{-.5\baselineskip}
% to enable the line numbers for displayed equations.
%
% \begin{mindflow}\vspace{-\baselineskip}
% \begin{align*}
%     (a + b)^2  &= a^2 + 2ab + b^2\\
%     (a + b)^3  &= a^3 + 3a^2b + 3ab^2 + b^3
% \end{align*}
% \end{mindflow}
% \smallskip
%
% \clearpage
% \makebox[\linewidth]{\rule{4\paperwidth}{\mindflowLineHeight}}%
% \centerline{\itshape ---~~Readers who are not interested in technical details can skip the rest~~---}
%
% \vspace{-\baselineskip}
% \makebox[\linewidth]{\rule{4\paperwidth}{\mindflowLineHeight}}%
%
% \section{Some \TeX{}nical details}
%
% \subsection{Internal macro for text elements}
% \DescribeMacro{\mindflowTextFont}
% \DescribeMacro{\mindflowNumFont}
% \DescribeMacro{\mindflowMarkerFont}
% The font for text, line numbers and markers within the \verb|mindflow|
% environments can be specified by redefining \lstinline|\mindflowTextFont|,
% \lstinline|\mindflowNumFont| and \lstinline|\mindflowMarkerFont|. By default, 
% they are defined as:
% \iffalse
%<*example>
% \fi
\begin{code*}
\newcommand{\mindflowTextFont}{\footnotesize}
\newcommand{\mindflowNumFont}{\scriptsize\ttfamily}
\newcommand{\mindflowMarkerFont}{\scriptsize\ttfamily}
\end{code*}
% \iffalse
%</example>
% \fi
% \vspace{-.5\baselineskip}
%
% \DescribeMacro{\mindflowLeft}
% \DescribeMacro{\mindflowRight}
% The left and right markers is defined by
% \lstinline|\mindflowLeft| and \lstinline|\mindflowRight|. Both have the default
% value ``\verb|*|''.
%
% \DescribeMacro{\mindflowLineHeight}
% The height of the separation lines is a length macro named \lstinline|\mindflowLineHeight|, 
% with default value \verb|0.4pt|. You can change this with \lstinline|\setlength|.
%
% \subsection{Internal name of the colors}
% \DescribeMacro{mindflowLine}
% \DescribeMacro{mindflowText}
% \DescribeMacro{mindflowNum}
% \DescribeMacro{mindflowMarker}
% And finally, the colors for the separation lines, text, line numbers and
% markers within the \verb|mindflow| environments are called
% \verb|mindflowLine|, \verb|mindflowText|, \verb|mindflowNum| and
% \verb|mindflowMarker|, respectively. By default, they have the same color as
% the context, with opacity 40\% for the separation lines and text, 20\% for 
% the markers, and 8\% for line numbers. 
% 
% \DescribeMacro{*mindflowBackground}
% If the package option \verb|nonbreakable| is enabled, then there is also the background color \verb|mindflowBackground|, which by default is white.
%
% \section{The same example}
% With option \verb|leftlinenumber, rightlinenumber, leftmarker, rightmarker| 
% and the following settings:
% \iffalse
%<*example>
% \fi
\begin{code*}
\colorlet{mindflowLine}{green!50!black}
\colorlet{mindflowText}{blue!50!cyan}
\colorlet{mindflowNum}{red!50!orange}
\colorlet{mindflowMarker}{orange}
\renewcommand{\mindflowTextFont}{\footnotesize\sffamily\itshape}
\renewcommand{\mindflowNumFont}{\small\footnotesize\ttfamily}
\renewcommand{\mindflowMarkerFont}{\footnotesize\sffamily}
\renewcommand{\mindflowLeft}{\hspace{1em}\(\succ\)}
\renewcommand{\mindflowRight}{\(\prec\)\hspace{1em}}
\setlength{\mindflowLineHeight}{1pt}
\end{code*}
% \iffalse
%</example>
% \fi
% \vspace{-.5\baselineskip}
% One will get the same result as in the previous example.
%
% \section{Known issues}
% 
% \begin{itemize}
%     \item There is no absolute guarantee that the separation lines will not fall alone at the top or bottom of the page, though great effort has been made to reduce the occurrence of such situation.
%     \item The \textsf{tcolorbox} version doesn't support line numbering quite well, especially for displayed equations. This is due to the usage of \verb|internallinenumbers|.
% \end{itemize}
%
% \StopEventually{}
%
% \clearpage
% \section{Implementation}
% Below is the complete source code of this package.
%
% \begin{code}
\RequirePackage{kvoptions}
\SetupKeyvalOptions{%
    family = @mindflow,
    prefix = @mindflow@
}
\DeclareBoolOption[true]{on}               % Turn on mindflow
\DeclareBoolOption[false]{off}             % Turn off mindflow
\DeclareBoolOption[false]{leftmarker}      % Left marker
\DeclareBoolOption[false]{rightmarker}     % Right marker
\DeclareBoolOption[false]{linenumber}      % Left line numbers
\DeclareBoolOption[false]{leftlinenumber}  % Left line numbers
\DeclareBoolOption[false]{rightlinenumber} % Right line numbers
\DeclareBoolOption[false]{twocolumn}       % Two column
\DeclareBoolOption[false]{incolumn}        % Separation line fits in column
\DeclareBoolOption[false]{nonbreakable}    % Use the tcolorbox version

\ProcessKeyvalOptions*\relax

\if@mindflow@on
  \@mindflow@offfalse
\fi
\if@mindflow@twocolumn
  \@mindflow@incolumntrue
\fi
\if@mindflow@linenumber
  \@mindflow@leftlinenumbertrue
\fi

%%================================
%% Initialization
%%================================
\RequirePackage{lineno}
\RequirePackage{xcolor}

\colorlet{mfSavedColor}{.}
\colorlet{mindflowLine}{mfSavedColor!40}
\colorlet{mindflowText}{mfSavedColor!40}
\colorlet{mindflowMarker}{mfSavedColor!20}
\colorlet{mindflowNum}{mfSavedColor!8}

\newcommand{\mindflowTextFont}{\footnotesize}
\newcommand{\mindflowNumFont}{\scriptsize\ttfamily}
\newcommand{\mindflowMarkerFont}{\scriptsize\ttfamily}
\newcommand{\mindflowLeft}{*}
\newcommand{\mindflowRight}{*}
\newlength{\mindflowLineHeight}
\setlength{\mindflowLineHeight}{0.4pt}

%%================================
%% The mindflow environment
%%================================
\PassOptionsToPackage{all}{nowidow}
\RequirePackage{nowidow}
\RequirePackage{verbatim}

\newif\ifLNturnsON
\def\LocallyStopLineNumbers{\LNturnsONfalse%
    \ifLineNumbers\LNturnsONtrue\fi\nolinenumbers}
\def\ResumeLineNumbers{\ifLNturnsON\linenumbers\fi}

\newcounter{recordLN}
\newcounter{mfLN}
\setcounter{mfLN}{1}

\newcommand*{\mfSepLine}{%
  \par%\nobreak%
  \LocallyStopLineNumbers%
  \vspace*{-.5\baselineskip}%
  \noindent%
  \if@mindflow@incolumn%
    \makebox[\linewidth]{\color{mindflowLine}\rule{\linewidth}{\mindflowLineHeight}}%
  \else%
    \hspace*{-\paperwidth}%
    \makebox[\linewidth]{\color{mindflowLine}\rule{4\paperwidth}{\mindflowLineHeight}}%
  \fi%
  \par%\nobreak%
  \ResumeLineNumbers%
}

\newcommand{\mindflow@makeLineNumber}{%
  \hss%
  \if@mindflow@leftlinenumber%
    \normalfont\mindflowNumFont\color{mindflowNum}\LineNumber\hspace{1em}%
  \fi%
  \color{mindflowMarker}%
  \if@mindflow@leftmarker%
    \normalfont\mindflowMarkerFont\mindflowLeft\hspace{1em}%
  \fi%
  \rlap{\hskip\textwidth%
    \if@mindflow@rightmarker%
      \hspace{1em}\mindflowRight%
    \fi%
    \if@mindflow@rightlinenumber%
      \hspace{1em}%
      \normalfont\mindflowNumFont\color{mindflowNum}\LineNumber%
    \fi%
  }%
}%

\let\mindflowOFF=\comment
\let\endmindflowOFF=\endcomment

\def\mindflowON{%
  \postdisplaypenalty=10000
  \parskip=0pt
  \medskip%
  \setcounter{recordLN}{\value{linenumber}}%
  \setcounter{linenumber}{\value{mfLN}}%
  \mfSepLine%
  \normalfont\mindflowTextFont\color{mindflowText}%
  \linenumbers%
  \let\makeLineNumber\mindflow@makeLineNumber%
  \nopagebreak%
}
\def\endmindflowON{%
  \nowidow[3]%
  \mfSepLine%
  \setcounter{mfLN}{\value{linenumber}}%
  \setcounter{linenumber}{\value{recordLN}}%
  \medskip%
}

\colorlet{mindflowBackground}{white}

\if@mindflow@nonbreakable
\PassOptionsToPackage{many}{tcolorbox}
\RequirePackage{tcolorbox}
\newlength{\mindflow@parindent}
\newlength{\mindflow@parskip}
\def\mindflowON{%
  \LNturnsONfalse%
  \ifLineNumbers\LNturnsONtrue\fi\nolinenumbers%
  \setlength{\mindflow@parindent}{\parindent}%
  \setlength{\mindflow@parskip}{\parskip}%
  \begin{tcolorbox}[enhanced jigsaw,
    colback=mindflowBackground,colframe=mindflowLine, 
    leftrule=0pt, rightrule=0pt, sharp corners,
    toprule=\mindflowLineHeight, bottomrule=\mindflowLineHeight, 
    boxsep=0pt, top=.3\baselineskip, bottom=.3\baselineskip,
    grow to left by=\linewidth,left=\linewidth,
    grow to right by=\linewidth,right=\linewidth,
    fontupper=\mindflowTextFont,extras={colupper={mindflowText}},
    before upper={ 
      \setlength{\parindent}{\mindflow@parindent}%
      \setlength{\parskip}{\mindflow@parskip}%
    }]
    \begin{internallinenumbers}%
      \let\makeLineNumber\mindflow@makeLineNumber%
}
\def\endmindflowON{%
      \nowidow[3]%
    \end{internallinenumbers}%
  \end{tcolorbox}\par%
  \ifLNturnsON\linenumbers\fi%
}
\fi

\if@mindflow@off
  \let\mindflow=\mindflowOFF
  \let\endmindflow=\endmindflowOFF
\else
  \let\mindflow=\mindflowON
  \let\endmindflow=\endmindflowON
\fi

%%================================
%% \mindflowset
%%================================
\define@key{mindflow}{on}[true]{%
  \csname @mindflow@on#1\endcsname%
  \if@mindflow@off%
    \let\mindflow=\mindflowOFF%
    \let\endmindflow=\endmindflowOFF%
  \else%
    \let\mindflow=\mindflowON%
    \let\endmindflow=\endmindflowON%
  \fi%
}
\define@key{mindflow}{off}[true]{%
  \csname @mindflow@off#1\endcsname%
  \if@mindflow@off%
    \let\mindflow=\mindflowOFF%
    \let\endmindflow=\endmindflowOFF%
  \else%
    \let\mindflow=\mindflowON%
    \let\endmindflow=\endmindflowON%
  \fi%
}
\define@key{mindflow}{leftmarker}[true]{\csname @mindflow@leftmarker#1\endcsname}
\define@key{mindflow}{rightmarker}[true]{\csname @mindflow@rightmarker#1\endcsname}
\define@key{mindflow}{linenumber}[true]{\csname @mindflow@linenumber#1\endcsname}
\define@key{mindflow}{leftlinenumber}[true]{\csname @mindflow@leftlinenumber#1\endcsname}
\define@key{mindflow}{rightlinenumber}[true]{\csname @mindflow@rightlinenumber#1\endcsname}
\define@key{mindflow}{twocolumn}[true]{\csname @mindflow@twocolumn#1\endcsname}
\define@key{mindflow}{incolumn}[true]{\csname @mindflow@incolumn#1\endcsname}
\define@key{mindflow}{linecolor}{\colorlet{mindflowLine}{#1}}
\define@key{mindflow}{textcolor}{\colorlet{mindflowText}{#1}}
\define@key{mindflow}{numcolor}{\colorlet{mindflowNum}{#1}}
\define@key{mindflow}{markercolor}{\colorlet{mindflowMarker}{#1}}
\define@key{mindflow}{backgroundcolor}{
  \if@mindflow@nonbreakable
    \colorlet{mindflowBackground}{#1}
  \else
    \PackageWarning{mindflow}{The key ``backgroundcolor'' is only available when the package option\MessageBreak ``nonbreakable'' is enabled.}
  \fi
}
\define@key{mindflow}{textfont}{\renewcommand{\mindflowTextFont}{#1}}
\define@key{mindflow}{numfont}{\renewcommand{\mindflowNumFont}{#1}}
\define@key{mindflow}{markerfont}{\renewcommand{\mindflowMarkerFont}{#1}}
\define@key{mindflow}{left}{\renewcommand{\mindflowLeft}{#1}}
\define@key{mindflow}{right}{\renewcommand{\mindflowRight}{#1}}
\define@key{mindflow}{lineheight}{\setlength{\mindflowLineHeight}{#1}}
\newcommand{\mindflowset}[1]{%
  \setkeys{mindflow}{#1}%
  \if@mindflow@twocolumn%
    \@mindflow@incolumntrue%
  \fi%
  \if@mindflow@linenumber%
    \@mindflow@leftlinenumbertrue%
  \fi%
}

%%================================
%% lineno patch
%%================================
\ifdefined\linenomathpatch\else
  \RequirePackage{amsmath}
  \RequirePackage{etoolbox}
  \newcommand*\linenomathpatch[1]{%
    \cspreto{#1}{\linenomath}%
    \cspreto{#1*}{\linenomath}%
    \csappto{end#1}{\endlinenomath}%
    \csappto{end#1*}{\endlinenomath}%
  }
  \newcommand*\linenomathpatchAMS[1]{%
    \cspreto{#1}{\linenomathAMS}%
    \cspreto{#1*}{\linenomathAMS}%
    \csappto{end#1}{\endlinenomath}%
    \csappto{end#1*}{\endlinenomath}%
  }
  \expandafter\ifx\linenomath\linenomathWithnumbers
    \let\linenomathAMS\linenomathWithnumbers
    \patchcmd\linenomathAMS{\advance\postdisplaypenalty\linenopenalty}{}{}{}
  \else
    \let\linenomathAMS\linenomathNonumbers
  \fi
  \linenomathpatch{equation}
  \linenomathpatchAMS{gather}
  \linenomathpatchAMS{multline}
  \linenomathpatchAMS{align}
  \linenomathpatchAMS{alignat}
  \linenomathpatchAMS{flalign}
\fi
% \end{code}
%
% \Finale
\endinput
