%%
%% This is file `reledmac.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% reledmac.dtx  (with options: `code')
%% 
%%   Author: Author: Peter Wilson ; Herries Press herries dot press at earthlink dot net ; Maïeul Rouquette maieul at maieul dot net
%%   Copyright 2004, 2005 Peter R. Wilson
%%  2011- Maïeul Rouquette
%%   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 the license is in
%%      http://www.latex-project.org/lppl.txt
%%   and version 1.3 or later is part of all distributions of
%%   LaTeX version 2003/06/01 or later.
%% 
%%   This work has the LPPL maintenance status "maintained".
%% 
%%   This work consists of the files listed in the README file.













%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{reledmac}[2021/02/14 v2.36.1 typesetting critical editions]%
\RequirePackage{xkeyval}
\newif\ifparledgroup
\newif\ifsameparallelpagenumber%
\newif\ifprevpgnotnumbered%%
\DeclareOptionX{series}[A,B,C,D,E]{\xdef\default@series{#1}}
\ExecuteOptionsX{series}%

\newif\if@noeled@sec%
\DeclareOptionX{noeledsec}{\@noeled@sectrue}

\newif\ifnocritical@%
\DeclareOptionX{nocritical}{\nocritical@true}%

\newif\ifnofamiliar@%
\DeclareOptionX{nofamiliar}{\nofamiliar@true}%

\newif\ifnoledgroup@%
\DeclareOptionX{noledgroup}{\noledgroup@true}%

\newif\ifnoend@%
\DeclareOptionX{noend}{%
 \let\l@dend@open\@gobble%
 \let\l@dend@close\relax%
  \global\let\l@dend@stuff=\relax%
  \noend@true%
}%

\newif\ifnoquotation@
\DeclareOptionX{noquotation}{\noquotation@true}

\newif\ifledfinal
\DeclareOptionX{final}{\ledfinaltrue}
\DeclareOptionX{draft}{\ledfinalfalse}
\ExecuteOptionsX{final}

\newif\ifparapparatus@
\DeclareOptionX{parapparatus}{\parapparatus@true}

\newif\iflednopbinverse
\DeclareOptionX{nopbinverse}{\lednopbinversetrue}

\newif\ifwidthliketwocolumns%
\DeclareOptionX{widthliketwocolumns}{\widthliketwocolumnstrue}%

\newif\ifcontinuousnumberingwithcolumns
\DeclareOptionX{continuousnumberingwithcolumns}{\continuousnumberingwithcolumnstrue}%

\newif\ifxindy@
\DeclareOptionX{xindy}[eledmac-markup-attr.xdy]{%
  \AtBeginDocument{\immediate\openout\eledmac@xindy@out=#1}%
  \newwrite\eledmac@xindy@out%
  \xindy@true%
  \gdef\eledmacmarkuplocrefdepth{:depth 1}%
  \AtEndDocument{\immediate\closeout\eledmac@xindy@out}%
}%

\newif\ifxindyhyperref@
\DeclareOptionX{xindy+hyperref}{%
  \xindyhyperref@true%
}%

\newif\ifeledmaccompat@%
\DeclareOptionX{eledmac-compat}{%
  \eledmaccompat@true%
}%
\DeclareOptionX{nopenalties}{%
  \AtBeginDocument{\let\add@penalties\relax}%
}
\def\l@auxdir{}%
\DeclareOptionX{auxdir}{%
  \xdef\l@auxdir{#1/}%
}%

\newif\ifsw@caseinsensitive%
\DeclareOptionX{swcaseinsensitive}{%
  \sw@caseinsensitivetrue%
}%
\newif\ifnoresetlinenumannotation@
\DeclareOptionX{noresetlinenumannotation}{%
  \noresetlinenumannotation@true%
}%
\newif\ifantilabe@%
\DeclareOptionX{antilabe}{%
  \antilabe@true%
}%
\ProcessOptionsX*\relax

\RequirePackage{xargs}
\RequirePackage{xparse}[2017/03/07]%
\RequirePackage{etoolbox}
\@ifl@t@r\fmtversion{2015/10/01}
 {\ifboolexpr{not test{\@ifl@t@r\fmtversion{2016/03/31}} or (test{\ifdefstring{\fmtversion}{2016/03/31}} and test {\ifnumless{\patch@level}{3}})}%
   {\PackageWarning{reledmac}{You are using a LaTeX version older than 2016/03/31 patch 3.%
   \MessageBreak You are strongly encouraged to use a newer version.}}%
   {}%
 }%
 {\RequirePackage{etex}%
 \csname reserveinserts\endcsname{32}%
 }%
\RequirePackage{suffix}
\RequirePackage{xstring}
\RequirePackage{ifluatex}
\RequirePackage{ragged2e}
\RequirePackage{ifxetex}%
\ifx\directlua\undefined\else%
  \directlua{tex.enableprimitives("",{"textdir","pardir","bodydir"})}
\fi
\ifluatex%
  \ifnum\luatexversion<110%
    \PackageWarning{reledmac}{You need to use LuaTeX 1.1.0 or higher}{\@ehc}%
  \fi%
\fi%
\newif\ifl@dmemoir
\@ifclassloaded{memoir}{\l@dmemoirtrue}{\l@dmemoirfalse}

\newif\if@ledgroup%
\newif\ifl@imakeidx
\@ifpackageloaded{imakeidx}{\l@imakeidxtrue}{}%False is the default value

\newif\ifl@indextools%
\@ifpackageloaded{indextools}{%
  \l@indextoolstrue%
  \l@imakeidxtrue%
  \let\imki@wrindexentry\indtl@wrindexentry%
  }{}%
\newif\ifl@footmisc
\@ifpackageloaded{footmisc}{\l@footmisctrue}{}%False is the default value
\ifdef{\if@RTL}{}{\newif\if@RTL}
\newif\if@firstlineofpage%
\newif\if@firstlineofpageR%
\newcommand{\reledmac@warning}[1]{\PackageWarning{reledmac}{#1}}
\newcommand{\reledmac@error}[2]{\PackageError{reledmac}{#1}{#2}}
\newcommand*{\led@err@NumberingStarted}{%
  \reledmac@error{Numbering has already been started}{\@ehc}}
\newcommand*{\led@err@NumberingNotStarted}{%
  \reledmac@error{Numbering was not started}{\@ehc}}
\newcommand*{\led@err@NumberingShouldHaveStarted}{%
  \reledmac@error{Numbering should already have been started}{\@ehc}}
\newcommand*{\led@err@samewordRTL}{%
  \reledmac@error{You can't use \string\sameword \MessageBreak with XeLaTeX \MessageBreak when typesetting RTL text.\MessageBreak Please use LuaTeX instead.}{\@ehc}%
}%
\newcommand*{\led@err@edtextoutsidepstart}{%
  \reledmac@error{\string\edtext\space outside numbered paragraph (\pstart\space ...\space\pend)}{\@ehc}}%

\newcommand{\led@err@PstartInEdtext}[1]{%
  \reledmac@error{\string\pstart\space in \string\edtext\space #1 argument}{\@ehc}%
}%

\newcommand{\led@err@PendInEdtext}[1]{%
  \reledmac@error{\string\pend\space in \string\edtext\space #1 argument}{\@ehc}%
}%
\newcommand*{\led@mess@NotesChanged}{%
  \typeout{reledmac reminder: }%
  \typeout{ The number of the footnotes in this section
            has changed since the last run.}%
  \typeout{ You will need to run LaTeX two more times
            before the footnote placement}%
  \typeout{ and line numbering in this section are
            correct.}}
\newcommand*{\led@mess@SectionContinued}[1]{%
  \message{Section #1 (continuing the previous section)}}
\newcommand*{\led@err@LineationInNumbered}{%
  \reledmac@error{You can't use \string\lineation\space within
                a numbered section}{\@ehc}}
\newcommand*{\led@warn@BadLineation}{%
  \reledmac@warning{Bad \string\lineation\space argument}}
\newcommand*{\led@warn@BadLinenummargin}{%
  \reledmac@warning{Bad \string\linenummargin\space argument}}
\newcommand*{\led@warn@BadLockdisp}{%
  \reledmac@warning{Bad \string\lockdisp\space argument}}
\newcommand*{\led@warn@BadSublockdisp}{%
  \reledmac@warning{Bad \string\sublockdisp\space argument}}
\newcommand*{\led@warn@NoFile}[1]{%
  \reledmac@warning{File `#1' not found}}
\newcommand*{\led@warn@Obsolete}[1]{%
  \reledmac@warning{Line-list file #1 was obsolete. We have not read it. Please run LaTeX again.}}
\newcommand*{\led@warn@BadAdvancelineSubline}{%
  \reledmac@warning{\string\advanceline\space produced a sub-line
                  number less than zero.}}
\newcommand*{\led@warn@BadAdvancelineLine}{%
  \reledmac@warning{\string\advanceline\space produced a line
                  number less than zero.}}
\newcommand*{\led@warn@BadSetline}{%
  \reledmac@warning{Bad \string\setline\space argument}}
\newcommand*{\led@warn@BadSetlinenum}{%
  \reledmac@warning{Bad \string\setlinenum\space argument}}
\newcommand*{\led@err@PstartNotNumbered}{%
  \reledmac@error{\string\pstart\space must be used within a
                numbered section %
                 (\string\beginnumbering\space ...\space\string\endnumbering)}{\@ehc}}%
\newcommand*{\led@err@PstartInPstart}{%
  \reledmac@error{\string\pstart\space encountered while another
                \string\pstart\space was in effect}{\@ehc}}
\newcommand*{\led@err@PendNotNumbered}{%
  \reledmac@error{\string\pend\space must be used within a
                numbered section}{\@ehc}}
\newcommand*{\led@err@PendNoPstart}{%
  \reledmac@error{\string\pend\space must follow a \string\pstart}{\@ehc}}
\newcommand*{\led@err@AutoparNotNumbered}{%
  \reledmac@error{\string\autopar\space must be used within a
                numbered section}{\@ehc}}
\newcommand*{\led@err@NumberingWithoutPstart}{%
  \reledmac@error{\string\beginnumbering...\string\endnumbering\space without \string\pstart}{\@ehc}}%
\newcommand*{\led@warn@BadAction}{%
  \reledmac@warning{Bad action code, value \next@action.}}
\newcommand*{\led@warn@DuplicateLabel}[1]{%
  \reledmac@warning{Duplicate definition of label `#1'\@gobble}%
  \@latex@warning@no@line{Label `#1' multiply defined}%
}%
\newcommand*{\led@warn@AppLabelOutSecondArgEdtext}[1]{%
  \reledmac@warning{\string\applabel\space outside of the second argument of an \string\edtext\space `#1' on page \thepage.}}%
\newcommand*{\led@warn@RefUndefined}[1]{%
  \G@refundefinedtrue%
  \reledmac@warning{Reference `#1' on page \thepage\space undefined.%
                  Using `000'.}%
  \@latex@warning{Reference `#1' undefined\on@line}%
}%
\newcommand*{\led@warn@pairRefUndefined}[1]{%
  \G@refundefinedtrue%
  \reledmac@warning{Reference `#1:start' and/or `#1:end' on page \thepage\space undefined.
                  Using `??'.}%
  \@latex@warning{Reference `#1:start' and/or `#1:end' undefined\on@line}%
}
\newcommand*{\led@warn@NoMarginpars}{%
  \reledmac@warning{You can't use \string\marginpar\space in numbered text}}
\newcommand{\led@warn@BadHookSeries}[2]{%
  \reledmac@warning{Bad series #1 for #2 hook. Skipped.}%
}%
\newcommand*{\led@warn@BadSidenotemargin}{%
  \reledmac@warning{Bad \string\sidenotemmargin\space argument}}
\newcommand*{\led@warn@NoIndexFile}[1]{%
  \reledmac@warning{Undefined index file #1}}
\newcommand{\led@warn@SeriesStillExist}[1]{%
  \reledmac@warning{Series #1 is still existing !}%
}%
\newcommand*{\led@err@StanzaIndentNotDefined}{%
  \reledmac@error{You have not defined the indentation for the line \number\stanza@count}{\@ehc}}%
\newcommand{\led@err@ManySidenotes}{%
  \ifledRcol@%
     \reledmac@warning{\itemcount@\space sidenotes on line \the\line@numR\space p. \the\page@numR}%
  \else%
     \reledmac@warning{\itemcount@\space sidenotes on line \the\line@num\space p. \the\page@num}%
  \fi%
}%
\newcommand{\led@err@ManyLeftnotes}{%
  \ifledRcol@%
    \reledmac@warning{\itemcount@\space leftnotes on line \the\line@numR\space p. \the\page@numR}%
  \else%
    \reledmac@warning{\itemcount@\space leftnotes on line \the\line@num\space p. \the\page@num}%
  \fi%
}%
\newcommand{\led@err@ManyRightnotes}{%
  \ifledRcol@%
    \reledmac@warning{\itemcount@\space rightnotes on line \the\line@numR\space p. \the\page@numR}%
  \else%
    \reledmac@warning{\itemcount@\space rightnotes on line \the\line@num\space p. \the\page@num}%
  \fi%
}%
\newcommand*{\led@err@TooManyColumns}{%
  \reledmac@error{Too many columns}{\@ehc}}
\newcommand*{\led@err@UnequalColumns}{%
  \reledmac@error{Number of columns is not equal to the number
                in the previous row (or \protect\\ \space forgotten?)}{\@ehc}}
\newcommand*{\led@err@LowStartColumn}{%
  \reledmac@error{Start column is too low}{\@ehc}}
\newcommand*{\led@err@HighEndColumn}{%
  \reledmac@error{End column is too high}{\@ehc}}
\newcommand*{\led@err@ReverseColumns}{%
  \reledmac@error{Start column is greater than end column}{\@ehc}}
\newcommand{\led@err@toendnotes@outsidenumbering}{%
  \reledmac@error{\string\toendnotes\space and related commands must be called inside a numbered texte (\string\beginnumbering\space ...\space\string\endnumbering)}{\@ehc}%
}%
\newcommand{\led@err@EdtextWithoutFootnote}{%
  \reledmac@error{edtext without Xfootnote. Check syntaxis}{\@ehc}%
}%
\newcommand{\led@err@FootnoteNotInSecondArgEdtext}[1]{%
  \reledmac@error{#1footnote outside of the second argument of an edtext. Check syntax}{\@ehc}%
}%
\newcommand{\led@error@PackageAfterEledmac}[1]{%
  \reledmac@error{#1 must be loaded before reledmac}{\@ehc}%
}%
\newcommand{\led@error@fail@patch@@makecol}{%
  \reledmac@error{Fail to patch \string\@makecol\space command}{\@ehc}%
}%
\newcommand{\led@error@fail@patch@@reinserts}{%
  \reledmac@error{Fail to patch \string\@reinserts\space command}{\@ehc}%
}%
\newcommand{\led@error@fail@patch@@doclearpage}{%
  \reledmac@error{Fail to patch \string\@doclearpage\space command}{\@ehc}%
}%
\newcommand{\led@error@fail@patch@@iiiminipage}{%
  \reledmac@error{Fail to patch \string\@iiiminipage\space command}{\@ehc}%
}%
\newcommand{\led@error@fail@patch@endminipage}{%
  \reledmac@error{Failed to patch the \string\endminipage\space command}{\@ehc}%
}%
\newcommand{\led@error@fail@patch@makeindex}{%
  \reledmac@error{Failed to patch the \string\makeindex\space command}{\@ehc}%
}%
\newcommand{\led@error@fail@patch@setcounter}{%
  \reledmac@error{Failed to patch the \string\setcounter\space command}{\@ehc}%
}%
\newcommand{\led@error@fail@patch@addtocounter}{%
  \reledmac@error{Failed to patch the \string\addtocounter\space command}{\@ehc}%
}%
\newcommand{\led@error@fail@patch@stepcounter}{%
  \reledmac@error{Failed to patch the \string\stepcounter\space command}{\@ehc}%
}%
\newcommand{\led@warn@edinde@outsidenumbering}{%
  \reledmac@warning{\string\edindex\space called outside of \string\beginnumbering\space ...\space \string\endnumbering. \MessageBreak Automatically switched to \string\index.}%
}%
\newcommand{\led@warning@hsizeX@deprecated}{%
  \reledmac@warning{\string\hsizeX\space command deprecated, use \string\widthX\space instead.}%
}%
\newcommand{\led@warning@Xhsize@deprecated}{%
  \reledmac@warning{\string\Xhsize\space command deprecated, use \string\Xwidth\space instead.}%
}%
\newcommand{\led@warning@msdatawithoutstop}{%
  \reledmac@warning{\string\msdata\space without corresponding \string\stopmsdata}%
}%
\newcommand{\led@warning@preXnotes@deprecated}{%
  \reledmac@warning@preXnotes@deprecated%
}%
\providecommand*{\@gobblethree}[3]{}
\providecommand*{\@gobblefour}[4]{}
\providecommand*{\@gobbleseven}[7]{}
\newcommand{\l@wrap@ifnotemptybox}[2]{%
  \setbox0=\hbox{#2}%
  \ifdim\wd0=\z@\else%
    #1{#2}%
  \fi%
}%
\newcommand{\l@wrapcs@ifnotemptybox}[2]{%
  \l@wrap@ifnotemptybox{\csname #1\endcsname}{#2}%
}%
\ifledfinal
  \newcommand*{\showlemma}[1]{#1}
\else
  \newcommand*{\showlemma}[1]{\textbf{#1}}%
\fi

\let\linenumberlist=\empty

\newcount\@l@dtempcnta \newcount\@l@dtempcntb

\newif\ifl@dpairing
\newif\ifl@dpaging%
\newif\ifl@dprintingpages%
\newif\ifl@dprintingcolumns%
\newif\ifpst@rtedL
\newcount\l@dnumpstartsL
\newif\ifledRcol
\newif\ifledRcol@
\newif\ifnumberingR
\newif\ifafterendnumberingR%
\newif\ifXnote@%
\providebool{indtl@innote}%
\providebool{indtl@notenumber}%
 \newif\ifinstanzaL%
 \newif\ifinstanzaR%
 \newif\ifinastanzaL%
 \newif\ifinastanzaR%
\newcount\section@num
\section@num=0
\let\extensionchars=\empty
\newif\ifnumbering
\newcommand*{\beginnumbering}{%
  \ifnumbering
    \led@err@NumberingStarted
    \endnumbering
  \fi
  \global\numberingtrue
  \global\afterendnumberingRfalse%
  \global\advance\section@num \@ne
  \initnumbering@reg
  \message{Section \the\section@num }%
  \line@list@stuff{\jobname.\extensionchars\the\section@num}%
  \ifcontinuousnumberingwithcolumns%
    \unless\ifafterendnumberingR%
      \unless\ifl@dpairing%
        \ledRcoltrue%
        \global\advance\section@numR \@ne%
        \message{Section \the\section@numR R (continuoousnumbering)}%
        \line@list@stuffR{\jobname.\extensionchars\the\section@numR R}%
        \ledRcolfalse%
      \fi%
    \fi%
  \fi%
  \l@dend@stuff
  \setcounter{pstart}{1}
  \ifl@dpairing
    \global\l@dnumpstartsL \z@
    \global\pst@rtedLfalse
  \else
    \begingroup
    \global\@afterindenttrue%In order to retablish normal feature if the \begingroup was not here
    \initnumbering@quote
    \ifwidthliketwocolumns%
       \setwidthliketwocolumns%
       \csuse{setpositionliketwocolumns@\columns@position}%
    \fi%
  \fi
  \gdef\eled@sections@@{}%
  \if@noeled@sec\else%
    \makeatletter%
    \InputIfFileExists%
      {\l@auxdir\jobname.eledsec\the\section@num}%
      {}%
      {\led@warn@NoFile{\l@auxdir\jobname.eledsec\the\section@num}}%
    \makeatother%
    \immediate\openout\eled@sectioning@out=\l@auxdir\jobname.eledsec\the\section@num\relax%
  \fi%
}
\newcommand*{\initnumbering@reg}{%
  \global\pst@rtedLfalse
  \global\l@dnumpstartsL \z@
  \global\absline@num \z@
  \gdef\normal@page@break{}
  \gdef\l@prev@pb{}
  \gdef\l@prev@nopb{}
  \global\line@num \z@
  \global\subline@num \z@
  \global\@lock \z@
  \global\sub@lock \z@
  \global\sublines@false
  \global\let\next@page@num=\relax
  \global\let\this@section@next@page@num=\relax%
  \global\let\sub@change=\relax
  \global\last@page@num=-10000%
  \ifdefined\line@numR%
    \line@numR=\z@%
    \last@page@numR=\z@%
  \fi%
  \resetprevline@
  \resetprevpage@num
  \global\stopmsdata@inserted@true%
  \global\let\@msdata@list\relax%
  \global\csundef{@msdata@\add@msd@c @data}%
  }

\def\endnumbering{%
  \ifnumbering
    \global\numberingfalse
    \normal@pars
    \ifnum\l@dnumpstartsL=0%
      \led@err@NumberingWithoutPstart%
    \fi%
    \global\page@num=\this@section@page@num%
    \global\last@page@num=\this@section@last@page@num%
    \global\let\next@page@num\this@section@next@page@num%
    \ifl@dpairing
      \global\pst@rtedLfalse
    \else
      \ifx\insertlines@list\empty\else
        \global\noteschanged@true
      \fi
      \ifx\line@list\empty\else
        \global\noteschanged@true
      \fi
    \fi
    \ifnoteschanged@
      \led@mess@NotesChanged
    \fi
  \else
    \led@err@NumberingNotStarted
  \fi
  \autoparfalse
  \if@noeled@sec\else%
    \immediate\closeout\eled@sectioning@out%
  \fi%
  \ifl@dpairing\else
    \global\l@dnumpstartsL=\z@%
    \endgroup
  \fi
}
\newcount\pausenumbering@page@num%
\newcommand{\pausenumbering}{%
  \ifx\this@section@next@page@num\relax%
    \global\pausenumbering@page@num=0%
  \else%
    \global\pausenumbering@page@num=\this@section@next@page@num%
  \fi%
  \ifautopar\global\autopar@pausetrue\fi%
  \endnumbering\global\numberingtrue}
\newif\ifresumenumbering@start%
\newcommand*{\resumenumbering}{%
  \ifnumbering
     \ifautopar@pause\autopar\fi
     \global\pst@rtedLtrue
     \global\advance\section@num \@ne
     \global\resumenumbering@starttrue%
     \led@mess@SectionContinued{\the\section@num}%
     \set@continuousnumberingforL%
     \line@list@stuff{\jobname.\extensionchars\the\section@num}%
     \ifcontinuousnumberingwithcolumns%
       \unless\ifafterendnumberingR%
          \unless\ifl@dpairing%
            \ledRcoltrue%
            \global\advance\section@numR \@ne%
            \message{Section \the\section@numR R (continuoousnumbering)}%
            \line@list@stuffR{\jobname.\extensionchars\the\section@numR R}%
            \ledRcolfalse%
          \fi%
        \fi%
     \fi%
     \l@dend@stuff
     \ifl@dpairing\else%
        \begingroup%
        \initnumbering@quote%
        \ifwidthliketwocolumns%
          \setwidthliketwocolumns%
          \csuse{setpositionliketwocolumns@\columns@position}%
        \fi%
     \fi%
  \else
    \led@err@NumberingShouldHaveStarted
    \endnumbering
    \beginnumbering
  \fi}

\newcommand{\set@continuousnumberingforL}{%
 \ifcontinuousnumberingwithcolumns%
   \ifl@dpairing%
     \unless\ifl@dpaging%
       \global\c@pstartL=\c@pstart%
     \fi%
   \fi%
 \fi%
}%
\newcommand*{\list@create}[1]{%
  \global\let#1=\empty%
}%
\newcommand*{\list@clear}[1]{%
  \global\let#1=\empty%
}
\newtoks\led@toksa \newtoks\led@toksb
\global\led@toksa={\\}
\long\def\xright@appenditem#1\to#2{%
  \global\led@toksb=\expandafter{#2}%
  \xdef#2{\the\led@toksb\the\led@toksa\expandafter{#1}}%
  \global\led@toksb={}}
\long\def\xleft@appenditem#1\to#2{%
  \global\led@toksb=\expandafter{#2}%
  \xdef#2{\the\led@toksa\expandafter{#1}\the\led@toksb}%
  \global\led@toksb={}}
\def\gl@p#1\to#2{\expandafter\gl@poff#1\gl@poff#1#2}
\long\def\gl@poff\\#1#2\gl@poff#3#4{\gdef#4{#1}\gdef#3{#2}}

\newif\ifbypage@
\newif\ifbypstart@
\newif\ifbypage@R
\newif\ifbypstart@R
\newcommand*{\lineation}[1]{{
  \ifnumbering
    \led@err@LineationInNumbered
  \else
    \def\@tempa{#1}\def\@tempb{page}%
    \ifx\@tempa\@tempb
        \global\bypage@true
        \global\bypstart@false
        \unless\ifnocritical@%
          \Xpstart[][false]%
        \fi%
    \else
       \def\@tempb{pstart}%
       \ifx\@tempa\@tempb
           \global\bypage@false
           \global\bypstart@true
           \unless\ifnocritical@%
             \Xpstart%
           \fi%
       \else
          \def\@tempb{section}
          \ifx\@tempa\@tempb
            \global\bypage@false
            \global\bypstart@false
            \unless\ifnocritical@%
              \Xpstart[][false]%
            \fi%
          \else
            \led@warn@BadLineation
          \fi
       \fi
    \fi
  \fi}}
\newcount\line@margin%
\newcount\line@margin@columns%Only for parallel typesetting
\line@margin@columns=\m@ne%

\newcommand*{\linenummargin}[1]{{%
  \l@dgetline@margin{#1}%
  \ifnum\@l@dtempcntb>\m@ne
    \ifledRcol
      \global\line@marginR=\@l@dtempcntb
      \led@warn@setting@in@rightside{\linenummargin}%
    \else
      \global\line@margin=\@l@dtempcntb
    \fi
  \fi}}

\newcommand*{\l@dgetline@margin}[1]{%
  \def\@tempa{#1}\def\@tempb{left}%
  \ifx\@tempa\@tempb
    \@l@dtempcntb \z@
  \else
     \def\@tempb{right}%
     \ifx\@tempa\@tempb
       \@l@dtempcntb \@ne
     \else
       \def\@tempb{outer}%
       \ifx\@tempa\@tempb
         \@l@dtempcntb \tw@
       \else
         \def\@tempb{inner}%
         \ifx\@tempa\@tempb
           \@l@dtempcntb \thr@@
         \else
           \led@warn@BadLinenummargin
           \@l@dtempcntb \m@ne
         \fi
       \fi
     \fi
  \fi}

\newcounter{firstlinenum}
  \setcounter{firstlinenum}{5}
\newcounter{linenumincrement}
  \setcounter{linenumincrement}{5}
\newcounter{firstsublinenum}
  \setcounter{firstsublinenum}{5}
\newcounter{sublinenumincrement}
  \setcounter{sublinenumincrement}{5}


\newcommand*{\firstlinenum}[1]{%
  \ifledRcol%
    \setcounter{firstlinenumR}{#1}%
    \led@warn@setting@in@rightside{\firstlinenum}%
  \else%
    \setcounter{firstlinenum}{#1}%
  \fi%
}
\newcommand*{\linenumincrement}[1]{%
  \ifledRcol%
    \setcounter{linenumincrementR}{#1}%
    \led@warn@setting@in@rightside{\linenumincrement}%
  \else%
    \setcounter{linenumincrement}{#1}%
  \fi%
}
\newcommand*{\firstsublinenum}[1]{%
  \ifledRcol%
    \setcounter{firstsublinenumR}{#1}%
    \led@warn@setting@in@rightside{\firstsublinenum}%
  \else%
    \setcounter{firstsublinenum}{#1}%
  \fi%
}
\newcommand*{\sublinenumincrement}[1]{%
  \ifledRcol%
    \setcounter{sublinenumincrementR}{#1}%
    \led@warn@setting@in@rightside{\sublinenumincrement}%
  \else%
    \setcounter{sublinenumincrement}{#1}%
  \fi%
}

\newcount\lock@disp
\newcommand{\lockdisp}[1]{{%
  \l@dgetlock@disp{#1}%
  \ifnum\@l@dtempcntb>\m@ne
    \global\lock@disp=\@l@dtempcntb
  \else
    \led@warn@BadLockdisp
  \fi}}
\newcommand*{\l@dgetlock@disp}[1]{
  \def\@tempa{#1}\def\@tempb{first}%
  \ifx\@tempa\@tempb
    \@l@dtempcntb \z@
  \else
     \def\@tempb{last}%
     \ifx\@tempa\@tempb
       \@l@dtempcntb \@ne
     \else
       \def\@tempb{all}%
       \ifx\@tempa\@tempb
         \@l@dtempcntb \tw@
       \else
         \@l@dtempcntb \m@ne
       \fi
     \fi
  \fi}

\newcount\sublock@disp
\newcommand{\sublockdisp}[1]{{%
  \l@dgetlock@disp{#1}%
  \ifnum\@l@dtempcntb>\m@ne
    \global\sublock@disp=\@l@dtempcntb
  \else
    \led@warn@BadSublockdisp
  \fi}}

\newcommand*{\linenumberstyle}[1]{%
  \def\linenumrep##1{\@nameuse{@#1}{##1}}}
\newcommand*{\sublinenumberstyle}[1]{%
  \def\sublinenumrep##1{\@nameuse{@#1}{##1}}}
\linenumberstyle{arabic}
  \let\linenumr@p\linenumrep
\sublinenumberstyle{arabic}
  \let\sublinenumr@p\sublinenumrep

\newlength{\linenumsep}
  \setlength{\linenumsep}{1pc}
\newcommand*{\numlabfont}{\normalfont\scriptsize}
\newcommand*{\ledlinenum}{%
  \bgroup%
  \ifluatex%
    \textdir TLT%
  \fi%
  \numlabfont%
  \unless\iflinenumannotationotherside%
    \ifdefstring{\Xlinenumannotationposition@side}{before}{%
      \l@wrap@ifnotemptybox{\Xwraplinenumannotation@side}{%
        \csuse{annot@\the\absline@num @\the\section@num}%
        }%
    }{}%
  \fi%
  \linenumrep{\line@num}%
  \ifsublines@
    \ifnum\subline@num>0\relax
      \unskip%
      \Xsublinesep@side%
      \sublinenumrep{\subline@num}%
    \fi
  \fi%
  \unless\iflinenumannotationotherside%
    \ifdefstring{\Xlinenumannotationposition@side}{after}{%
      \l@wrap@ifnotemptybox{\Xwraplinenumannotation@side}{%
        \csuse{annot@\the\absline@num @\the\section@num}%
        }%
    }{}%
  \fi%
  \egroup%
}%

\newcommand*{\leftlinenum}{%
  \ledlinenum%
  \kern\linenumsep%
}%
\newcommand*{\rightlinenum}{%
  \kern\linenumsep%
  \ledlinenum%
}%

\newcommand*{\leftlinenumannotation}{%
  \l@wrap@ifnotemptybox{\Xwraplinenumannotation@side}{%
    \csuse{annot@\the\absline@num @\the\section@num}%
    }%
  \kern\linenumsep%
}%
\newcommand*{\rightlinenumannotation}{%
  \kern\linenumsep%
  \l@wrap@ifnotemptybox{\Xwraplinenumannotation@side}{%
    \csuse{annot@\the\absline@num @\the\section@num}%
  }%
}%
\newcount\line@num
\newcount\subline@num
\newif\ifsublines@
\newif\ifsublines@R%
\newcount\absline@num
\newcount\@lock
\newcount\sub@lock
\list@create{\line@list}
\list@create{\insertlines@list}
\list@create{\actionlines@list}
\list@create{\actions@list}
\list@create{\annot@list}%
\newcount\page@num
\newcount\endpage@num
\newcount\endline@num
\newcount\endsubline@num
\newcount\this@section@page@num%
\newif\ifnoteschanged@
\newcommand*{\resetprevline@}{%
    \def\do##1{\global\csundef{prevline##1}}%
    \dolistloop{\@series}%
}
\newcommand*{\resetprevpage@num}{%
    \def\do##1{%
      \ifcsdef{prevpage##1@num}{%
        \global\csname prevpage##1@num\endcsname=\z@%
        \global\csname prevpage##1@numR\endcsname=\z@%
        }%
        {}%
      \ifcsdef{##1prevpage@num}{%
        \global\csname ##1prevpage@num\endcsname=\z@%
        \global\csname ##1prevpage@numR\endcsname=\z@%
        }%
        {}%
      }%
    \dolistloop{\@series}%
}
\newread\@inputcheck
\newcommand*{\read@linelist}[1]{%
  \ifledRcol%
    \list@clearing@regR%
  \else%
    \list@clearing@reg%
  \fi%
  \list@clear{\maxlinesinpar@list}
  \get@linelistfile{#1}%
  \@stopmsd%Security if last \endms{} is forgotten
  \unless\ifledRcol%Get the last line of the last page
    \csnumgdef{@lastabsline@forpage@\the\page@num}{\the\absline@num}%
    \csnumgdef{@lastline@forpage@\the\page@num}{\the\line@num}%
    \csnumgdef{@lastsubline@forpage@\the\page@num}{\the\subline@num}%
  \else%
    \csnumgdef{@lastabsline@forpageR@\the\page@numR}{\the\absline@numR}%
    \csnumgdef{@lastline@forpageR@\the\page@numR}{\the\line@numR}%
    \csnumgdef{@lastsubline@forpageR@\the\page@numR}{\the\subline@numR}%
  \fi%
  \endgroup
  \ifledRcol
    \global\page@numR=\m@ne
    \ifx\actionlines@listR\empty
      \gdef\next@actionlineR{1000000}%
    \else
      \gl@p\actionlines@listR\to\next@actionlineR
      \gl@p\actions@listR\to\next@actionR
    \fi
  \else
    \global\page@num=\m@ne
    \ifx\actionlines@list\empty
      \gdef\next@actionline{1000000}%
    \else
      \gl@p\actionlines@list\to\next@actionline
      \gl@p\actions@list\to\next@action
    \fi
  \fi
}
\newcommand*{\list@clearing@reg}{%
  \list@clear{\line@list}%
  \list@clear{\insertlines@list}%
  \list@clear{\actionlines@list}%
  \list@clear{\actions@list}%
  \list@clear{\linesinpar@listL}%
  \list@clear{\linesonpage@listL}%
  }%
\newcommand*{\get@linelistfile}[1]{%
  \InputIfFileExists{\l@auxdir#1}{%
    \global\noteschanged@false
    \begingroup
      \catcode`\[=1 \catcode`\]=2
      \makeatletter \catcode`\^^M=9}{%
    \led@warn@NoFile{\l@auxdir#1}%
    \global\noteschanged@true
    \begingroup}%
}

\newcommand{\line@list@version}[1]{%
  \IfStrEq{#1}{\this@line@list@version}%
    {}%
    {\ifledRcol%
       \led@warn@Obsolete{\jobname.\extensionchars\the\section@num}%
     \else%
       \led@warn@Obsolete{\jobname.\extensionchars\the\section@num}%
     \fi%
     \endinput%
    }%
}%

\newcommand*{\@nl}[2]{%
  \@page{#1}%
  \ifledRcol%
    \@nl@regR%
  \else%
    \@nl@reg%
  \fi%
}
\newcommand*{\@nl@reg}{%
  \ifx\l@dchset@num\relax \else
    \advance\absline@num \@ne
    \csgdef{l@dchset@num@\the\absline@num}{}%To remember this line have been marked by a \setlinenum
    \set@line@action
    \let\l@dchset@num=\relax
    \advance\absline@num \m@ne
    \advance\line@num \m@ne
  \fi
  \reset@current@annot%
  \advance\absline@num \@ne
         \ifx\next@page@num\relax \else
             \page@action
             \let\next@page@num=\relax
         \fi
         \ifx\sub@change\relax \else
            \ifnum\sub@change>\z@
               \sublines@true
            \else
               \sublines@false
            \fi
            \sub@action
            \let\sub@change=\relax
         \fi
         \ifcase\@lock
            \or
               \@lock \tw@
            \or \or
               \@lock \z@
         \fi
         \ifcase\sub@lock
            \or
               \sub@lock \tw@
            \or \or
               \sub@lock \z@
         \fi
         \ifsublines@
              \ifnum\sub@lock<\tw@
                \advance\subline@num \@ne
              \fi
         \else
              \ifnum\@lock<\tw@
                \advance\line@num \@ne \subline@num \z@
              \fi
         \fi}

\newcount\last@page@num
  \last@page@num=-10000

\newcount\this@section@last@page@num%
  \this@section@last@page@num=-10000%

\newcommand*{\@page}[1]{%
  \ifledRcol
    \ifnum #1=\last@page@numR
    \else
      \csnumgdef{@lastabsline@forpageR@\the\page@numR}{\the\absline@numR}%
      \csnumgdef{@lastline@forpageR@\the\page@numR}{\the\line@numR}%
      \csnumgdef{@lastsubline@forpageR@\the\page@numR}{\the\subline@numR}%
      \ifbypage@R
        \ifx\l@dchset@num\relax%Not resetting if preceded by a \setlinenum
          \ifboolexpr{%
            bool{resumenumberingR@start}%
            and test {\ifnumequal{\last@page@numR}{-10000}}%
          }%
          {}%
          {%
            \line@numR \z@%
            \subline@numR \z@%
            \global\csdef{reset@lineR\the\numexpr\absline@numR+1\relax @\the\section@numR}{}%
          }%
        \fi%
      \fi
      \page@numR=#1%
      \global\this@section@page@numR=#1%
      \last@page@numR=#1%
      \global\this@section@last@page@numR=#1%
      \def\next@page@numR{#1}%
      \gdef\this@section@next@page@numR{#1}%
    \fi
  \else
    \ifnum #1=\last@page@num
    \else
      \csnumgdef{@lastabsline@forpage@\the\page@num}{\the\absline@num}%
      \csnumgdef{@lastline@forpage@\the\page@num}{\the\line@num}%
      \csnumgdef{@lastsubline@forpage@\the\page@num}{\the\subline@num}%
      \ifbypage@
        \ifx\l@dchset@num\relax%Not resetting if preceded by a \setlinenum
          \line@num \z@%
          \subline@num \z@%
          \global\csdef{reset@line\the\numexpr\absline@num+1\relax @\the\section@num}{}%
        \fi%
      \fi
      \page@num=#1%
      \global\this@section@page@num=#1%
      \last@page@num=#1%
      \global\this@section@last@page@num=#1%
      \def\next@page@num{#1}%
      \gdef\this@section@next@page@num{#1}%
      \listxadd{\normal@page@break}{\the\absline@num}
    \fi
  \fi}
\newcommand*{\@pend}[1]{}
\newcommand*{\@pendR}[1]{}
\newcommand*{\@lopL}[1]{}
\newcommand*{\@lopR}[1]{}

\newcommand*{\sub@on}{%
  \ifboolexpr{%
    bool{sublines@}%
    or%
    (bool{sublines@R} and bool{ledRcol})%
  }{%
     \let\sub@change=\relax
  }{%
     \def\sub@change{1}%
  }%
}%
\newcommand*{\sub@off}{%
  \ifboolexpr{%
    bool{sublines@}%
    or%
    (bool{sublines@R} and bool{ledRcol})%
  }{%
     \def\sub@change{-1}%
  }{%
     \let\sub@change=\relax
   }%
}%


\newcommand*{\@adv}[1]{%
  \ifsublines@
    \ifledRcol
      \advance\subline@numR by #1\relax
      \ifnum\subline@numR<\z@
        \led@warn@BadAdvancelineSubline
        \subline@numR \z@
      \fi
    \else
      \advance\subline@num by #1\relax
      \ifnum\subline@num<\z@
        \led@warn@BadAdvancelineSubline
        \subline@num \z@
      \fi
    \fi
  \else
    \ifledRcol
      \advance\line@numR by #1\relax
      \ifnum\line@numR<\z@
        \led@warn@BadAdvancelineLine
        \line@numR \z@
      \fi
    \else
      \advance\line@num by #1\relax
      \ifnum\line@num<\z@
        \led@warn@BadAdvancelineLine
        \line@num \z@
      \fi
    \fi
  \fi
  \set@line@action}


\newcommand*{\@set}[1]{%
  \ifledRcol
    \ifsublines@R%
      \subline@numR=#1\relax
    \else
      \line@numR=#1\relax
    \fi
    \set@line@action
  \else
    \ifsublines@
      \subline@num=#1\relax
    \else
      \line@num=#1\relax
    \fi
    \set@line@action
  \fi}


\newcommand*{\l@d@set}[1]{%
  \ifledRcol
    \line@numR=#1\relax
    \advance\line@numR \@ne
    \def\l@dchset@num{#1}
  \else
    \line@num=#1\relax
    \advance\line@num \@ne
    \def\l@dchset@num{#1}
  \fi}
\let\l@dchset@num\relax



\newcommand*{\page@action}{%
  \ifledRcol
    \xright@appenditem{\the\absline@numR}\to\actionlines@listR
    \xright@appenditem{\next@page@numR}\to\actions@listR
  \else
    \xright@appenditem{\the\absline@num}\to\actionlines@list
    \xright@appenditem{\next@page@num}\to\actions@list
  \fi}

\newcommand*{\set@line@action}{%
  \ifledRcol
    \xright@appenditem{\the\absline@numR}\to\actionlines@listR
    \ifsublines@R%
       \@l@dtempcnta=-\subline@numR
    \else
       \@l@dtempcnta=-\line@numR
    \fi
    \advance\@l@dtempcnta by -5000\relax
    \xright@appenditem{\the\@l@dtempcnta}\to\actions@listR
  \else
    \xright@appenditem{\the\absline@num}\to\actionlines@list
    \ifsublines@
       \@l@dtempcnta=-\subline@num
    \else
       \@l@dtempcnta=-\line@num
    \fi
    \advance\@l@dtempcnta by -5000\relax
    \xright@appenditem{\the\@l@dtempcnta}\to\actions@list
  \fi}

\newcommand*{\sub@action}{%
  \ifledRcol
    \xright@appenditem{\the\absline@numR}\to\actionlines@listR
    \ifsublines@R%
      \xright@appenditem{-1001}\to\actions@listR
    \else
      \xright@appenditem{-1002}\to\actions@listR
    \fi
  \else
    \xright@appenditem{\the\absline@num}\to\actionlines@list
    \ifsublines@
      \xright@appenditem{-1001}\to\actions@list
    \else
      \xright@appenditem{-1002}\to\actions@list
    \fi
  \fi}
\newcommand*{\lock@on}{\futurelet\next\do@lockon}

\newcommand*{\do@lockon}{%
  \ifx\next\lock@off
    \global\let\lock@off=\skip@lockoff
  \else
    \ifledRcol
      \do@lockonR
    \else
      \do@lockonL
    \fi
  \fi}

\newcommand*{\do@lockonL}{%
  \xright@appenditem{\the\absline@num}\to\actionlines@list
  \ifsublines@
    \xright@appenditem{-1005}\to\actions@list
    \ifnum\sub@lock=\z@
      \sub@lock \@ne
    \else
      \ifnum\sub@lock=\thr@@
        \sub@lock \@ne
      \fi
    \fi
  \else
    \xright@appenditem{-1003}\to\actions@list
    \ifnum\@lock=\z@
      \@lock \@ne
    \else
      \ifnum\@lock=\thr@@
        \@lock \@ne
      \fi
    \fi
  \fi}

\newcommand*{\do@lockoffL}{%
  \xright@appenditem{\the\absline@num}\to\actionlines@list
  \ifsublines@
    \xright@appenditem{-1006}\to\actions@list
    \ifnum\sub@lock=\tw@
      \sub@lock \thr@@
    \else
      \sub@lock \z@
    \fi
  \else
    \xright@appenditem{-1004}\to\actions@list
    \ifnum\@lock=\tw@
      \@lock \thr@@
    \else
      \@lock \z@
    \fi
  \fi}

\newcommand*{\do@lockoff}{%
  \reset@current@annot%
  \ifledRcol
    \do@lockoffR
  \else
    \do@lockoffL
  \fi}
\newcommand*{\skip@lockoff}{\global\let\lock@off=\do@lockoff}
\global\let\lock@off=\do@lockoff

\newcommand*{\n@num}{%
  \ifledRcol%
    \xright@appenditem{\the\absline@numR}\to\actionlines@listR
    \xright@appenditem{-1007}\to\actions@listR
  \else%
    \xright@appenditem{\the\absline@num}\to\actionlines@list%
    \xright@appenditem{-1007}\to\actions@list%
  \fi%
}%

\newcommand*{\n@num@stanza}{%
  \ifledRcol%
    \xright@appenditem{\the\absline@numR}\to\actionlines@listR%
    \xright@appenditem{-1008}\to\actions@listR%
  \else%
    \xright@appenditem{\the\absline@num}\to\actionlines@list%%
    \xright@appenditem{-1008}\to\actions@list%
  \fi%
}
\newif\ifl@dhidenumber
\newcommand*{\hidenumbering}{%
  \ifledRcol%
    \write\linenum@outR{\string\hide@num}%
  \else%
    \write\linenum@out{\string\hide@num}%
  \fi%
}%
\newcommand*{\hide@num}{%
  \ifledRcol%
    \xright@appenditem{\the\absline@numR}\to\actionlines@listR%
    \xright@appenditem{-1009}\to\actions@listR%
  \else%
    \xright@appenditem{\the\absline@num}\to\actionlines@list%%
    \xright@appenditem{-1009}\to\actions@list%
  \fi%
}
\newcommand*{\hidenumberingonleftpage}{%
  \ifledRcol%
    \write\linenum@outR{\string\hide@num@left}%
  \else%
    \write\linenum@out{\string\hide@num@left}%
  \fi%
}%

\newcommand*{\hide@num@left}{%
  \ifledRcol%
    \ifodd\page@numR\else%
      \xright@appenditem{\the\absline@numR}\to\actionlines@listR%
      \xright@appenditem{-1009}\to\actions@listR%
    \fi%
  \else%
    \ifodd\page@num\else%
      \xright@appenditem{\the\absline@num}\to\actionlines@list%%
      \xright@appenditem{-1009}\to\actions@list%
    \fi%
  \fi%
}%

\newcommand*{\hidenumberingonrightpage}{%
  \ifledRcol%
    \write\linenum@outR{\string\hide@num@right}%
  \else%
    \write\linenum@out{\string\hide@num@right}%
  \fi%
}%

\newcommand*{\hide@num@right}{%
  \ifledRcol%
    \ifodd\page@numR%
      \xright@appenditem{\the\absline@numR}\to\actionlines@listR%
      \xright@appenditem{-1009}\to\actions@listR%
    \fi%
  \else%
    \ifodd\page@num%
      \xright@appenditem{\the\absline@num}\to\actionlines@list%%
      \xright@appenditem{-1009}\to\actions@list%
    \fi%
  \fi%
}%

\newcount\insert@count
\newcommand*{\dummy@ref}[2]{#2}
\newcommand*{\@ref}[2]{%
  \ifledRcol%
    \@ref@regR{#1}{#2}%
  \else%
    \@ref@reg{#1}{#2}%
  \fi%
}%
\newcommand*{\@ref@reg}[2]{%
  \global\insert@count=#1\relax
  \global\advance\@edtext@level by 1%
  \loop\ifnum\insert@count>\z@
    \xright@appenditem{\the\absline@num}\to\insertlines@list
    \global\advance\insert@count \m@ne
  \repeat
  \begingroup
    \let\@ref=\dummy@ref
    \let\@lopL\@gobble
    \let\page@action=\relax
    \let\sub@action=\relax
    \let\set@line@action=\relax
    \let\@lab=\relax
    \let\@lemma=\relax%
    \let\@sw\@gobblethree%
    \let\store@annot@to@absline\@gobble%
    #2
    \global\endpage@num=\page@num
    \global\endline@num=\line@num
    \global\endsubline@num=\subline@num
    \global\let\endcurrent@annot=\current@annot%
  \endgroup
    \xright@appenditem%
      {\the\page@num|\the\line@num|%
       \ifsublines@ \the\subline@num \else 0\fi|%
       \the\endpage@num|\the\endline@num|%
       \ifsublines@ \the\endsubline@num \else 0\fi}\to\line@list
    \xright@appenditem%
      {\current@annot|\endcurrent@annot}\to\annot@list%
     \@ref@reg@parse{#2}%
  \global\advance\@edtext@level by -1%
}
\newcommand{\@ref@reg@parse}[1]{%
     \expandafter\list@create\expandafter{\csname sw@list@edtext@tmp@\the\@edtext@level\endcsname}%
     \providebool{lemmacommand@\the\@edtext@level}%
     \boolfalse{lemmacommand@\the\@edtext@level}%
  #1%
  \ifnum\@edtext@level>0%
    \def\create@this@edtext@level{\expandafter\list@create\expandafter{\csname sw@list@edtext@\the\@edtext@level\endcsname}}%
    \ifcsundef{sw@list@edtext@\the\@edtext@level}{\create@this@edtext@level}{}%
    \letcs{\@tmp}{sw@list@edtext@\the\@edtext@level}%
    \letcs{\@tmpp}{sw@list@edtext@tmp@\the\@edtext@level}
    \xright@appenditem{\expandonce\@tmpp}\to\@tmp%
    \global\cslet{sw@list@edtext@\the\@edtext@level}{\@tmp}%
  \fi%
}

\newcommand{\@ref@later}[1]{%
  \global\advance\@edtext@level by \@ne%
  \ifledRcol%
    \@ref@reg@parseR{#1}%
  \else%
    \@ref@reg@parse{#1}%
  \fi%
  \global\advance\@edtext@level by -\@ne%
}%
\newwrite\linenum@out
\newif\iffirst@linenum@out@
  \first@linenum@out@true
\newcommand{\this@line@list@version}{8}%
\let\next@line@list@stuff\relax%
\newcommand*{\line@list@stuff}[1]{%
  \global\newtoggle{notfirstrun@#1}%
  \IfFileExists{\l@auxdir#1}%
    {\global\toggletrue{notfirstrun@#1}}%
    {\global\togglefalse{notfirstrun@#1}}%
  \next@line@list@stuff%
  \global\let\next@line@list@stuff\relax%
  \read@linelist{#1}%
  \iffirst@linenum@out@
     \global\first@linenum@out@false%
     \immediate\openout\linenum@out=\l@auxdir#1\relax%
     \immediate\write\linenum@out{\string\line@list@version{\this@line@list@version}}%
     \ifl@dpaging%
       \immediate\write\linenum@out{\string\@par@sync@option{\@par@this@sync@option}}%
     \fi%
  \else
     \if@minipage%
       \leavevmode%
     \fi%
     \closeout\linenum@out%
     \openout\linenum@out=\l@auxdir#1\relax%
     \write\linenum@out{\string\line@list@version{\this@line@list@version}}%
     \ifl@dpaging%
       \write\linenum@out{\string\@par@sync@option{\@par@this@sync@option}}%
     \fi%
  \fi%
}%

\newcommand*{\new@line}{%
  \ifnumberline%
    \IfStrEq{\led@pb@setting}{after}%
      {\xifinlist{\the\absline@num}{\l@prev@nopb}%
        {\xifinlist{\the\absline@num}{\normal@page@break}%
          {\numgdef{\@next@page}{\c@par@page+\@ne}%
          \write\linenum@out{\string\@nl[\@next@page][\@next@page]}%
        }%
        {\write\linenum@out{\string\@nl[\the\@this@c@page][\thepage]}}%
      }%
      {\write\linenum@out{\string\@nl[\the\@this@c@page][\thepage]}}}%
    {}%
    \IfStrEq{\led@pb@setting}{before}%
      {\numdef{\next@absline}{\the\absline@num+\@ne}%
      \xifinlist{\next@absline}{\l@prev@nopb}%
        {\xifinlist{\the\absline@num}{\normal@page@break}%
          {\numgdef{\nc@page}{\c@par@page+\@ne}%
          \write\linenum@out{\string\@nl[\nc@page][\nc@page]}%
          }%
          {\write\linenum@out{\string\@nl[\the\@this@c@page][\thepage]}}%
        }%
        {\write\linenum@out{\string\@nl[\the\@this@c@page][\thepage]}}%
      }%
      {}%
    \IfStrEqCase{\led@pb@setting}%
      {%
      {before}{\relax}%
      {after}{\relax}%
    }[%
      \write\linenum@out{\string\@nl[\the\@this@c@page][\thepage]}%
    ]%
  \fi%
}

\newcount\@this@c@page%%
\newcommand{\set@this@c@page}{%
  \ifboolexpr{%
    bool{sameparallelpagenumber}%
    or bool{prevpgnotnumbered}%
  }%
  {\global\@this@c@page=\c@par@page}%
  {\global\@this@c@page=\c@page}%
}%
\newif\if@noneed@Footnote%

\newcommand*{\flag@start}{%
  \ifledRcol%
    \edef\next{\write\linenum@outR{%
                  \string\@ref[\the\insert@countR][}}%
    \next%
    \ifnum\insert@countR<1%
      \if@noneed@Footnote\else%
        \led@err@EdtextWithoutFootnote%
      \fi%
    \fi%
  \else%
    \edef\next{\write\linenum@out{%
                  \string\@ref[\the\insert@count][}}%
    \next%
    \ifnum\insert@count<1%
      \if@noneed@Footnote\else%
        \led@err@EdtextWithoutFootnote%
      \fi%
    \fi%
  \fi}%

\newcommand*{\flag@end}{%
  \ifledRcol%
    \write\linenum@outR{]}%
  \else%
    \write\linenum@out{]}%
  \fi}%

\newcommand{\flag@start@RTL}{%
  \edlabel{edtext:start:\csuse{thisedtext@\the\@edtext@level}}%
  \IfStrEq{\xabslineref{edtext:start:\csuse{thisedtext@\the\@edtext@level}}}%
    {000}%
    {}%
    {%
    \ifnumequal%
      {\xabslineref{edtext:start:\csuse{thisedtext@\the\@edtext@level}}}%
      {\xabslineref{edtext:end:\csuse{thisedtext@\the\@edtext@level}}}%
      {\flag@end}%
      {\flag@start}%
  }%
}%

\newcommand{\flag@end@RTL}{%
  \edlabel{edtext:end:\csuse{thisedtext@\the\@edtext@level}}%
  \IfStrEq{\xabslineref{edtext:start:\csuse{thisedtext@\the\@edtext@level}}}%
      {000}%
      {}%
      {%
      \ifnumequal%
        {\xabslineref{edtext:start:\csuse{thisedtext@\the\@edtext@level}}}%
        {\xabslineref{edtext:end:\csuse{thisedtext@\the\@edtext@level}}}%
        {\flag@start}%
        {\flag@end}%
    }%
}%
\newcommand*{\flag@start@later}{%
  \ifledRcol%
    \write\linenum@outR{\string\@ref@later[}%
  \else%
    \write\linenum@out{\string\@ref@later[}%
  \fi%
}%
\newcommand{\flag@end@later}{%
  \ifledRcol%
    \write\linenum@outR{]}%
  \else%
    \write\linenum@out{]}%
  \fi%
}

\newcommand*{\startsub}{%
  \dimen0\lastskip%
  \ifdim\dimen0>0pt%
    \unskip%
  \fi%
  \ifledRcol%
    \write\linenum@outR{\string\sub@on}%
  \else%
    \write\linenum@out{\string\sub@on}%
  \fi%
  \ifdim\dimen0>0pt%
    \hskip\dimen0%
  \fi%
}%

\def\endsub{%
  \dimen0\lastskip%
  \ifdim\dimen0>0pt%
    \unskip%
  \fi%
  \ifledRcol%
    \write\linenum@outR{\string\sub@off}%
  \else%
    \write\linenum@out{\string\sub@off}%
  \fi%
  \ifdim\dimen0>0pt%
    \hskip\dimen0%
  \fi%
}%

\newcommand*{\advanceline}[1]{\leavevmode%
  \ifledRcol \write\linenum@outR{\string\@adv[#1]}%
  \else      \write\linenum@out{\string\@adv[#1]}%
  \fi%
}

\newcommand*{\setline}[1]{%
 \leavevmode%
  \ifnum#1<\z@
    \led@warn@BadSetline%
  \else%
    \ifledRcol%
      \write\linenum@outR{\string\@set[#1]}%
    \else%
      \write\linenum@out{\string\@set[#1]}%
    \fi%
  \fi%
}%


\newcommand*{\setlinenum}[1]{%
  \ifnum#1<\z@%
    \led@warn@BadSetlinenum%
  \else%
    \ifledRcol%
      \write\linenum@outR{\string\l@d@set[#1]}%
    \else%
      \write\linenum@out{\string\l@d@set[#1]}%
    \fi%
  \fi%
}%


\newcommand*{\startlock}{%
  \ifledRcol \write\linenum@outR{\string\lock@on}%
  \else      \write\linenum@out{\string\lock@on}%
  \fi}
\def\endlock{%
  \ifledRcol \write\linenum@outR{\string\lock@off}%
  \else      \write\linenum@out{\string\lock@off}%
  \fi}
\newif\ifl@dskipnumber
\newif\ifl@dskipversenumber%
\newcommand*{\skipnumbering}{%
  \leavevmode%
  \ifledRcol%
    \ifinstanza%
       \write\linenum@outR{\string\n@num@stanza}%
    \else%
      \write\linenum@outR{\string\n@num}%
    \fi%
    \advanceline{-1}%
  \else%
    \ifinstanza%
       \write\linenum@out{\string\n@num@stanza}%
    \else%
      \write\linenum@out{\string\n@num}%
    \fi%
    \advanceline{-1}%
  \fi%
}%

\list@create{\end@lemmas}
\newcommand{\dummy@edtext}[2]{#1}
\newcommand{\dummy@edtext@showlemma}[2]{\showlemma{#1}}%
\newcommand*{\no@expands}{%
  \let\select@@lemmafont=0%
  \let\startsub=\relax  \let\endsub=\relax
  \let\startlock=\relax \let\endlock=\relax
  \let\edlabel=\@gobble
  \let\setline=\@gobble \let\advanceline=\@gobble
  \let\sameword\sameword@inedtext%
  \let\edtext=\dummy@edtext
  \let\edindex\dummy@edindex%
  \l@dtabnoexpands
  \l@noexpands@edgl%
  \let\linenumannotation=\@gobble%
  \morenoexpands}
\let\morenoexpands=\relax

\newcommand{\@tag}{}
\newcount\@edtext@level%
\@edtext@level=0%
\newif\if@edtext@secondarg@%
\newcounter{edtext}
\renewcommand{\theedtext}{edtxt@\the\c@edtext}%
\newcommand{\edtext}[2]{\leavevmode%
  \ifnumberedpar@%
      \@check@edtext@args{#1}{#2}%
      \global\advance\@edtext@level by 1%
      \stepcounter{edtext}%
      \csxdef{thisedtext@\the\@edtext@level}{\theedtext}%
      \global\@lemmacommand@false%
      \begingroup%
        \ifledRcol%
          \ifcsvoid{sw@list@edtextR@\the\@edtext@level}%
                {\global\let\sw@inthisedtext\empty}%
                {\expandafter\gl@p\csname sw@list@edtextR@\the\@edtext@level\endcsname\to\sw@inthisedtext}%
        \else%
          \ifcsvoid{sw@list@edtext@\the\@edtext@level}%
                {\global\let\sw@inthisedtext\empty}%
                {\expandafter\gl@p\csname sw@list@edtext@\the\@edtext@level\endcsname\to\sw@inthisedtext}%
        \fi%
        \global\renewcommand{\@tag}{%
          \no@expands #1%
        }%
        \set@line%
        \ifledRcol \global\insert@countR \z@%
        \else      \global\insert@count \z@ \fi%
        \@edtext@secondarg@true%
        \ignorespaces #2\relax%
        \@edtext@secondarg@false%
        \if@RTL%
          \flag@start@RTL%
        \else%
          \flag@start%
        \fi%
       \if@lemmacommand@%
         \ifledRcol%
           \write\linenum@outR{\string\@lemma}%
         \else%
           \write\linenum@out{\string\@lemma}%
         \fi%
       \fi%
      \endgroup%
      \ifdef{\hypertarget}%
        {%
        \Hy@raisedlink@left{\hypertarget{\csuse{thisedtext@\the\@edtext@level}:start}{}}%
        \showlemma{#1}%
        \Hy@raisedlink{\hypertarget{\csuse{thisedtext@\the\@edtext@level}:end}{}}%
        }%
        {%
        \showlemma{#1}%
        }%
      \ifx\end@lemmas\empty \else%
        \gl@p\end@lemmas\to\x@lemma%
        \x@lemma%
        \global\let\x@lemma=\relax%
      \fi%
      \if@RTL%
        \flag@end@RTL%
      \else%
        \flag@end%
      \fi%
      \global\@noneed@Footnotefalse%
      \global\advance\@edtext@level by -1%
      \global\@lemmacommand@false%
      \global\let\@beforeinsertofthisedtext\relax%
  \else%
  \showlemma{#1} (\textbf{\textsc{Edtext outside numbered paragraph}})\led@err@edtextoutsidepstart%
  \fi%
}%

\newcommand{\@check@edtext@args}[2]{%
  \begingroup%
    \noexpandarg%
    \IfSubStr{#1}{\pstart}{\led@err@PstartInEdtext{first}}{}%
    \IfSubStr{#1}{\pend}{\led@err@PendInEdtext{first}}{}%
    \IfSubStr{#2}{\pstart}{\led@err@PstartInEdtext{second}}{}%
    \IfSubStr{#2}{\pend}{\led@err@PendInEdtext{second}}{}%
  \endgroup%
}%
\let\@beforeinsertofthisedtext\relax
\newif\ifnumberline
\numberlinetrue
\newcommand*{\set@line}{%
  \ifledRcol
    \ifx\line@listR\empty
      \global\noteschanged@true
      \xdef\l@d@nums{000|000|000|000|000|000|\edfont@info}%
    \else
      \gl@p\line@listR\to\@tempb
      \xdef\l@d@nums{\@tempb|\edfont@info}%
      \global\let\@tempb=\undefined
    \fi
    \ifx\annot@listR\empty%
      \xdef\l@current@annotR{|}%
    \else%
      \gl@p\annot@listR\to\@tempb%
      \xdef\l@current@annotR{\@tempb}%
      \global\let\@tempb=\undefined%
    \fi%
  \else
    \ifx\line@list\empty
      \global\noteschanged@true
      \xdef\l@d@nums{000|000|000|000|000|000|\edfont@info}%
    \else
      \gl@p\line@list\to\@tempb
      \xdef\l@d@nums{\@tempb|\edfont@info}%
      \global\let\@tempb=\undefined
    \fi
    \ifx\annot@list\empty%
      \xdef\l@current@annot{|}%
    \else%
      \gl@p\annot@list\to\@tempb%
      \xdef\l@current@annot{\@tempb}%
      \global\let\@tempb=\undefined%
    \fi%
  \fi}

\newcommand*{\edfont@info}{\f@encoding/\f@family/\f@series/\f@shape}

\newcommand*{\lemma}[1]{%
  \global\@lemmacommand@true%
  \global\renewcommand{\@tag}{%
    \no@expands #1%
  }%
  \ignorespaces%
}%
\newcommand{\@lemma}{%
  \booltrue{lemmacommand@\the\@edtext@level}%
}%
\newif\if@lemmacommand@%
\newcommand*{\linenum}[1]{%
  \xdef\@tempa{#1|||||||\noexpand\\\l@d@nums}%
  \global\let\l@d@nums=\empty
  \expandafter\line@set\@tempa|\\\ignorespaces}
\def\line@set#1|#2\\#3|#4\\{%
  \gdef\@tempb{#1}%
  \ifx\@tempb\empty
        \l@d@add{#3}%
  \else
        \l@d@add{#1}%
  \fi
  \gdef\@tempb{#4}%
  \ifx\@tempb\empty\else
      \l@d@add{|}\line@set#2\\#4\\%
  \fi}
\newcommand{\l@d@add}[1]{\xdef\l@d@nums{\l@d@nums#1}}

\newcommand*{\lineannot}[1]{%
  \lineannot@set#1|%
}%
\def\lineannot@set#1|#2|{%
  \expandafter\parse@annot#1|#2|%
  \IfStrEq{#1}{}%
    {\let\@tempa\annot@start}%
    {\def\@tempa{#1}}%
  \IfStrEq{#2}{}%
    {\let\@tempb\annot@start}%
    {\def\@tempb{#2}}%
  \xdef\l@current@annot{\@tempa|\@tempb}%
}%
\newcommand{\get@sw@txt}[1]{%
  \begingroup%
    \swnoexpands%
    \ifsw@caseinsensitive%
      \def\@tmpa##1{\lowercase{##1}}%
    \else%
      \def\@tmpa##1{##1}%
    \fi%
      \@tmpa{\protected@xdef\sw@txt{#1}}%
  \endgroup%
}%
\newcommand{\swnoexpands}{%
    \let\sameword\l@secondmandarg%Allow to have nested \sameword
    \let\emph\@firstofone%
    \let\textit\@firstofone%
    \let\textbf\@firstofone%
    \let\textsc\@firstofone%
    \let\framebox\@firstofone%
    \let\edtext\dummy@edtext%
    \RenewExpandableDocumentCommand{\edindex}{om}{}%
    \ifdefined\index%
      \RenewExpandableDocumentCommand{\index}{om}{}%
    \fi%
    \let\selectlanguage\@gobble%
    \let\foreignlanguage\@secondoftwo%
    \ifdefined\xpg@loaded%
      \renewcommand\do[1]{%
        \expandafter\RenewExpandableDocumentCommand\expandafter{\csname text##1\endcsname}{om}{####2}%
      }%
      \expandafter\docsvlist\expandafter{\xpg@loaded}%
    \fi%
}%
\newcommandx{\sameword}[2][1,usedefault]{%
     \ifxetex%
       \if@RTL%
        \led@err@samewordRTL%
       \fi%
     \fi%
     \leavevmode%
     \get@sw@txt{#2}%
  \unless\ifledRcol%
     \csnumgdef{sw@\sw@txt}{\csuse{sw@\sw@txt}+\@ne}%
     \protected@write\linenum@out{}{\string\@sw{\sw@txt}{\csuse{sw@\sw@txt}}{#1}}%
  \else%
     \csnumgdef{sw@\sw@txt}{\csuse{sw@\sw@txt}+\@ne}%
     \protected@write\linenum@outR{}{\string\@sw{\sw@txt}{\csuse{sw@\sw@txt}}{#1}}%
  \fi%
  #2%
}%
\newif\if@addsw%
\newcommand{\@sw}[3]{%
  \get@sw@txt{#1}%
  \unless\ifledRcol%
    \csxdef{sw@\sw@txt @\the\absline@num @\the\section@num}{#2}%
    \numdef{\prev@line}{\the\absline@num-1}%
    \ifcsundef{sw@\sw@txt @\prev@line @\the\section@num}{%
      \csnumgdef{sw@\sw@txt @\prev@line @\the\section@num}{#2-1}%
     }{}%
    \numdef{\the@sw}{#2-\csuse{sw@\sw@txt @\prev@line @\the\section@num}}%
  \else%
    \csxdef{sw@\sw@txt @\the\absline@numR @\the\section@numR @R}{#2}%
    \numdef{\prev@line}{\the\absline@numR-1}%
    \ifcsundef{sw@\sw@txt @\prev@line @\the\section@numR @R}{%
      \csnumgdef{sw@\sw@txt @\prev@line @\the\section@numR @R}{#2-1}%
     }{}%
    \numdef{\the@sw}{#2-\csuse{sw@\sw@txt @\prev@line @\the\section@numR @R}}%
  \fi%
  \@tempcnta=\@edtext@level
  \@whilenum{\@tempcnta>0}\do{%
    \ifcsdef{sw@list@edtext@tmp@\the\@tempcnta}%
      {%
        \@addswfalse%
        \notbool{lemmacommand@\the\@tempcnta}%
          {\@addswtrue}%
          {\IfStrEq{#3}{inlemma}%
            {\@addswtrue}%
            {%
             \def\do##1{%
                \ifnumequal{##1}{\the\@tempcnta}%
                  {\@addswtrue\listbreak}%
                  {}%
             }%
             \docsvlist{#3}%
            }%
          }%
          \if@addsw%
            \letcs{\@tmp}{sw@list@edtext@tmp@\the\@tempcnta}%
            \ifledRcol%
              \xright@appenditem{{\the@sw}{\the\absline@numR}}\to\@tmp%
            \else%
              \xright@appenditem{{\the@sw}{\the\absline@num}}\to\@tmp%
            \fi%
            \cslet{sw@list@edtext@tmp@\the\@tempcnta}{\@tmp}%
          \fi%
      }%
      {}%
    \advance\@tempcnta by -1%
  }%
}%
\newcommandx{\sameword@inedtext}[2][1,usedefault]{%
  \get@sw@txt{#2}%
  \unless\ifledRcol@%
      \ifx\sw@list@inedtext\empty%
        \def\the@sw{999}%
        \def\this@absline{-99}%
      \else%
        \gl@p\sw@list@inedtext\to\@tmp%
        \edef\the@sw{\expandafter\@firstoftwo\@tmp}%
        \edef\this@absline{\expandafter\@secondoftwo\@tmp}%
      \fi%
      \ifcsdef{sw@\sw@txt @\this@absline @\the\section@num}{%
        \numdef{\prev@line}{\this@absline-1}%
        \numdef{\sw@atthisline}{\csuse{sw@\sw@txt @\this@absline @\the\section@num}-\csuse{sw@\sw@txt @\prev@line @\the\section@num}}%
        }%
        {\numdef{\sw@atthisline}{0}}%
      \ifnumgreater{\sw@atthisline}{1}%
          {\showwordrank{#2}{\the@sw}}%
          {#2}%
  \else%
      \ifx\sw@list@inedtext\empty%
        \def\the@sw{999}%
        \def\this@absline{-99}%
      \else%
        \gl@p\sw@list@inedtext\to\@tmp%
        \edef\the@sw{\expandafter\@firstoftwo\@tmp}%
        \edef\this@absline{\expandafter\@secondoftwo\@tmp}%
      \fi%
      \ifcsdef{sw@\sw@txt @\this@absline @\the\section@numR @R}{%
        \numdef{\prev@line}{\this@absline-1}%
        \numdef{\sw@atthisline}{\csuse{sw@\sw@txt @\this@absline @\the\section@numR @R}-\csuse{sw@\sw@txt @\prev@line @\the\section@numR @R}}%
        }%
        {\numdef{\sw@atthisline}{0}}%
        \ifnumgreater{\sw@atthisline}{1}%
          {\showwordrank{#2}{\the@sw}}%
          {#2}%
  \fi%
}%
\newcommand{\showwordrank}[2]{%
  #1\textsuperscript{#2}%
}%

\newbox\raw@text
\newif\ifnumberedpar@
\newcount\num@lines
\newbox\one@line
\newcount\par@line
\newcount\pstarts@typeset@L%
\newcount\pstarts@read@L%

\newcommand{\AtStartEveryPstart}[1]{%
  \ifstrempty{#1}%
    {\gdef\@at@start@every@pstart{}}%
    {\gdef\@at@start@every@pstart{#1}}%
}%
\def\@at@start@every@pstart{}%

\newif\ifat@every@pstart@star@%
\newcommand{\AtEveryPstart}[1]{%
  \ifstrempty{#1}%
    {\gdef\at@every@pstart{}}%
    {\gdef\at@every@pstart{\noindent#1}}%
 \global\at@every@pstart@star@false%
}%
\WithSuffix\newcommand\AtEveryPstart*[1]{%
  \ifstrempty{#1}%
    {\gdef\at@every@pstart{}}%
    {\gdef\at@every@pstart{#1}}%
  \global\at@every@pstart@star@true%
}%
\def\at@every@pstart{}%

\newcounter{pstart}
\renewcommand{\thepstart}{{\bfseries\@arabic\c@pstart}. }
\newif\ifnumberpstart
\numberpstartfalse
\newif\iflabelpstart
\labelpstartfalse
\newcommandx*{\pstart}[2][1,2,usedefault]{%
  \normal@pars%
  \ifboolexpr{%
    test {\ifstrempty{#1}}%
    and test {\ifstrempty{#2}}%
  }%
    {\at@every@pstart}%
    {%
    \ifstrempty{#1}{}{\noindent#1}%
    \ifstrempty{#2}{}{#2}%
    }%
  \ifautopar%
    \autopar%
  \fi%
  \ifluatex%
    \edef\l@luatextextdir@L{\the\textdir}%
  \fi%
  \@nobreaktrue%
  \ifnumbering \else%
    \led@err@PstartNotNumbered%
    \beginnumbering%
  \fi%
  \ifnumberedpar@%
    \led@err@PstartInPstart%
    \pend%
  \fi%
  \list@clear{\inserts@list}%
  \global\let\next@insert=\empty%
  \begingroup\normal@pars%
  \global\advance \l@dnumpstartsL\@ne
  \global\advance \pstarts@typeset@L\@ne%
  \global\advance \pstarts@read@L\@ne%
  \global\setbox\raw@text=\vbox\bgroup%
    \if@nobreak%
      \if@afterindent\else%
        \noindent%
        \global\@afterindenttrue%
      \fi%
    \fi%
    \ifboolexpr{%
      bool{autopar}%
      and  bool{by@autopar}%
    }%
    {}%
    {%
      \ifnumberpstart%
        \ifinstanza\else%
           \ifsidepstartnum\else%
             \thepstart%
           \fi%
        \fi%
      \fi%
    }%
  \numberedpar@true%
  \iflabelpstart%
    \protected@edef\@currentlabel{\p@pstart\thepstart}%
  \fi%
  \l@dzeropenalties%
  \@at@start@every@pstart%
  \global\by@autoparfalse%
  \ignorespaces%because not automatically ignored if an optional argument is used (classical TeX behavior for space after commands)
  }
\newcommandx*{\pend}[2][1,2,usedefault]{\ifnumbering \else%
    \led@err@PendNotNumbered%
  \fi%
  \global\l@dskipversenumberfalse%
  \ifnumberedpar@ \else%
    \led@err@PendNoPstart%
  \fi%
  \l@dzeropenalties%
  \@at@end@every@pend%
  \endgraf\global\num@lines=\prevgraf\egroup%
  \global\par@line=0%
  \get@firsthalf@antilabe@wd%
  \loop\ifvbox\raw@text%
    \do@line%
  \repeat%
  \flush@notes%
  \endgroup%
  \ignorespaces%
  \ifnumberpstart%
     \global\pstartnumtrue%
  \fi%
  \addtocounter{pstart}{1}%
  \ifcontinuousnumberingwithcolumns%
    \addtocounter{pstartL}{1}%
    \addtocounter{pstartR}{1}%
  \fi%
  \normal@pars%
  \ifboolexpr{%
    test {\ifstrempty{#1}}%
    and test {\ifstrempty{#2}}%
  }%
    {\at@every@pend}%
    {%
    \ifstrempty{#1}{}{\noindent#1}%
    \ifstrempty{#2}{}{#2}%
    }%
  \@nobreakfalse%
  \ifautopar%
    \autopar%
  \fi%
    \ifbypstart@%
       \begingroup%
         \unless\ifinstanza%
           \let\leavevmode\relax%
           \setlinenum{0}%
        \fi%
       \endgroup%
       \resetprevline@%
    \fi%
}

\newif\ifat@every@pend@star@%
\newcommand{\AtEveryPend}[1]{%
  \ifstrempty{#1}%
    {\gdef\at@every@pend{}}%
    {\gdef\at@every@pend{\noindent#1}}%
  \global\at@every@pend@star@false%
}%
\WithSuffix\newcommand\AtEveryPend*[1]{%
  \ifstrempty{#1}%
    {\gdef\at@every@pend{}}%
    {\gdef\at@every@pend{#1}}%
  \global\at@every@pend@star@true%
}%
\xdef\at@every@pend{}%

\newcommand{\AtEndEveryPend}[1]{%
  \ifstrempty{#1}%
    {\xdef\@at@end@every@pend{}}%
    {\gdef\@at@end@every@pend{#1}}%
}%
\def\@at@end@every@pend{}%
\newcommand*{\l@dzeropenalties}{%
  \brokenpenalty \z@ \clubpenalty \z@
  \displaywidowpenalty \z@ \interlinepenalty \z@ \predisplaypenalty \z@
  \postdisplaypenalty \z@ \widowpenalty \z@}

\newif\ifautopar
\newif\ifby@autopar%
\newcommand*{\autopar}{
  \ifledRcol
    \ifnumberingR \else
    \led@err@AutoparNotNumbered
    \beginnumberingR
    \fi
    \else
    \ifnumbering \else
    \led@err@AutoparNotNumbered
    \beginnumbering
    \fi
  \fi
  \autopartrue
  \everypar{\setbox0=\lastbox
    \endgraf \vskip-\parskip
    \global\by@autopartrue%
    \pstart \noindent \kern\wd0%
    \ifnumberpstart%
      \ifinstanza\else%
        \thepstart%
      \fi%
    \fi%
    \let\par=\pend}%
  \ignorespaces}
\newcommand*{\normal@pars}{\everypar{}\let\par\endgraf}

\newif\ifautopar@pause
 \newcommand*{\l@dunhbox@line}[1]{\unhbox #1}
 \newcommand*{\do@line}{%
  {\vbadness=10000
   \splittopskip=\z@
   \do@linehook
\l@demptyd@ta
   \global\setbox\one@line=\vsplit\raw@text to\baselineskip}%
  \unvbox\one@line \global\setbox\one@line=\lastbox
  \getline@num
  \IfStrEq{\led@pb@setting}{before}{\led@check@pb\led@check@nopb}{}
  \ifnum\@lock>\@ne
    \inserthangingsymboltrue
  \else
    \inserthangingsymbolfalse
  \fi
  \check@pb@in@verse
  \ifl@dhidenumber%
    \global\l@dhidenumberfalse%
    \f@x@l@cks%
  \else%
    \affixline@num%
  \fi%
  \xifinlist{\the\pstarts@typeset@L}{\eled@sections@@}%
     {\print@eledsection}%
     {\print@line}%
  \IfStrEq{\led@pb@setting}{after}{\led@check@pb\led@check@nopb}{}
  }%
\def\print@line{
    \affixpstart@num%
    \hb@xt@ \linewidth{%
        \do@insidelinehook%
        \csuse{insidethis@\the\absline@num @\the\section@num}%
        \global\csundef{insidethis@\the\absline@num @\the\section@num}%
        \l@dld@ta%
        \if@firstlineofpage%
          \set@Xtxtbeforenotes%
          \set@txtbeforenotesX%
          \global\@firstlineofpagefalse%
        \fi%
        \ifdefstring{\ms@data@position}{msdata-regular}{%
          \insert@msdata%
          \add@inserts%
          \add@Xgroupbyline%
        }{%
          \add@inserts%
          \add@Xgroupbyline%
          \insert@msdata%
        }%
        \affixside@note%
        \l@dlsn@te
        {\ledllfill\hb@xt@ \wd\one@line{%
          \new@line%
        \continuousnumberingwithcolumns@sync@linenumber@singletext%
          \ifluatex%
            \textdir\l@luatextextdir@L%
          \fi%
          \inserthangingsymbol%
          \l@dunhbox@line{\one@line}}%
          \ledrlfill\l@drd@ta%
          \l@drsn@te%
      }}%
    \add@penalties%
}
\def\print@eledsection{%
    \disable@edindex%
    \if@firstlineofpage%
      \set@Xtxtbeforenotes%
      \set@txtbeforenotesX%
      \global\@firstlineofpagefalse%
    \fi%
    \ifdefstring{\ms@data@position}{msdata-regular}{%
      \insert@msdata%
      \add@inserts%
      \add@Xgroupbyline%
    }{%
      \add@inserts%
      \add@Xgroupbyline%
      \insert@msdata%
    }%
    \affixside@note%
    \numdef{\temp@}{\pstarts@typeset@L-1}%
    \xifinlist{\temp@}{\eled@sections@@}{\@nobreaktrue}{\@nobreakfalse}%
    \@eled@sectioningtrue%
    \csuse{eled@sectioning@\the\pstarts@typeset@L}%
    \@eled@sectioningfalse%
    \global\csundef{eled@sectioning@\the\pstarts@typeset@L}%
    \if@RTL%
      \hspace{-3\paperwidth}%
    {\hbox{\l@dunhbox@line{\one@line}} \new@line}%
    \else%
      \hspace{3\paperwidth}%
    {\new@line \hbox{\l@dunhbox@line{\one@line}}}%
    \fi%
    \vskip-\baselineskip%
    \continuousnumberingwithcolumns@sync@linenumber@singletext%
    \restore@edindex%
}
\newcommand*{\do@linehook}{}
\newcommand*{\do@insidelinehook}{}
\newcommand*{\dolinehook}[1]{\gdef\do@linehook{#1}}%
\newcommand*{\doinsidelinehook}[1]{\gdef\do@insidelinehook{#1}}%

\newcommand{\doinsidethislinehook}[1]{%
  \leavevmode%In case it begins with a \pstart, ensure the \@insidethisline is written after \@nl
  \ifledRcol%
    \write\linenum@outR{\string\@insidethisline[\unexpanded{#1}]}%
  \else%
    \write\linenum@out{\string\@insidethisline[\unexpanded{#1}]}%
  \fi%
}%
\newcommand{\@insidethisline}[1]{%
  \ifledRcol%
    \csgappto{insidethisR@\the\absline@numR @\the\section@numR}{#1}%
  \else%
    \csgappto{insidethis@\the\absline@num @\the\section@num}{#1}%
  \fi%
}%
\newcommand*{\l@demptyd@ta}{%
  \gdef\l@dld@ta{}%
  \gdef\l@drd@ta{}%
  \gdef\l@dcsnotetext@l{}%
  \gdef\l@dcsnotetext@r{}%
  \gdef\l@dcsnotetext{}}

\newcommand{\l@dlsn@te}{%
  \ifboolexpr{%
      bool {l@dprintingcolumns}%
      and bool {ledRcol@}%
  }{%  If we are on a right column
    \@tempdima=\@morespace@leftnote@rightcolumn%
  }{%
    \@tempdima=\z@%
  }%
  \hb@xt@ \z@{\hss\box\l@dlp@rbox\kern\ledlsnotesep\hskip\@tempdima}%
}%
\newcommand{\l@drsn@te}{%
  \ifboolexpr{%
      bool {l@dprintingcolumns}%
      and not bool {ledRcol@}%
  }{%  If we are on a left column
    \@tempdima=\@morespace@rightnote@leftcolumn%
  }{%
    \@tempdima=\z@%
  }%
  \hb@xt@ \z@{\hskip\@tempdima\kern\ledrsnotesep\box\l@drp@rbox\hss}%
}%

\newcommand*{\ledllfill}{\hfil}
\newcommand*{\ledrlfill}{}

\newcommand*{\getline@num}{%
  \global\advance\absline@num \@ne%
  \do@actions
  \do@ballast
  \ifnumberline
      \ifsublines@
         \ifnum\sub@lock<\tw@
           \global\advance\subline@num \@ne
         \fi
      \else
        \ifnum\@lock<\tw@
           \global\advance\line@num \@ne
           \global\subline@num \z@
        \fi
      \fi
  \fi
}
\newcount\ballast@count
\newcounter{ballast}
  \setcounter{ballast}{0}
\newcommand*{\do@ballast}{\global\ballast@count \z@
  \begingroup
    \advance\absline@num \@ne
    \ifnum\next@actionline=\absline@num
      \ifnum\next@action>-1001\relax
        \global\advance\ballast@count by -\c@ballast
       \fi
     \fi
  \endgroup}
\newcommand*{\do@actions}{%
  \global\let\do@actions@next=\relax
  \ifnum\absline@num<\next@actionline\else
    \ifnum\next@action>-1001
       \global\page@num=\next@action
       \ifresumenumbering@start%
         \setbox0=\hbox{}%Required to get the correct page number, when the \resumenumbering is just after a \newpage
         \ifnum\pausenumbering@page@num<\page@num%
           \global\resumenumbering@startfalse%
         \fi%
       \fi%
       \ifboolexpr{%
         bool{resumenumbering@start}%
          and test {\ifdimgreater{\pagedepth}{\z@}}%
       }%
         {}%
         {\global\@firstlineofpagetrue}%
       \ifcsdef{reset@line\the\absline@num @\the\section@num}%
         {%
         \global\line@num=\z@%
         \global\subline@num=\z@%
         \resetprevline@%
         }%
         {}%
       \global\resumenumbering@startfalse%
       \add@msdata@firstlineofpage%
    \else
       \ifnum\next@action<-4999
          \@l@dtempcnta=-\next@action
          \advance\@l@dtempcnta by -5001
          \ifsublines@
             \global\subline@num=\@l@dtempcnta
          \else
             \global\line@num=\@l@dtempcnta
          \fi
       \else
          \@l@dtempcnta=-\next@action
          \advance\@l@dtempcnta by -1000
          \do@actions@fixedcode
       \fi
    \fi
    \ifx\actionlines@list\empty
         \gdef\next@actionline{1000000}%
    \else
         \gl@p\actionlines@list\to\next@actionline
         \gl@p\actions@list\to\next@action
         \global\let\do@actions@next=\do@actions
    \fi
  \fi
\do@actions@next}

\newcommand*{\do@actions@fixedcode}{%
  \ifcase\@l@dtempcnta
  \or%                     % 1001 = starting sublineation
    \global\sublines@true
  \or%                     % 1002 = ending sublineation
    \global\sublines@false
  \or%                     % 1003 = starting locking number
      \global\@lock=\@ne
  \or%                     % 1004 = ending locking number
    \ifnum\@lock=\tw@
      \global\@lock=\thr@@
    \else
      \global\@lock=\z@
    \fi
  \or%                     % 1005 = starting locking subnumber
     \global\sub@lock=\@ne
  \or%                     % 1006 = ending locking subnumber
    \ifnum\sub@lock=\tw@
      \global\sub@lock=\thr@@
    \else
      \global\sub@lock=\z@
    \fi
  \or%                     % 1007 = skipping numbering
    \l@dskipnumbertrue
  \or%                     % 1008 = skipping numbering in stanza
    \l@dskipversenumbertrue%
  \or%                    % 1009 = hiding number
    \l@dhidenumbertrue
  \or%                    % 1010 = inserting msdata
    \add@msdata%
  \else
    \led@warn@BadAction
  \fi}

\newcommand{\continuousnumberingwithcolumns@sync@linenumber@singletext}{%
  \ifcontinuousnumberingwithcolumns%
    \unless\ifafterendnumberingR%
      \new@lineR%
      \xappto\next@line@list@stuffR{%
        \unexpanded{\global\line@numR=}\the\line@num%
      }%
    \fi%
  \fi%
}%
\newcommand{\linenumannotation}[1]{%
  \leavevmode%In case it begins with a \pstart, ensure the \@annot is written after \@nl
  \ifledRcol%
    \write\linenum@outR{\string\@annot[#1]}%
  \else%
    \write\linenum@out{\string\@annot[#1]}%
  \fi%
}%

\newcommand{\resetlinenumannotation}[0]{%
  \leavevmode%In case it begins with a \pstart, ensure the \@resetannot is written after \@nl
  \ifledRcol%
    \write\linenum@outR{\string\@resetannot}%
  \else%
    \write\linenum@out{\string\@resetannot}%
  \fi%
}%

\def\Xlinenumannotationposition@side{after}%
\def\Xwraplinenumannotation@ref{\textsuperscript}%
\def\Xwraplinenumannotation@side{\textsuperscript}%
\newtoggle{Xnoidenticallinenumannotation@ref}%
\newtoggle{Xnoidenticallinenumannotation@side}%
\newif\iflinenumannotationotherside%
\newcommand{\@annot}[1]{%
  \store@annot@to@absline{#1}%
  \def\current@annot{#1}%
}%
\newcommand{\store@annot@to@absline}[1]{%
  \ifledRcol%
    \ifcsdef{annotR@\the\absline@numR @\the\section@numR}{%
      \csgappto{annotR@\the\absline@numR @\the\section@numR}{\@linenumannotationsep#1}%
    }{%
      \csgdef{annotR@\the\absline@numR @\the\section@numR}{#1}%
    }%
  \else%
    \ifcsdef{annot@\the\absline@num @\the\section@num}{%
      \csgappto{annot@\the\absline@num @\the\section@num}{\@linenumannotationsep#1}%
    }{%
      \csgdef{annot@\the\absline@num @\the\section@num}{#1}%
    }%
  \fi%
}%
\let\current@annot=\empty%
\newcommand{\reset@current@annot}{%
  \unless\ifnoresetlinenumannotation@
    \let\current@annot\empty%
  \fi
}%
\newcommand{\@resetannot}{%
  \let\current@annot\empty%
}%
\def\parse@annot#1|#2|{%
  \gdef\annot@start{#1}%
  \gdef\annot@end{#2}%
}%
\newcommand{\setlinenumannotationsep}[1]{\gdef\@linenumannotationsep{#1}}%
\def\@linenumannotationsep{, }%
\newcommand*{\affixline@num}{%
  \ifledgroupnotesL@\else
    \ifnumberline
      \ifl@dskipnumber
        \global\l@dskipnumberfalse
      \else
        \ifsublines@
           \@l@dtempcntb=\subline@num
           \ifnum\subline@num>\c@firstsublinenum
               \@l@dtempcnta=\subline@num
               \advance\@l@dtempcnta by-\c@firstsublinenum
               \divide\@l@dtempcnta by\c@sublinenumincrement
               \multiply\@l@dtempcnta by\c@sublinenumincrement
               \advance\@l@dtempcnta by\c@firstsublinenum
           \else
               \@l@dtempcnta=\c@firstsublinenum
           \fi
           \ch@cksub@l@ck
        \else
           \@l@dtempcntb=\line@num
           \ifx\linenumberlist\empty
              \ifnum\line@num>\c@firstlinenum
                 \@l@dtempcnta=\line@num
                 \advance\@l@dtempcnta by-\c@firstlinenum
                 \divide\@l@dtempcnta by\c@linenumincrement
                 \multiply\@l@dtempcnta by\c@linenumincrement
                 \advance\@l@dtempcnta by\c@firstlinenum
              \else
                 \@l@dtempcnta=\c@firstlinenum
              \fi
           \else
              \@l@dtempcnta=\line@num
              \edef\rem@inder{,\linenumberlist,\number\line@num,}%
              \edef\sc@n@list{\def\noexpand\sc@n@list
                ####1,\number\@l@dtempcnta,####2|{\def\noexpand\rem@inder{####2}}}%
              \sc@n@list\expandafter\sc@n@list\rem@inder|%
              \ifx\rem@inder\empty%
                \advance\@l@dtempcnta\@ne
              \fi
           \fi
           \ch@ck@l@ck
        \fi
        \ifnum\@l@dtempcnta=\@l@dtempcntb
           \ifl@dskipversenumber\else
             \if@twocolumn
                \if@firstcolumn
                   \gdef\l@dld@ta{\llap{{\leftlinenum}}}%
                   \iflinenumannotationotherside%
                     \gdef\l@drd@ta{\rlap{{\rightlinenumannotation}}}%
                   \fi%
                \else
                   \gdef\l@drd@ta{\rlap{{\rightlinenum}}}%
                   \iflinenumannotationotherside%
                     \gdef\l@dld@ta{\llap{{\leftlinenumannotation}}}%
                   \fi%
                \fi
             \else
                \ifboolexpr{bool {l@dprintingcolumns} and test {\ifnumgreater{\line@margin@columns}{\m@ne}}}%
                  {\@l@dtempcntb=\line@margin@columns}%
                  {\@l@dtempcntb=\line@margin}%
                \ifnum\@l@dtempcntb>\@ne
                   \advance\@l@dtempcntb \page@num
                \fi
                \ifboolexpr{%
                  bool {l@dprintingcolumns}%
                  and (%
                   (test {\ifdefstring{\linenum@OnlyPages@ForColumns}{left}}%
                   and test {\ifnumodd{\page@num}}%
                   )%
                   or%
                   (test {\ifdefstring{\linenum@OnlyPages@ForColumns}{right}}%
                   and not test {\ifnumodd{\page@num}}%
                   )%
                 )%
                }%
                {}%
                {%
                \ifodd\@l@dtempcntb%
                   \gdef\l@drd@ta{\rlap{{\rightlinenum}}}%
                     \iflinenumannotationotherside%
                       \gdef\l@dld@ta{\llap{{\leftlinenumannotation}}}%
                     \fi%
                 \else%
                   \gdef\l@dld@ta{\llap{{\leftlinenum}}}%
                     \iflinenumannotationotherside%
                       \gdef\l@drd@ta{\rlap{{\rightlinenumannotation}}}%
                     \fi%
                \fi%
            }%
             \fi
          \fi
       \fi
       \f@x@l@cks
      \fi
    \fi
  \fi
}

\newcommand*{\ch@cksub@l@ck}{%
    \ifcase\sub@lock
      \or
        \ifnum\sublock@disp=\@ne
           \@l@dtempcntb=\z@ \@l@dtempcnta=\@ne
        \fi
      \or
        \ifnum\sublock@disp=\tw@ \else
           \@l@dtempcntb=\z@ \@l@dtempcnta=\@ne
        \fi
      \or
        \ifnum\sublock@disp=\z@
           \@l@dtempcntb=\z@ \@l@dtempcnta=\@ne
        \fi
    \fi}
\newcommand*{\ch@ck@l@ck}{%
    \ifcase\@lock
       \or
         \ifnum\lock@disp=\@ne
            \@l@dtempcntb=\z@ \@l@dtempcnta=\@ne
         \fi
       \or
         \ifnum\lock@disp=\tw@ \else
            \@l@dtempcntb=\z@ \@l@dtempcnta=\@ne
         \fi
       \or
         \ifnum\lock@disp=\z@
            \@l@dtempcntb=\z@ \@l@dtempcnta=\@ne
         \fi
    \fi}
\newcommand*{\f@x@l@cks}{%
  \ifcase\@lock
  \or
    \global\@lock=\tw@
  \or \or
    \global\@lock=\z@
  \fi
  \ifcase\sub@lock
  \or
    \global\sub@lock=\tw@
  \or \or
    \global\sub@lock=\z@
  \fi}


\newif\ifsidepstartnum
\newcommand*{\affixpstart@num}{%
    \ifsidepstartnum
        \if@twocolumn
            \if@firstcolumn
                  \gdef\l@dld@ta{\llap{{\leftpstartnum}}}%
            \else
                  \gdef\l@drd@ta{\rlap{{\rightpstartnum}}}%
            \fi
        \else
            \@l@dtempcntb=\line@margin%
            \ifnum\@l@dtempcntb>\@ne
                  \advance\@l@dtempcntb \page@num
            \fi
            \ifodd\@l@dtempcntb
                  \gdef\l@drd@ta{\rlap{{\rightpstartnum}}}%
            \else
                  \gdef\l@dld@ta{\llap{{\leftpstartnum}}}%
            \fi
        \fi
    \fi

}

\newif\ifpstartnum
\pstartnumtrue
\newcommand*{\leftpstartnum}{
    \ifpstartnum\thepstart
    \kern\linenumsep\fi
    \global\pstartnumfalse
}
\newcommand*{\rightpstartnum}{
    \ifpstartnum
    \kern\linenumsep
    \thepstart
    \fi
    \global\pstartnumfalse
}
\list@create{\inserts@list}
\newcommand*{\add@inserts}{%
  \global\let\add@inserts@next=\relax
  \ifx\inserts@list\empty \else
  \ifx\next@insert\empty
    \ifx\insertlines@list\empty
      \global\noteschanged@true
      \gdef\next@insert{100000}%
    \else
      \gl@p\insertlines@list\to\next@insert
    \fi
  \fi
  \ifnum\next@insert=\absline@num
    \gl@p\inserts@list\to\@insert
    \@insert
    \global\let\@insert=\undefined
    \global\let\next@insert=\empty
    \global\let\add@inserts@next=\add@inserts
  \fi
\fi
\add@inserts@next}

\newcommand{\add@Xgroupbyline}{%
  \unless\ifnocritical@%
    \def\do##1{%Looping on the series
      \let\olddo\do%Save the old \do macro, that is this macro itself!
      \def\do####1{%Looping on the ##1@forinserting command
         \ifcsdef{##1@forinserting@####1}{%
           \X@beforeinsertion{##1}%
           \if@ledgroup%
             \global\setbox\@nameuse{mp##1footins}=\vbox%
           \else%
             \insert\csname ##1footins\endcsname%
           \fi%
           {%
             \Xsetparindent{##1}%
             \ifcsdef{Xhsize\csuse{series@display##1}@##1}%
               {\hsize \csuse{Xhsize\csuse{series@display##1}@##1}}%
               {}%
             \if@ledgroup%
               \unvbox\@nameuse{mp##1footins}%
             \fi%
             \X@atbegininsertion{##1}%
             \ifcsstring{series@display##1}{normal}%
               {%
                  \Xledsetnormalparstuff{##1}%
                  \rule\z@\splittopskip%
               }%
               {}%
             \csuse{##1@forinserting@####1}%
             \strut\par%
           }%
           \global\csundef{##1@forinserting@####1}%
           }%
           {}%
         }%
      \ifcsdef{##1@forinserting}{%
        \dolistcsloop{##1@forinserting}%
      }{}%
      \global\csundef{##1@forinserting}%
      \let\do\olddo%Restore old do
      }%
    \dolistloop{\@series}%
  \fi%
}%

\newcommand*{\add@penalties}{\@l@dtempcnta=\ballast@count
  \ifnum\num@lines>\@ne
    \global\advance\par@line \@ne
    \ifnum\par@line=\@ne
      \advance\@l@dtempcnta \clubpenalty
    \fi
    \@l@dtempcntb=\par@line \advance\@l@dtempcntb \@ne
    \ifnum\@l@dtempcntb=\num@lines
      \advance\@l@dtempcnta \widowpenalty
    \fi
    \ifnum\par@line<\num@lines
      \advance\@l@dtempcnta \interlinepenalty
    \fi
  \fi
    \ifnum\@l@dtempcnta=\z@
      \relax
    \else
      \ifnum\@l@dtempcnta>-10000
        \penalty\@l@dtempcnta
      \else
        \penalty -10000
      \fi
    \fi}

\newcommand*{\flush@notes}{%
  \iftoggle{notfirstrun@\jobname.\extensionchars\the\section@num}{%
    \@xloop%
      \ifx\inserts@list\empty \else%
        \gl@p\inserts@list\to\@insert%
        \@insert%
        \global\let\@insert=\undefined%
    \repeat%
  }{}%
}%

\def\@xloop#1\repeat{%
  \def\body{#1\expandafter\body\fi}%
  \body}

\newcommand{\set@Xtxtbeforenotes}{%
  \unless\ifnocritical@%
    \def\do##1{%
      \nottoggle{Xtxtbeforenotesonlyonce@##1}{%
        \global\togglefalse{Xtxtbeforesnotes@##1@typeset}%
      }{}%
    }%
    \dolistloop{\@series}%
  \fi%
}%
\newcommand{\set@txtbeforenotesX}{%
  \unless\ifnofamiliar@%
    \def\do##1{%
      \nottoggle{txtbeforenotesonlyonceX@##1}{%
        \global\togglefalse{txtbeforesnotesX@##1@typeset}%
      }{}%
    }%
    \dolistloop{\@series}%
  \fi%
}%
\newcommand{\insert@Xtxtbeforenotes}[1]{%
  \nottoggle{Xtxtbeforesnotes@#1@typeset}{%
    \global\toggletrue{Xtxtbeforesnotes@#1@typeset}%
    \ifcsvoid{Xtxtbeforenotes@#1}{}{%
      \ifcsstring{series@display#1}{paragraph}%
        {\noindent\csuse{Xtxtbeforenotes@#1}}%
        {\expandafter\insert\csname#1footins\endcsname%
          \bgroup%
            \noindent%
            \ifcsdef{\csuse{series@display#1}@begin@insert}{%
              \csuse{\csuse{series@display#1}@begin@insert}{#1}%
            }{}%
            \strut\csuse{Xnotefontsize@#1}%
            \csuse{Xtxtbeforenotes@#1}%
          \egroup%
        }%
    }%
  }%
  {}%
}%

\newcommand{\insert@txtbeforenotesX}[1]{%
  \nottoggle{txtbeforesnotesX@#1@typeset}{%
    \global\toggletrue{txtbeforesnotesX@#1@typeset}%
    \ifcsvoid{txtbeforenotesX@#1}{}{%
      \ifcsstring{series@displayX#1}{paragraph}%
        {\noindent\csuse{txtbeforenotesX@#1}}%
        {\expandafter\insert\csname footins#1\endcsname%
          \bgroup%
            \noindent%
            \ifcsdef{\csuse{series@displayX#1}@begin@insert}{%
              \csuse{\csuse{series@displayX#1}@begin@insert}{#1}%
            }{}%
            \strut\csuse{notefontsizeX@#1}\csuse{txtbeforenotesX@#1}%
          \egroup%
        }%
    }%
  }%
  {}%
}%

  \def\select@lemmafont#1|#2|#3|#4|#5|#6|#7|{\select@@lemmafont#7|}
  \def\select@@lemmafont#1/#2/#3/#4|%
    {\fontencoding{#1}\fontfamily{#2}\fontseries{#3}\fontshape{#4}%
    \selectfont}

\newcommand*{\footnoteoptions@}[3]{%
   \def\do##1{%
      \ifstrequal{#1}{L}{% On the left side
          \xright@appenditem{\noexpand\setkeys[mac]{#3footnoteoption}{\unexpanded{##1}}}\to\inserts@list%
         \global\advance\insert@count \@ne% Increment the left insert counter.
      }%
      {%
          \xright@appenditem{\noexpand\setkeys[mac]{#3footnoteoption}{\unexpanded{##1}}}\to\inserts@listR%
          \global\advance\insert@countR \@ne% Increment the right insert counter insert.
      }%
   }%
   \notblank{#2}{\docsvlist{#2}}{}% Parsing all options
}
\newcommandx*{\footnotelang@lua}[1][1=L,usedefault]{%
   \ifstrequal{#1}{L}{%
   \xright@appenditem{{\csxdef{footnote@luatextextdir}{\the\textdir}}}\to\inserts@list%Know the dir of lemma
    \global\advance\insert@count \@ne%
    \xright@appenditem{{\csxdef{footnote@luatexpardir}{\the\pardir}}}\to\inserts@list%Know the dir of lemma
    \global\advance\insert@count \@ne%
   }%
   {%
   \xright@appenditem{{\csxdef{footnote@luatextextdir}{\the\textdir}}}\to\inserts@listR%Know the dir of lemma
    \global\advance\insert@countR \@ne%
    \xright@appenditem{{\csxdef{footnote@luatexpardir}{\the\pardir}}}\to\inserts@listR%Know the dir of lemma
    \global\advance\insert@countR \@ne%
   }%
}
\newcommandx*{\footnotelang@poly}[1][1=L,usedefault]{%
   \ifstrequal{#1}{L}{%
    \if@RTL%
        \xright@appenditem{{\csxdef{footnote@dir}{@RTLtrue}}}\to\inserts@list%Know the language used in the lemma
        \global\advance\insert@count \@ne%
    \else
         \xright@appenditem{{\csxdef{footnote@dir}{@RTLfalse}}}\to\inserts@list%Know the language of lemma
         \global\advance\insert@count \@ne%
    \fi%
    \xright@appenditem{{\csxdef{footnote@lang}{\expandonce\languagename}}}\to\inserts@list%Know the language of lemma
    \global\advance\insert@count \@ne%
   }%
   {%
    \if@RTL
        \xright@appenditem{{\csxdef{footnote@dir}{@RTLtrue}}}\to\inserts@listR%Know the language of lemma
        \global\advance\insert@countR \@ne%
    \else
         \xright@appenditem{{\csxdef{footnote@dir}{@RTLfalse}}}\to\inserts@listR%Know the language of lemma
         \global\advance\insert@countR \@ne%
    \fi
    \xright@appenditem{{\csxdef{footnote@lang}{\expandonce\languagename}}}\to\inserts@listR%Know the language of lemma
    \global\advance\insert@countR \@ne%
   }%
}
\newcommand*{\footsplitskips}{%
  \interlinepenalty=\interfootnotelinepenalty
  \unless\ifl@dprintingpages%
    \floatingpenalty=\@MM%
  \fi%
  \splittopskip=\ht\strutbox \splitmaxdepth=\dp\strutbox
  \leftskip=\z@skip \rightskip=\z@skip}

\let\normalfootnoterule=\footnoterule
\newcommandx{\Xarrangement}[2][1,usedefault]{%
  \def\do##1{%
     \csname Xarrangement@#2\endcsname{##1}%
  }%
  \ifstrempty{#1}%
    {%
    \dolistloop{\@series}%
    }%
    {
    \docsvlist{#1}%
    }%
}%

\newcommand*{\Xarrangement@normal}[1]{%
  \csgdef{series@display#1}{normal}
  \expandafter\let\csname #1footstart\endcsname=\normalfootstart
  \expandafter\let\csname v#1footnote\endcsname=\normalvfootnote
  \expandafter\let\csname #1footfmt\endcsname=\normalfootfmt
  \expandafter\let\csname #1footgroup\endcsname=\normalfootgroup
  \expandafter\let\csname #1footnoterule\endcsname=%
                                             \normalfootnoterule
  \count\csname #1footins\endcsname=1000
  \dimen\csname #1footins\endcsname=\csuse{Xmaxhnotes@#1}
  \skip\csname #1footins\endcsname=\csuse{Xbeforenotes@#1}%
  \advance\skip\csname #1footins\endcsname by\csuse{Xafterrule@#1}%

  \csxdef{default@#1footins}{1000}%Use this to confine the  notes to one side only
  \ifnoledgroup@\else%
    \expandafter\let\csname mpv#1footnote\endcsname=\mpnormalvfootnote
    \expandafter\let\csname mp#1footgroup\endcsname=\mpnormalfootgroup
    \count\csname mp#1footins\endcsname=1000
    \dimen\csname mp#1footins\endcsname=\csuse{Xmaxhnotes@#1}
    \skip\csname mp#1footins\endcsname=\csuse{Xbeforenotes@#1}%
    \advance\skip\csname mp#1footins\endcsname by\csuse{Xafterrule@#1}%
  \fi
}

\notbool{parapparatus@}{\newcommand*}{\newcommand}{\normalvfootnote}[2]{%
  \iftoggle{Xgroupbyline@#1}{%In the case we use \Xgroupbyline, the insertion is done later, in \add@Xgroupbyline.
    \prepare@Xgroupbyline{#1}{#2}{\normalvfootnote@inserted}%
  }{%In the case we don't use \Xgroupbyline, the insertion is made directly
    \X@beforeinsertion{#1}%
    \insert\csname #1footins\endcsname{%
      \X@atbegininsertion{#1}%
      \normalvfootnote@inserted{#1}{#2}%
    }%
  }%
}%
\notbool{parapparatus@}{\newcommand*}{\newcommand}{\normalvfootnote@inserted}[2]{%
  \nottoggle{Xgroupbyline@#1}{\noindent}{}\csuse{Xbhooknote@#1}%
  \csuse{Xnotefontsize@#1}%
  \iftoggle{Xgroupbyline@#1}{\strut}{}%
  \footsplitskips
  \ifl@dpairing\ifl@dpaging\else%
    \setXnoteswidthliketwocolumns@{#1}%
  \fi\fi%
  \setXnotespositionliketwocolumns@{#1}%
  \spaceskip=\z@skip \xspaceskip=\z@skip%
  \csname #1footfmt\endcsname #2{#1}%
}%
\newcommand{\X@beforeinsertion}[1]{%
  \if@ledgroup\else%
    \insert@Xtxtbeforenotes{#1}%
  \fi%
  \csuse{Xbeforeinserting@#1}%
}%
\newcommand{\beforeinsertion@X}[1]{%
  \if@ledgroup\else%
    \insert@txtbeforenotesX{#1}%
  \fi%
  \csuse{beforeinsertingX@#1}%
}%
\newcommand{\X@atbegininsertion}[1]{%
  \hsize=\expandafter\dimexpr\csuse{Xwidth@#1}\relax%
}%
\notbool{parapparatus@}{\newcommand*}{\newcommand}{\mpnormalvfootnote}[2]{%
  \iftoggle{Xgroupbyline@#1}{%
    \prepare@Xgroupbyline{#1}{#2}{\mpnormalvfootnote@inserted}%
  }%
  {%
      \global\setbox\@nameuse{mp#1footins}%
      \vbox{%
        \unvbox\@nameuse{mp#1footins}%
        \mpnormalvfootnote@inserted{#1}{#2}%
    }%
  }%
}%

\newcommand{\mpnormalvfootnote@inserted}[2]{%
    \noindent\csuse{Xbhooknote@#1}%
    \csuse{Xnotefontsize@#1}%
    \hsize\columnwidth%
    \@parboxrestore%
    \color@begingroup%
    \csname #1footfmt\endcsname #2{#1}\color@endgroup%
}%
\notbool{parapparatus@}{\newcommand*}{\newcommand}{\normalfootfmt}[4]{%
  \Xstorelineinfo{#1}{#4}%
  \nottoggle{Xgroupbyline@#4}{\Xledsetnormalparstuff{#4}}{}%
  \Xsethangindent{#4}%
  \nottoggle{Xgroupbyline@#4}{\rule\z@\splittopskip}{}%
  {\printlinefootnote{#1}{#4}}%
  \print@lemma{#1}{#2}{#4}%
  \csuse{Xwrapcontent@#4}{#3}%
  \nottoggle{Xgroupbyline@#4}{\strut\par}{}%
}%
\newcommand*{\normalfootstart}[1]{%
  \ifdimequal{0pt}{\Xprenotes@}{}%
      {%
      \iftoggle{Xprenotes@}{%
            \togglefalse{Xprenotes@}%
            \skip\csname #1footins\endcsname=%
              \glueexpr\csuse{Xprenotes@}+\csuse{Xafterrule@#1}\relax%
            }%
          {}%
      }%
  \vskip\skip\csname #1footins\endcsname%
  \leftskip0pt \rightskip0pt
  \ifl@dpairing\else%
     \hsize=\old@hsize%
  \fi%
  \setXnoteswidthliketwocolumns@{#1}%
  \setXnotespositionliketwocolumns@{#1}%
  \print@Xfootnoterule{#1}%
}%
\newcommand*{\normalfootgroup}[1]{%
  \csuse{Xbhookgroup@#1}%
  \unvbox\csname #1footins\endcsname%
  \hsize=\old@hsize%
  }%

\unless\ifnoledgroup@
\newcommand*{\mpnormalfootgroup}[1]{{
  \vskip\skip\@nameuse{mp#1footins}
  \ifl@dpairing\ifparledgroup%
    \leavevmode\marks\parledgroup@{begin}%
    \marks\parledgroup@series{#1}%
    \marks\parledgroup@type{Xfootnote}%
  \fi\fi\normalcolor%
  \ifparledgroup%
    \ifl@dpairing%
    \else%
      \setXnoteswidthliketwocolumns@{#1}%
      \setXnotespositionliketwocolumns@{#1}%
      \print@Xfootnoterule{#1}%%
    \fi%
  \else%
    \setXnoteswidthliketwocolumns@{#1}%
    \setXnotespositionliketwocolumns@{#1}%
    \print@Xfootnoterule{#1}%%
  \fi%
  \setlength{\parindent}{0pt}
 \csuse{Xbhookgroup@#1}%
 \unvbox\csname mp#1footins\endcsname}}
\fi
\newcommand*{\Xarrangement@paragraph}[1]{%
  \csgdef{series@display#1}{paragraph}
  \expandafter\let\csname #1footstart\endcsname=\parafootstart
  \expandafter\let\csname v#1footnote\endcsname=\paravfootnote
  \expandafter\let\csname #1footfmt\endcsname=\parafootfmt
  \expandafter\let\csname #1footgroup\endcsname=\parafootgroup
  \count\csname #1footins\endcsname=1000
  \csxdef{default@#1footins}{1000}%Use this to confine the  notes to one side only
  \dimen\csname #1footins\endcsname=\csuse{Xmaxhnotes@#1}
  \skip\csname #1footins\endcsname=\csuse{Xbeforenotes@#1}%
  \advance\skip\csname #1footins\endcsname by\csuse{Xafterrule@#1}%
  \para@footsetup{#1}
  \ifnoledgroup@\else
    \expandafter\let\csname mpv#1footnote\endcsname=\mpparavfootnote
    \expandafter\let\csname mp#1footgroup\endcsname=\mpparafootgroup
    \count\csname mp#1footins\endcsname=1000
    \dimen\csname mp#1footins\endcsname=\csuse{Xmaxhnotes@#1}
    \skip\csname mp#1footins\endcsname=\csuse{Xbeforenotes@#1}%
    \advance\skip\csname mp#1footins\endcsname by\csuse{Xafterrule@#1}%
  \fi
}
\providecommand{\footfudgefiddle}{64}
\newcommand*{\para@footsetup}[1]{{\csuse{Xbhookgroup@#1}\csuse{Xnotefontsize@#1}
  \setXnoteswidthliketwocolumns@{#1}%
  \ifcsempty{Xwidth@#1}%
    {}%
    {\columnwidth=\expandafter\dimexpr\csuse{Xwidth@#1}\relax}%
  \dimen0=\baselineskip
  \multiply\dimen0 by 1024
  \divide \dimen0 by \columnwidth \multiply\dimen0 by \footfudgefiddle\relax
  \csxdef{#1footfudgefactor}{%
    \expandafter\strip@pt\dimen0 }}}

\newcommand*{\parafootstart}[1]{%
  \rightskip=0pt \leftskip=0pt%
  \Xsetparindent{#1}%
    \ifdimequal{0pt}{\Xprenotes@}{}%
      {%
      \iftoggle{Xprenotes@}{%
            \togglefalse{Xprenotes@}%
            \skip\csname #1footins\endcsname=%
              \glueexpr\csuse{Xprenotes@}+\csuse{Xafterrule@#1}\relax%
            }%
          {}%
      }%
  \vskip\skip\csname #1footins\endcsname%
  \setXnoteswidthliketwocolumns@{#1}%
  \setXnotespositionliketwocolumns@{#1}%
  \print@Xfootnoterule{#1}%
  \let\old@bidi@RTL@everypar\bidi@RTL@everypar%
  \let\bidi@RTL@everypar\@empty%
  \noindent\leavevmode%
  \let\bidi@RTL@everypar\old@bidi@RTL@everypar%
}%
\newcommand*{\paravfootnote}[2]{%
  \csuse{Xbeforeinserting@#1}%
  \insert\csname #1footins\endcsname
  \bgroup
    \csuse{Xnotefontsize@#1}
    \footsplitskips
    \setbox0=\vbox{\hsize=\maxdimen%
      \let\bidi@RTL@everypar\@empty%
      \insert@Xtxtbeforenotes{#1}%
      \noindent\csuse{Xbhooknote@#1}%
      \csname #1footfmt\endcsname #2{#1}}%
    \setbox0=\hbox{\Xunvxh{0}{#1}}%
    \dp0=0pt
    \ht0=\csname #1footfudgefactor\endcsname\wd0
    \if@RTL\noindent \leavevmode\fi\box0%
    \penalty0
  \egroup}

\newcommand*{\mpparavfootnote}[2]{%
  \global\setbox\@nameuse{mp#1footins}\vbox{%
    \unvbox\@nameuse{mp#1footins}%
    \csuse{Xnotefontsize@#1}
    \footsplitskips
    \setbox0=\vbox{\hsize=\maxdimen%
      \let\bidi@RTL@everypar\@empty%
      \insert@Xtxtbeforenotes{#1}%
      \noindent\color@begingroup%
      \csuse{Xbhooknote@#1}%
      \csname #1footfmt\endcsname #2{#1}\color@endgroup}%
    \setbox0=\hbox{\Xunvxh{0}{#1}}%
    \dp0=\z@
    \ht0=\csname #1footfudgefactor\endcsname\wd0
    \box0
    \penalty0
}}

\newcommand*{\Xunvxh}[2]{%
  \setbox0=\vbox{\unvbox#1%
    \global\setbox1=\lastbox}%
  \unhbox1
  \unskip            % remove \rightskip,
  \unskip            % remove \parfillskip,
  \unpenalty         % remove \penalty of 10000,
  \hskip\csuse{Xafternote@#2}\relax}% add the glue to go between the notes

\newcommand*{\parafootfmt}[4]{%
  \Xstorelineinfo{#1}{#4}%
  \Xinsertparafootsep{#4}%
  \ledsetnormalparstuff@common%
  \printlinefootnote{#1}{#4}%
  \print@lemma{#1}{#2}{#4}%
  \csuse{Xwrapcontent@#4}{#3}%
  \penalty-10 }
\newcommand*{\parafootgroup}[1]{%
  \hsize=\expandafter\dimexpr\csuse{Xwidth@#1}\relax%
  \unvbox\csname #1footins\endcsname
  \ifcsstring{Xragged@#1}{L}{\RaggedLeft}{}%
  \ifcsstring{Xragged@#1}{R}{\RaggedRight}{}%
  \makehboxofhboxes
  \setbox0=\hbox{\unhbox0 \removehboxes}%
  \csuse{Xbhookgroup@#1}%
  \csuse{Xnotefontsize@#1}%
  \unhbox0\par%
  \global\hsize=\old@hsize%
  }%

\newcommand*{\mpparafootgroup}[1]{{%
  \setXnoteswidthliketwocolumns@{#1}%
  \vskip\skip\@nameuse{mp#1footins}
  \ifl@dpairing\ifparledgroup%
    \leavevmode\marks\parledgroup@{begin}%
    \marks\parledgroup@series{#1}%
    \marks\parledgroup@type{Xfootnote}%
  \fi\fi\normalcolor
  \ifparledgroup%
    \ifl@dpairing%
    \else%
      \setXnoteswidthliketwocolumns@{#1}%
      \setXnotespositionliketwocolumns@{#1}%
      \print@Xfootnoterule{#1}%%
    \fi%
  \else%
      \setXnoteswidthliketwocolumns@{#1}%
      \setXnotespositionliketwocolumns@{#1}%
      \print@Xfootnoterule{#1}%
  \fi%
  \unvbox\csname mp#1footins\endcsname
  \ifcsstring{Xragged@#1}{L}{\RaggedLeft}{}%
  \ifcsstring{Xragged@#1}{R}{\RaggedRight}{}%
  \makehboxofhboxes
  \setbox0=\hbox{\unhbox0 \removehboxes}%
  \csuse{Xbhookgroup@#1}%
  \csuse{Xnotefontsize@#1}%
  \Xsetparindent{#1}%
  \unhbox0\par}}

\newcommand*{\makehboxofhboxes}{\setbox0=\hbox{}%
  \loop
    \unpenalty
    \setbox2=\lastbox
  \ifhbox2
    \setbox0=\hbox{\box2\unhbox0}%
  \repeat}

\newcommand*{\removehboxes}{\setbox0=\lastbox
  \ifhbox0{\removehboxes}\unhbox0 \fi}

\newcommand{\Xinsertparafootsep}[1]{%
   \ifledRcol@%
     \ifnumequal{\csuse{#1prevpage@numR}}{\page@numR}%
        {\ifcsdef{prevline#1}% Be sur \prevline#1 exists.
          {\ifcsequal{prevline#1}{lineinfo@}%
             {\ifcsempty{Xsymlinenum@#1}{\csuse{Xparafootsep@#1}}{}}%
             {\csuse{Xparafootsep@#1}}%
          }%
          {\csuse{Xparafootsep@#1}}%
     }%
     {}%
     \global\csname #1prevpage@numR\endcsname=\page@numR%
   \else%
     \ifnumequal{\csuse{#1prevpage@num}}{\page@num}%
        {\ifcsdef{prevline#1}% Be sur \prevline#1 exists.
          {\ifcsequal{prevline#1}{lineinfo@}%
             {\ifcsempty{Xsymlinenum@#1}{\csuse{Xparafootsep@#1}}{}}%
             {\csuse{Xparafootsep@#1}}%
          }%
        {\csuse{Xparafootsep@#1}}%
     }%
     {}%
     \global\csname #1prevpage@num\endcsname=\page@num%
   \fi%
}
\newcount\@k \newdimen\@h
\newcommand*{\Xrigidbalance}[3]{%
    \hsize=\expandafter\dimexpr\csuse{Xwidth@\@currentseries}\relax%
    \rigidbalance{#1}{#2}{#3}%
}%

\newcommand*{\rigidbalanceX}[3]{%
    \hsize=\expandafter\dimexpr\csuse{widthX@\@currentseries}\relax%
    \rigidbalance{#1}{#2}{#3}%
}%

\newcommand*{\rigidbalance}[3]{%
  \setbox0=\box#1 \@k=#2 \@h=#3%
  \@@line{\splittopskip=\@h \vbadness=\@M \hfilneg
  \valign{##\vfil\cr\dosplits}}}

\newcommand*{\dosplits}{\ifnum\@k>0 \noalign{\hfil}\splitoff
  \global\advance\@k-1\cr\dosplits\fi}

\newcommand*{\splitoff}{\dimen0=\ht0
  \divide\dimen0 by\@k \advance\dimen0 by\@h
  \setbox2 \vsplit0 to \dimen0
  \unvbox2 }

\newcommand*{\Xarrangement@threecol}[1]{%
  \csgdef{series@display#1}{threecol}
  \expandafter\let\csname v#1footnote\endcsname=\threecolvfootnote
  \expandafter\let\csname #1footfmt\endcsname=\threecolfootfmt
  \expandafter\let\csname #1footgroup\endcsname=\threecolfootgroup
  \dimen\csname #1footins\endcsname=\csuse{Xmaxhnotes@#1}%
  \skip\csname #1footins\endcsname=\csuse{Xbeforenotes@#1}%
  \advance\skip\csname #1footins\endcsname by\csuse{Xafterrule@#1}%
  \threecolfootsetup{#1}
  \ifnoledgroup@\else
    \expandafter\let\csname mpv#1footnote\endcsname=\mpnormalvfootnote
    \expandafter\let\csname mp#1footgroup\endcsname=\mpthreecolfootgroup
    \skip\csname mp#1footins\endcsname=\csuse{Xbeforenotes@#1}%
    \advance\skip\csname mp#1footins\endcsname by\csuse{Xafterrule@#1}%
    \mpthreecolfootsetup{#1}
  \fi
}

\newcommand*{\threecolfootsetup}[1]{%
  \count\csname #1footins\endcsname 333
  \csxdef{default@#1footins}{333}%Use this to confine the  notes to one side only
  \multiply\dimen\csname #1footins\endcsname \thr@@}
\newcommand*{\mpthreecolfootsetup}[1]{%
  \count\csname mp#1footins\endcsname 333
  \multiply\dimen\csname mp#1footins\endcsname \thr@@}

\notbool{parapparatus@}{\newcommand*}{\newcommand}{\threecolvfootnote}[2]{%
  \iftoggle{Xgroupbyline@#1}{%
    \prepare@Xgroupbyline{#1}{#2}{\threecolvfootnote@inserted}%
  }%
  {%
    \let\bidi@RTL@everypar\relax%
    \X@beforeinsertion{#1}%
    \insert\csname #1footins\endcsname{%
      \threecolvfootnote@inserted{#1}{#2}%
    }%
  }%
}%
\notbool{parapparatus@}{\newcommand*}{\newcommand}{\threecolvfootnote@inserted}[2]{%
  \hsize=\expandafter\dimexpr\csuse{Xwidth@#1}\relax%
  \noindent\csuse{Xbhooknote@#1}%
  \csuse{Xnotefontsize@#1}%
  \footsplitskips%
  \csname #1footfmt\endcsname #2{#1}%
}%
\notbool{parapparatus@}{\newcommand*}{\newcommand}{\threecolfootfmt}[4]{%
  \Xstorelineinfo{#1}{#4}%
  \threecol@begin@insert{#4}%
  \hspace{\parindent}%
  \printlinefootnote{#1}{#4}%
  \print@lemma{#1}{#2}{#4}%
  \csuse{Xwrapcontent@#4}{#3}%
  \nottoggle{Xgroupbyline@#4}%
    {\strut\par\allowbreak}%
    {}%
}%
\newcommand{\threecol@begin@insert}[1]{%
  \normal@pars%
  \nottoggle{Xgroupbyline@#1}%
    {\hsize \csuse{Xhsizethreecol@#1}}%
    {}%
  \Xsetparindent{#1}%
  \tolerance=5000%
  \Xsethangindent{#1}%
  \@tempdima=\parindent%
  \csuse{Xcolalign@#1}%
  \parindent=\@tempdima%
  \strut%
}%
\newcommand*{\threecolfootgroup}[1]{%
  \begingroup%
    \csuse{Xbhookgroup@#1}%
    \csuse{Xnotefontsize@#1}%
    \par%
    \splittopskip=\ht\strutbox%
    \expandafter%
    \Xrigidbalance\csname #1footins\endcsname \thr@@ \splittopskip%
  \endgroup%
}%
\newcommand*{\mpthreecolfootgroup}[1]{{%
  \vskip\skip\@nameuse{mp#1footins}
  \ifl@dpairing\ifparledgroup%
    \leavevmode\marks\parledgroup@{begin}%
    \marks\parledgroup@series{#1}%
    \marks\parledgroup@type{Xfootnote}%
  \fi\fi\normalcolor
  \ifparledgroup%
    \ifl@dpairing%
    \else%
      \setXnoteswidthliketwocolumns@{#1}%
      \setXnotespositionliketwocolumns@{#1}%
      \print@Xfootnoterule{#1}%
    \fi%
  \else%
    \setXnoteswidthliketwocolumns@{#1}%
    \setXnotespositionliketwocolumns@{#1}%
    \print@Xfootnoterule{#1}%
  \fi%
  \csuse{Xbhookgroup@#1}\par%
  \splittopskip=\ht\strutbox
  \expandafter
  \Xrigidbalance\csname mp#1footins\endcsname \thr@@ \splittopskip}}

\newcommand*{\Xarrangement@twocol}[1]{%
  \csgdef{series@display#1}{twocol}
  \expandafter\let\csname v#1footnote\endcsname=\twocolvfootnote
  \expandafter\let\csname #1footfmt\endcsname=\twocolfootfmt
  \expandafter\let\csname #1footgroup\endcsname=\twocolfootgroup
  \dimen\csname #1footins\endcsname=\csuse{Xmaxhnotes@#1}%
  \skip\csname #1footins\endcsname=\csuse{Xbeforenotes@#1}%
  \advance\skip\csname #1footins\endcsname by\csuse{Xafterrule@#1}%
  \twocolfootsetup{#1}
  \ifnoledgroup@\else
    \expandafter\let\csname mpv#1footnote\endcsname=\mpnormalvfootnote
    \expandafter\let\csname mp#1footgroup\endcsname=\mptwocolfootgroup
    \skip\csname mp#1footins\endcsname=\csuse{Xbeforenotes@#1}%
    \advance\skip\csname mp#1footins\endcsname by\csuse{Xafterrule@#1}%
    \mptwocolfootsetup{#1}
  \fi
}

\newcommand*{\twocolfootsetup}[1]{%
  \count\csname #1footins\endcsname 500
  \csxdef{default@#1footins}{500}%
  \multiply\dimen\csname #1footins\endcsname \tw@}
\notbool{parapparatus@}{\newcommand*}{\newcommand}{\twocolvfootnote}[2]{%
  \iftoggle{Xgroupbyline@#1}{%
    \prepare@Xgroupbyline{#1}{#2}{\twocolvfootnote@inserted}%
  }{%
    \let\bidi@RTL@everypar\relax%
    \X@beforeinsertion{#1}%
    \insert\csname #1footins\endcsname{%
      \twocolvfootnote@inserted{#1}{#2}%
    }%
  }%
}%
\notbool{parapparatus@}{\newcommand*}{\newcommand}{\twocolvfootnote@inserted}[2]{%
  \hsize=\expandafter\dimexpr\csuse{Xwidth@#1}\relax%
  \noindent\csuse{Xbhooknote@#1}%
  \csuse{Xnotefontsize@#1}%
  \footsplitskips%
  \csname #1footfmt\endcsname #2{#1}%
}%
\notbool{parapparatus@}{\newcommand*}{\newcommand}{\twocolfootfmt}[4]{% 4th arg is optional, for backward compatibility
  \Xstorelineinfo{#1}{#4}%
  \twocol@begin@insert{#4}%
  \hspace{\parindent}%
  \printlinefootnote{#1}{#4}%
  \print@lemma{#1}{#2}{#4}%
  \csuse{Xwrapcontent@#4}{#3}%
  \nottoggle{Xgroupbyline@#4}%
    {\strut\par\allowbreak}%
    {}%
}%
\newcommand{\twocol@begin@insert}[1]{%
  \normal@pars%
  \nottoggle{Xgroupbyline@#1}%
    {\hsize \csuse{Xhsizetwocol@#1}}%
    {}%
  \Xsetparindent{#1}%
  \tolerance=5000%
  \Xsethangindent{#1}%
  \@tempdima=\parindent%
  \csuse{Xcolalign@#1}%
  \parindent=\@tempdima%
  \strut%
}%

\newcommand*{\twocolfootgroup}[1]{%
  \begingroup%
    \csuse{Xbhookgroup@#1}%
    \csuse{Xnotefontsize@#1}%
    \par%
    \splittopskip=\ht\strutbox%
    \expandafter%
    \Xrigidbalance\csname #1footins\endcsname \tw@ \splittopskip%
  \endgroup%
}%

\newcommand*{\mptwocolfootsetup}[1]{%
  \count\csname mp#1footins\endcsname 500
  \multiply\dimen\csname mp#1footins\endcsname \tw@}
\newcommand*{\mptwocolfootgroup}[1]{{%
  \vskip\skip\@nameuse{mp#1footins}
  \ifl@dpairing\ifparledgroup%
    \leavevmode\marks\parledgroup@{begin}%
    \marks\parledgroup@series{#1}%
    \marks\parledgroup@type{Xfootnote}%
  \fi\fi\normalcolor
  \ifparledgroup%
    \ifl@dpairing%
    \else%
      \setXnoteswidthliketwocolumns@{#1}%
      \setXnotespositionliketwocolumns@{#1}%
      \print@Xfootnoterule{#1}%
    \fi%
  \else%
    \setXnoteswidthliketwocolumns@{#1}%
    \setXnotespositionliketwocolumns@{#1}%
    \print@Xfootnoterule{#1}%
  \fi%
   \csuse{Xbhookgroup@#1}\par%
  \splittopskip=\ht\strutbox
  \expandafter
  \Xrigidbalance\csname mp#1footins\endcsname \tw@ \splittopskip}}

\newcommand{\Xsetparindent}[1]{%
  \nottoggle{Xparindent@#1}{%
    \parindent=\z@%
  }%
  {%
    \ifdef{\parindent@beforestanza}{\parindent=\parindent@beforestanza}{}%
  }%
}%
\newcommand{\setparindentX}[1]{%
  \nottoggle{parindentX@#1}{%
    \parindent=\z@%
  }%
  {%
    \ifdef{\parindent@beforestanza}{\parindent=\parindent@beforestanza}{}%
  }%
}%
\newcommand{\Xsethangindent}[1]{%
  \if@RTL%
    \hangindent=-\csuse{Xhangindent@#1}%
    \everypar{\hangindent=-\csuse{Xhangindent@#1}}%
  \else%
    \hangindent=\csuse{Xhangindent@#1}%
    \everypar{\hangindent=\csuse{Xhangindent@#1}}%
  \fi%
}%
\newcommand{\sethangindentX}[1]{%
  \if@RTL%
    \hangindent=-\csuse{hangindentX@#1}%
    \everypar{\hangindent=-\csuse{hangindentX@#1}}%
  \else%
    \hangindent=\csuse{hangindentX@#1}%
    \everypar{\hangindent=\csuse{hangindentX@#1}}%
  \fi%
}%

\def\endashchar{\textnormal{--}}

\newcommand*{\fullstop}{\textnormal{.}}
\def\Xsublinesep@side{\fullstop}

\newcommand*{\rbracket}{\textnormal{%
   \csuse{text\csuse{footnote@lang}}{%
         \ifluatex%
         \ifdefstring{\footnote@luatextextdir}{TRT}{\thinspace[}{\thinspace]}%
            \else%
         \thinspace]%
         \fi}%
   }%
}

\newcommand{\printpstart}[0]{%
    \ifboolexpr{bool{l@dpairing} or bool{l@dprintingpages} or bool{l@dprintingcolumns}}{%
        \ifledRcol%
            \thepstartR%
        \else%
            \thepstartL%
        \fi%
    }{%
        \thepstart%
    }%
}
\newcommand{\print@lemma}[3]{%
  \bgroup%
    \nottoggle{Xlemmadisablefontselection@#3}%
      {\select@lemmafont#1|}%
      {}%
    \bgroup%
      \csuse{Xlemmafont@#3}%Deprecated
      \csuse{Xwraplemma@#3}{#2}%
    \egroup%
  \egroup%
  \iftoggle{nosep@}{%
    \hskip\csuse{Xinplaceoflemmaseparator@#3}%
    \relax%
    }%
    {\ifcsempty{Xlemmaseparator@#3}%
      {%
        \hskip\csuse{Xinplaceoflemmaseparator@#3}%
        \relax%
      }%
      {%
        \nobreak%
        \hskip\csuse{Xbeforelemmaseparator@#3}%
        \csuse{Xlemmaseparator@#3}%
        \hskip\csuse{Xafterlemmaseparator@#3}%
        \relax%
      }%
  }%
}%
\newcommand{\Xstorelineinfo}[2]{%
   \l@dp@rsefootspec#1|%
   \iftoggle{Xnumberonlyfirstintwolines@#2}{%
       \xdef\lineinfo@{\l@dparsedstartline - \l@dparsedstartsub - \l@dparsedendline - \l@dparsedendsub}%
       }%
       {%
       \xdef\lineinfo@{\l@dparsedstartline - \l@dparsedstartsub}%
       }%
}%
\newcommand{\printlinefootnote}[2]{%
   \xdef\@currentseries{#2}%
   \iftoggle{nonum@}{%Try if the line number must printed for this specific not (by default, yes)
   \hspace{\csuse{Xinplaceofnumber@#2}}%
   }%
   {%
       {%
           \iftoggle{Xnonumber@#2}%Try if the line number must printed (by default, yes)
           {%
           \hspace{\csuse{Xinplaceofnumber@#2}}%
           }%
           {%
              {\iftoggle{Xnumberonlyfirstinline@#2}% If for this series the line number must be printed only in the first time.
                 {%
                 \ifcsdef{prevline#2}%
                    {%Be sure the \prevline exists.
                    \ifcsequal{prevline#2}{lineinfo@}%Try it
                       {%
                       \ifcsempty{Xsymlinenum@#2}%
                        {%
                            \hspace{\csuse{Xinplaceofnumber@#2}}%
                        }%
                        {\printsymlinefootnotearea{#2}}%
                       }%
                       {%
                        \printlinefootnotearea{#1}{#2}%
                        }%
                    }%
                    {%
                    \printlinefootnotearea{#1}{#2}%
                    }%
              }%
              {%
              \printlinefootnotearea{#1}{#2}%
              }%
              \csxdef{prevline#2}{\lineinfo@}%
           }%
        }%
     }%
   }%
}
\newcommand{\printsymlinefootnotearea}[1]{%
  \hspace{\csuse{Xbeforesymlinenum@#1}}%
  \csuse{Xnotenumfont@#1}%
  \ifdimequal{\csuse{Xboxsymlinenum@#1}}{\z@}%
     {\csuse{Xsymlinenum@#1}}%
     {\hbox to \csuse{Xboxsymlinenum@#1}%
       {\csuse{Xsymlinenum@#1}\hfill}%
     }%
  \hspace{\csuse{Xaftersymlinenum@#1}}%
}%
\newcommand{\printlinefootnotearea}[2]{%
  \printXbeforenumber{#2}%
  \csuse{Xnotenumfont@#2}%
  \boxfootnotenumbers{#1}{#2}%
  \printXafternumber{#2}%
}%
\newcommand{\boxfootnotenumbers}[2]{%
  \ifdimequal{\csuse{Xboxlinenum@#2}}{0pt}{%
       \printlinefootnotenumbers{#1}{#2}%
     }%
     {%
       \hbox to \csuse{Xboxlinenum@#2}%
         {%
         \IfSubStr{RC}{\csuse{Xboxlinenumalign@#2}}{\hfill}{}%
         \printlinefootnotenumbers{#1}{#2}%
         \IfSubStr{LC}{\csuse{Xboxlinenumalign@#2}}{\hfill}{}%
         }%
     }%
}%
\newcommand{\printlinefootnotenumbers}[2]{%
  \get@pstartcounter%
  \ifboolexpr{%
   (togl{Xpstart@#2} and bool{numberpstart})%
   or togl{Xpstarteverytime@#2}%
   and not (%
      togl{Xpstartonlyfirst@#2}%
      and test {\ifnumequal{\csuse{Xprevpstart@#2}}{\current@pstart}}%
   )%
  }%
  {%
    \printpstart%
    \csuse{Xpstartseparator@#2}%
  }{}%
  \global\cslet{Xprevpstart@#2}{\current@pstart}%
  \get@stanzacounter%
  \ifboolexpr{%
    togl{Xstanza@#2}%
    and bool {numberstanza}%
    and not (%
      togl{Xpstartonlyfirst@#2}%
      and test {\ifnumequal{\csuse{Xprevstanza@#2}}{\current@stanza}}%
    )%
  }{%
    \printstanza%
    \csuse{Xstanzaseparator@#2}%
  }{}%
  \global\cslet{Xprevstanza@#2}{\current@stanza}%
  \iftoggle{Xonlypstart@#2}{}{%
    \csuse{Xtxtbeforenumber@#2}%
    \printlines#1|\ifledRcol@\@Rlineflag\fi|}%
}%
\newcommand{\get@pstartcounter}[0]{%
  \ifboolexpr{bool{l@dprintingpages} or bool{l@dprintingcolumns}}%
  {%
    \ifledRcol%
      \global\numdef\current@pstart{\the\c@pstartR}%
    \else%
      \global\numdef\current@pstart{\the\c@pstartL}%
    \fi%
  }%
  {%
    \global\numdef\current@pstart{\the\c@pstart}%
  }%
}%
\newcommand{\get@stanzacounter}[0]{%
  \ifboolexpr{bool{l@dprintingpages} or bool{l@dprintingcolumns}}%
  {%
    \ifledRcol%
      \global\numdef\current@stanza{\the\c@stanzaR}%
    \else%
      \global\numdef\current@stanza{\the\c@stanzaL}%
    \fi%
  }%
  {%
    \global\numdef\current@stanza{\the\c@stanza}%
  }%
}%
\newcommand{\printXbeforenumber}[1]{%
  \hspace{\csuse{Xbeforenumber@#1}}%
}%
\newcommand{\printXafternumber}[1]{%
  \iftoggle{Xnonbreakableafternumber@#1}{\nobreak}{}%
  \hspace{\csuse{Xafternumber@#1}}%
}%
\newif\ifl@d@pnum
\newif\ifl@d@ssub
\newif\ifl@d@elin
\newif\ifl@d@esl
\newif\ifl@d@dash
\newif\ifl@d@Xtwolines%
\newif\ifl@d@Xmorethantwolines%
\let\@annot@start@print\relax%
\let\@annot@end@print\relax%
\newif\ifl@d@Xlinenumber%
\newcommand*{\l@dparsefootspec}[3]{\l@dp@rsefootspec#1|}
\def\l@dp@rsefootspec#1|#2|#3|#4|#5|#6|#7|{%
  \gdef\l@dparsedstartpage{#1}%
  \gdef\l@dparsedstartline{#2}%
  \gdef\l@dparsedstartsub{#3}%
  \gdef\l@dparsedendpage{#4}%
  \gdef\l@dparsedendline{#5}%
  \gdef\l@dparsedendsub{#6}%
}
\def\l@dparsedstartpage{0}%
\def\l@dparsedstartline{0}%
\def\l@dparsedstartsub{0}%
\def\l@dparsedendpage{0}%
\def\l@dparsedendline{0}%
\def\l@dparsedendsub{0}%

\newcommand*{\setprintlines}[6]{%
  \let\@annot@start@print\relax%
  \let\@annot@end@print\relax%
  \l@d@pnumfalse%
  \l@d@dashfalse%
  \l@d@elinfalse%
  \l@d@Xlinenumbertrue%
  \ifboolexpr{togl{Xnolinenumber@\@currentseries}%
    or (%
      togl{Xnolinenumberifannotation@\@currentseries}%
      and not test{\ifdefvoid{\annot@start}}%
      and not test{\ifdefvoid{\annot@end}}%
      )%
  }{%
    \l@d@Xlinenumberfalse%
  }{}%
  \ifbypage@
     \ifnum#4=#1 \else
        \l@d@pnumtrue
        \l@d@dashtrue
     \fi
  \fi
  \ifboolexpr{%
    bool{l@d@pnum}%
    or (%
      not test{\ifnumequal{#2}{#5}}%
      and bool{l@d@Xlinenumber}%
    )%
   }{%
    \l@d@elintrue%
    \l@d@dashtrue%
    \unless\ifx\relax\annot@end%
      \def\@annot@end@print{%
        \l@wrapcs@ifnotemptybox{Xwraplinenumannotation@\@currentseries}{\annot@end}%
      }%
    \fi%
    }{}%
  \ifl@d@elin%
    \def\@annot@start@print{%
      \l@wrapcs@ifnotemptybox{Xwraplinenumannotation@\@currentseries}{\annot@start}%
    }%
  \else%
    \ifx\annot@start\annot@end%
      \unless\ifx\@annot@start\relax%
        \def\@annot@start@print{%
          \l@wrapcs@ifnotemptybox{Xwraplinenumannotation@\@currentseries}{\annot@start}%
        }%
      \fi%
    \else%
      \ifx\@annot@end@print\relax%
        \def\@annot@start@print{%
          \l@wrapcs@ifnotemptybox{Xwraplinenumannotation@\@currentseries}{%
            \ifx\annot@start\empty\else%
              \annot@start%
              \ifdefined\linerangesep@%
                \linerangesep@%
              \else%
                \csuse{Xlinerangeseparator@\@currentseries}%
              \fi%
            \fi%
            \annot@end%
            }%
          }%
       \else%
         \let\@annot@start@print\@annot@end@print%
         \let\@annot@end@print\relax%
      \fi%
    \fi%
  \fi%
  \l@d@ssubfalse
  \ifnum#3=0 \else
      \l@d@ssubtrue
  \fi
  \l@d@eslfalse
  \ifnum#6=0 \else
      \ifnum#6=#3
         \ifl@d@elin \l@d@esltrue \else \l@d@eslfalse \fi
      \else
         \l@d@esltrue
         \l@d@dashtrue
      \fi
  \fi%
  \ifl@d@dash%
    \ifboolexpr{togl{fulllines@} or test{\ifcsempty{Xtwolines@\@currentseries}}}%
      {}%
      {%
      \setistwofollowinglines{#1}{#2}{#4}{#5}%
        \ifboolexpr{%
          (%
               togl {Xtwolinesbutnotmore@\@currentseries}%
               and not%
                 (%
                  bool {istwofollowinglines@}%
                 )%
          )%
          or%
          (%
             (not test{\ifnumequal{#1}{#4}})%
               and togl{Xtwolinesonlyinsamepage@\@currentseries}%
             )%
          }%
          {}%
          {%
          \l@d@dashfalse%
          \l@d@Xtwolinestrue%
          \l@d@elinfalse%
          \l@d@eslfalse%
          \ifcsempty{Xmorethantwolines@\@currentseries}%
            {}%
            {\ifistwofollowinglines@\else%
              \l@d@Xmorethantwolinestrue%
             \fi%
            }%
          }%
      }%
  \fi%
  \iftoggle{Xnoidenticallinenumannotation@\@currentseries}{%
    \ifx\annot@start\annot@end%
      \let\@annot@end@print\relax%
      \ifboolexpr{%Do not print the dash if we do not print the line number
        test {\ifdefempty{\linenumrep}}%
        or not bool {l@d@Xlinenumber}%
      }{%
        \l@d@dashfalse%
      }{}%
    \fi%
  }{}%
  \iftoggle{Xlinenumannotationonlyfirst@\@currentseries}{%
    \ifboolexpr{%
      (%
        togl{Xlinenumannotationonlyfirstintwo@\@currentseries}%
        and test{\ifcsequal{annot@start}{prevannot@start@\@currentseries}}%
        and test{\ifcsequal{annot@end}{prevannot@end@\@currentseries}}%
      )%
      or%
      (%
        not togl{Xlinenumannotationonlyfirstintwo@\@currentseries}%
        test{\ifcsequal{annot@start}{prevannot@start@\@currentseries}}%
      )%
    }{%
      \def\@annot@start@print{%
        \l@wrapcs@ifnotemptybox{Xwraplinenumannotation@\@currentseries}{\csuse{Xsymlinenumannotation@\@currentseries}}%
      }%
      \let\@annot@end@print\relax%
      \ifboolexpr{%Do not print the dash if we do not print the line number
        test {\ifdefempty{\linenumrep}}%
        or not bool {l@d@Xlinenumber}%
      }{%
        \l@d@dashfalse%
      }{}%
    }{}%
    \global\cslet{prevannot@start@\@currentseries}{\annot@start}%
    \global\cslet{prevannot@end@\@currentseries}{\annot@end}%
  }%
  {}%
}%
\newif\ifistwofollowinglines@%
\newcommand{\setistwofollowinglines}[4]{%
    \ifcsdef{lastlinenumberon@#1}%
      {\numdef{\tmp}{\csuse{lastlinenumberon@#1}}}%
      {\numdef{\tmp}{0}}%
    \istwofollowinglines@false%
    \ifnumequal{#4-#2}{1}%
    {\istwofollowinglines@true}%
    {\ifbypage@%
      \ifnumequal{#3-#1}{1}%
      {%
      \ifnumequal{#2}{\tmp}%
        {\ifnumequal{#4}{1}{\istwofollowinglines@true}{}}%
        {}%
      }%
      {}%
     \fi%
    }%
}%
\def\printlines#1|#2|#3|#4|#5|#6|#7|#8|{%
  \begingroup%
  \setprintlines{#1}{#2}{#3}{#4}{#5}{#6}%
  \ifdimequal{\csuse{Xboxstartlinenum@\@currentseries}}{0pt}%
    {\bgroup}%
    {\leavevmode\hbox to \csuse{Xboxstartlinenum@\@currentseries}\bgroup\hfill}%
  \ifcsstring{Xlinenumannotationposition@\@currentseries}{before}%
    {\@annot@start@print}%
    {}%
  \ifl@d@pnum%
    \wrap@edcrossref{\@this@crossref@start}{#1}%
    \ifl@d@Xlinenumber%
      \csuse{Xpagelinesep@\@currentseries}%
    \fi%
  \fi%
  \ifl@d@Xlinenumber%
    \wrap@edcrossref{\@this@crossref@start}{%
      \linenumrep{#2}%
      \iftoggle{Xlineflag@\@currentseries}{#8}{}%
    }%
    \ifl@d@ssub%
      \csuse{Xsublinesep@\@currentseries}%
      \wrap@edcrossref{\@this@crossref@start}{\sublinenumrep{#3}}%
    \fi%
  \fi%
  \ifcsstring{Xlinenumannotationposition@\@currentseries}{after}%
    {\@annot@start@print}%
    {}%
  \egroup%
  \ifdimequal{\csuse{Xboxendlinenum@\@currentseries}}{0pt}%
    {\bgroup}%
    {\hbox to \csuse{Xboxendlinenum@\@currentseries}\bgroup}%
  \ifl@d@Xtwolines%
    \ifl@d@Xlinenumber%
      \ifl@d@Xmorethantwolines%
        \csuse{Xmorethantwolines@\@currentseries}%
      \else%
        \csuse{Xtwolines@\@currentseries}%
      \fi%
    \fi%
  \else%
    \ifl@d@dash%
      \ifdefined\linerangesep@%
        \linerangesep@%
      \else%
        \csuse{Xlinerangeseparator@\@currentseries}%
      \fi%
    \fi%
    \ifcsstring{Xlinenumannotationposition@\@currentseries}{before}%
      {\@annot@end@print}%
      {}%
    \ifl@d@pnum%
       \wrap@edcrossref{\@this@crossref@end}{#4}%
       \csuse{Xpagelinesep@\@currentseries}%
    \fi%
    \ifl@d@Xlinenumber%
      \ifl@d@elin%
        \wrap@edcrossref{\@this@crossref@end}{%
          \linenumrep{#5}%
          \iftoggle{Xlineflag@\@currentseries}{#8}{}%
         }%
      \fi%
      \ifl@d@esl%
         \ifl@d@elin%
           \csuse{Xsublinesep@\@currentseries}%
         \fi%
         \wrap@edcrossref{\@this@crossref@end}{\sublinenumrep{#6}}%
      \fi%
    \fi%
    \ifcsstring{Xlinenumannotationposition@\@currentseries}{after}%
      {\@annot@end@print}%
      {}%
  \fi%
  \ifdimequal{\csuse{Xboxendlinenum@\@currentseries}}{0pt}%
    {}%
    {\hfill}%Prevent underfull hbox
  \egroup%
  \endgroup%
}%
\newcommand{\prepare@Xgroupbyline}[3]{%
  \iftoggle{Xgroupbylineseparetwolines@#1}{%
    \l@dparsefootspec#2%
    \ifcsdef{#1@forinserting@\l@dparsedendpage-\l@dparsedendline-\l@dparsedendsub}%
      {%
        \csgappto{#1@forinserting@\l@dparsedendpage-\l@dparsedendline-\l@dparsedendsub}{%
          \hskip\csuse{Xafternote@#1}\relax%
        }%
      }%
      {}%
    \add@hooktoggle@specific@to@cs{#1@forinserting@\l@dparsedendpage-\l@dparsedendline-\l@dparsedendsub}%
    \add@hookarg@specific@to@cs{#1@forinserting@\l@dparsedendpage-\l@dparsedendline-\l@dparsedendsub}%
    \csxappto%
      {#1@forinserting@\l@dparsedendpage-\l@dparsedendline-\l@dparsedendsub}%
      {%
        \keep@this@crossref@forinserting%
        \unexpanded{%
          \ifcsempty{Xsymlinenum@#1}%
            {\csuse{Xparafootsep@#1}}%
            {}%
          #3{#1}{#2}%
        }%
      }%
    \listcsxadd{#1@forinserting}{\l@dparsedendpage-\l@dparsedendline-\l@dparsedendsub}%
  }{%
  \ifcsdef{#1@forinserting@all}{%
    \csgappto%
      {#1@forinserting@all}%
      {\hskip\csuse{Xafternote@#1}\relax}%
    }{}%
  \add@hooktoggle@specific@to@cs{#1@forinserting@all}%
  \add@hookarg@specific@to@cs{#1@forinserting@all}%
  \csxappto%
    {#1@forinserting@all}%
    {%
      \keep@this@crossref@forinserting%
      \unexpanded{%
        \ifcsempty{Xsymlinenum@#1}%
          {\csuse{Xparafootsep@#1}}%
          {}%
        #3{#1}{#2}%
      }%
    }%
  }%
  \listcsgadd{#1@forinserting}{all}%
}%
\newcommand{\keep@this@crossref@forinserting}{%
  \unexpanded{\gdef\@this@crossref@start}{\@this@crossref@start}%
  \unexpanded{\gdef\@this@crossref@end}{\@this@crossref@end}%
}%
\providecommand*{\multiplefootnotemarker}{3sp}
\providecommand*{\multfootsep}{\textsuperscript{\normalfont,}}

\providecommand*{\m@mmf@prepare}{%
  \kern-\multiplefootnotemarker
  \kern\multiplefootnotemarker\relax}
\providecommand*{\m@mmf@check}{%
  \ifdim\lastkern=\multiplefootnotemarker\relax
    \edef\@x@sf{\the\spacefactor}%
    \unkern
    \multfootsep
    \spacefactor\@x@sf\relax
  \fi}

\@ifclassloaded{memoir}{}{%
\apptocmd{\@footnotetext}{\m@mmf@prepare}{}{}

\patchcmd{\@footnotemark}
  {\nobreak}
  {\m@mmf@check
  \nobreak
  }
  {}{}
\patchcmd{\@footnotemark}
  {\@makefnmark}
  {\@makefnmark
  \m@mmf@prepare
  }
  {}{}
}

\pretocmd{\@footnotetext}{%
  \ifnumberedpar@
    \edtext{}{\l@dbfnote{#1}}%
  \else
  }{}{}
\apptocmd{\@footnotetext}{\fi}{}{}%

\patchcmd%
  {\footnote}%
  {\stepcounter\@mpfn}%
  {%
  \ifboolexpr{bool{l@dpairing} or bool{l@dprintingpages} or bool{l@dprintingcolumns}}{%
    \global\advance\footnote@reading by \@ne%
    \get@thisfootnote%
    \get@fnmark{\thisc@footnote}%
    \ifcsdef{footnotereading\the\footnote@reading=typeset}%
      {\setcounter{\@mpfn}{\csuse{footnotereading\the\footnote@reading=typeset}}}%
      {\setcounter{\@mpfn}{\footnote@reading}}%
    }{%
      \stepcounter\@mpfn%
    }%
  }%
  {}
  {}

\newcommand{\get@thisfootnote}{%
    \ifboolexpr{bool{l@dpairing} or bool{l@dprintingpages} or bool{l@dprintingcolumns}}{%
      \protected@xdef\thisc@footnote{\the\footnote@reading}%
    }{%
      \protected@xdef\thisc@footnote{\the\c@footnote}%
    }%
}%

\newcommand{\l@dbfnote}[1]{%
 \get@thisfootnote%
  \gdef\@tag{#1\relax}%
    \ifledRcol%
      \xright@appenditem{%
          \ifdefined\Hy@footnote@currentHref%
            \noexpand\def\noexpand\Hy@footnote@currentHref{\Hy@footnote@currentHref}%
          \fi%
          \noexpand\vl@dbfnote{{\expandonce\@tag}}{\thisc@footnote}%
        }%
                         \to\inserts@listR
       \global\advance\insert@countR \@ne%
    \else%
      \xright@appenditem{%
          \ifdefined\Hy@footnote@currentHref%
            \noexpand\def\noexpand\Hy@footnote@currentHref{\Hy@footnote@currentHref}%
          \fi%
          \noexpand\vl@dbfnote{{\expandonce\@tag}}{\thisc@footnote}%
       }%
                         \to\inserts@list
      \global\advance\insert@count \@ne%
    \fi
  \ignorespaces%
}%

\newcommand{\get@fnmark}[1]{%
  \ifboolexpr{bool{l@dpairing} or bool{l@dprintingpages} or bool{l@dprintingcolumns}}%
    {%
      \stepcounter{footnote@typeset}%
      \setcounter{footnote}{\c@footnote@typeset}%
        \immediate\write\@mainaux{%
          \csgdef{footnotereading#1=typeset}{\the\c@footnote@typeset}%
         }%
      \def\@thefnmark{\thefootnote}%
    }%
    {%
      \setcounter{footnote}{#1}%
      \def\@thefnmark{\thefootnote}%
    }%
}%

\newcommand{\vl@dbfnote}[2]{%
  \get@fnmark{#2}%
  \@footnotetext{#1}%
}%
\newcommand*{\prebodyfootmark}{%
  \leavevmode
  \ifhmode
    \edef\@x@sf{\the\spacefactor}%
    \m@mmf@check
    \nobreak
  \fi}
\newcommand{\postbodyfootmark}{%
  \m@mmf@prepare
  \ifhmode\spacefactor\@x@sf\fi\relax}

\newcommandx{\arrangementX}[2][1,usedefault]{%
  \def\do##1{%
    \csname arrangementX@#2\endcsname{##1}%
  }%
  \ifstrempty{#1}%
    {%
    \dolistloop{\@series}%
    }%
    {
    \docsvlist{#1}%
    }%
}%
\newcommand*{\normal@footnotemarkX}[1]{%
  \prebodyfootmark
  \wrapped@bodyfootmarkX{#1}%
  \postbodyfootmark}

\newcommand*{\normalbodyfootmarkX}[1]{%
  \hbox{\textsuperscript{\normalfont\@nameuse{@thefnmark#1}}}}
\notbool{parapparatus@}{\newcommand*}{\newcommand}{\normalvfootnoteX}[2]{%
  \beforeinsertion@X{#1}%
  \insert\@nameuse{footins#1}\bgroup
    \fontseries{\seriesdefault}%
    \fontshape{\shapedefault}%
    \hsize=\expandafter\dimexpr\csuse{widthX@#1}\relax%
    \noindent\csuse{bhooknoteX@#1}%
    \csuse{notefontsizeX@#1}%
    \footsplitskips
    \ifl@dpairing\ifl@dpaging\else%
      \setnoteswidthliketwocolumnsX@{#1}%
    \fi\fi%
    \setnotesXpositionliketwocolumns@{#1}%
    \spaceskip=\z@skip \xspaceskip=\z@skip
    \csuse{\csuse{footnote@dir}}\@nameuse{footfmt#1}{#1}{#2}\egroup}

\newcommand*{\mpnormalvfootnoteX}[3]{%
  \get@thisfootnoteX{#1}%
  \get@fnmarkX{#1}{\thisc@footnote}{#3}%
  \ifstrempty{#3}{%
    \edef\this@footnoteX@reading{\the\csname footnote#1@reading\endcsname}%
  }{%
    \edef\this@footnoteX@reading{###3}%
  }%
  \global\setbox\@nameuse{mpfootins#1}\vbox{%
    \unvbox\@nameuse{mpfootins#1}
    \noindent\csuse{bhooknoteX@#1}%
    \csuse{notefontsizeX@#1}%
    \hsize\columnwidth
    \@parboxrestore
    \color@begingroup
    \@nameuse{footfmt#1}{#1}{#2}\color@endgroup}}

\notbool{parapparatus@}{\newcommand*}{\newcommand}{\normalfootfmtX}[2]{%
 \ifluatex%
    \textdir\footnote@luatextextdir%
    \pardir\footnote@luatexpardir%
 \fi%
   \protected@edef\@currentlabel{%
       \@nameuse{@thefnmark#1}%
   }%
  \ledsetnormalparstuffX{#1}%
  \sethangindentX{#1}%
  \rule\z@\splittopskip%
  {{\csuse{notenumfontX@#1}\wrapped@footfootmarkX{#1}}%
    \csuse{wrapcontentX@#1}{#2}%
  \strut\par}}

\newcommand*{\normalfootfootmarkX}[1]{%
  \textsuperscript{\@nameuse{@thefnmark#1}}}

\newcommand*{\normalfootstartX}[1]{%
    \ifdimequal{0pt}{\prenotesX@}{}%
      {%
      \iftoggle{prenotesX@}{%
           \togglefalse{prenotesX@}%
           \skip\csname footins#1\endcsname=%
             \glueexpr\csuse{prenotesX@}+\csuse{afterruleX@#1}\relax%
           }%
          {}%
      }%
  \vskip\skip\csname footins#1\endcsname%
  \leftskip=\z@
  \rightskip=\z@
  \ifl@dpairing\else%
     \hsize=\old@hsize%
  \fi%
  \setnoteswidthliketwocolumnsX@{#1}%
  \setnotesXpositionliketwocolumns@{#1}%
  \print@footnoteXrule{#1}%
}%

\let\normalfootnoteruleX=\footnoterule

\newcommand*{\normalfootgroupX}[1]{%
  \csuse{bhookgroupX@#1}%
  \unvbox\@nameuse{footins#1}%
  \hsize=\old@hsize%
  }%

\newcommand*{\mpnormalfootgroupX}[1]{%
  \vskip\skip\@nameuse{mpfootins#1}
  \ifl@dpairing\ifparledgroup%
    \leavevmode\marks\parledgroup@{begin}%
    \marks\parledgroup@series{#1}%
    \marks\parledgroup@type{footnoteX}%
  \fi\fi\normalcolor
  \ifparledgroup%
    \ifl@dpairing%
    \else%
      \setnoteswidthliketwocolumnsX@{#1}%
      \setnotesXpositionliketwocolumns@{#1}%
      \print@footnoteXrule{#1}%
    \fi%
  \else%
    \setnoteswidthliketwocolumnsX@{#1}%
    \setnotesXpositionliketwocolumns@{#1}%
    \print@footnoteXrule{#1}%
  \fi%
  \csuse{bhookgroupX@#1}%
  \unvbox\@nameuse{mpfootins#1}}


\newcommand{\normalbfnoteX}[3]{%
  \get@thisfootnoteX{#1}%
    \ifledRcol%
      \ifluatex
          \footnotelang@lua[R]%
      \fi
      \@ifundefined{xpg@main@language}%if polyglossia
        {}%
        {\footnotelang@poly[R]}%
      \xright@appenditem{%
        \noexpand\led@set@index@fornote{#1}%
        \noexpand\prepare@edindex@fornote{\l@d@nums}%
        \unexpanded{\gdef\@currentnotetype{familiar}}%
        \unexpanded{\def\this@footnoteX@reading}{\the\csname footnote#1@reading\endcsname}%
        \noexpand\vbfnoteX{#1}{#2}{\thisc@footnote}{#3}%
        \noexpand\led@reinit@index@fornote%
        \unexpanded{\advance\@edindex@fornote@\m@ne}%
      }%
                         \to\inserts@listR
       \global\advance\insert@countR \@ne%
    \else%
      \ifluatex
          \footnotelang@lua%
      \fi
      \@ifundefined{xpg@main@language}%if polyglossia
        {}%
        {\footnotelang@poly}%
      \xright@appenditem{%
        \noexpand\led@set@index@fornote{#1}%
        \noexpand\prepare@edindex@fornote{\l@d@nums}%
        \unexpanded{\gdef\@currentnotetype{familiar}}%
        \unexpanded{\def\this@footnoteX@reading}{\the\csname footnote#1@reading\endcsname}%
        \noexpand\vbfnoteX{#1}{#2}{\thisc@footnote}{#3}%
        \noexpand\led@reinit@index@fornote%
        \unexpanded{\advance\@edindex@fornote@\m@ne}%
      }%
                         \to\inserts@list
      \global\advance\insert@count \@ne%
    \fi
  \ignorespaces}

\newcommand{\get@thisfootnoteX}[1]{%
    \ifboolexpr{bool{l@dpairing} or bool{l@dprintingpages} or bool{l@dprintingcolumns}}{%
      \protected@xdef\thisc@footnote{\the\csname footnote#1@reading\endcsname}%
    }{%
      \protected@xdef\thisc@footnote{\the\csname c@footnote#1\endcsname}%
    }%
}%
\newcommand{\vbfnoteX}[4]{%
  \get@fnmarkX{#1}{#3}{#4}\relax%
  \@nameuse{regvfootnote#1}{#1}{#2}%
}%
\newcommand{\get@fnmarkX}[3]{%
  \ifstrempty{#3}{%
    \ifboolexpr{bool{l@dpairing} or bool{l@dprintingpages} or bool{l@dprintingcolumns}}%
      {%
        \stepcounter{footnote#1@typeset}%
        \setcounter{footnote#1}{\value{footnote#1@typeset}}%
        \@namedef{@thefnmark#1}{\csuse{thefootnote#1}}%
          \immediate\write\@mainaux{%
            \csgdef{footnote#1reading#2=typeset}{\the\csname c@footnote#1@typeset\endcsname}%
           }%
      }%
      {%
        \setcounter{footnote#1}{#2}%
        \@namedef{@thefnmark#1}{\csuse{thefootnote#1}}%
      }%
  }%
  {%
    \csdef{@thefnmark#1}{#3}%
  }%
}%

\newcommand{\vnumfootnoteX}[3]{%
  \ifnumberedpar@
    \edtext{}{\normalbfnoteX{#1}{#2}{#3}}%
  \else
    \def\this@footnoteX@reading{\the\csname footnote#1@reading\endcsname}%
    \get@thisfootnoteX{#1}%
    \get@fnmarkX{#1}{\expandonce\thisc@footnote}{#3}%
    \@nameuse{regvfootnote#1}{#1}{#2}%
  \fi}
\newcommand*{\arrangementX@normal}[1]{%
  \csgdef{series@displayX#1}{normal}
  \expandafter\let\csname footstart#1\endcsname=\normalfootstartX
  \@namedef{@footnotemark#1}{\normal@footnotemarkX{#1}}
  \@namedef{bodyfootmark#1}{\normalbodyfootmarkX{#1}}
  \expandafter\let\csname regvfootnote#1\endcsname=\normalvfootnoteX
  \expandafter\let\csname vfootnote#1\endcsname=\vnumfootnoteX
  \expandafter\let\csname footfmt#1\endcsname=\normalfootfmtX
  \@namedef{footfootmark#1}{\normalfootfootmarkX{#1}}
  \expandafter\let\csname footgroup#1\endcsname=\normalfootgroupX
  \expandafter\let\csname footnoterule#1\endcsname=\normalfootnoteruleX
  \count\csname footins#1\endcsname=1000
  \csxdef{default@footins#1}{1000}%Use to have note only for one side
  \dimen\csname footins#1\endcsname=\csuse{maxhnotesX@#1}
  \skip\csname footins#1\endcsname=\csuse{beforenotesX@#1}%
  \advance\skip\csname footins#1\endcsname by\csuse{afterruleX@#1}%
  \ifnoledgroup@\else%
    \expandafter\let\csname mpvfootnote#1\endcsname=\mpnormalvfootnoteX
    \expandafter\let\csname mpfootgroup#1\endcsname=\mpnormalfootgroupX
    \count\csname mpfootins#1\endcsname=1000
    \dimen\csname mpfootins#1\endcsname=\csuse{maxhnotesX@#1}
    \skip\csname mpfootins#1\endcsname=\csuse{beforenotesX@#1}%
    \advance\skip\csname mpfootins#1\endcsname by\csuse{afterruleX@#1}%
  \fi
}

\newcommand*{\arrangementX@twocol}[1]{%
  \csgdef{series@displayX#1}{twocol}
  \expandafter\let\csname regvfootnote#1\endcsname=\twocolvfootnoteX
  \expandafter\let\csname footfmt#1\endcsname=\twocolfootfmtX
  \expandafter\let\csname footgroup#1\endcsname=\twocolfootgroupX
  \dimen\csname footins#1\endcsname=\csuse{maxhnotesX@#1}%
  \skip\csname footins#1\endcsname=\csuse{beforenotesX@#1}%
  \advance\skip\csname footins#1\endcsname by \csuse{afterruleX@#1}\relax%
  \twocolfootsetupX{#1}
  \ifnoledgroup@\else%
    \expandafter\let\csname mpvfootnote#1\endcsname=\mpnormalvfootnoteX
    \expandafter\let\csname mpfootgroup#1\endcsname=\mptwocolfootgroupX
    \skip\csname mpfootins#1\endcsname=\csuse{beforenotesX@#1}%
    \advance\skip\csname mpfootins#1\endcsname by\csuse{afterruleX@#1}
    \mptwocolfootsetupX{#1}
  \fi%
}

\newcommand*{\twocolfootsetupX}[1]{%
  \count\csname footins#1\endcsname 500
  \csxdef{default@footins#1}{500}%Use this to confine the  notes to one side only
  \multiply\dimen\csname footins#1\endcsname by \tw@}
\newcommand*{\mptwocolfootsetupX}[1]{%
  \count\csname mpfootins#1\endcsname 500
  \multiply\dimen\csname mpfootins#1\endcsname by \tw@}

\notbool{parapparatus@}{\newcommand*}{\newcommand}{\twocolvfootnoteX}[2]{%
  \beforeinsertion@X{#1}%
  \let\bidi@RTL@everypar\relax%
  \insert\csname footins#1\endcsname\bgroup%
    \hsize=\expandafter\dimexpr\csuse{widthX@#1}\relax%
    \noindent\csuse{bhooknoteX@#1}%
    \csuse{notefontsizeX@#1}%
    \footsplitskips%
    \spaceskip=\z@skip \xspaceskip=\z@skip%
    \@nameuse{footfmt#1}{#1}{#2}\egroup}

\notbool{parapparatus@}{\newcommand*}{\newcommand}{\twocolfootfmtX}[2]{%
   \protected@edef\@currentlabel{%
       \@nameuse{@thefnmark#1}%
   }%
  \normal@pars%
  \sethangindentX{#1}%
  \hsize \csuse{hsizetwocolX@#1}%
  \setparindentX{#1}%
  \tolerance=5000\relax%
  \par%
 \@tempdima=\parindent%
 \csuse{colalignX@#1}%
 \parindent=\@tempdima%
  {\hspace{\parindent}%
  \csuse{notenumfontX@#1}\wrapped@footfootmarkX{#1}\strut%
    \csuse{wrapcontentX@#1}{#2}%
    \strut\par}%
 \allowbreak%
}%

\newcommand*{\twocolfootgroupX}[1]{{\csuse{bhookgroupX@#1}\csuse{notefontsizeX@#1}
  \splittopskip=\ht\strutbox
  \expandafter
  \rigidbalanceX\csname footins#1\endcsname \tw@ \splittopskip}}

\newcommand*{\mptwocolfootgroupX}[1]{{%
  \vskip\skip\@nameuse{mpfootins#1}
  \ifl@dpairing\ifparledgroup%
    \leavevmode\marks\parledgroup@{begin}%
    \marks\parledgroup@series{#1}%
    \marks\parledgroup@type{footnoteX}%
  \fi\fi\normalcolor
  \ifparledgroup%
    \ifl@dpairing%
    \else%
      \setnoteswidthliketwocolumnsX@{#1}%
      \setnotesXpositionliketwocolumns@{#1}%
      \print@footnoteXrule{#1}%
    \fi%
  \else%
    \setnoteswidthliketwocolumnsX@{#1}%
    \setnotesXpositionliketwocolumns@{#1}%
    \print@footnoteXrule{#1}%
  \fi%
  \csuse{bhookgroupX@#1}%
  \splittopskip=\ht\strutbox
  \expandafter
  \rigidbalanceX\csname mpfootins#1\endcsname \tw@ \splittopskip}}

\newcommand*{\arrangementX@threecol}[1]{%
  \csgdef{series@displayX#1}{threecol}
  \expandafter\let\csname regvfootnote#1\endcsname=\threecolvfootnoteX
  \expandafter\let\csname footfmt#1\endcsname=\threecolfootfmtX
  \expandafter\let\csname footgroup#1\endcsname=\threecolfootgroupX
  \dimen\csname footins#1\endcsname=\csuse{maxhnotesX@#1}%
  \skip\csname footins#1\endcsname=\csuse{beforenotesX@#1}%
  \advance\skip\csname footins#1\endcsname by \csuse{afterruleX@#1}\relax%
  \threecolfootsetupX{#1}
  \ifnoledgroup@\else%
    \expandafter\let\csname mpvfootnote#1\endcsname=\mpnormalvfootnoteX
    \expandafter\let\csname mpfootgroup#1\endcsname=\mpthreecolfootgroupX
    \skip\csname mpfootins#1\endcsname=\csuse{beforenotesX@#1}%
    \advance\skip\csname mpfootins#1\endcsname by\csuse{afterruleX@#1}
    \mpthreecolfootsetupX{#1}
  \fi%
}

\newcommand*{\threecolfootsetupX}[1]{%
  \count\csname footins#1\endcsname 333
  \csxdef{default@footins#1}{333}%Use this to confine the  notes to one side only
  \multiply\dimen\csname footins#1\endcsname by \thr@@}
\newcommand*{\mpthreecolfootsetupX}[1]{%
  \count\csname mpfootins#1\endcsname 333
  \multiply\dimen\csname mpfootins#1\endcsname by \thr@@}

\notbool{parapparatus@}{\newcommand*}{\newcommand}{\threecolvfootnoteX}[2]{%
  \let\bidi@RTL@everypar\relax%
  \beforeinsertion@X{#1}%
  \insert\csname footins#1\endcsname\bgroup%
    \hsize=\expandafter\dimexpr\csuse{widthX@#1}\relax%
    \noindent\csuse{bhooknoteX@#1}%
    \csuse{notefontsizeX@#1}%
    \footsplitskips%
    \@nameuse{footfmt#1}{#1}{#2}\egroup}

\notbool{parapparatus@}{\newcommand*}{\newcommand}{\threecolfootfmtX}[2]{%
   \protected@edef\@currentlabel{%
       \@nameuse{@thefnmark#1}%
   }%
  \sethangindentX{#1}%
  \normal@pars%
  \hsize \csuse{hsizethreecolX@#1}%
  \setparindentX{#1}%
  \tolerance=5000\relax%
 \@tempdima=\parindent%
  \csuse{colalignX@#1}%
  \parindent=\@tempdima%
  {\hspace{\parindent}%
  \csuse{notenumfontX@#1}\wrapped@footfootmarkX{#1}\strut%
    \csuse{wrapcontentX@#1}{#2}%
    \strut\par}\allowbreak}

\newcommand*{\threecolfootgroupX}[1]{{\csuse{bhookgroupX@#1}\csuse{notefontsizeX@#1}
  \splittopskip=\ht\strutbox
  \expandafter
  \rigidbalanceX\csname footins#1\endcsname \thr@@ \splittopskip}}

\newcommand*{\mpthreecolfootgroupX}[1]{{%
  \vskip\skip\@nameuse{mpfootins#1}
  \ifl@dpairing\ifparledgroup
    \leavevmode\marks\parledgroup@{begin}%
    \marks\parledgroup@series{#1}%
    \marks\parledgroup@type{footnoteX}%
  \fi\fi\normalcolor
  \ifparledgroup%
    \ifl@dpairing%
    \else%
      \setnoteswidthliketwocolumnsX@{#1}%
      \setnotesXpositionliketwocolumns@{#1}%
      \print@footnoteXrule{#1}%
    \fi%
  \else%
    \setnoteswidthliketwocolumnsX@{#1}%
    \setnotesXpositionliketwocolumns@{#1}%
    \print@footnoteXrule{#1}%
  \fi%
  \csuse{bhookgroupX@#1}%
  \splittopskip=\ht\strutbox
  \expandafter
  \rigidbalanceX\csname mpfootins#1\endcsname \thr@@ \splittopskip}}

\newcommand*{\arrangementX@paragraph}[1]{%
  \csgdef{series@displayX#1}{paragraph}%
  \expandafter\let\csname footstart#1\endcsname=\parafootstartX
  \expandafter\let\csname regvfootnote#1\endcsname=\para@vfootnoteX
  \expandafter\let\csname footfmt#1\endcsname=\parafootfmtX
  \expandafter\let\csname footgroup#1\endcsname=\para@footgroupX
  \expandafter\let\csname footnoterule#1\endcsname=\normalfootnoteruleX
  \count\csname footins#1\endcsname=1000
  \csxdef{default@footins#1}{1000}%Use this to confine the  notes to one side only
  \dimen\csname footins#1\endcsname=\csuse{maxhnotesX@#1}
  \skip\csname footins#1\endcsname=\csuse{beforenotesX@#1}%
  \advance\skip\csname footins#1\endcsname by\csuse{afterruleX@#1}%
  \para@footsetupX{#1}
  \ifnoledgroup@\else
    \expandafter\let\csname mpvfootnote#1\endcsname=\mppara@vfootnoteX
    \expandafter\let\csname mpfootgroup#1\endcsname=\mppara@footgroupX
    \count\csname mpfootins#1\endcsname=1000
    \dimen\csname mpfootins#1\endcsname=\csuse{maxhnotesX@#1}
    \skip\csname mpfootins#1\endcsname=\csuse{beforenotesX@#1}%
    \advance\skip\csname mpfootins#1\endcsname by\csuse{afterruleX@#1}%
  \fi
  }

\newcommand*{\para@footsetupX}[1]{{\csuse{bhookgroupX@#1}\csuse{notefontsizeX@#1}
  \setnoteswidthliketwocolumnsX@{#1}%
  \ifcsempty{widthX@#1}%
    {}%
    {\columnwidth=\expandafter\dimexpr\csuse{widthX@#1}\relax}%
  \dimen0=\baselineskip
  \multiply\dimen0 by 1024
  \divide\dimen0 by \columnwidth \multiply\dimen0 by \footfudgefiddle\relax%
  \expandafter
  \xdef\csname footfudgefactor#1\endcsname{%
    \expandafter\strip@pt\dimen0 }}}

\newcommand*{\parafootstartX}[1]{%
    \ifdimequal{0pt}{\prenotesX@}{}%
      {%
      \iftoggle{prenotesX@}{%
            \togglefalse{prenotesX@}%
            \skip\csname footins#1\endcsname=%
              \glueexpr\csuse{prenotesX@}+\csuse{afterruleX@#1}\relax%
            }%
          {}%
      }%
  \leftskip=\z@
  \rightskip=\z@
  \setparindentX{#1}%
  \vskip\skip\@nameuse{footins#1}%
  \setnoteswidthliketwocolumnsX@{#1}%
  \setnotesXpositionliketwocolumns@{#1}%
  \print@footnoteXrule{#1}%
  }

\newcommand*{\para@vfootnoteX}[2]{%
 \csuse{beforeinsertingX@#1}%
 \insert\csname footins#1\endcsname%
  \bgroup
    \csuse{notefontsizeX@#1}
    \footsplitskips
    \setbox0=\vbox{\hsize=\maxdimen%
      \let\bidi@RTL@everypar\@empty%
      \insert@txtbeforenotesX{#1}%
      \noindent\csuse{bhooknoteX@#1}%
      \@nameuse{footfmt#1}{#1}{#2}}%
    \setbox0=\hbox{\unvxhX{0}{#1}}%
    \dp0=\z@
    \ht0=\csname footfudgefactor#1\endcsname\wd0
    \box0
    \penalty0
  \egroup}
\newcommand*{\mppara@vfootnoteX}[3]{%
  \get@thisfootnoteX{#1}%
  \get@fnmarkX{#1}{\thisc@footnote}{#3}%
  \edef\this@footnoteX@reading{\the\csname footnote#1@reading\endcsname}%
  \global\setbox\@nameuse{mpfootins#1}\vbox{%
    \unvbox\@nameuse{mpfootins#1}
    \csuse{notefontsizeX@#1}
    \footsplitskips
    \setbox0=\vbox{\hsize=\maxdimen%
      \let\bidi@RTL@everypar\@empty%
      \noindent\color@begingroup%
      \csuse{bhooknoteX@#1}%
      \@nameuse{footfmt#1}{#1}{#2}\color@endgroup}%
    \setbox0=\hbox{\unvxhX{0}{#1}}%
    \dp0=\z@
    \ht0=\csname footfudgefactor#1\endcsname\wd0
    \box0
    \penalty0}}

\newcommand*{\unvxhX}[2]{% 2th is optional for retro-compatibility
  \setbox0=\vbox{\unvbox#1%
    \global\setbox1=\lastbox}%
  \unhbox1
  \unskip            % remove \rightskip,
  \unskip            % remove \parfillskip,
  \unpenalty         % remove \penalty of 10000,
  \hskip\csuse{afternoteX@#2}%
  \relax}% but add the glue to go between the notes

\newcommand*{\parafootfmtX}[2]{%
   \protected@edef\@currentlabel{%
       \@nameuse{@thefnmark#1}%
   }%
  \insertparafootsepX{#1}%
  \ledsetnormalparstuff@common%
  {\csuse{notenumfontX@#1}%
   \csuse{notenumfontX@#1}%
   \wrapped@footfootmarkX{#1}%
   \strut%
    \csuse{wrapcontentX@#1}{#2}%
    \penalty-10}}

\newcommand*{\para@footgroupX}[1]{%
  \hsize=\expandafter\dimexpr\csuse{widthX@#1}\relax%
  \unvbox\csname footins#1\endcsname
  \ifcsstring{raggedX@#1}{L}{\RaggedLeft}{}%
  \ifcsstring{raggedX@#1}{R}{\RaggedRight}{}%
  \makehboxofhboxes
  \setbox0=\hbox{\unhbox0 \removehboxes}%
  \csuse{bhookgroupX@#1}
  \csuse{notefontsizeX@#1}
  \unhbox0\par}

\newcommand*{\mppara@footgroupX}[1]{{%
  \setnoteswidthliketwocolumnsX@{#1}%
  \vskip\skip\@nameuse{mpfootins#1}
  \ifl@dpairing\ifparledgroup
    \leavevmode%
    \leavevmode\marks\parledgroup@{begin}%
    \marks\parledgroup@series{#1}%
    \marks\parledgroup@type{footnoteX}%
  \fi\fi\normalcolor
  \ifparledgroup%
    \ifl@dpairing%
    \else%
      \setnoteswidthliketwocolumnsX@{#1}%
      \setnotesXpositionliketwocolumns@{#1}%
      \print@footnoteXrule{#1}%
    \fi%
  \else%
      \setnoteswidthliketwocolumnsX@{#1}%
      \setnotesXpositionliketwocolumns@{#1}%
      \print@footnoteXrule{#1}%
  \fi%
  \unvbox\csname mpfootins#1\endcsname
  \ifcsstring{raggedX@#1}{L}{\RaggedLeft}{}%
  \ifcsstring{raggedX@#1}{R}{\RaggedRight}{}%
  \makehboxofhboxes
  \setbox0=\hbox{\unhbox0 \removehboxes}%
  \csuse{bhookgroupX@#1}%
  \csuse{notefontsizeX@#1}%
  \setparindentX{#1}%
  \unhbox0\par}}

\newcommand{\insertparafootsepX}[1]{%
   \ifledRcol@%
     \ifnumequal{\csuse{prevpage#1@numR}}{\page@numR}%
        {\csuse{parafootsepX@#1}}%
        {}%
     \global\csname prevpage#1@numR\endcsname=\page@numR%
   \else%
     \ifboolexpr{bool{l@dprintingpages} or bool{l@dprintingcolumns} or bool{numbering}}{%
       \ifnumequal{\csuse{prevpage#1@num}}{\page@num}%
          {\csuse{parafootsepX@#1}}%
          {}%
       \global\csname prevpage#1@num\endcsname=\page@num%
     }{%We can't use directly the page counter, see https://tex.stackexchange.com/a/239457/7712
       \global\expandafter\advance\csname footnote#1@parafootsepX\endcsname by 1%
       \edef\@tmpa{parafootsepX@#1@\the\csname footnote#1@parafootsepX\endcsname}%
       \expandafter\edlabel\expandafter{\@tmpa}%
       \edef\@tmpb{\expandafter\xpageref\expandafter{\@tmpa}}%
       \IfStrEq{\csuse{prevpage#1@num@outnumbering}}{\@tmpb}%
           {\csuse{parafootsepX@#1}}%
           {}%
       \global\cslet{prevpage#1@num@outnumbering}{\@tmpb}%
     }%
   \fi%
}
\newcommand{\wrapped@footfootmarkX}[1]{%
  \ifdefined\hypertarget%
    \hyperlink%
        {@bodyfootmark#1@\this@footnoteX@reading}%
        {\@nameuse{footfootmark#1}}%
    \Hy@raisedlink{%
      \hypertarget%
        {@footnotemark#1@\this@footnoteX@reading}%
        {}%
      }%
  \else%
    \@nameuse{footfootmark#1}%
  \fi%
}%
\newcommand{\wrapped@bodyfootmarkX}[1]{%
  \ifdefined\hypertarget%
    \hyperlink%
        {@footnotemark#1@\expandafter\the\csname footnote#1@reading\endcsname}%
        {\@nameuse{bodyfootmark#1}}%
    \Hy@raisedlink{%
      \hypertarget%
        {@bodyfootmark#1@\expandafter\the\csname footnote#1@reading\endcsname}%
        {}%
      }%
  \else%
    \@nameuse{bodyfootmark#1}%
  \fi%
}%
\newcommand*{\ledsetnormalparstuff@common}{%
  \ifluatex%
   \ifdefstring{\footnote@luatextextdir}{TLT}{}%
     {\textdir\footnote@luatextextdir}%
   \pardir\footnote@luatexpardir%
  \fi%
  \csuse{\csuse{footnote@dir}}%
  \normal@pars%
  \parfillskip \z@ \@plus 1fil}%

\newcommand*{\Xledsetnormalparstuff}[1]{%
  \ledsetnormalparstuff@common%
  \Xsetparindent{#1}%
  \iftoggle{Xparindent@#1}{\hspace{\parindent}}{}%
}%

\newcommand*{\ledsetnormalparstuffX}[1]{%
  \ledsetnormalparstuff@common%
  \setparindentX{#1}%
  \iftoggle{parindentX@#1}{\hspace{\parindent}}{}%
}%

\newdimen\old@hsize%
\AtBeginDocument{\old@hsize=\hsize}%

\newcommand{\setXnoteswidthliketwocolumns@}[1]{%
  \global\let\hsize@fornote=\hsize%
  \global\old@hsize=\hsize%
  \let\old@columwidth=\columnwidth%
  \iftoggle{Xnoteswidthliketwocolumns@#1}%
    {%
    \setwidthliketwocolumns%
    \global\let\hsize@fornote=\hsize%
    }%
    {}%
  \let\hsize=\hsize@fornote%
  \let\columnwidth=\old@columwidth%
}%

\newcommand{\setnoteswidthliketwocolumnsX@}[1]{%
  \global\let\hsize@fornote=\hsize%
  \global\old@hsize=\hsize%
  \let\old@columwidth=\columnwidth%
  \iftoggle{noteswidthliketwocolumnsX@#1}%
    {%
    \setwidthliketwocolumns%
    \global\let\hsize@fornote=\hsize%
    }%
    {}%
  \let\hsize=\hsize@fornote%
  \let\columnwidth=\old@columwidth%
}%

\newcommand{\setXnotespositionliketwocolumns@}[1]{%
  \iftoggle{Xnoteswidthliketwocolumns@#1}{%
    \csuse{setnotespositionliketwocolumns@\columns@position}%
  }{}%
}%

\newcommand{\setnotesXpositionliketwocolumns@}[1]{%
  \iftoggle{noteswidthliketwocolumnsX@#1}{%
    \csuse{setnotespositionliketwocolumns@\columns@position}%
  }{}%
}%

\def\@fnpos{familiar-critical}
\def\@mpfnpos{critical-familiar}
\newcommand{\fnpos}[1]{\xdef\@fnpos{#1}}
\newcommand{\mpfnpos}[1]{\xdef\@mpfnpos{#1}}
\newcommand{\print@Xfootnoterule}[1]{%
  \vskip-\csuse{Xafterrule@#1}%Because count in \dimen\csuse{#1footins}
  \nointerlineskip%
  \moveleft-\leftskip\vbox{\csuse{#1footnoterule}}%
  \nointerlineskip%
  \vskip\csuse{Xafterrule@#1}%
}%

\newcommand{\print@footnoteXrule}[1]{%
  \vskip-\csuse{afterruleX@#1}%Because count in \dimen\csuse{footins#1}
  \nointerlineskip%
  \moveleft-\leftskip\vbox{\csuse{footnoterule#1}}%
  \nointerlineskip%
  \vskip\csuse{afterruleX@#1}%
}%

\newtoggle{Xprenotes@}%
\toggletrue{Xprenotes@}%
\newcommand{\Xprenotes@}{0pt}%
\newcommand*{\Xprenotes}[1]{\renewcommand{\Xprenotes@}{#1}}%
\newcommand{\preXnotes}[1]{\led@warning@preXnotes@deprecated\Xprenotes{#1}}%For compatibility
\newtoggle{prenotesX@}
\toggletrue{prenotesX@}
\newcommand{\prenotesX@}{0pt}
\newcommand*{\prenotesX}[1]{\renewcommand{\prenotesX@}{#1}}
\gdef\firstXseries@{}
\newcommand{\prepare@Xprenotes}[1]{%
  \ifdimequal{0pt}{\Xprenotes@}%
  {}%
  {%
    \IfStrEq{\firstXseries@}{}{%
      \global\skip\csuse{#1footins}=\Xprenotes@%
      \global\advance\skip\csname #1footins\endcsname by\csuse{Xafterrule@#1}%
      \gdef\firstXseries@{#1}%
    }%
    {%
     \ifseriesbefore{#1}{\firstXseries@}%
       {%
       \global\skip\csuse{#1footins}=\csuse{Xbeforenotes@\firstXseries@}%
       \global\advance\skip\csname #1footins\endcsname by\csuse{Xafterrule@#1}%
       \gdef\firstXseries@{#1}%
       }%
       {}%
    }%
  }%
}
\gdef\firstseriesX@{}
\newcommand{\prepare@prenotesX}[1]{%
  \ifdimequal{0pt}{\prenotesX@}%
  {}%
  {%
    \IfStrEq{\firstseriesX@}{}{%
      \global\skip\csuse{footins#1}=\prenotesX@%
      \global\advance\skip\csname footins#1\endcsname by\csuse{afterruleX@#1}%
      \gdef\firstseriesX@{#1}%
    }%
    {%
     \ifseriesbefore{#1}{\firstseriesX@}%
       {%
       \global\skip\csuse{footins#1}=\csuse{beforenotesX@\firstseriesX@}%
       \global\advance\skip\csname footins#1\endcsname by\csuse{afterruleX@#1}%
       \gdef\firstXseries@{#1}%
       }%
       {}%
    }%
  }%
}
\ifbool{noend@}{}{%Used instead of \ifnoend@ to prevent expansion problem
\newcommand{\l@dend@open}[1]{%
  \global\booltrue{l@dend@#1}%
  \expandafter\immediate%
    \expandafter\openout%
      \csname l@d@#1end\endcsname%
      =\l@auxdir\jobname.#1end\relax%
}%
\newcommand{\l@dend@close}[1]{%
  \global\boolfalse{l@dend@#1}%
  \expandafter\immediate%
    \expandafter\closeout\csname l@d@#1end\endcsname%
}%

\newcommand{\l@dend@stuff}{%
 \def\do##1{%
   \ifbool{l@dend@##1}{}%
     {\l@dend@open{##1}}%
   \expandafter\immediate\expandafter\write\csname l@d@##1end\endcsname{\string\l@d@section{\the\section@num}\@percentchar}%
   }%
 \dolistloop{\@series}%
}%

\global\notbool{parapparatus@}{}{\long}\def\endprint#1#2#3#4#5#6#7{{%
  \iftoggle{Xendfirstnote@#4}{%
    \global\togglefalse{Xendfirstnote@#4}%
    \csuse{Xendtxtbeforenotes@#4}%
  }{}%
  \csuse{Xendbhooknote@#4}%
  \csuse{Xendnotefontsize@#4}%
  \hangindent=\csuse{Xendhangindent@#4}%
  \Xendstorelineinfo{#1}{#4}%
  \ifXendinsertsep@%
    \hskip\csuse{Xendafternote@#4}\relax%
    \ifcsdef{prevendline#4}%
      {\ifcsequal{prevendline#4}{lineinfo@}%
        {\ifcsempty{Xendsymlinenum@#4}{\csuse{Xendsep@#4}}{}}%
        {\csuse{Xendsep@#4}}%
      }%
      {\csuse{Xendsep@#4}}%
  \else%
    \iftoggle{Xendparagraph@#4}%
      {\global\Xendinsertsep@true}%
      {}%
  \fi%
  \xdef\@currentseries{#4}%
  \def\do##1{%
    \setkeys[mac]{truefootnoteoption}{##1}%
  }%
  \notblank{#5}{\docsvlist{#5}}{}%
  \IfStrEq{#6}{R}{\ledRcol@true}{}%
  \def\@this@crossref@start{#7:start}%
  \def\@this@crossref@end{#7:end}%
  \printlineendnote{#1}{#4}%
  \IfStrEq{#6}{R}{\ledRcol@false}{}%
  \nottoggle{Xendlemmadisablefontselection@#4}%
    {\select@lemmafont#1|}%
    {}%
  \bgroup%
    \csuse{Xendlemmafont@#4}%
    \csuse{Xendwraplemma@#4}{#2}%
  \egroup%
  \ifboolexpr{%
    togl {nosep@}%
    or test{\ifcsempty{Xendlemmaseparator@#4}}%
    }%
    {\hskip\csuse{Xendinplaceoflemmaseparator@#4}\relax}%
    {\nobreak%
     \hskip\csuse{Xendbeforelemmaseparator@#4}%
     \csuse{Xendlemmaseparator@#4}%
     \hskip\csuse{Xendafterlemmaseparator@#4}%
     \relax%
    }%
  \csuse{Xendwrapcontent@#4}{#3}%
  \nottoggle{Xendparagraph@#4}{\par}{}%
  \def\do##1{%
    \setkeys[mac]{falsefootnoteoption}{##1}%
  }%
  \notblank{#5}{\docsvlist{#5}}{}%
}}%

\let\l@d@section=\@gobble

\newcommand{\Xendstorelineinfo}[2]{%
  \l@dp@rsefootspec#1|%
  \iftoggle{Xendnumberonlyfirstintwolines@#2}{%
    \xdef\lineinfo@{\l@dparsedstartpage - \l@dparsedstartline - \l@dparsedstartsub - \l@dparsedendpage - \l@dparsedendline - \l@dparsedendsub}%
  }%
  {%
    \xdef\lineinfo@{\l@dparsedstartpage - \l@dparsedstartline - \l@dparsedstartsub}%
  }%
}%
\newcommand{\printlineendnote}[2]{%
  \ifboolexpr{%
    togl {nonum@}%
    or togl {Xendnonumber@#2}%
    }%
    {\hspace{\csuse{Xendinplaceofnumber@#2}}}%
    {%
      \iftoggle{Xendnumberonlyfirstinline@#2}%
        {\ifcsdef{prevendline#2}%
          {\ifcsequal{prevendline#2}{lineinfo@}%
            {%
            \csuse{Xendbhookinplaceofnumber@#2}%
            \ifcsempty{Xendsymlinenum@#2}%
              {\hspace{\csuse{Xendinplaceofnumber@#2}}}%
              {\printsymlineendnotearea{#2}}%
            \csuse{Xendahookinplaceofnumber@2}%
            }%
            {\printlineendnotearea{#1}{#2}}}%
          {\printlineendnotearea{#1}{#2}}%
        }%
        {\printlineendnotearea{#1}{#2}}%We keep every time line
    \csxdef{prevendline#2}{\lineinfo@}%
    }%
}%
\newcommand{\printsymlineendnotearea}[1]{%
  \hspace{\csuse{Xendbeforesymlinenum@#1}}%
  \csuse{Xendnotenumfont@#1}%
  \ifdimequal{\csuse{Xendboxsymlinenum@#1}}{\z@}%
     {\csuse{Xendsymlinenum@#1}}%
     {\hbox to \csuse{Xendboxsymlinenum@#1}%
       {\csuse{Xendsymlinenum@#1}\hfill}%
     }%
  \hspace{\csuse{Xendaftersymlinenum@#1}}%
}%
\newcommand{\printlineendnotearea}[2]{%
  \csuse{Xendbhooklinenumber@#2}%
  \hspace{\csuse{Xendbeforenumber@#2}}%
  \bgroup%
    \csuse{Xendnotenumfont@#2}%
    \ifdimequal{\csuse{Xendboxlinenum@#2}}{0pt}%
      {\printendlines#1||\ifledRcol@\@Rlineflag\fi}%
      {\leavevmode%
       \hbox to \csuse{Xendboxlinenum@#2}%
       {%
       \IfSubStr{RC}{\csuse{Xendboxlinenumalign@#2}}{\hfill}{}%
       \printendlines#1||\ifledRcol@\@Rlineflag\fi%
       \IfSubStr{LC}{\csuse{Xendboxlinenumalign@#2}}{\hfill}{}%
      }}%
  \egroup%
  \hspace{\csuse{Xendafternumber@#2}}%
  \csuse{Xendahooklinenumber@#2}%
}%
\newcommandx{\toendnotes}[2][1,usedefault]{%
  \ifboolexpr{bool{numbering} or bool{numberingR}}{%
    \def\do##1{%
      \expandafter\immediate\expandafter\write\csname l@d@##1end\endcsname%
        {\unexpanded{#2}\@percentchar}%
    }%
    \ifstrempty{#1}%
      {\dolistloop{\@series}}%
      {\docsvlist{#1}}%
  }{\led@err@toendnotes@outsidenumbering}%
}%
\WithSuffix\newcommandx\toendnotes*[2][1,usedefault]{%
  \ifboolexpr{bool{numbering} or bool{numberingR}}{%
    \def\do##1{%
      \expandafter\immediate\expandafter\write\csname l@d@##1end\endcsname%
        {#2\@percentchar}%
    }%
    \ifstrempty{#1}%
      {\dolistloop{\@series}}%
      {\docsvlist{#1}}%
  }{\led@err@toendnotes@outsidenumbering}%
}%
\newif\ifXendinsertsep@%
\newcommand*{\doendnotes}[1]{%
  \l@dend@close{#1}%
   \begingroup
      \csxdef{prevpagenum@#1}{}%
      \csxdef{prevpagerange@#1}{}%
      \global\toggletrue{Xendfirstnote@#1}%
      \makeatletter
      \expandafter\let\csname #1end\endcsname=\endprint
      \input\l@auxdir\jobname.#1end%
      \global\Xendinsertsep@false%
   \endgroup}
\newcommand*{\doendnotesbysection}[1]{%
 \l@dend@close{#1}%
 \csxdef{prevpagenum@#1}{}%
 \csxdef{prevpagerange@#1}{}%
 \global\expandafter\advance\csname #1end@bysection\endcsname by 1%
 \begingroup%
    \makeatletter%
    \def\l@d@section##1{%
      \global\toggletrue{Xendfirstnote@#1}%
      \ifnumequal{##1}{\csname #1end@bysection\endcsname}%
        {\cslet{#1end}{\endprint}}%
        {\cslet{#1end}{\@gobbleseven}}%
    }%
    \input\l@auxdir\jobname.#1end%
    \global\Xendinsertsep@false%
 \endgroup%
}%
}%
\newif\ifl@d@Xendlinenumber%
\newcommand*{\setprintendlines}[6]{%
  \let\@annot@start@print\relax%
  \let\@annot@end@print\relax%
  \l@d@pnumfalse%
  \l@d@dashfalse%
  \l@d@elinfalse%
  \l@d@Xendlinenumbertrue%
  \ifboolexpr{togl{Xendnolinenumber@\@currentseries}%
    or (%
      togl{Xendnolinenumberifannotation@\@currentseries}%
      and not test{\ifdefvoid{\annot@start}}%
      and not test{\ifdefvoid{\annot@end}}%
      )%
  }{%
    \l@d@Xendlinenumberfalse%
  }{}%
  \ifnum#4=#1 \else
    \l@d@pnumtrue
    \l@d@dashtrue
  \fi
  \ifboolexpr{%
      bool{l@d@pnum}%
      or not test{\ifnumequal{#2}{#5}}%
      and bool{l@d@Xendlinenumber}%
     }{%
      \l@d@elintrue%
      \l@d@dashtrue%
      \l@d@elintrue
      \l@d@dashtrue
      \unless\ifx\relax\annot@end%
        \def\@annot@end@print{%
          \l@wrapcs@ifnotemptybox{Xendwraplinenumannotation@\@currentseries}{\annot@end}%
        }%
      \fi%
   }%
   {}%
  \ifl@d@elin%
    \def\@annot@start@print{%
      \l@wrapcs@ifnotemptybox{Xendwraplinenumannotation@\@currentseries}{\annot@start}%
    }%
  \else%
    \ifx\annot@start\annot@end%
      \unless\ifx\@annot@start\relax%
        \def\@annot@start@print{%
          \l@wrapcs@ifnotemptybox{Xendwraplinenumannotation@\@currentseries}{\annot@start}%
        }%
      \fi%
    \else%
      \ifx\@annot@end@print\relax%
        \def\@annot@start@print{%
          \l@wrapcs@ifnotemptybox{Xendwraplinenumannotation@\@currentseries}{%
            \ifx\annot@start\empty\else%
              \annot@start%
              \ifdefined\linerangesep@%
                \linerangesep@%
              \else%
                \csuse{Xendlinerangeseparator@\@currentseries}%
              \fi%
            \fi%
            \annot@end%
            }%
          }%
       \else%
         \let\@annot@start@print\@annot@end@print%
         \let\@annot@end@print\relax%
      \fi%
    \fi%
  \fi%
  \l@d@ssubfalse
  \ifnum#3=0 \else
      \l@d@ssubtrue
  \fi
  \l@d@eslfalse
  \ifnum#6=0 \else
      \ifnum#6=#3
         \ifl@d@elin \l@d@esltrue \else \l@d@eslfalse \fi
      \else
         \l@d@esltrue
         \l@d@dashtrue
      \fi
  \fi%
  \ifl@d@dash%
    \ifboolexpr{togl{fulllines@} or test{\ifcsempty{Xendtwolines@\@currentseries}}}%
      {}%
      {%
      \setistwofollowinglines{#1}{#2}{#4}{#5}%
        \ifboolexpr{%
          (%
               togl {Xendtwolinesbutnotmore@\@currentseries}%
               and not%
                 (%
                  bool {istwofollowinglines@}%
                 )%
          )%
          or%
          (%
             (not test{\ifnumequal{#1}{#4}})%
               and togl{Xendtwolinesonlyinsamepage@\@currentseries}%
             )%
          }%
          {}%
          {%
          \l@d@dashfalse%
          \l@d@Xtwolinestrue%
          \l@d@elinfalse%
          \l@d@eslfalse%
          \ifcsempty{Xendmorethantwolines@\@currentseries}%
            {}%
            {\ifistwofollowinglines@\else%
              \l@d@Xmorethantwolinestrue%
             \fi%
            }%
          }%
      }%
  \fi%
  \iftoggle{Xendnoidenticallinenumannotation@\@currentseries}{%
    \ifx\annot@start\annot@end%
      \let\@annot@end@print\relax%
      \ifboolexpr{%Do not print the dash if we do not print the line number
        test{\ifdefempty{\linenumrep}}%
        or not bool {l@d@Xendlinenumber}%
      }{%
        \l@d@dashfalse%
      }{}%
    \fi%
  }{}%
  \iftoggle{Xendlinenumannotationonlyfirst@\@currentseries}{%
    \ifboolexpr{%
      (%
        togl{Xendlinenumannotationonlyfirstintwo@\@currentseries}%
        and test{\ifcsequal{annot@start}{prevannot@start@\@currentseries}}%
        and test{\ifcsequal{annot@end}{prevannot@end@\@currentseries}}%
      )%
      or%
      (%
        not togl{Xendlinenumannotationonlyfirstintwo@\@currentseries}%
        test{\ifcsequal{annot@start}{prevannot@start@\@currentseries}}%
      )%
    }{%
      \def\@annot@start@print{%
        \l@wrapcs@ifnotemptybox{Xendwraplinenumannotation@\@currentseries}{\csuse{Xendsymlinenumannotation@\@currentseries}}%
      }%
      \let\@annot@end@print\relax%
      \ifboolexpr{%Do not print the dash if do not print the line number
        test{\ifdefempty{\linenumrep}}%
        or not bool {l@d@Xendlinenumber}%
      }{%
        \l@d@dashfalse%
      }{}%
    }{}%
    \global\cslet{prevannot@start@\@currentseries}{\annot@start}%
    \global\cslet{prevannot@end@\@currentseries}{\annot@end}%
  }%
  {}%
}%
\def\printendlines#1|#2|#3|#4|#5|#6|#7|#8|{%
  \begingroup
  \setprintendlines{#1}{#2}{#3}{#4}{#5}{#6}%
  \ifdimequal{\csuse{Xendboxstartlinenum@\@currentseries}}{0pt}%
    {\bgroup}%
    {\leavevmode\hbox to \csuse{Xendboxstartlinenum@\@currentseries}\bgroup\hfill}%
  \ifboolexpr{%
    (%
      test{\ifcsstring{prevpagenum@\@currentseries}{#1}}%
        and not%
      (togl{Xendpagenumberonlyfirstifsingle@\@currentseries} and bool{l@d@pnum})%
    )%
    or%
    (%
    test {\ifcsstring{prevpagerange@\@currentseries}{#1-#4}}%
    )%
  }%
    {%
    \ifcsempty{Xendsympagenum@\@currentseries}%
      {\hspace{\csuse{Xendinplaceofpagenumber@\@currentseries}}}%
      {\csuse{Xendsympagenum@\@currentseries}}%
    }%
    {%
    \wrap@edcrossref{\@this@crossref@start}{\printnpnum{#1}}%
    }%
  \ifl@d@dash%
    \ifl@d@pnum%
      \csuse{Xendlineprefixsingle@\@currentseries}%
    \else%
      \ifcsempty{Xendlineprefixmore@\@currentseries}%
        {\csuse{Xendlineprefixsingle@\@currentseries}}%
        {\csuse{Xendlineprefixmore@\@currentseries}}%
    \fi%
  \else%
    \csuse{Xendlineprefixsingle@\@currentseries}%
  \fi%
  \ifcsstring{Xendlinenumannotationposition@\@currentseries}{before}%
    {\@annot@start@print}%
    {}%
  \ifl@d@Xendlinenumber%
    \wrap@edcrossref{\@this@crossref@start}{%
      \ifledRcol@%
        \linenumrepR{#2}%
      \else%
        \linenumrep{#2}%
      \fi%
    }%
  \fi%
  \iftoggle{Xendlineflag@\@currentseries}{\ifledRcol@\@Rlineflag\fi}{}%
  \ifl@d@Xendlinenumber%
    \ifl@d@ssub%
      \csuse{Xendsublinesep@\@currentseries}%
      \wrap@edcrossref{\@this@crossref@start}{%
        \ifledRcol@%
          \sublinenumrepR{#3}%
        \else%
          \sublinenumrep{#3}%
        \fi%
      }%
    \fi%
  \fi%
  \ifcsstring{Xendlinenumannotationposition@\@currentseries}{after}%
    {\@annot@start@print}%
    {}%
  \egroup%
  \ifdimequal{\csuse{Xendboxendlinenum@\@currentseries}}{0pt}%
    {\bgroup}%
    {\hbox to \csuse{Xendboxendlinenum@\@currentseries}\bgroup}%
  \ifl@d@Xtwolines%
    \ifl@d@Xendlinenumber%
      \ifl@d@Xmorethantwolines%
        \csuse{Xendmorethantwolines@\@currentseries}%
      \else%
        \csuse{Xendtwolines@\@currentseries}%
      \fi%
    \fi%
  \else%
    \ifl@d@dash%
      \ifdefined\linerangesep@%
        \linerangesep@%
      \else%
        \csuse{Xendlinerangeseparator@\@currentseries}%
      \fi%
    \fi%
    \ifl@d@pnum%
     \ifcsstring{prevpagerange@\@currentseries}{#1-#4}%
       {%
       \ifcsempty{Xendsympagenum@\@currentseries}%
         {\hspace{\csuse{Xendinplaceofpagenumber@\@currentseries}}}%
         {\csuse{Xendsympagenum@\@currentseries}}%
       }%
       {%
       \wrap@edcrossref{\@this@crossref@end}{\printnpnum{#4}}%
       }%
    \fi%
    \ifcsstring{Xendlinenumannotationposition@\@currentseries}{before}%
      {\@annot@end@print}%
      {}%
    \ifl@d@Xendlinenumber%
      \ifl@d@elin%
        \ifl@d@pnum\csuse{Xendlineprefixsingle@\@currentseries}\fi%
        \wrap@edcrossref{\@this@crossref@end}{%
          \ifledRcol@%
            \linenumrepR{#5}%
          \else%
            \linenumrep{#5}%
          \fi%
        }%
        \iftoggle{Xendlineflag@\@currentseries}{\ifledRcol@\@Rlineflag\fi}{}%
      \fi%
      \ifl@d@esl%
        \ifl@d@elin%
          \csuse{Xendsublinesep@\@currentseries}%
        \fi%
        \wrap@edcrossref{\@this@crossref@end}{%
          \ifledRcol@%
            \sublinenumrepR{#6}%
          \else%
            \sublinenumrep{#6}%
          \fi%
        }%
    \fi%
    \fi%
    \ifcsstring{Xendlinenumannotationposition@\@currentseries}{after}%
      {\@annot@end@print}%
      {}%
  \fi%
  \ifdimequal{\csuse{Xendboxendlinenum@\@currentseries}}{0pt}%
    {}%
    {\hfill}%Prevent underfull hbox
  \egroup%
  \iftoggle{Xendpagenumberonlyfirst@\@currentseries}%
    {\iftoggle{Xendpagenumberonlyfirstintwo@\@currentseries}%
      {\csxdef{prevpagerange@\@currentseries}{#1-#4}}%
      {\csxdef{prevpagenum@\@currentseries}{#4}}%
    }%
    {}%
  \endgroup%
}%

\newcommand*{\printnpnum}[1]{\csuse{Xendbeforepagenumber@\@currentseries}#1\csuse{Xendafterpagenumber@\@currentseries}}

\newcommand{\newseries}[1]{%
    \def\do##1{\newseries@{##1}}%
    \docsvlist{#1}
}
\newcommand{\@series}{}
\newcommand{\newseries@}[1]{
    \xifinlist{#1}{\@series}{\led@warn@SeriesStillExist{#1}}%
    {%
      \ifdefined\newseries@par%
        \newseries@par{#1}%
      \fi%
    \unless\ifnocritical@
      \newtoggle{Xlineflag@#1}
      \newtoggle{Xparindent@#1}
      \newtoggle{Xlemmadisablefontselection@#1}
      \csgdef{Xwrapcontent@#1}{}%
      \csgdef{Xbeforeinserting@#1}{}%
      \csgdef{Xhangindent@#1}{0pt}%
      \csgdef{Xragged@#1}{}%
      \csgdef{Xhsizetwocol@#1}{0.45 \hsize}%
      \csgdef{Xhsizethreecol@#1}{.3 \hsize}%
      \csgdef{Xcolalign@#1}{\raggedright}%
      \csgdef{Xnotenumfont@#1}{\normalfont}%
      \csgdef{Xnotefontsize@#1}{\footnotesize}%
      \csgdef{Xbhooknote@#1}{}%
      \csgdef{Xbhookgroup@#1}{}%

      \csgdef{Xboxlinenum@#1}{0pt}%
      \csgdef{Xboxlinenumalign@#1}{L}%

      \csgdef{Xboxstartlinenum@#1}{0pt}%
      \csgdef{Xboxendlinenum@#1}{0pt}%

      \csgdef{Xboxsymlinenum@#1}{0pt}%
      \newtoggle{Xgroupbyline@#1}%
      \newtoggle{Xgroupbylineseparetwolines@#1}%

      \newtoggle{Xnumberonlyfirstinline@#1}%
      \newtoggle{Xnumberonlyfirstintwolines@#1}%

      \newtoggle{Xlinenumannotationonlyfirst@#1}%
      \newtoggle{Xlinenumannotationonlyfirstintwo@#1}%

      \csgdef{Xtwolines@#1}{}%
      \csgdef{Xmorethantwolines@#1}{}%
      \csgdef{Xsublinesep@#1}{\fullstop}%
      \csgdef{Xpagelinesep@#1}{\csname Xsublinesep@#1\endcsname}%for backward compatibility, call Xsublinesep@#1
      \newtoggle{Xtwolinesbutnotmore@#1}%
      \newtoggle{Xtwolinesonlyinsamepage@#1}%
      \newtoggle{Xonlypstart@#1}%
      \newtoggle{Xpstarteverytime@#1}%

      \newtoggle{Xpstart@#1}%
      \newtoggle{Xpstartonlyfirst@#1}%
      \csdef{Xprevpstart@#1}{0}%Not a hook, but a tool for the hook Xpstartonlyfirst
      \csgdef{Xpstartseparator@#1}{}%

      \newtoggle{Xstanza@#1}%
      \newtoggle{Xstanzaonlyfirst@#1}%
      \csdef{Xprevstanza@#1}{0}%Not a hook, but a tool for the hook Xstanzaonlyfirst

      \csgdef{Xstanzaseparator@#1}{}%

      \csgdef{Xsymlinenum@#1}{}%
      \csgdef{Xsymlinenumannotation@#1}{}%

      \newtoggle{Xnonote@#1}%

      \newtoggle{Xnonumber@#1}%
      \newtoggle{Xnolinenumber@#1}%
      \newtoggle{Xnolinenumberifannotation@#1}%
      \csgdef{Xbeforenumber@#1}{0pt}%
      \csgdef{Xtxtbeforenumber@#1}{}%
      \csgdef{Xafternumber@#1}{0.5em}%
      \newtoggle{Xnonbreakableafternumber@#1}%
      \csgdef{Xbeforesymlinenum@#1}{\csuse{Xbeforenumber@#1}}%
      \csgdef{Xaftersymlinenum@#1}{\csuse{Xafternumber@#1}}%
      \csgdef{Xinplaceofnumber@#1}{1em}%
      \global\cslet{Xlemmaseparator@#1}{\rbracket}%
      \csgdef{Xbeforelemmaseparator@#1}{0em}%
      \csgdef{Xafterlemmaseparator@#1}{0.5em}%
      \csgdef{Xinplaceoflemmaseparator@#1}{1em}%
      \csgdef{Xbeforenotes@#1}{1.2em \@plus .6em \@minus .6em}
      \csgdef{Xafterrule@#1}{0pt}

      \csgdef{Xtxtbeforenotes@#1}{}
      \newtoggle{Xtxtbeforesnotes@#1@typeset}%Not directly used by user, but internal
      \newtoggle{Xtxtbeforenotesonlyonce@#1}%

      \csgdef{Xmaxhnotes@#1}{0.8\vsize}
      \newtoggle{Xnoteswidthliketwocolumns@#1}%
      \csgdef{Xparafootsep@#1}{}%
      \csgdef{Xafternote@#1}{1em plus.4em minus.4em}
      \csgdef{Xlinerangeseparator@#1}{\endashchar}%

      \csgdef{Xlemmafont@#1}{}%
      \csgdef{Xwraplemma@#1}{}
      \csgdef{Xwidth@#1}{\hsize}%
      \csgdef{Xwraplinenumannotation@#1}{\textsuperscript}%
      \csgdef{Xlinenumannotationposition@#1}{after}%
      \expandafter\csgdef{Xinnotemark@#1}##1{\ledinnotemark{##1}}%
      \newtoggle{Xnoidenticallinenumannotation@#1}%
      \expandafter\newinsert\csname #1footins\endcsname%
      \unless\ifnoledgroup@%
        \expandafter\newinsert\csname mp#1footins\endcsname%
      \fi%
      \global\notbool{parapparatus@}{\expandafter\newcommand\expandafter *}{\expandafter\newcommand}\csname #1footnote\endcsname[2][]{%
          \nottoggle{Xnonote@#1}{%
          \if@edtext@secondarg@%
            \ifledRcol%
              \ifcsstring{Xonlyside@#1}{L}{\led@error@note@called@onrightside{#1footnote}}{}%
            \else%
              \ifcsstring{Xonlyside@#1}{R}{\led@error@note@called@onleftside{#1footnote}}{}%
            \fi%
            \begingroup%
            \newcommand{\content}{##2}%
            \ifnumberedpar@%
                \ifledRcol%
                   \ifluatex%
                       \footnotelang@lua[R]%
                   \fi%
                   \@ifundefined{xpg@main@language}%if polyglossia
                         {}%
                         {\footnotelang@poly[R]}%
                    \footnoteoptions@{R}{##1}{true}%
                    \xright@appenditem{%
                      \ifbool{indtl@innote}%
                        {\unexpanded{\let\index\nindex}}%
                        {}%
                      \ifbool{indtl@notenumber}%
                        {\unexpanded{\let\index\nindex}}%There is no note number... so
                        {}%
                      \noexpand\Xnote@true%
                      \unexpanded{\gdef\@currentnotetype{critical}}%
                      \noexpand\prepare@Xprenotes{#1}%
                      \noexpand\prepare@edindex@fornote{\l@d@nums}%
                      \unexpanded{\def\sw@list@inedtext}{\expandafter\unexpanded\expandafter{\sw@inthisedtext}}%The value of the \sw@inthisedtext of current \edtext will be pushed to  \sw@list@inedtext when the notes are expanded.
                      \noexpand\setcounter{stanzaR}{\the\c@stanzaR}%Save stanzaR counter for footnote
                      \unexpanded{\def\@this@crossref@start}{\theedtext:start}%
                      \unexpanded{\def\@this@crossref@end}{\theedtext:end}%
                      \expandonce{\@beforeinsertofthisedtext}% Internal for now, no reason to make it public
                      \noexpand\parse@annot\l@current@annotR|%
                      \noexpand\csuse{v#1footnote}{#1}%
                       {{\l@d@nums}{\expandonce\@tag}{\expandonce\content}}%
                      \noexpand\Xnote@false%
                      \unexpanded{\advance\@edindex@fornote@\m@ne}%
                      \ifbool{indtl@innote}%
                        {\unexpanded{\let\index\orig@@index}}%
                        {}%
                      \ifbool{indtl@notenumber}%
                        {\unexpanded{\let\index\orig@@index}}%
                        {}%
                    }\to\inserts@listR
                    \footnoteoptions@{R}{##1}{false}%
                    \global\advance\insert@countR \@ne%
                \else%
                     \ifluatex%
                       \footnotelang@lua%
                     \fi%
                   \@ifundefined{xpg@main@language}%if polyglossia
                         {}%
                         {\footnotelang@poly}%
                    \footnoteoptions@{L}{##1}{true}%
                    \xright@appenditem{%
                      \ifbool{indtl@innote}%
                        {\unexpanded{\let\index\nindex}}%
                        {}%
                      \ifbool{indtl@notenumber}%
                        {\unexpanded{\let\index\nindex}}%There is no note number... so
                        {}%
                      \noexpand\Xnote@true%
                      \unexpanded{\gdef\@currentnotetype{critical}}%
                      \noexpand\prepare@Xprenotes{#1}%
                      \noexpand\prepare@edindex@fornote{\l@d@nums}%
                      \unexpanded{\def\sw@list@inedtext}{\expandafter\unexpanded\expandafter{\sw@inthisedtext}}%The value of the \sw@inthisedtext of current edtext will be pushed to \sw@list@inedtext when the notes are expanded.
                      \ifl@dpairing%
                        \noexpand\setcounter{stanzaL}{\the\c@stanzaL}%Save stanzaR counter for footnote
                      \fi%
                      \unexpanded{\def\@this@crossref@start}{\theedtext:start}%
                      \unexpanded{\def\@this@crossref@end}{\theedtext:end}%
                      \expandonce{\@beforeinsertofthisedtext}%Internal for now, no reason to make it public
                      \noexpand\parse@annot\l@current@annot|%
                      \noexpand\csuse{v#1footnote}%
                         {#1}%
                         {{\l@d@nums}{\expandonce\@tag}{\expandonce\content}}%
                      \noexpand\Xnote@false%
                      \unexpanded{\advance\@edindex@fornote@\m@ne}%
                      \ifbool{indtl@innote}%
                        {\unexpanded{\let\index\orig@@index}}%
                        {}%
                      \ifbool{indtl@notenumber}%
                        {\unexpanded{\let\index\orig@@index}}%
                        {}%
                     }\to\inserts@list
                    \global\advance\insert@count \@ne%
                    \footnoteoptions@{L}{##1}{false}%
                \fi
            \else
                \csuse{v#1footnote}{#1}{{0|0|0|0|0|0|0}{}{##1}}%
            \fi%
            \endgroup%
          \else%
            \led@err@FootnoteNotInSecondArgEdtext{#1}%
          \fi%
        }{\@noneed@Footnotetrue}% End of \nottoggle{Xnonote@#1}{%
   \ignorespaces%
          }
      \expandafter\newcount\csname #1prevpage@num\endcsname%
      \expandafter\newcount\csname #1prevpage@numR\endcsname%
      \global\csletcs{#1@@footnote}{#1footnote}
      \Xarrangement@normal{#1}%
    \fi
    \unless\ifnofamiliar@
      \newtoggle{parindentX@#1}
      \csgdef{wrapcontentX@#1}{}%
      \csgdef{hangindentX@#1}{0pt}%
      \csgdef{beforeinsertingX@#1}{}%
      \csgdef{raggedX@#1}{}%
      \csgdef{hsizetwocolX@#1}{0.45 \hsize}%
      \csgdef{hsizethreecolX@#1}{.3 \hsize}%
      \csgdef{colalignX@#1}{\raggedright}%
      \csgdef{notenumfontX@#1}{\normalfont}%
      \csgdef{notefontsizeX@#1}{\footnotesize}%
      \csgdef{bhooknoteX@#1}{}%
      \csgdef{bhookgroupX@#1}{}%
      \csgdef{afterruleX@#1}{0pt}
      \csgdef{beforenotesX@#1}{1.2em \@plus .6em \@minus .6em}
      \csgdef{maxhnotesX@#1}{0.8\vsize}%
      \newtoggle{noteswidthliketwocolumnsX@#1}%
      \csgdef{parafootsepX@#1}{}%
      \csgdef{afternoteX@#1}{1em plus.4em minus.4em}
      \csgdef{widthX@#1}{\hsize}%
      \expandafter\csgdef{innotemarkX@#1}##1{\ledinnotemark{##1}}%
      \csgdef{txtbeforenotesX@#1}{}%
      \newtoggle{nonoteX@#1}%
      \newtoggle{txtbeforesnotesX@#1@typeset}%Not directly used by user, but internal
      \newtoggle{txtbeforenotesonlyonceX@#1}%
      \expandafter\newinsert\csname footins#1\endcsname%
      \unless\ifnoledgroup@%
        \expandafter\newinsert\csname mpfootins#1\endcsname%
      \fi%

      \global\expandafter\newcommand\csname footnote#1\endcsname[2][]{%
        \nottoggle{nonoteX@#1}{%
          \begingroup%
              \prepare@prenotesX{#1}%
              \newcommand{\content}{##2}%
              \ifdefined\csq@qlevel%
                \csq@qlevel=0\relax%
              \fi%
               \global\expandafter\advance\csname footnote#1@reading\endcsname by \@ne%
               \ifstrempty{##1}{%
                 \ifboolexpr{bool{l@dpairing} or bool{l@dprintingpages} or bool{l@dprintingcolumns}}{%
                   \ifcsdef{footnote#1reading\the\csname footnote#1@reading\endcsname=typeset}%
                   {\setcounter{footnote#1}{\csuse{footnote#1reading\the\csname footnote#1@reading\endcsname=typeset}}}%
                   {\setcounter{footnote#1}{\the\csname footnote#1@reading\endcsname}}%
                 }{%
                   \stepcounter{footnote#1}%
                 }%
               }{}%
            \ifledRcol%
              \ifcsstring{onlysideX@#1}{L}{\led@error@note@called@onrightside{footnote#1}}{}%
            \else%
              \ifcsstring{onlysideX@#1}{R}{\led@error@note@called@onleftside{footnote#1}}{}%
            \fi%
              \ifstrempty{##1}%
                {\protected@csxdef{@thefnmark#1}{\csuse{thefootnote#1}}}%
                {\protected@csxdef{@thefnmark#1}{##1}}%
              \nottoggle{nomk@}%Nomk is set to true when using \footnoteXnomk with \parpackage
                 {\csuse{@footnotemark#1}}%
                 {}%
              \ifluatex%
                \xdef\footnote@luatextextdir{\the\textdir}%
                \xdef\footnote@luatexpardir{\the\pardir}%
              \fi%
              \if@ledgroup%
                \led@set@index@fornote{#1}%
              \fi%
              \csuse{vfootnote#1}{#1}{\expandonce\content}{##1}\m@mmf@prepare%
              \ifbool{indtl@innote}%
                {\let\index\orig@@index}%
                {}%
              \ifbool{indtl@notenumber}%
                {\let\index\orig@@index}%
                {}%
          \endgroup%
        }{}%End of the notoggleX@#1
      }
      \newcounter{footnote#1}
        \global\expandafter\renewcommand\csname thefootnote#1\endcsname{\arabic{footnote#1}}
      \expandafter\newcount\csname footnote#1@reading\endcsname%
      \expandafter\newcount\csname prevpage#1@num\endcsname%
      \expandafter\newcount\csname prevpage#1@numR\endcsname%
      \expandafter\newcount\csname footnote#1@parafootsepX\endcsname%For the case we use \parafootsepX outside of numbered section
      \expandafter\gappto\expandafter\no@expands\expandafter{\expandafter\let\csname footnote#1\endcsname\@gobble}%
      \expandafter\newcommand\csname footnote#1mark\endcsname[1][]{%
         \begingroup%
            \prepare@prenotesX{#1}%
            \ifstrempty{##1}{%
              \stepcounter{footnote#1}%
            }{%
              \setcounter{footnote#1}{##1}%
            }%
            \protected@csxdef{@thefnmark#1}{\csuse{thefootnote#1}}%
            \csuse{@footnotemark#1}%
            \m@mmf@prepare%
         \endgroup%
      }%
      \expandafter\newcommand\csname footnote#1text\endcsname[2][]{%
         \begingroup%
           \csuse{vfootnote#1}{#1}{\expandonce{##2}}{##1}%
         \endgroup%
      }%
     \arrangementX@normal{#1}%
  \fi
  \unless\ifnoend@
      \expandafter\newwrite\csname l@d@#1end\endcsname%
      \expandafter\newif\csname ifl@dend@#1\endcsname%

      \global\expandafter\newcommandx\csname #1endnote\endcsname[2][1,usedefault]{%
         \bgroup%
         \newlinechar='40%
         \global\@noneed@Footnotetrue%
         \newcommand{\content}{##2}%
         \stepcounter{labidx}%
         \expandafter\immediate\expandafter\write\csname l@d@#1end\endcsname{%
           \unexpanded{\def\sw@list@inedtext}%
           {\expandafter\unexpanded\expandafter{\sw@inthisedtext}}%
           \@percentchar\space%Explicit space, to add a linebreak in the output file
           \noexpand\parse@annot\l@current@annot|\@percentchar\space%
           \expandafter\string\csname #1end\endcsname%
           {\ifnumberedpar@\l@d@nums\fi}%
           {\ifnumberedpar@\expandonce\@tag\fi}%
           {\expandonce\content}%
           {#1}%
           {\unexpanded{##1}}%
           {\ifledRcol R\else L\fi}%
           {\theedtext}%
           \@percentchar%
         }%
         \egroup%
         \ignorespaces%
     }%

     \global\expandafter\newcommand\csname #1toendnotes\endcsname[1]{%
       \toendnotes[#1]{##1}%
     }%

     \expandafter\WithSuffix\expandafter\newcommand\csname #1toendnotes\endcsname*[1]{%
       \toendnotes*[#1]{##1}%
     }%


      \global\cslet{#1end}{\@gobbleseven}
   \global\expandafter\newcount\csname #1end@bysection\endcsname%
      \csgdef{Xendwraplemma@#1}{}
      \csgdef{Xendwrapcontent@#1}{}%
      \csgdef{Xendtwolines@#1}{}%
      \csgdef{Xendmorethantwolines@#1}{}%
      \newtoggle{Xendtwolinesbutnotmore@#1}{}%
      \newtoggle{Xendtwolinesonlyinsamepage@#1}{}%
      \newtoggle{Xendlemmadisablefontselection@#1}%
      \csgdef{Xendnotenumfont@#1}{\normalfont}%
      \csgdef{Xendnotefontsize@#1}{\footnotesize}%
      \csgdef{Xendbhooknote@#1}{}%

      \csgdef{Xendsublinesep@#1}{\fullstop}%

      \csgdef{Xendbeforenumber@#1}{0pt}
      \csgdef{Xendafternumber@#1}{0.5em}

      \csgdef{Xendboxlinenum@#1}{0pt}%
      \csgdef{Xendboxlinenumalign@#1}{L}%

      \csgdef{Xendboxstartlinenum@#1}{0pt}%
      \csgdef{Xendboxendlinenum@#1}{0pt}%

      \csgdef{Xendlemmaseparator@#1}{}%
      \csgdef{Xendbeforelemmaseparator@#1}{0em}%
      \csgdef{Xendafterlemmaseparator@#1}{0.5em}%
      \csgdef{Xendinplaceoflemmaseparator@#1}{0.5em}%

      \newtoggle{Xendparagraph@#1}%
      \csgdef{Xendafternote@#1}{1em plus.4em minus.4em}%
      \csgdef{Xendsep@#1}{}%

      \csgdef{Xendinplaceofnumber@#1}{0pt}%
      \newtoggle{Xendnonumber@#1}%
      \newtoggle{Xendnolinenumber@#1}%
      \newtoggle{Xendnolinenumberifannotation@#1}%

      \csgdef{Xendhangindent@#1}{0pt}%

      \newtoggle{Xendnumberonlyfirstinline@#1}%
      \newtoggle{Xendnumberonlyfirstintwolines@#1}%

      \newtoggle{Xendlinenumannotationonlyfirst@#1}%
      \newtoggle{Xendlinenumannotationonlyfirstintwo@#1}%

      \csgdef{Xendbeforesymlinenum@#1}{\csuse{Xendbeforenumber@#1}}%
      \csgdef{Xendaftersymlinenum@#1}{\csuse{Xendafternumber@#1}}%

      \csgdef{Xendsymlinenum@#1}{}%
      \csgdef{Xendsymlinenumannotation@#1}{}%

      \csgdef{Xendboxsymlinenum@#1}{0pt}%

      \csgdef{Xendbhooklinenumber@#1}{}%
      \csgdef{Xendehooklinenumber@#1}{}%
      \csgdef{Xendbhookinplaceofnumber@#1}{}%
      \csgdef{Xendehookinplaceofnumber@#1}{}%

      \csgdef{Xendlinerangeseparator@#1}{\endashchar}%

      \csgdef{Xendbeforepagenumber@#1}{p.}%
      \csgdef{Xendafterpagenumber@#1}{) }%
      \csgdef{Xendlineprefixsingle@#1}{}%
      \csgdef{Xendlineprefixmore@#1}{}%

      \newtoggle{Xendlineflag@#1}

      \csgdef{Xendlemmafont@#1}{}%

      \csgdef{Xendlinenumannotationposition@#1}{after}%
      \csgdef{Xendwraplinenumannotation@#1}{\textsuperscript}%
      \newtoggle{Xendnoidenticallinenumannotation@#1}%

      \newtoggle{Xendpagenumberonlyfirst@#1}%
      \newtoggle{Xendpagenumberonlyfirstifsingle@#1}%
      \newtoggle{Xendpagenumberonlyfirstintwo@#1}%
      \csgdef{Xendsympagenum@#1}{}%
      \csgdef{Xendinplaceofpagenumber@#1}{0pt}%

      \csgdef{Xendtxtbeforenotes@#1}{}%
      \newtoggle{Xendfirstnote@#1}%Not a hook, but used to apply Xendtxtbeforenotes

  \fi%
    \listxadd{\@series}{#1}
  }
}% End of \newseries
\expandafter\newseries\expandafter{\default@series}
\newcommand{\seriesatbegin}[1]{%
   \StrDel{\@series}{#1}[\@series]%
   \edef\@new{}%
   \listeadd{\@new}{#1}%
   \listeadd{\@new}{\@series}%
   \xdef\@series{\@new}%
}
\newcommand{\seriesatend}[1]{%
   \StrDel{\@series}{#1}[\@series]%
   \edef\@new{}%
   \listeadd{\@new}{\@series}%
   \listeadd{\@new}{#1}%
   \xdef\@series{\@new}%
}
\newcommand{\ifseriesbefore}[4]{%
 \StrPosition{\@series}{#1}[\@first]%
 \StrPosition{\@series}{#2}[\@second]%
 \ifnumgreater{\@second}{\@first}{#3}{#4}%
}
\newcommand{\@getfirstseries}{%
 \ifdefempty{\@series}%
   {\xdef\@firstseries{}}%
   {\StrChar{\@series}{1}[\@firstseries]}%
}%
\newcommandx{\settoggle@series}[5][4,5,usedefault]{%
    \def\do##1{%
      \ifcsdef{etb@tgl@#2@##1}{%
        \global\settoggle{#2@##1}{#3}%
      }{%
        \led@warn@BadHookSeries{##1}{#2}%
      }%
      \ifstrequal{#4}{critical}{
          \csuse{Xarrangement@\csuse{series@display##1}}{##1}%
      }{}
      \ifstrequal{#4}{familiar}{%
          \csuse{arrangementX@\csuse{series@displayX##1}}{##1}%
      }{}%
    }%
    \ifstrempty{#1}{%
              \dolistloop{\@series}%
              \ifstrempty{#5}{}{%
                 \docsvlist{#5}%
               }
           }%
           {%
              \docsvlist{#1}%
           }%
}
\newcommandx{\setcommand@series}[6][4,5,6=0,usedefault]{%
    \def\do##1{
      \ifcsdef{#2@##1}{%
        \expandafter\global\expandafter\renewcommand\csname#2@##1\endcsname[#6]{#3}%
      }{%
        \led@warn@BadHookSeries{##1}{#2}%
      }%
      \ifstrequal{#4}{critical}{%
            \csuse{Xarrangement@\csuse{series@display##1}}{##1}%
      }{}
      \ifstrequal{#4}{familiar}{%
          \csuse{arrangementX@\csuse{series@displayX##1}}{##1}%
      }{}%
    }%
    \ifstrempty{#1}{%
              \dolistloop{\@series}%
              \ifstrempty{#5}{}{%
                 \docsvlist{#5}
               }
    }%
    {%
              \docsvlist{#1}%
    }%
}%
\newcommandx{\newhookcommand@series}[3][2,3=0,usedefault]{%
  \global\expandafter\newcommand\expandafter*\csname #1\endcsname[2][]{%
    \setcommand@series{##1}{#1}{##2}[][#2][#3]%
  }%
  \ifstrempty{#2}{}{%
    \def\do##1{%
      \global\expandafter\newcommand\expandafter*\csname #1##1\endcsname[1]{%
        \csuse{#1}[##1]{####1}%
      }%
    }%
    \docsvlist{#2}%
  }%
}
\newcommandx{\newhooktoggle@series}[2][2,usedefault]{%
  \global\expandafter\newcommandx\expandafter*\csname #1\endcsname[2][1,2={true},usedefault]{%
    \settoggle@series{##1}{#1}{##2}[][#2]%
    }%
  \ifstrempty{#2}{}{%
    \def\do##1{%
      \global\expandafter\newcommand\expandafter*\csname #1##1\endcsname{%
        \csuse{#1}[##1]%
      }%
    }%
    \docsvlist{#2}%
  }%
}
\newcommand{\newhooktoggle@series@reload}[2]{%
  \global\expandafter\newcommandx\expandafter*\csname #1\endcsname[2][1,2={true},usedefault]{%
    \settoggle@series{##1}{#1}{##2}[#2]%
    }%
}%
\newcommand{\newhookcommand@series@reload}[2]{%
  \global\expandafter\newcommand\expandafter*\csname #1\endcsname[2][]{%
    \setcommand@series{##1}{#1}{##2}[#2]%
  }%
}
\unless\ifnocritical@
  \newhookcommand@series{Xwrapcontent}%
  \newhookcommand@series{Xbeforeinserting}%
  \newhookcommand@series{Xlemmafont}%
  \newhookcommand@series{Xwraplemma}%
  \newhooktoggle@series{Xparindent}
  \newhookcommand@series{Xhangindent}
  \newhookcommand@series{Xragged}
  \newhookcommand@series{Xhsizetwocol}
  \newhookcommand@series{Xhsizethreecol}
  \newhookcommand@series{Xcolalign}%
  \newhookcommand@series{Xnotenumfont}
  \newhookcommand@series{Xbhooknote}
  \newhookcommand@series@reload{Xbhookgroup}{critical}
  \newhookcommand@series{Xboxsymlinenum}%
  \newhookcommand@series{Xsymlinenum}
  \newhookcommand@series{Xsymlinenumannotation}%
  \newhookcommand@series{Xbeforenumber}
  \newhookcommand@series{Xtxtbeforenumber}
  \newhookcommand@series{Xafternumber}
  \newhookcommand@series{Xbeforesymlinenum}
  \newhookcommand@series{Xaftersymlinenum}
  \newhookcommand@series{Xinplaceofnumber}
  \newhookcommand@series{Xlemmaseparator}
  \newhookcommand@series{Xbeforelemmaseparator}
  \newhookcommand@series{Xafterlemmaseparator}
  \newhookcommand@series{Xinplaceoflemmaseparator}
  \newhookcommand@series{Xtxtbeforenotes}
  \newhooktoggle@series{Xtxtbeforenotesonlyonce}%
  \newhookcommand@series@reload{Xafterrule}{critical}
  \newhooktoggle@series{Xnumberonlyfirstinline}
  \newhooktoggle@series{Xnumberonlyfirstintwolines}
  \newhooktoggle@series{Xlinenumannotationonlyfirst}%
  \newhooktoggle@series{Xlinenumannotationonlyfirstintwo}%
  \newhooktoggle@series{Xgroupbyline}%
  \newhooktoggle@series{Xgroupbylineseparetwolines}%
  \newhooktoggle@series{Xnonumber}
  \newhooktoggle@series{Xnolinenumber}%
  \newhooktoggle@series{Xnolinenumberifannotation}%

  \newhooktoggle@series{Xpstart}
  \newhooktoggle@series{Xpstartonlyfirst}%
  \newhooktoggle@series{Xpstarteverytime}%
  \newhookcommand@series{Xpstartseparator}%

  \newhooktoggle@series{Xstanza}%
  \newhooktoggle@series{Xstanzaonlyfirst}%
  \newhookcommand@series{Xstanzaseparator}%

  \newhooktoggle@series{Xnonote}%

  \newhooktoggle@series{Xonlypstart}
  \newhooktoggle@series{Xnonbreakableafternumber}
  \newhooktoggle@series{Xlemmadisablefontselection}
  \newhookcommand@series@reload{Xmaxhnotes}{critical}
  \newhookcommand@series@reload{Xbeforenotes}{critical}
  \newhooktoggle@series@reload{Xnoteswidthliketwocolumns}{critical}%
  \newhookcommand@series@reload{Xnotefontsize}{critical}

  \newhookcommand@series{Xboxlinenum}%
  \newhookcommand@series{Xboxlinenumalign}%

  \newhookcommand@series{Xboxstartlinenum}%
  \newhookcommand@series{Xboxendlinenum}%

  \newhookcommand@series{Xafternote}%
  \newhookcommand@series{Xparafootsep}
  \newhookcommand@series@reload{Xwidth}{critical}%

  \newhookcommand@series{Xinnotemark}[][1]%

  \ifundef{\Xhsize}%
    {%
      \newcommandx{\Xhsize}[2][1,usedefault]{%
        \led@warning@Xhsize@deprecated%
        \Xwidth[#1]{#2}%
      }%
    }%
    {}%
\fi
\newhooktoggle@series{Xlineflag}[appref,SEref]
\newhookcommand@series{Xtwolines}[appref,SEref]
\newhookcommand@series{Xmorethantwolines}[appref,SEref]
\newhookcommand@series{Xsublinesep}[appref,SEref,side]%
\newhookcommand@series{Xpagelinesep}[appref,SEref,side]%
\newhooktoggle@series{Xtwolinesbutnotmore}[appref,SEref]
\newhooktoggle@series{Xtwolinesonlyinsamepage}[appref,SEref]
\newhookcommand@series{Xlinerangeseparator}[appref,SEref]
\newhookcommand@series{Xlinenumannotationposition}[side,appref,SEref]%
\newhookcommand@series{Xwraplinenumannotation}[side,ref,appref,SEref]%
\newhooktoggle@series{Xnoidenticallinenumannotation}[side,ref,appref,SEref]%
\unless\ifnofamiliar@
  \newhookcommand@series{wrapcontentX}%
  \newhookcommand@series{beforeinsertingX}%
  \newhooktoggle@series{parindentX}
  \newhookcommand@series{hangindentX}
  \newhookcommand@series{raggedX}
  \newhookcommand@series{hsizetwocolX}
  \newhookcommand@series{hsizethreecolX}
  \newhookcommand@series{colalignX}%
  \newhookcommand@series{notenumfontX}
  \newhookcommand@series{bhooknoteX}
  \newhookcommand@series@reload{bhookgroupX}{familiar}
  \newhookcommand@series@reload{beforenotesX}{familiar}
  \newhookcommand@series@reload{maxhnotesX}{familiar}
  \newhooktoggle@series@reload{noteswidthliketwocolumnsX}{familiar}%
  \newhookcommand@series@reload{afterruleX}{familiar}
  \newhookcommand@series@reload{notefontsizeX}{familiar}
  \newhookcommand@series{afternoteX}
  \newhookcommand@series{parafootsepX}
  \newhookcommand@series{txtbeforenotesX}%
  \newhooktoggle@series{txtbeforenotesonlyonceX}%
  \newhookcommand@series@reload{widthX}{familiar}%
  \newhookcommand@series{innotemarkX}[][1]%
  \newhooktoggle@series{nonoteX}%
  \ifundef{\hsizeX}%
    {%
      \newcommandx{\hsizeX}[2][1,usedefault]{%
        \led@warning@hsizeX@deprecated%
        \widthX[#1]{#2}%
      }%
    }%
    {}%
\fi
\unless\ifnoend@
  \newhookcommand@series{Xendwraplemma}
  \newhookcommand@series{Xendwrapcontent}
  \newhookcommand@series{Xendnotenumfont}
  \newhookcommand@series{Xendlemmafont}%
  \newhookcommand@series{Xendbhooknote}

  \newhookcommand@series{Xendboxlinenum}%
  \newhookcommand@series{Xendboxlinenumalign}%

  \newhookcommand@series{Xendboxstartlinenum}%
  \newhookcommand@series{Xendboxendlinenum}%

  \newhookcommand@series{Xendnotefontsize}
  \newhooktoggle@series{Xendlemmadisablefontselection}
  \newhookcommand@series{Xendlemmaseparator}
  \newhookcommand@series{Xendbeforelemmaseparator}
  \newhookcommand@series{Xendafterlemmaseparator}
  \newhookcommand@series{Xendinplaceoflemmaseparator}

  \newhookcommand@series{Xendbeforenumber}%
  \newhookcommand@series{Xendafternumber}%

  \newhooktoggle@series{Xendparagraph}
  \newhookcommand@series{Xendafternote}
  \newhookcommand@series{Xendsep}

  \newhookcommand@series{Xendinplaceofnumber}%
  \newhooktoggle@series{Xendnonumber}%
  \newhooktoggle@series{Xendnolinenumber}%
  \newhooktoggle@series{Xendnolinenumberifannotation}%

  \newhooktoggle@series{Xendnumberonlyfirstinline}%
  \newhooktoggle@series{Xendnumberonlyfirstintwolines}%

  \newhooktoggle@series{Xendlinenumannotationonlyfirst}%
  \newhooktoggle@series{Xendlinenumannotationonlyfirstintwo}%

  \newhookcommand@series{Xendsymlinenum}%
  \newhookcommand@series{Xendbeforesymlinenum}%
  \newhookcommand@series{Xendaftersymlinenum}%
  \newhookcommand@series{Xendboxsymlinenum}%

  \newhookcommand@series{Xendsymlinenumannotation}%

  \newhookcommand@series{Xendbhooklinenumber}%
  \newhookcommand@series{Xendahooklinenumber}%
  \newhookcommand@series{Xendbhookinplaceofnumber}%
  \newhookcommand@series{Xendahookinplaceofnumber}%

  \newhookcommand@series{Xendhangindent}%

  \newhooktoggle@series{Xendpagenumberonlyfirst}%
  \newhooktoggle@series{Xendpagenumberonlyfirstifsingle}%
  \newhooktoggle@series{Xendpagenumberonlyfirstintwo}%
  \newhookcommand@series{Xendsympagenum}%
  \newhookcommand@series{Xendinplaceofpagenumber}%

  \newhookcommand@series{Xendtxtbeforenotes}%

\fi
\newhooktoggle@series{Xendlineflag}[apprefwithpage,SErefwithpage]
\newhookcommand@series{Xendtwolines}[apprefwithpage,SErefwithpage]
\newhookcommand@series{Xendmorethantwolines}[apprefwithpage,SErefwithpage]
\newhooktoggle@series{Xendtwolinesbutnotmore}[apprefwithpage,SErefwithpage]
\newhooktoggle@series{Xendtwolinesonlyinsamepage}[apprefwithpage,SErefwithpage]
\newhookcommand@series{Xendlinerangeseparator}[apprefwithpage,SErefwithpage]
\newhookcommand@series{Xendbeforepagenumber}[apprefwithpage,SErefwithpage,SErefonlypage]
\newhookcommand@series{Xendafterpagenumber}[apprefwithpage,SErefwithpage]
\newhookcommand@series{Xendlineprefixsingle}[apprefwithpage,SErefwithpage]
\newhookcommand@series{Xendlineprefixmore}[apprefwithpage,SErefwithpage]
\newhookcommand@series{Xendsublinesep}[apprefwithpage,SErefwithpage]

\newhookcommand@series{Xendlinenumannotationposition}[apprefwithpage,SErefwithpage]%
\newhookcommand@series{Xendwraplinenumannotation}[apprefwithpage,SErefwithpage]%
\newhooktoggle@series{Xendnoidenticallinenumannotation}[apprefwithpage,SErefwithpage]%
\newcommand{\newhooktoggle@specific}[1]{%
  \newtoggle{#1@}%
  \listgadd{\hooktoggle@specific}{#1}%
  \define@key[mac]{truefootnoteoption}{#1}[]{\global\settoggle{#1@}{true}}%When enabling footnote option
  \define@key[mac]{falsefootnoteoption}{#1}[]{\global\settoggle{#1@}{false}}
}
\newcommand{\newhookarg@specific}[1]{%
  \listgadd{\hookarg@specific}{#1}%
  \define@key[mac]{truefootnoteoption}{#1}{\csgdef{#1@}{##1}}%When enabling footnote option
  \define@key[mac]{falsefootnoteoption}{#1}{\global\csundef{#1@}}%When disabling footnote option
}
\def\hooktoggle@specific{}%
\newcommand{\add@hooktoggle@specific@to@cs}[1]{%
  \def\do##1{%
    \iftoggle{##1@}{%
      \ifcsdef{#1}{%
        \csgappto{#1}{\toggletrue{##1@}}%
      }{%
        \csgdef{#1}{\toggletrue{##1@}}%
      }%
    }{%
      \ifcsdef{#1}{%
        \csgappto{#1}{\togglefalse{##1@}}%
      }{%
        \csgdef{#1}{\togglefalse{##1@}}%
      }%
    }%
  }%
  \dolistloop{\hooktoggle@specific}%
}%
\def\hookarg@specific{}%
\newcommand{\add@hookarg@specific@to@cs}[1]{%
  \def\do##1{%
    \ifcsvoid{##1@}{}{%
      \ifcsdef{#1}{%
        \csxappto{#1}{%
          \noexpand\csdef%
            {##1@}{\csname##1@\endcsname}%
          }%
      }{%
        \csxdef{#1}{%
          \noexpand\csdef%
            {##1@}{\csname##1@\endcsname}%
          }%
      }%
    }%
  }%
  \dolistloop{\hookarg@specific}%
}%
\newhooktoggle@specific{fulllines}%
\newhooktoggle@specific{nonum}
\newhooktoggle@specific{nosep}
\newhooktoggle@specific{noprefix}%
\newhooktoggle@specific{prefixmore}%
\newhookarg@specific{linerangesep}
\newtoggle{nomk@}%
\newcommandx*{\Xnolemmaseparator}[1][1]{\Xlemmaseparator[#1]{}}

\newcommand*{\l@ddoxtrafeet}{%
  \IfStrEq{familiar-critical}{\@fnpos}
    {\do@feetX\do@Xfeet}%
    {%
    \IfStrEq{critical-familiar}{\@fnpos}%
        {\do@Xfeet\do@feetX}%
        {%
          \setbox\@outputbox \vbox{%
            \unvbox\@outputbox%
            \do@feet@custom@order{}{\@fnpos}%
          }%
        }%
    }%
}%

\newcommand{\do@feet@custom@order}[2]{%
  \def\do##1{%
    \edef\@@notesseries{\@firstoftwo##1}%
    \edef\@@notetype{\@secondoftwo##1}%
    \ifdefstring{\@@notetype}{critical}%
      {\csuse{#1append@Xnotes}{\@@notesseries}}%
      {\ifdefstring{\@@notetype}{familiar}%
        {\csuse{#1append@notesX}{\@@notesseries}}%
        {}%
      }%
  }%
  \expandafter\docsvlist\expandafter{#2}%
}%
\newcommand*{\do@Xfeet}{%
  \setbox\@outputbox \vbox{%
    \unvbox\@outputbox
    \@opXfeet}}
\newcommand{\append@Xnotes}[1]{%
  \ifvoid\csuse{#1footins}\else%
      \global\skip\csuse{#1footins}=\csuse{Xbeforenotes@#1}%
      \global\advance\skip\csuse{#1footins} by\csuse{Xafterrule@#1}%
      \print@Xnotes{#1}%
   \fi%
}%
\newcommand\print@Xnotes[1]{%
  \xdef\@currentseries{#1}%
  \csuse{#1footstart}{#1}%
  \csuse{#1footgroup}{#1}%%
}%
\newcommand*{\@opXfeet}{%
  \unless\ifnocritical@%
    \gdef\firstXseries@{}%
    \def\do##1{%
      \append@Xnotes{##1}%
      }%
    \dolistloop{\@series}%
  \fi%
}%
\newcommand*{\l@ddodoreinxtrafeet}{%
  \IfStrEq{familiar-critical}{\@fnpos}
    {\@doreinfeetX\X@doreinfeet}%
    {%
    \IfStrEq{critical-familiar}{\@fnpos}%
        {\X@doreinfeet\@doreinfeetX}%
        {\@doreinfeetX\X@doreinfeet}%
    }%
}

\newcommand*{\X@doreinfeet}{%
  \unless\ifnocritical@%
    \def\do##1{%
      \ifvoid\csuse{##1footins}\else%
        \insert\csuse{##1footins}{\unvbox\csuse{##1footins}}%
      \fi}%
    \dolistloop{\@series}
  \fi%
}

\newcommand{\append@notesX}[1]{%
  \ifvoid\csuse{footins#1}\else%
      \global\skip\csuse{footins#1}=\csuse{beforenotesX@#1}%
      \global\advance\skip\csuse{footins#1} by\csuse{afterruleX@#1}%
      \print@notesX{#1}%
  \fi%
}%
\newcommand\print@notesX[1]{%
  \xdef\@currentseries{#1}%
  \csuse{footstart#1}{#1}%
  \csuse{footgroup#1}{#1}%
}%
\newcommand*{\do@feetX}{%
  \unless\ifnofamiliar@%
    \gdef\firstseriesX@{}%
    \setbox\@outputbox \vbox{%
      \unvbox\@outputbox%
      \def\do##1{%
        \append@notesX{##1}%
      }%
      \dolistloop{\@series}}%
  \fi%
}%

\newcommand{\@doreinfeetX}{%
  \unless\ifnofamiliar@%
    \def\do##1{%
      \ifvoid\csuse{footins##1}\else
        \insert%
          \csuse{footins##1}
          {\unvbox\csuse{footins##1}}%
       \fi%
    }%
    \dolistloop{\@series}%
  \fi%
}%

\@ifclassloaded{memoir}{%
  \g@addto@macro{\m@mdoextrafeet}{\l@ddoxtrafeet}%
  \g@addto@macro{\m@mdodoreinextrafeet}{\l@ddodoreinxtrafeet}%
  }{%
  \ifboolexpr{%
    test{\@ifpackageloaded{fancyhdr}}%
    and test {\ifdef{\latex@makecol}}%
  }{%
    \patchcmd%
      {\latex@makecol}%
      {\xdef\@freelist{\@freelist\@midlist}}%
      {\xdef\@freelist{\@freelist\@midlist}\l@ddoxtrafeet}%
      {}%
      {\led@error@fail@patch@@makecol}%
    }{%
    \patchcmd%
      {\@makecol}%
      {\xdef\@freelist{\@freelist\@midlist}}%
      {\xdef\@freelist{\@freelist\@midlist}\l@ddoxtrafeet}%
      {}%
      {\led@error@fail@patch@@makecol}%
    }%

  \patchcmd%
    {\@reinserts}%
    {\ifvbox}%
    {\l@ddodoreinxtrafeet\ifvbox}%
    {}%
    {\led@error@fail@patch@@reinserts}%
}


\newif\if@led@nofoot

\@ifclassloaded{memoir}{%
\g@addto@macro{\@mem@extranofeet}{%%
  \def\do#1{%
    \unless\ifnocritical@%
      \ifvoid\csuse{#1footins}\else\@mem@nofootfalse\fi%
    \fi%
    \unless\ifnofamiliar@%
      \ifvoid\csuse{footins#1}\else\@mem@nofootfalse\fi%
    \fi%
    }
  \dolistloop{\@series}%
  }%
}{%
\newcommand*{\@led@testifnofoot}{%
  \@led@nofoottrue%
  \ifvoid\footins\else%
    \@led@nofootfalse%
  \fi%
  \def\do##1{%
    \unless\ifnocritical@%
      \ifvoid\csuse{##1footins}\else%
        \@led@nofootfalse%
      \fi%
    \fi%
    \unless\ifnofamiliar@%
      \ifvoid\csuse{footins##1}\else%
        \@led@nofootfalse%
      \fi%
    \fi%
  }%
  \dolistloop{\@series}%
}%

\pretocmd%
 {\@doclearpage}%
 {\@led@testifnofoot}%
 {}%
 {\led@error@fail@patch@@doclearpage}%

\patchcmd%
 {\@doclearpage}%
 {\ifvoid\footins}%
 {\if@led@nofoot}%
 {}%
 {\led@error@fail@patch@@doclearpage}%

}




\newcommand{\par@patch@thepage}{%
  \ifboolexpr{%
    bool{sameparallelpagenumber}%
    or bool{prevpgnotnumbered}%
  }%
  {%
    \patchcmd{\thepage}%
      {page}{par@page}%
      {}%
      {\led@error@fail@patch@thepage}%
  }{}%
}%

\newcommand{\par@patch@pagenumbering}{%
  \setcounter{par@page}{1}%
  \par@patch@thepage%
}%

\ifl@dmemoir%
  \apptocmd{\@mempnum}%
    {\par@patch@pagenumbering}%
    {}%%
    {\led@error@fail@patch@@mempnum}%

\else%
  \apptocmd{\pagenumbering}%
    {\par@patch@pagenumbering}%
    {}%
    {\led@error@fail@patch@pagenumbering}%
\fi%

\AtBeginDocument{\par@patch@thepage}%
\AtBeginDocument{%
  \set@this@c@page%
  \apptocmd{\@outputpage}{%
    \ifsameparallelpagenumber%
      \ifl@dprintingpages%
        \ifodd\c@page\else%
          \stepcounter{par@page}%
        \fi%
      \else%
        \stepcounter{par@page}%
      \fi%
    \else%
      \stepcounter{par@page}%
    \fi%
    }%
    {}%
    {\led@error@fail@patch@@outputpage}%
}%
\catcode`\#=12%
\AtBeginDocument{%
    \apptocmd{\setcounter}{%
    \IfStrEq{#1}{page}{\set@this@c@page}{}%
    }%
    {}%
    {\led@error@fail@patch@setcounter}%
  \apptocmd{\addtocounter}{%
    \IfStrEq{#1}{page}{\set@this@c@page}{}%
    }%
    {}%
    {\led@error@fail@patch@addtocounter}%
  \@ifpackageloaded{calc}{%
    \apptocmd{\stepcounter}{%Because calc.sty does not use addtocounter in stepcounter
      \IfStrEq{#1}{page}{\set@this@c@page}{}%
      }%
      {}%
      {\led@error@fail@patch@steptocounter}%
  }{}%
}%
\catcode`\#=6%
\newcounter{par@page}%
\setcounter{par@page}{1}%
\list@create{\labelref@list}
\newcommand*{\zz@@@}{000|000|000}% Set three counters to zero in one go
\newcommand*{\zz@@@@}{000|000|000|000}% Set four counters to zero in one go

\newcommand*{\edlabel}[1]{%
  \leavevmode%
  \@bsphack%
  \ifboolexpr{bool{ledRcol} or bool{ledRcol@}}{%
    \ifXnote@%
     \protected@write\@auxout{}%
      {\string\l@dmake@labelsR\space\thepage|\l@dparsedstartline|\l@dparsedstartsub||\annot@start|\the\c@pstartR|{#1}}%
      \ifdef{\hypertarget}%
        {\Hy@raisedlink{\hypertarget{#1}{}}}%
        {}%
    \else%
      \write\linenum@outR{\string\@lab}%
      \ifx\labelref@listR\empty%
        \xdef\label@refs{\zz@@@@}%
      \else%
        \gl@p\labelref@listR\to\label@refs%
      \fi%
      \ifvmode%
          \advancelabel@refs%
      \fi%
      \protected@write\@auxout{}%
        {\string\l@dmake@labelsR\space\thepage|\label@refs|\the\c@pstartR|{#1}}%
      \ifdef{\hypertarget}%
        {\Hy@raisedlink{\hypertarget{#1}{}}}%
        {}%
    \fi%
  }{%
    \ifXnote@%
       \ifl@dpairing%pstart or pstartL?
         \protected@write\@auxout{}%
          {\string\l@dmake@labels\space\thepage|\l@dparsedstartline|\l@dparsedstartsub||\annot@start|\the\c@pstartL|{#1}}%
          \ifdef{\hypertarget}%
            {\Hy@raisedlink{\hypertarget{#1}{}}}%
            {}%
        \else%
          \protected@write\@auxout{}%
          {\string\l@dmake@labels\space\thepage|\l@dparsedstartline|\l@dparsedstartsub||\annot@start|\the\c@pstart|{#1}}%
          \ifdef{\hypertarget}%
            {\Hy@raisedlink{\hypertarget{#1}{}}}%
            {}%
        \fi%
    \else%
      \write\linenum@out{\string\@lab}%
      \ifx\labelref@list\empty%
        \xdef\label@refs{\zz@@@@}%
      \else%
        \gl@p\labelref@list\to\label@refs%
      \fi%
      \ifvmode%
          \advancelabel@refs%
      \fi%
      \ifl@dpairing%Pstart or PstartL?
        \protected@write\@auxout{}%
          {\string\l@dmake@labels\space\thepage|\label@refs|\the\c@pstartL|{#1}}%
          \ifdef{\hypertarget}%
            {\Hy@raisedlink{\hypertarget{#1}{}}}%
            {}%
       \else%
         \protected@write\@auxout{}%
          {\string\l@dmake@labels\space\thepage|\label@refs|\the\c@pstart|{#1}}%
          \ifdef{\hypertarget}%
            {\Hy@raisedlink{\hypertarget{#1}{}}}%
            {}%
       \fi%
    \fi%
  }%
\@esphack}%

\newcounter{line}%
\newcounter{subline}%
\newcounter{absline}%
\newcommand{\advancelabel@refs}{%
    \setcounter{line}{\expandafter\labelrefsparseline\label@refs}%
    \stepcounter{line}%
    \setcounter{absline}{\expandafter\labelrefsparseabsline\label@refs}%
    \stepcounter{absline}%
    \ifsublines@%
        \setcounter{subline}{\expandafter\labelrefsparsesubline\label@refs}%
        \stepcounter{subline}{1}%
        \def\label@refs{\theline|\thesubline|\theabsline}%
    \else%
        \def\label@refs{\theline|0|\theabsline}%
    \fi%
}
\def\labelrefsparseline#1|#2|#3{#1}%
\def\labelrefsparsesubline#1|#2|#3{#2}%
\def\labelrefsparseabsline#1|#2|#3{#3}%
\newcommand*{\l@dmake@labels}{}
\def\l@dmake@labels#1|#2|#3|#4|#5|#6|#7{%
  \expandafter\ifx\csname the@label\csuse{XR@prefix}#7\endcsname%
    \relax%
  \else%
    \led@warn@DuplicateLabel{\csuse{XR@prefix}#7}%
  \fi
  \global\providetoggle{label@#7@ledRcol}%False is the default value of this toggle, which tells us whether a label is linked to the right or left side
  \csgdef{the@label\csuse{XR@prefix}#7}{#1|#2|#3|#4|#5|#6|\relax}%
  \ignorespaces}

\AtBeginDocument{%
  \def\l@dmake@labels#1|#2|#3|#4|#5|#6|#7{}%
}


\newcommand*{\@lab}{%
  \ifledRcol
    \xright@appenditem{\linenumr@p{\line@numR}|%
      \ifsublines@ \sublinenumr@p{\subline@numR}\else 0\fi|%
      \the\absline@numR|%
      \current@annot%
      }%
      \to\labelref@listR
  \else
    \xright@appenditem{%
      \linenumr@p{\line@num}|%
      \ifsublines@ \sublinenumr@p{\subline@num}\else 0\fi|%
      \the\absline@num|%
      \current@annot%
      }%
      \to\labelref@list
  \fi}
\newcommand*{\applabel}[1]{%
  \if@edtext@secondarg@%
      \ifcsundef{the@label#1}{%
        \csdef{the@label#1}{applabel}%
       }%
       {%
         \led@warn@DuplicateLabel{#1 (applabel)}%
       }%
     \expandafter\l@dp@rsefootspec\l@d@nums|%
     \expandafter\parse@annot\l@current@annot|%
     \@bsphack%
      \ifledRcol%
        \protected@write\@auxout{}%
          {\string\l@dmake@labelsR\space\l@dparsedstartpage|\l@dparsedstartline|\l@dparsedstartsub||\annot@start|\the\c@pstartR|{#1:start}}%
        \ifdef{\hypertarget}%
          {\Hy@raisedlink{\hypertarget{#1:start}{}}}%
          {}%
        \protected@write\@auxout{}%
          {\string\l@dmake@labelsR\space\l@dparsedendpage|\l@dparsedendline||\l@dparsedendsub||\annot@end|\the\c@pstartR|{#1:end}}%
      \else%
        \ifl@dpairing%pstart or pstartL?
          \protected@write\@auxout{}%
            {\string\l@dmake@labels\space\l@dparsedstartpage|\l@dparsedstartline|\l@dparsedstartsub||\annot@start|\the\c@pstartL|{#1:start}}%
          \ifdef{\hypertarget}%
            {\Hy@raisedlink{\hypertarget{#1:start}{}}}%
            {}%
          \protected@write\@auxout{}%
            {\string\l@dmake@labels\space\l@dparsedendpage|\l@dparsedendline|\l@dparsedendsub||\annot@end|\the\c@pstartL|{#1:end}}%
         \else%
            \protected@write\@auxout{}%
              {\string\l@dmake@labels\space\l@dparsedstartpage|\l@dparsedstartline|\l@dparsedstartsub||\annot@start|\the\c@pstart|{#1:start}}%
            \ifdef{\hypertarget}%
              {\Hy@raisedlink{\hypertarget{#1:start}{}}}%
              {}%
            \protected@write\@auxout{}%
              {\string\l@dmake@labels\space\l@dparsedendpage|\l@dparsedendline|\l@dparsedendsub||\annot@end|\the\c@pstart|{#1:end}}%
         \fi%
      \fi%
      \@esphack%
  \else%
    \led@warn@AppLabelOutSecondArgEdtext{#1}%
  \fi%
}%
\newcommand{\edlabelS}[1]{%
  \edlabel{#1:start}%
}
\newcommand{\edlabelE}[1]{%
  \edlabel{#1:end}%
}
\newcommand{\edlabelSE}[1]{%
  \edlabelS{#1}%
  \edlabelE{#1}%
}
\newrobustcmd{\wrap@edcrossref}[2]{%
  \ifdef{\hyperlink}%
    {\hyperlink{#1}{#2}}%
    {#2}%
}
\newcommand*{\edpageref}[1]{\l@dref@undefined{#1}\wrap@edcrossref{#1}{\l@dgetref@num{1}{#1}}}
\newcommand*{\xpageref}[1]{\l@dgetref@num{1}{#1}}

\newcommand*{\edlineref}[1]{%
  \l@dref@undefined{#1}%
  \wrap@edcrossref{#1}{%
    \providetoggle{label@#1@ledRcol}%Required for the first run, when the label has not yet been parsed on the .aux file
    \iftoggle{label@#1@ledRcol}%
        {\linenumrepR{\l@dgetref@num{2}{#1}}}%
        {\linenumrep{\l@dgetref@num{2}{#1}}}%
    \xflagref{#1}%
  }%
}%
\newcommand*{\xlineref}[1]{\l@dgetref@num{2}{#1}}%

\newcommand*{\sublineref}[1]{%
  \l@dref@undefined{#1}%
  \wrap@edcrossref{#1}{%
    \providetoggle{label@#1@ledRcol}%Required for the first run, when the label has not yet been parsed on the .aux file
    \iftoggle{label@#1@ledRcol}%
      {\sublinenumrepR{\l@dgetref@num{3}{#1}}}%
      {\sublinenumrep{\l@dgetref@num{3}{#1}}}%
  }%
}%
\newcommand*{\xsublineref}[1]{\l@dgetref@num{3}{#1}}

\newcommand*{\xabslineref}[1]{\l@dgetref@num{4}{#1}}%
\newcommand*{\annotationref}[1]{%
  \l@dref@undefined{#1}%
  \wrap@edcrossref{#1}{%
    \l@wrap@ifnotemptybox{\Xwraplinenumannotation@ref}{%
      \l@dgetref@num{5}{#1}%
    }%
  }%
}%
\newcommand*{\xannotationref}[1]{%
  \l@dgetref@num{5}{#1}%
}%
\newcommand*{\pstartref}[1]{\l@dref@undefined{#1}\wrap@edcrossref{#1}{\l@dgetref@num{6}{#1}}}%
\newcommand*{\xpstartref}[1]{\l@dgetref@num{6}{#1}}%

\newcommand*{\xflagref}[1]{\l@dgetref@num{7}{#1}}%
\newcommand*{\l@dref@undefined}[1]{%
  \expandafter\ifx\csname the@label#1\endcsname\relax
    \led@warn@RefUndefined{#1}%
  \fi}

\newcommand*{\l@dgetref@num}[2]{%
  \expandafter
  \ifx\csname the@label#2\endcsname \relax
    000%
  \else
    \expandafter\expandafter\expandafter
    \l@dlabel@parse\csname the@label#2\endcsname|#1%
  \fi}

\newcommand*{\l@dlabel@parse}{}
\def\l@dlabel@parse#1|#2|#3|#4|#5|#6|#7|#8{%
  \ifcase #8%
  \or #1%
  \or #2%
  \or #3%
  \or #4%
  \or #5%
  \or #6%
  \or #7%
  \fi}
\newcommand*{\xxref}[2]{%
  {%
   \expandafter\ifx\csname the@label#1\endcsname\relax%
      \let\@tempa\zz@@@%
      \def\@tempc{}%
    \else%
      \def\@tempa{%
        \l@dgetref@num{1}{#1}|%
        \l@dgetref@num{2}{#1}|%
        \l@dgetref@num{3}{#1}%
      }%
      \edef\@tempc{%
        \l@dgetref@num{5}{#1}%
      }%
   \fi%
   \expandafter\ifx\csname the@label#2\endcsname\relax%
      \let\@tempb\zz@@@%
      \def\@tempd{}%
   \else%
      \def\@tempb{%
        \l@dgetref@num{1}{#2}|%
        \l@dgetref@num{2}{#2}|%
        \l@dgetref@num{3}{#2}%
      }%
      \edef\@tempd{%
        \l@dgetref@num{5}{#2}%
      }%
   \fi%
   \global\appto\@beforeinsertofthisedtext{%
     \def\@this@crossref@start{#1}%
   }%
   \global\appto\@beforeinsertofthisedtext{%
     \def\@this@crossref@end{#2}%
   }%
   \linenum{\@tempa|\@tempb}%
   \edef\@tempe{\@tempc|\@tempd}%
   \expandafter\lineannot\expandafter{\@tempe}%
 }%
}%

\def\Xtwolines@appref{}%
\def\Xtwolines@SEref{}%

\def\Xmorethantwolines@appref{}%
\def\Xmorethantwolines@SEref{}%

\def\Xlinerangeseparator@appref{\endashchar}%
\def\Xlinerangeseparator@SEref{\endashchar}%

\def\Xsublinesep@appref{\fullstop}%
\def\Xsublinesep@SEref{\fullstop}%

\def\Xpagelinesep@appref{\fullstop}%
\def\Xpagelinesep@SEref{\fullstop}%

\newtoggle{Xtwolinesbutnotmore@appref}%
\newtoggle{Xtwolinesbutnotmore@SEref}%

\newtoggle{Xtwolinesonlyinsamepage@appref}%

\newtoggle{Xtwolinesonlyinsamepage@SEref}%

\newtoggle{Xlineflag@appref}%
\toggletrue{Xlineflag@appref}%Here exception
\newtoggle{Xlineflag@SEref}%
\toggletrue{Xlineflag@SEref}%%Here exception

\newtoggle{Xlinenumannotationonlyfirst@SEref}%Never changed, just for the test in \setprintlines
\newtoggle{Xlinenumannotationonlyfirst@appref}%Idem
\newtoggle{Xnolinenumber@SEref}%Idem
\newtoggle{Xnolinenumber@appref}%Idem
\newtoggle{Xnolinenumberifannotation@SEref}%Idem
\newtoggle{Xnolinenumberifannotation@appref}%Idem

\def\Xlinenumannotationposition@appref{after}%
\def\Xlinenumannotationposition@SEref{after}%

\def\Xwraplinenumannotation@appref{\textsuperscript}%
\def\Xwraplinenumannotation@SEref{\textsuperscript}%
\newtoggle{Xnoidenticallinenumannotation@appref}%
\newtoggle{Xnoidenticallinenumannotation@SEref}%

\def\Xendtwolines@apprefwithpage{}%
\def\Xendtwolines@SErefwithpage{}%

\def\Xendmorethantwolines@apprefwithpage{}%
\def\Xendmorethantwolines@SErefwithpage{}%

\def\Xendlinerangeseparator@apprefwithpage{\endashchar}
\def\Xendlinerangeseparator@SErefwithpage{\endashchar}
\def\Xendlinerangeseparator@SErefonlypage{\endashchar}

\def\Xendbeforepagenumber@apprefwithpage{p.}%
\def\Xendbeforepagenumber@SErefwithpage{p.}%
\def\Xendbeforepagenumber@SEonlypage{p.}%

\def\Xendafterpagenumber@apprefwithpage{) }%
\def\Xendafterpagenumber@SErefwithpage{) }%

\def\Xendlineprefixsingle@apprefwithpage{}%
\def\Xendlineprefixsingle@SErefwithpage{}%

\def\Xendlineprefixmore@apprefwithpage{}%
\def\Xendlineprefixmore@SErefwithpage{}%

\newtoggle{Xendtwolinesbutnotmore@apprefwithpage}%
\newtoggle{Xendtwolinesbutnotmore@SErefwithpage}%

\def\Xendsublinesep@apprefwithpage{\fullstop}%
\def\Xendsublinesep@SErefwithpage{\fullstop}%

\newtoggle{Xendtwolinesonlyinsamepage@apprefwithpage}%
\newtoggle{Xendtwolinesonlyinsamepage@SErefwithpage}%

\newtoggle{Xendlineflag@apprefwithpage}
\toggletrue{Xendlineflag@apprefwithpage}%Here, exception
\newtoggle{Xendlineflag@SErefwithpage}
\toggletrue{Xendlineflag@SErefwithpage}%Here, exception

\def\Xendlinenumannotationposition@apprefwithpage{after}%
\def\Xendlinenumannotationposition@SErefwithpage{after}%

\def\Xendlinenumannotationposition@apprefwithpage{after}%
\def\Xendlinenumannotationposition@apprefwithpage@SErefwithpage{after}%

\def\Xendwraplinenumannotation@apprefwithpage{\textsuperscript}%
\def\Xendwraplinenumannotation@SErefwithpage{\textsuperscript}%

\newtoggle{Xendnoidenticallinenumannotation@apprefwithpage}%
\newtoggle{Xendnoidenticallinenumannotation@SErefwithpage}%

\newtoggle{Xendnolinenumber@SErefwithpage}%Never changed, only for test in  \setprintendlines
\newtoggle{Xendnolinenumber@apprefwithpage}%Idem
\newtoggle{Xendnolinenumberifannotation@SErefwithpage}%Idem
\newtoggle{Xendnolinenumberifannotation@apprefwithpage}%Idem


\newtoggle{Xendlinenumannotationonlyfirst@SEref}%Never changed, just for the test in \setprintendlines
\newtoggle{Xendlinenumannotationonlyfirst@SErefwithpage}%Idem
\newtoggle{Xendlinenumannotationonlyfirst@appref}%Idem
\newtoggle{Xendlinenumannotationonlyfirst@apprefwithpage}%Idem

\gdef\Xboxstartlinenum@appref{0pt}
\gdef\Xboxstartlinenum@SEref{0pt}

\gdef\Xboxendlinenum@appref{0pt}
\gdef\Xboxendlinenum@SEref{0pt}

\gdef\Xendboxstartlinenum@apprefwithpage{0pt}
\gdef\Xendboxstartlinenum@SErefwithpage{0pt}

\gdef\Xendboxendlinenum@apprefwithpage{0pt}
\gdef\Xendboxendlinenum@SErefwithpage{0pt}

\newtoggle{Xendpagenumberonlyfirst@apprefwithpage}
\newtoggle{Xendpagenumberonlyfirst@SErefwithpage}

\newtoggle{Xendpagenumberonlyfirstifsingle@apprefwithpage}
\newtoggle{Xendpagenumberonlyfirstifsingle@SErefwithpage}

\newtoggle{Xendpagenumberonlyfirstintwo@apprefwithpage}
\newtoggle{Xendpagenumberonlyfirstintwo@SErefwithpage}

\gdef\Xendsympagenum@apprefwithpage{}
\gdef\Xendsympagenum@SErefwithpage{}

\gdef\Xendinplaceofpagenumber@apprefwithpage{}
\gdef\Xendinplaceofpagenumber@SErefwithpage{}

\newcommand\@apprefprefixsingle{}%
\newcommand\@SErefprefixsingle{}%

\newcommand\@apprefprefixmore{}%
\newcommand\@SErefprefixmore{}%

\newcommand{\setapprefprefixsingle}[1]{%
  \gdef\@apprefprefixsingle{#1}%
}
\newcommand{\setSErefprefixsingle}[1]{%
  \gdef\@SErefprefixsingle{#1}%
}

\newcommand{\setapprefprefixmore}[1]{%
  \gdef\@apprefprefixmore{#1}%
}
\newcommand{\setSErefprefixmore}[1]{%
  \gdef\@SErefprefixmore{#1}%
}

\newcommand{\setSErefonlypageprefixsingle}[1]{%
  \gdef\SErefonlypage@prefixsingle{#1}%
}%
\newcommand{\setSErefonlypageprefixmore}[1]{%
  \gdef\SErefonlypage@prefixmore{#1}%
}%

\newcommandx{\appref}[2][1,usedefault]{\refformated@{#1}{#2}{appref}}
\newcommandx{\SEref}[2][1,usedefault]{\refformated@{#1}{#2}{SEref}}

\newcommandx{\apprefwithpage}[2][1,usedefault]{\refformatedwithpage@{#1}{#2}{appref}}
\newcommandx{\SErefwithpage}[2][1,usedefault]{\refformatedwithpage@{#1}{#2}{SEref}}
\newcommandx{\SErefonlypage}[2][1,usedefault]{\refformatedonlypage@{#1}{#2}{SEref}}

\newcommand{\refformated@}[3]{%
  \def\do##1{%
    \setkeys[mac]{truefootnoteoption}{##1}%
  }%
 \notblank{#1}{\docsvlist{#1}}{}%
 \xdef\@currentseries{#3}%
 \iftoggle{noprefix@}{}%
  {%
    \ifcsempty{@#3prefixmore}%
    {\@apprefprefixsingle}%
    {%
      \IfEq{\xlineref{#2:start}}{\xlineref{#2:end}}%
      {\iftoggle{prefixmore@}%
        {\csuse{@#3prefixmore}}%
        {\csuse{@#3prefixsingle}}%
      }%
      {\csuse{@#3prefixmore}}%
    }%
  }%
 \ifboolexpr{%
   test{\ifcsundef{the@label#2:start}}%
   or test{\ifcsundef{the@label#2:end}}%
  }%
   {\led@warn@pairRefUndefined{#2}\nfss@text{\reset@font\bfseries ??}}%
   {%
    \def\@this@crossref@start{#2:start}%
    \def\@this@crossref@end{#2:end}%
    \xdef\annot@start{\xannotationref{#2:start}}%
    \xdef\annot@end{\xannotationref{#2:end}}%
    \printlines\xpageref{#2:start}|\xlineref{#2:start}|\xsublineref{#2:start}|\xpageref{#2:end}|\xlineref{#2:end}|\xsublineref{#2:end}|\relax|\xflagref{#2:start}|%
   }%
  \def\do##1{%
    \setkeys[mac]{falsefootnoteoption}{##1}%
  }%
 \notblank{#1}{\docsvlist{#1}}{}%
}%

\newcommand{\refformatedwithpage@}[3]{%
  \def\do##1{%
    \setkeys[mac]{truefootnoteoption}{##1}%
  }%
 \notblank{#1}{\docsvlist{#1}}{}%
 \xdef\@currentseries{#3withpage}%
 \ifboolexpr{%
   test{\ifcsundef{the@label#2:start}}%
   or test{\ifcsundef{the@label#2:end}}%
  }%
   {\led@warn@pairRefUndefined{#2}\nfss@text{\reset@font\bfseries ??}}%
   {%
   \def\@this@crossref@start{#2:start}%
   \def\@this@crossref@end{#2:end}%
   \xdef\annot@start{\xannotationref{#2:start}}%
   \xdef\annot@end{\xannotationref{#2:end}}%
   \printendlines\xpageref{#2:start}|\xlineref{#2:start}|\xsublineref{#2:start}|\xpageref{#2:end}|\xlineref{#2:end}|\xsublineref{#2:end}|\relax|\xflagref{#2:start}|%
   }%
  \def\do##1{%
    \setkeys[mac]{falsefootnoteoption}{##1}%
  }%
 \notblank{#1}{\docsvlist{#1}}{}%
}%

\newcommand{\refformatedonlypage@}[3]{%
  \def\do##1{%
    \setkeys[mac]{truefootnoteoption}{##1}%
  }%
 \notblank{#1}{\docsvlist{#1}}{}%
 \xdef\@currentseries{#3onlypage}%
 \ifboolexpr{%
   test{\ifcsundef{the@label#2:start}}%
   or test{\ifcsundef{the@label#2:end}}%
  }%
   {\led@warn@pairRefUndefined{#2}\nfss@text{\reset@font\bfseries ??}}%
   {\ifnumequal{\xpageref{#2:end}}{\xpageref{#2:start}}%
       {%
       \ifcsvoid{#3onlypage@prefixsingle}%
         {}%
         {\csletcs{Xendbeforepagenumber@#3onlypage}{#3onlypage@prefixsingle}}%
       \printnpnum{%
         \wrap@edcrossref{#2:start}{\xpageref{#2:start}}%
         }%
       }%
     {%
      \ifcsvoid{#3onlypage@prefixmore}%
        {}%
        {\csletcs{Xendbeforepagenumber@#3onlypage}{#3onlypage@prefixmore}}%
      \ifdefined\linerangesep@%
        \printnpnum{%
          \wrap@edcrossref{#2:start}{\xpageref{#2:start}}%
          \linerangesep@%
          \wrap@edcrossref{#2:end}{\xpageref{#2:end}}%
        }%
      \else%
        \printnpnum{%
          \wrap@edcrossref{#2:start}{\xpageref{#2:start}}%
          \csuse{Xendlinerangeseparator@\@currentseries}%
          \wrap@edcrossref{#2:end}{\xpageref{#2:end}}%
        }%
      \fi%
     }%
   }%
  \def\do##1{%
    \setkeys[mac]{falsefootnoteoption}{##1}%
  }%
 \notblank{#1}{\docsvlist{#1}}{}%
}%
\newcommand*{\edmakelabel}[2]{\expandafter\xdef\csname the@label#1\endcsname{#2}}

\pretocmd{\XR@test}%
  {\XR@test@mac+++#1#2#3#4+++}%
  {}%
  {}%
\long\def\XR@test@mac+++#1+++{\XR@test@mac@test#1}
\long\def\XR@test@mac@test#1#2...{%The triple dots (NOT \ldots) are because of the line 22 of xr.sty  v5.02 1994/05/28
  \ifx#1\l@dmake@labels%
   \l@dmake@labels#2%
  \else
    \ifx#1\l@dmake@labelsR%
      \l@dmake@labelsR #2%
    \fi%
  \fi%
}%
\pretocmd{\@xympar}%
  {\ifnumberedpar@
    \led@warn@NoMarginpars
    \@esphack
  \else}%
  {}%
  {}%

\apptocmd{\@xympar}%
  {\fi}%
  {}
  {}

\newcount\sidenote@margin
\newcommand*{\sidenotemargin}[1]{{%
  \l@dgetsidenote@margin{#1}%
  \ifnum\@l@dtempcntb>\m@ne
    \ifledRcol
      \global\sidenote@marginR=\@l@dtempcntb
    \else
      \global\sidenote@margin=\@l@dtempcntb
    \fi
  \fi}}
\newcommand*{\l@dgetsidenote@margin}[1]{%
  \def\@tempa{#1}\def\@tempb{left}%
  \ifx\@tempa\@tempb
    \@l@dtempcntb \z@
  \else
    \def\@tempb{right}%
    \ifx\@tempa\@tempb
      \@l@dtempcntb \@ne
    \else
      \def\@tempb{outer}%
      \ifx\@tempa\@tempb
        \@l@dtempcntb \tw@
      \else
        \def\@tempb{inner}%
        \ifx\@tempa\@tempb
          \@l@dtempcntb \thr@@
        \else
          \led@warn@BadSidenotemargin
          \@l@dtempcntb \m@ne
        \fi
      \fi
    \fi
  \fi}
\sidenotemargin{right}

\newbox\l@dlp@rbox
\newbox\l@drp@rbox

\newdimen\ledlsnotewidth \ledlsnotewidth=\marginparwidth
\newdimen\ledrsnotewidth \ledrsnotewidth=\marginparwidth
\newdimen\ledlsnotesep \ledlsnotesep=\linenumsep
\newdimen\ledrsnotesep \ledrsnotesep=\linenumsep
\newcommand*{\ledlsnotefontsetup}{\raggedleft\footnotesize}
\newcommand*{\ledrsnotefontsetup}{\raggedright\footnotesize}

\newcommand*{\ledleftnote}[1]{\edtext{}{\l@dlsnote{#1}}}
\newcommand*{\ledrightnote}[1]{\edtext{}{\l@drsnote{#1}}}
\newcommand*{\ledsidenote}[1]{\edtext{}{\l@dcsnote{#1}}}%
\newcommand*{\ledinnernote}[1]{\edtext{}{\l@disnote{#1}}}%
\newcommand*{\ledouternote}[1]{\edtext{}{\l@dosnote{#1}}}%
\newif\ifrightnoteup
  \rightnoteuptrue

\newcommand*{\l@dlsnote}[1]{%
  \begingroup%
  \newcommand{\content}{#1}%
  \ifnumberedpar@
    \ifledRcol%
      \xright@appenditem{\noexpand\vl@dlsnote{\expandonce\content}}%
                         \to\inserts@listR
      \global\advance\insert@countR \@ne%
    \else%
      \xright@appenditem{\noexpand\vl@dlsnote{\expandonce\content}}%
                         \to\inserts@list
      \global\advance\insert@count \@ne%
    \fi
  \fi%
  \ignorespaces%
  \endgroup%
}%

\newcommand*{\l@drsnote}[1]{%
  \begingroup%
  \newcommand{\content}{#1}%
  \ifnumberedpar@
    \ifledRcol%
      \xright@appenditem{\noexpand\vl@drsnote{\expandonce\content}}%
                         \to\inserts@listR
       \global\advance\insert@countR \@ne%
    \else%
      \xright@appenditem{\noexpand\vl@drsnote{\expandonce\content}}%
                         \to\inserts@list
      \global\advance\insert@count \@ne%
    \fi
  \fi\ignorespaces%
  \endgroup%
}%

\newcommand*{\l@dcsnote}[1]{%
  \begingroup%
  \newcommand{\content}{#1}%
  \ifnumberedpar@
    \ifledRcol%
      \xright@appenditem{\noexpand\vl@dcsnote{\expandonce\content}}%
                         \to\inserts@listR
       \global\advance\insert@countR \@ne%
    \else%
      \xright@appenditem{\noexpand\vl@dcsnote{\expandonce\content}}%
                         \to\inserts@list
      \global\advance\insert@count \@ne%
    \fi
  \fi\ignorespaces%
  \endgroup%
}%

\newcommand*{\l@disnote}[1]{%
  \begingroup%
  \newcommand{\content}{#1}%
  \ifnumberedpar@%
    \ifledRcol%
      \xright@appenditem{\noexpand\vl@disnote{\expandonce\content}}%
                         \to\inserts@listR%
       \global\advance\insert@countR \@ne%
    \else%
      \xright@appenditem{\noexpand\vl@disnote{\expandonce\content}}%
                         \to\inserts@list%
      \global\advance\insert@count \@ne%
    \fi%
  \fi\ignorespaces%
  \endgroup%
}%

\newcommand*{\l@dosnote}[1]{%
  \begingroup%
  \newcommand{\content}{#1}%
  \ifnumberedpar@%
    \ifledRcol%
      \xright@appenditem{\noexpand\vl@dosnote{\expandonce\content}}%
                         \to\inserts@listR%
       \global\advance\insert@countR \@ne%
    \else%
      \xright@appenditem{\noexpand\vl@dosnote{\expandonce\content}}%
                         \to\inserts@list%
      \global\advance\insert@count \@ne%
    \fi%
  \fi\ignorespaces%
  \endgroup%
}%

\newcommand*{\vl@dlsnote}[1]{%
  \ifledRcol@%
    \@l@dtempcntb=\sidenote@marginR%
    \ifnum\@l@dtempcntb>\@ne%
      \advance\@l@dtempcntb by\page@numR%
    \fi%
  \else%
    \@l@dtempcntb=\sidenote@margin%
    \ifnum\@l@dtempcntb>\@ne%
      \advance\@l@dtempcntb by\page@num%
    \fi%
  \fi%
  \ifodd\@l@dtempcntb%
    \listgadd{\l@dcsnotetext@l}{#1}%
  \else%
    \listgadd{\l@dcsnotetext}{#1}%
  \fi
}
\newcommand*{\vl@drsnote}[1]{%
  \ifledRcol@%
    \@l@dtempcntb=\sidenote@marginR%
    \ifnum\@l@dtempcntb>\@ne%
      \advance\@l@dtempcntb by\page@numR%
    \fi%
  \else%
    \@l@dtempcntb=\sidenote@margin%
    \ifnum\@l@dtempcntb>\@ne%
      \advance\@l@dtempcntb by\page@num%
    \fi%
  \fi%
  \ifodd\@l@dtempcntb%
    \listgadd{\l@dcsnotetext}{#1}%
  \else%
    \listgadd{\l@dcsnotetext@r}{#1}%
  \fi%
}
\newcommand*{\vl@dcsnote}[1]{\listgadd{\l@dcsnotetext}{#1}}

\newcommand{\vl@disnote}[1]{%
  \ifledRcol@%
    \@tempcnta=\page@numR%
  \else%
    \@tempcnta=\page@num%
  \fi%
  \ifodd\@tempcnta% ODD => right page => inner side =  left side
    \vl@dlsnote{#1}%
  \else%
    \vl@drsnote{#1}%
  \fi%
}%

\newcommand{\vl@dosnote}[1]{%
  \ifledRcol@%
    \@tempcnta=\page@numR%
  \else%
    \@tempcnta=\page@num%
  \fi%
  \ifodd\@tempcnta% ODD => right page => outer side = right side
    \vl@drsnote{#1}%
  \else%
    \vl@dlsnote{#1}%
  \fi%
}%

\newcommand*{\setl@dlp@rbox}[1]{%
   \begingroup%
     \parindent\z@\hsize=\ledlsnotewidth%
     \ledlsnotefontsetup%We kept it outside of the vbox, because can affect the ragging
     \global\setbox\l@dlp@rbox%
     \ifleftnoteup%
       =\vbox to\z@{{\ledlsnotefontsetup\vss #1}}%We put \ledlsnotefontsetup inside footnote because required for color command. Note the {} to keep setting local.
     \else%
       =\vbox to 0.70\baselineskip{{\ledlsnotefontsetup\strut#1\vss}}%
     \fi%
   \endgroup%
}

\newcommand*{\setl@drp@rbox}[1]{%
  \begingroup%
     \parindent\z@\hsize=\ledrsnotewidth%
     \ledrsnotefontsetup%We kept it outside of the vbox, because can affect the ragging
     \global\setbox\l@drp@rbox%
     \ifrightnoteup%
       =\vbox to\z@{{\ledrsnotefontsetup\vss#1}}%We put \ledrsnotefontsetup inside footnote because required for color command. Note the {} to keep setting local.
     \else%
       =\vbox to0.7\baselineskip{{\ledrsnotefontsetup\strut#1\vss}}%
     \fi%
  \endgroup%
}%
\newif\ifleftnoteup
  \leftnoteuptrue
\newcommand{\setsidenotesep}[1]{\gdef\@sidenotesep{#1}}
\newcommand{\@sidenotesep}{, }
\newcommand*{\affixside@note}{%
    \prepare@edindex@fornote{\the\page@num|\the\line@num|\the\subline@num|\the\page@num|\the\line@num|\the\subline@num|}%
    \def\sidenotecontent@{}%
    \numgdef{\itemcount@}{0}%
    \def\do##1{%
        \ifnumequal{\itemcount@}{0}%
            {%
            \appto\sidenotecontent@{##1}}% Not print not separator before the 1st note
            {\appto\sidenotecontent@{\@sidenotesep ##1}%
            }%
            \numgdef{\itemcount@}{\itemcount@+\@ne}%
    }%
    \dolistloop{\l@dcsnotetext}%
    \ifnumgreater{\itemcount@}{1}{\led@err@ManySidenotes}{}%
  \gdef\@templ@d{}%
  \gdef\@templ@n{\l@dcsnotetext\l@dcsnotetext@l\l@dcsnotetext@r}%
  \ifx\@templ@d\@templ@n \else%
    \if@twocolumn%
      \if@firstcolumn%
        \setl@dlp@rbox{##1}{\sidenotecontent@}%
      \else%
        \setl@drp@rbox{\sidenotecontent@}%
      \fi%
    \else%
      \@l@dtempcntb=\sidenote@margin%
      \ifnum\@l@dtempcntb>\@ne%
        \advance\@l@dtempcntb by\page@num%
      \fi%
      \ifodd\@l@dtempcntb%
        \setl@drp@rbox{\sidenotecontent@}%
        \gdef\sidenotecontent@{}%
        \numgdef{\itemcount@}{0}%
        \dolistloop{\l@dcsnotetext@l}%
        \ifnumgreater{\itemcount@}{1}{\led@err@ManyLeftnotes}{}%
        \setl@dlp@rbox{\sidenotecontent@}%
      \else%
        \setl@dlp@rbox{\sidenotecontent@}%
        \gdef\sidenotecontent@{}%
        \numgdef{\itemcount@}{0}%
        \dolistloop{\l@dcsnotetext@r}%
        \ifnumgreater{\itemcount@}{1}{\led@err@ManyRightnotes}{}%
        \setl@drp@rbox{\sidenotecontent@}%
      \fi%
    \fi%
  \fi%
  \advance\@edindex@fornote@\m@ne%
}
\ifnoledgroup@\else%
\newcommand*{\l@dfeetbeginmini}{\@ledgrouptrue\l@dedbeginmini\l@dfambeginmini}
\newcommand*{\l@dfeetendmini}{%
    \IfStrEq{critical-familiar}{\@mpfnpos}%
        {\l@dedendmini\l@dfamendmini}%
        {%
            \IfStrEq{familiar-critical}{\@mpfnpos}%
                {\l@dfamendmini\l@dedendmini}%
                {\do@feet@custom@order{mp@}{\@mpfnpos}}%
        }%
    }%
\newcommand*{\l@dedbeginmini}{%
  \unless\ifnocritical@%
    \def\do##1{%
      \csletcs{v##1footnote}{mpv##1footnote}%
    }%
    \dolistloop{\@series}%
  \fi%
  }
\newcommand*{\l@dedendmini}{%
  \unless\ifnocritical@%
    \ifl@dpairing%
      \ifledRcol%
        \flush@notesR%
      \else%
        \flush@notes%
      \fi%
    \fi
    \def\do##1{%
      \mp@append@Xnotes{##1}%
    }%
    \dolistloop{\@series}%
  \fi%
}%
\newcommand{\mp@append@Xnotes}[1]{%
\ifvoid\csuse{mp#1footins}\else%
  \ifl@dpairing%
    \ifparledgroup%
      \ifledRcol%
        \dimgdef{\parledgroup@beforenotesR}{\parledgroup@beforenotesR+\skip\@nameuse{mp#1footins}}%
      \else%
         \dimgdef{\parledgroup@beforenotesL}{\parledgroup@beforenotesL+\skip\@nameuse{mp#1footins}}%
      \fi%
     \fi%
   \fi%
  \ifcsstring{series@display#1}{paragraph}{}{%
    \setbox\@nameuse{mp#1footins}=\vbox{%
      \csuse{Xnotefontsize@#1}%
      \ifcsdef{Xhsize\csuse{series@display#1}@#1}{%
        \hsize\csuse{Xhsize\csuse{series@display#1}@#1}%
      }{}%
      \noindent\csuse{Xtxtbeforenotes@#1}%
      \unvbox\@nameuse{mp#1footins}%
      \@parboxrestore%
    }%
  }%
  \csuse{mp#1footgroup}{#1}%
\fi%
}%
\newcommand*{\l@dfambeginmini}{%
 \unless\ifnofamiliar@%
   \def\do##1{\csletcs{vfootnote##1}{mpvfootnote##1}}%
     \dolistloop{\@series}%
 \fi%
}%

\newcommand*{\l@dfamendmini}{%
 \unless\ifnofamiliar@%
   \def\do##1{%
     \mp@append@notesX{##1}%
   }%
   \dolistloop{\@series}%
 \fi%
}%
\newcommand{\mp@append@notesX}[1]{%
  \ifvoid\csuse{mpfootins#1}\else%
    \ifcsstring{series@displayX#1}{paragraph}{}{%
      \setbox\@nameuse{mpfootins#1}=\vbox{%
        \csuse{notefontsizeX@#1}%
        \ifcsdef{hsize\csuse{series@display#1}X@#1}{%
          \hsize\csuse{hsize\csuse{series@display#1}X@#1}%
        }{}%
        \noindent\csuse{txtbeforenotesX@#1}%
        \unvbox\@nameuse{mpfootins#1}%
        \@parboxrestore%
      }%
    }%
    \csuse{mpfootgroup#1}{#1}%
  \fi%
}%
\patchcmd%
  {\@iiiminipage}%
  {\let\@footnotetext\@mpfootnotetext}%
  {\let\@footnotetext\@mpfootnotetext\l@dfeetbeginmini}%
  {}%
  {\led@error@fail@patch@@iiiminipage}%
\patchcmd%
  {\endminipage}%
  {\footnoterule}%
  {\footnoterule\l@advance@parledgroup@beforenormalnotes}%
  {}%
  {\led@error@fail@patch@endminipage}

\patchcmd%
  {\endminipage}%
  {\@minipagefalse}%
  {\l@dfeetendmini\@minipagefalse}%
  {}%
  {\led@error@fail@patch@endminipage}

\newcommand*{\l@dunboxmpfoot}{%
    \vskip\skip\@mpfootins
    \normalcolor
    \footnoterule
    \l@advance@parledgroup@beforenormalnotes
    \unvbox\@mpfootins%
}
\newcommand{\l@advance@parledgroup@beforenormalnotes}{%
  \ifparledgroup
    \ifl@dpairing
      \ifledRcol
          \dimgdef{\parledgroup@beforenotesR}{\parledgroup@beforenotesR+\skip\@mpfootins}
      \else
          \dimgdef{\parledgroup@beforenotesL}{\parledgroup@beforenotesL+\skip\@mpfootins}
      \fi
    \fi
  \fi
}

\newenvironment{ledgroup}{%
  \resetprevpage@num%
  \def\@mpfn{mpfootnote}\def\thempfn{\thempfootnote}\c@mpfootnote\z@%
  \let\@footnotetext\@mpfootnotetext
  \l@dfeetbeginmini%
}{%
  \par
  \unskip
  \ifvoid\@mpfootins\else
    \l@dunboxmpfoot
  \fi
  \l@dfeetendmini%
  \@ledgroupfalse%
}

\newenvironment{ledgroupsized}[2][l]{%
  \hsize #2\relax
  \let\ledllfill\hfil
  \let\ledrlfill\hfil
  \def\@tempa{#1}\def\@tempb{l}%
      \ifx\@tempa\@tempb
    \let\ledllfill\relax
  \else
    \def\@tempb{r}%
    \ifx\@tempa\@tempb
      \let\ledrlfill\relax
    \fi
  \fi
  \def\@mpfn{mpfootnote}\def\thempfn{\thempfootnote}\c@mpfootnote\z@
  \let\@footnotetext\@mpfootnotetext
  \l@dfeetbeginmini%
}{%
  \par
  \unskip
  \ifvoid\@mpfootins\else
    \l@dunboxmpfoot
  \fi
  \l@dfeetendmini%
}

\fi%
\newif\ifledgroupnotesL@
\newif\ifledgroupnotesR@
\AtBeginDocument{%
  \unless\ifl@imakeidx%
    \@ifpackageloaded{imakeidx}{\led@error@PackageAfterEledmac{imakeidx}}{}%
  \fi%
  \unless\ifl@indextools%
    \@ifpackageloaded{indextools}{\led@error@PackageAfterEledmac{indextools}}{}%
  \fi%
  \unless\ifl@footmisc%
    \@ifpackageloaded{footmisc}{\led@error@PackageAfterEledmac{footmisc}}{}%
  \fi%
}
\newcommand{\pagelinesep}{-}
\newcommand{\edindexlab}{$&}
\newcounter{labidx}
\setcounter{labidx}{0}

\newcommand{\doedindexlabel}{%
  \stepcounter{labidx}%
  \edlabel{\edindexlab\thelabidx}%
}

\newcounter{pageline}%
\renewcommand{\thepageline}{%
  \thepage%
  \pagelinesep%
  \xlineref{\edindexlab\thelabidx}%
}
\newcommand{\thestartpageline}{%
  \l@dparsedstartpage%
  \pagelinesep%
  \l@dparsedstartline%
}
\newcommand{\theendpageline}{%
  \l@dparsedendpage%
  \pagelinesep%
  \l@dparsedendline%
}
\newcount\@edindex@fornote@
\newcommand{\prepare@edindex@fornote}[1]{%
    \l@dp@rsefootspec#1|%
    \advance\@edindex@fornote@\@ne%
}
\newcommand{\get@edindex@ledinnote@command}{%
  \ifxindy@%
    \gdef\@ledinnote@command{%
      ledinnote\thelabidx%
    }%
    \ifxindyhyperref@%
      \immediate\write\eledmac@xindy@out{%
        (define-attributes ("ledinnote\thelabidx"))^^J
        \space\space(markup-locref^^J
          \eledmacmarkuplocrefdepth^^J
          :open "\string\ledinnote[\edindexlab\thelabidx]{\@index@command}{\@currentseries}{\@currentnotetype}{"^^J
          :close "}"^^J
          :attr "ledinnote\thelabidx"^^J
          )
        }%
    \else%
      \immediate\write\eledmac@xindy@out{%
        (define-attributes ("ledinnote\thelabidx"))^^J
        \space\space(markup-locref^^J
          \eledmacmarkuplocrefdepth^^J
          :open "\string\ledinnote{\@index@command}{\@currentseries}{\@currentnotetype}{"^^J
          :close "}"^^J
          :attr "ledinnote\thelabidx"^^J
          )
        }%
    \fi%
  \else%
    \gdef\@ledinnote@command{%
      ledinnote[\edindexlab\thelabidx]{\@index@command}{\@currentseries}{\@currentnotetype}%
    }%
  \fi%
}
\def\get@index@command#1|#2+{%
  \gdef\@index@txt{#1}%
  \gdef\@index@command{#2}%
  \xdef\@index@parenthesis{}%
  \IfBeginWith{\@index@command}{(}{%
    \StrGobbleLeft{\@index@command}{1}[\@index@command@]%
    \global\let\@index@command\@index@command@%
    \xdef\@index@parenthesis{(}%
    }{}%
  \IfBeginWith{\@index@command}{)}{%
    \StrGobbleLeft{\@index@command}{1}[\@index@command@]%
    \global\let\@index@command\@index@command@%
    \xdef\@index@parenthesis{)}%
    }{}%
}
\newcommandx{\ledinnote}[5][1,usedefault]{%
  \IfStrEq{#4}{critical}{%
    \def\tmp{Xinnotemark}%
  }{%
    \def\tmp{innotemarkX}%
  }%
  \ifboolexpr{%
    test{\ifdefequal{\iftrue}{\ifHy@hyperindex}}%
    or%
    bool {xindyhyperref@}%
    }%
    {%
      \csuse{#2}{\hyperlink{#1}{\csuse{\tmp @#3}{#5}}}%
    }%
    {%
    \csuse{#2}{\csuse{\tmp @#3}{#5}}%
    }%
}%
\newcommand{\ledinnotemark}[1]{#1\emph{n}}%
\newcommandx{\@wredindex}[2][1=\expandonce\jobname,usedefault]{%#1 = the index name, #2 = the text
  \begingroup%
    \let\emph\@firstofone%
    \let\textbf\@firstofone%
    \let\textit\@firstofone%
    \let\textmd\@firstofone%
    \let\textnormal\@firstofone%
    \let\textrm\@firstofone%
    \let\textsc\@firstofone%
    \let\textsf\@firstofone%
    \let\textsl\@firstofone%
    \let\texttt\@firstofone%
    \let\textup\@firstofone%
    \xdef\@tmp{#2}%To be used in IfSubStr instead of #2 directly. Avoid some expansion bugs (for example with \edindex{textsc{something}})
  \endgroup%
  \ifl@imakeidx%
    \ifnum\@edindex@fornote@>\z@%
      \IfSubStr[1]{\@tmp}{|}{\get@index@command#2+}{\get@index@command#2|+}%
      \get@edindex@ledinnote@command%
      \expandafter\imki@wrindexentry{#1}{\@index@txt|(\@ledinnote@command}{\thestartpageline}%
      \expandafter\imki@wrindexentry{#1}{\@index@txt|)\@ledinnote@command}{\theendpageline}%
    \else%
      \get@edindex@hyperref{#2}%
      \imki@wrindexentry{#1}{\@index@txt\@edindex@hyperref}{\thepageline}%
    \fi%
  \else%
    \ifnum\@edindex@fornote@>\z@%
      \IfSubStr[1]{\@tmp}{|}{\get@index@command#2+}{\get@index@command#2|+}%
      \get@edindex@ledinnote@command%
      \expandafter\protected@write\@indexfile{}%
  {\string\indexentry{\@index@txt|(\@ledinnote@command}{\thestartpageline}
  }%
      \expandafter\protected@write\@indexfile{}%
  {\string\indexentry{\@index@txt|)\@ledinnote@command}{\theendpageline}
       }%
    \else%
      \protected@write\@indexfile{}%
  {\string\indexentry{#2}{\thepageline}
  }%
    \fi%
   \fi%
   \endgroup
   \@esphack%
}
\pretocmd{\makeindex}{%
  \def\edindex{%
    \ifboolexpr{bool{numbering} or bool{numberingR} or bool{l@dprintingpages} or bool{l@dprintingcolumns}}{%
      \@bsphack%
      \doedindexlabel%
      \begingroup%
      \@sanitize%
      \@wredindex%
    }%
    {%
      \led@warn@edinde@outsidenumbering%
      \index%
    }%
  }%
}%
{}%
{\led@error@fail@patch@makeindex}%
\newcommand{\edindex}[1]{\@bsphack\@esphack}
\newcommandx{\dummy@edindex}[2][1=\expandonce\jobname,usedefault]{}%
\newcommand{\disable@edindex}{\let\old@edindex\edindex\let\edindex\dummy@edindex}%
\newcommand{\restore@edindex}{\let\edindex\old@edindex}%
\newcommand{\hyperlinkformat}[3]{%
  \ifstrempty{#1}%
    {\hyperlink{#2}{#3}}%
    {\csuse{#1}{\hyperlink{#2}{#3}}%
  }}
\newcommand{\hyperlinkR}[2]{%
 \hyperlink{#1}{#2\@Rlineflag}%
}%

\newcommand{\hyperlinkformatR}[3]{%
  \hyperlinkformat{#1}{#2}{#3\@Rlineflag}%
}%

\newcommand{\get@edindex@hyperref}[1]{%
  \edef\temp@{%
  \catcode`\ =9 %space need for catcode
  \detokenize{#1}%For active character in unicode
  \catcode`\ =10 % space need for catcode
  }%
  \ifdefequal{\iftrue}{\ifHy@hyperindex}{%
    \IfSubStr{\temp@}{|}%
      {\get@index@command#1+%
      \ifledRcol%
        \gdef\@edindex@hyperref{|\@index@parenthesis %space kept
        hyperlinkformatR{\@index@command}%
        {\edindexlab\thelabidx}}%
      \else%
        \gdef\@edindex@hyperref{|\@index@parenthesis %space kept
        hyperlinkformat{\@index@command}%
        {\edindexlab\thelabidx}}%
      \fi%
      }%
      {\get@index@command#1|+%
       \ifledRcol%
        \gdef\@edindex@hyperref{|hyperlinkR{\edindexlab\thelabidx}}%
       \else%
        \gdef\@edindex@hyperref{|hyperlink{\edindexlab\thelabidx}}%
       \fi%
      }%
    }%
    {\ifxindyhyperref@%
      \IfSubStr{\temp@}{|}%
        {\get@index@command#1+}%
        {\get@index@command#1|+}%
     \gdef\@edindex@hyperref{|eledmac\thelabidx}%
      \IfStrEq{\@index@parenthesis}{(}%
        {%
        \csxdef{xindyparenthesis@\@index@txt}{\thelabidx}%
        \gdef\@edindex@hyperref{|(eledmac\thelabidx}%
        }%
        {}%
      \IfStrEq{\@index@parenthesis}{)}%
        {%
        \xdef\@edindex@hyperref{|)eledmac\csuse{xindyparenthesis@\@index@txt}}%
        \global\csundef{xindyparenthesis@\@index@txt}%
        }%
        {%
        \immediate\write\eledmac@xindy@out{%
          (define-attributes ("eledmac\thelabidx"))^^J
          \space\space(markup-locref^^J
            \eledmacmarkuplocrefdepth^^J
            :open "\string\hyperlink%
                   \ifledRcol R\fi%
                   {\edindexlab\thelabidx}%
                   {\ifdefempty{\@index@command}%
                     {}%
                     {\@backslashchar\@index@command}%
                   {"^^J
            :close "}}"^^J
            :attr "eledmac\thelabidx"^^J
            )
          }%
        }%
    \else%
      \gdef\@index@txt{#1}%
      \gdef\@edindex@hyperref{}%
    \fi%
    }%
}
\newcommand{\led@set@index@fornote}[1]{%
  \ifbool{indtl@innote}%
    {\let\index\nindex}%
    {}%
  \ifbool{indtl@notenumber}%
    {%
    \renewcommand{\index}[2][\indtl@jobname]{%
      \orig@@index[##1]{%
        ##2|innotenumber{\this@footnoteX@reading}%
        }%
      }%
    }%
    {}%
}%
\newcommand{\led@reinit@index@fornote}{%
  \ifbool{indtl@innote}%
    {\let\index\orig@@index}%
    {}%
 \ifbool{indtl@notenumber}%
  {\let\index\orig@@index}%
  {}%
}%
\AtBeginDocument{%
  \@ifpackageloaded{glossaries}{%
    \gdef\edglsomo{}%
    \listgadd{\edglsomo}{gls}%
    \listgadd{\edglsomo}{Gls}%
    \listgadd{\edglsomo}{GLS}%
    \listgadd{\edglsomo}{glspl}%
    \listgadd{\edglsomo}{Glspl}%
    \listgadd{\edglsomo}{GLSpl}%
    \listgadd{\edglsomo}{glstext}%
    \listgadd{\edglsomo}{Glstext}%
    \listgadd{\edglsomo}{GLStext}%
    \listgadd{\edglsomo}{Glsfirst}%
    \listgadd{\edglsomo}{GLSfirst}%
    \listgadd{\edglsomo}{glsplural}%
    \listgadd{\edglsomo}{Glsplural}%
    \listgadd{\edglsomo}{GLSplural}%
    \listgadd{\edglsomo}{glsfirstplural}%
    \listgadd{\edglsomo}{Glsfirstplural}%
    \listgadd{\edglsomo}{GLSfirstplural}%
    \listgadd{\edglsomo}{glsname}%
    \listgadd{\edglsomo}{Glsname}%
    \listgadd{\edglsomo}{GLSname}%
    \listgadd{\edglsomo}{glssymbol}%
    \listgadd{\edglsomo}{Glssymbol}%
    \listgadd{\edglsomo}{GLSsymbol}%
    \listgadd{\edglsomo}{glsdesc}%
    \listgadd{\edglsomo}{Glsdesc}%
    \listgadd{\edglsomo}{GLSdesc}%
    \listgadd{\edglsomo}{glsuseri}%
    \listgadd{\edglsomo}{Glsuseri}%
    \listgadd{\edglsomo}{GLSuseri}%
    \listgadd{\edglsomo}{glsuserii}%
    \listgadd{\edglsomo}{Glsuserii}%
    \listgadd{\edglsomo}{GLSuserii}%
    \listgadd{\edglsomo}{glsuseriii}%
    \listgadd{\edglsomo}{Glsuseriii}%
    \listgadd{\edglsomo}{GLSuseriii}%
    \listgadd{\edglsomo}{glsuseriv}%
    \listgadd{\edglsomo}{Glsuseriv}%
    \listgadd{\edglsomo}{GLSuseriv}%
    \listgadd{\edglsomo}{glsuserv}%
    \listgadd{\edglsomo}{Glsuserv}%
    \listgadd{\edglsomo}{GLSuserv}%
    \listgadd{\edglsomo}{glsuservi}%
    \listgadd{\edglsomo}{Glsuservi}%
    \listgadd{\edglsomo}{GLSuservi}%
    \renewcommand{\do}[1]{%
      \expandafter\DeclareRobustCommandx\csname ed#1\endcsname[3][1,3,usedefault]{%
        \doedindexlabel%
        \ifdef{\hypertarget}{%
          \Hy@raisedlink@left{\hypertarget{pageline.\thepageline}{}}%
        }{}%
        \csname#1\endcsname[counter=pageline,##1]{##2}[##3]%
      }%
      \expandafter\WithSuffix\expandafter\DeclareRobustCommandx\csname ed#1\endcsname*[3][1,3,usedefault]{%
        \doedindexlabel%
        \ifdef{\hypertarget}{%
          \Hy@raisedlink@left{\hypertarget{pageline.\thepageline}{}}%
        }{}%
        \csname#1\endcsname*[counter=pageline,##1]{##2}[##3]%
      }%
    }%
    \dolistloop{\edglsomo}%
    \gdef\edglsomm{}%
    \listgadd\edglsomm{glsdisp}%
    \listgadd\edglsomm{glslink}%
    \renewcommand{\do}[1]{%
      \expandafter\DeclareRobustCommandx\csname ed#1\endcsname[3][1,usedefault]{%
        \doedindexlabel%
        \ifdef{\hypertarget}{%
          \Hy@raisedlink@left{\hypertarget{pageline.\thepageline}{}}%
        }{}%
        \csname#1\endcsname[counter=pageline,##1]{##2}{##3}%
      }%
      \expandafter\WithSuffix\expandafter\DeclareRobustCommandx\csname ed#1\endcsname*[3][1,usedefault]{%
        \doedindexlabel%
        \ifdef{\hypertarget}{%
          \Hy@raisedlink@left{\hypertarget{pageline.\thepageline}{}}%
        }{}%
        \csname#1\endcsname*[counter=pageline,##1]{##2}{##3}%
      }%
    }%
    \dolistloop{\edglsomm}%
    \gdef\edglsom{}%
    \listgadd\edglsom{glsadd}%
    \renewcommand{\do}[1]{%
      \expandafter\DeclareRobustCommandx\csname ed#1\endcsname[2][1,usedefault]{%
        \doedindexlabel%
        \ifdef{\hypertarget}{%
          \Hy@raisedlink@left{\hypertarget{pageline.\thepageline}{}}%
        }{}%
        \csname#1\endcsname[counter=pageline,##1]{##2}%
      }%
      \expandafter\WithSuffix\expandafter\DeclareRobustCommandx\csname ed#1\endcsname*[2][1,usedefault]{%
        \doedindexlabel%
        \ifdef{\hypertarget}{%
          \Hy@raisedlink@left{\hypertarget{pageline.\thepageline}{}}%
        }{}%
        \csname#1\endcsname*[counter=pageline,##1]{##2}%
      }%
    }%
    \dolistloop{\edglsom}%
    \def\l@noexpands@edgl{}%
    \renewcommand\do[1]{%
      \xappto\l@noexpands@edgl{%
        \noexpand\RenewDocumentCommand\csname ed#1\endcsname{omos}{}%
      }%
    }%
    \dolistloop{\edglsomo}%
    \renewcommand\do[1]{%
      \xappto\l@noexpands@edgl{%
        \noexpand\RenewDocumentCommand\csname ed#1\endcsname{omms}{}%
      }%
    }%
    \dolistloop{\edglsomm}%
    \renewcommand\do[1]{%
      \xappto\l@noexpands@edgl{%
        \noexpand\RenewDocumentCommand\csname ed#1\endcsname{oms}{}%
      }%
    }%
    \dolistloop{\edglsom}%
  }{\let\l@noexpands@edgl\relax}%
}%
\def\@hangingsymbol{}
\newcommand*{\sethangingsymbol}[1]{%
  \gdef\@hangingsymbol{#1}%
}%
\newif\ifinstanza
\newif\ifinserthangingsymbol
\newcommand{\inserthangingsymbol}{%
\ifinserthangingsymbol%
   \ifinstanza%
      \@hangingsymbol%
   \fi%
\fi%
}
\newcommand*{\ampersand}{\char`\&}

  \chardef\body=\catcode`\@
  \catcode`\@=11
  \chardef\next=\catcode`\&
  \catcode`\&=\active

  \newcount\stanza@count
  \newlength{\stanzaindentbase}
  \setlength{\stanzaindentbase}{20pt}

\def\strip@szacnt#1,#2|{\def\@tempb{#1}\def\@tempa{#2|}}
\newcommand*{\setstanzavalues}[2]{\def\@tempa{#2,,|}%
        \stanza@count\z@
     \def\next{\expandafter\strip@szacnt\@tempa
            \ifx\@tempb\empty\let\next\relax\else
            \expandafter\mathchardef\csname #1@\number\stanza@count
            @\endcsname\@tempb\relax
            \advance\stanza@count\@ne\fi\next}%
      \next}

\newcommand*{\setstanzaindents}[1]{\setstanzavalues{sza}{#1}}
\newcommand*{\setstanzapenalties}[1]{\setstanzavalues{szp}{#1}}
\newcounter{stanzaindentsrepetition}
\newcounter{startstanzaindentsrepetition}%
\setcounter{startstanzaindentsrepetition}{1}%
\newcount\stanza@modulo

\newcommand*{\managestanza@modulo}[0]{%
    \advance\stanza@modulo\@ne%
    \ifnum\stanza@modulo>\value{stanzaindentsrepetition}%
     \stanza@modulo\@ne%
    \fi%
}
\newcommand{\stanzaindent}[1]{%
  \hspace{\dimexpr#1\stanzaindentbase-\parindent\relax}%
  \ignorespaces%
}%
\WithSuffix\newcommand\stanzaindent*[1]{%
  \stanzaindent{#1}%
  \global\advance\stanza@modulo-\@ne%
  \ifnum\stanza@modulo=0%
    \global\stanza@modulo=\value{stanzaindentsrepetition}%
  \fi%
  \ignorespaces%
}%
\newcounter{stanza}
\renewcommand{\thestanza}{%
  \textbf{\arabic{stanza}}%
}
\newif\ifnumberstanza%
\newcommand{\@insertstanzanumber}[0]{%
  \ifnumberstanza%
    \ifl@dpairing%
      \ifledRcol%
      \stanzanumwrapper{\thestanzaR}%
      \else%
      \stanzanumwrapper{\thestanzaL}%
      \fi%
    \else%
      \stanzanumwrapper{\thestanza}%
    \fi%
    \setline{1}%
  \fi%
}%
\newcommand{\@advancestanzanumber}[0]{%
  \ifnumberstanza%
    \ifl@dpairing%
      \ifledRcol%
        \addtocounter{stanzaR}{1}%
      \else%
        \addtocounter{stanzaL}{1}%
      \fi%
    \else%
      \addtocounter{stanza}{1}%
    \fi%
  \fi%
}%
\newcommand{\stanzanumwrapper}[1]{%
  \flagstanza{#1}%
}%
\newcommand{\printstanza}[0]{%
   \ifboolexpr{bool{l@dpairing} or bool{l@dprintingpages} or bool{l@dprintingcolumns}}{%
        \ifledRcol@%
            \thestanzaR%
        \else%
            \thestanzaL%
        \fi%
    }{%
        \thestanza%
    }%
}

\newcommandx{\stanza@line}[2][1,2,usedefault]{%
  \stanza@indent%
  \pstart[#1][#2]\stanza@hang\ignorespaces%
}%
\newcommand{\stanza@indent}{%
    \ifboolexpr{%
      test {\ifnumequal{\value{stanzaindentsrepetition}}{0}}%
      or%
      test {\ifnumless{\number\stanza@count}{\value{startstanzaindentsrepetition}}}%
    }{%
        \ifcsdef{sza@\number\stanza@count @}%
          {%
          \parindent=\csname sza@\number\stanza@count  @\endcsname\stanzaindentbase%
          }{%
          \led@err@StanzaIndentNotDefined%
          }%
    }{%
        \ifcsdef{sza@\number\stanza@modulo @}{%
          \parindent=\csname sza@%
          \the\numexpr%
            \stanza@modulo%
            - 1%
            + \value{startstanzaindentsrepetition}%
          \relax%
          @\endcsname\stanzaindentbase%
          \managestanza@modulo%
        }%
        {%
        \led@err@StanzaIndentNotDefined%
        }%
    }%
}%
\xdef\stanza@hang{\noexpand\leavevmode\noexpand\startlock
            \hangindent\expandafter
            \noexpand\csname sza@0@\endcsname\stanzaindentbase
            \hangafter\@ne}
\def\sza@penalty{\count@\csname szp@\number\stanza@count @\endcsname
         \ifnum\count@>\@M\advance\count@-\@M\penalty-\else
         \penalty\fi\count@}
\xdef\@startstanza[#1][#2]{%
   \noexpand\instanzatrue\expandafter
   \begingroup%
   \catcode`\noexpand\&\active%
   \unexpanded{\newdimen\parindent@beforestanza%
   \parindent@beforestanza=\parindent}%Keep in memory the standard \parindent
   \global\stanza@count\@ne\stanza@modulo\@ne
   \noexpand\ifnum\expandafter\noexpand\csname sza@0@\endcsname=\z@%
      \def\noexpand\stanza@hang{\unexpanded{\leavevmode\null}}%
      \let\noexpand\endlock\relax%
   \noexpand\else%
     \interlinepenalty%
     \@M\rightskip\z@ plus 1fil\relax%
   \noexpand\fi%
   \noexpand\ifnum\expandafter\noexpand\csname szp@0@\endcsname=\z@%
     \let\noexpand\sza@penalty%
   \relax\noexpand\fi%
   \def\noexpand&{%
         \noexpand\newverse[][]}%
   \def\noexpand\&{\noexpand\@stopstanza}%
   \noexpand\@advancestanzanumber%
   \noexpand\stanza@line[#1][#2]%
   \noexpand\@insertstanzanumber%
   \let\par\relax\ignorespaces%No paragraph in verses
}

\newcommandx{\stanza}[2][1,2,usedefault]{%
  \ifboolexpr{%
    not test{\ifdefvoid{\at@every@stanza}}%
    and test{\ifstrempty{#1}}%
    and test{\ifstrempty{#2}}}%
    {\@startstanza[][\at@every@stanza]\at@start@every@stanza}%
    {\@startstanza[#1][#2]\at@start@every@stanza}%
}%

\newcommandx{\@stopstanza}[2][1,2,usedefault]{%
  \unskip%
  \endlock%
  \ifboolexpr{%
    not test{\ifdefvoid{\at@every@stop@stanza}}%
    and test{\ifstrempty{#1}}%
    and test{\ifstrempty{#2}}}%
      {\before@every@stop@stanza\pend[][\at@every@stop@stanza]}%
      {\before@every@stop@stanza\pend[#1][#2]}%
  \endgroup%
  \instanzafalse%
  \global\undef\parindent@beforestanza%
}

\newcommand{\AtEveryStopStanza}[1]{%
  \ifstrempty{#1}%
    {\gdef\at@every@stop@stanza{}}%
    {\gdef\at@every@stop@stanza{\noindent#1}}%
}%
\WithSuffix\newcommand\AtEveryStopStanza*[1]{%
  \ifstrempty{#1}%
    {\gdef\at@every@stop@stanza{}}%
    {\gdef\at@every@stop@stanza{#1}}%
}%
\def\at@every@stop@stanza{}%

\newcommand{\AtEveryStanza}[1]{%
  \ifstrempty{#1}%
    {\gdef\at@every@stanza{}}%
    {\gdef\at@every@stanza{\noindent#1}}%
}%
\WithSuffix\newcommand\AtEveryStanza*[1]{%
  \ifstrempty{#1}%
    {\gdef\at@every@stanza{}}%
    {\gdef\at@every@stanza{#1}}%
}%

\newcommand{\AtStartEveryStanza}[1]{%
  \ifstrempty{#1}%
    {\gdef\at@start@every@stanza{}}%
    {\gdef\at@start@every@stanza{#1}}%
}%
\def\at@start@every@stanza{}%

\newcommand{\BeforeEveryStopStanza}[1]{%
  \ifstrempty{#1}%
    {\gdef\before@every@stop@stanza{}}%
    {\gdef\before@every@stop@stanza{#1}}%
}%
\def\before@every@stop@stanza{}%

\newcommandx*{\newverse}[4][1,2,3,4,usedefault]{%
  \unskip%
  \endlock\pend[#1][#3]\sza@penalty\global%
  \advance\stanza@count\@ne\stanza@line[#2][#4]%
  }

\newcommand*{\flagstanza}[2][\stanzaindentbase]{%
  \hskip -#1\llap{#2}\hskip #1\ignorespaces}

  \catcode`\&=\next
  \catcode`\@=\body
  \setstanzavalues{szp}{0}

\ifantilabe@%
  \newdimen\firsthalf@antilabe@wd%
  \newcommand\get@firsthalf@antilabe@wd{%
    \ifboolexpr{bool {instanza} or bool {inastanzaL} or bool {inastanzaR}}{%
      \vbadness=10000%
      \splittopskip=\z@%
      \ifl@dpairing%
        \ifledRcol%
          \setbox0=\copy\csname l@dRcolrawbox\the\l@dnumpstartsR\endcsname%
        \else%
          \setbox0=\copy\csname l@dLcolrawbox\the\l@dnumpstartsL\endcsname%
        \fi%
      \else%
        \setbox0=\copy\raw@text%
      \fi%
      \dimen0=\dimexpr\ht0+\dp0-\baselineskip\relax%
      \ifdim\dimen0>0pt%
        \setbox1=\vsplit0 to \dimen0%
      \fi%
      \setbox0=\vbox{\unvbox0\global\setbox1=\lastbox}%
      \setbox1=\hbox{\noindent\unhbox1}%
      \global\firsthalf@antilabe@wd=\wd1%
    }{}%
  }%
  \def\beforeantilabe{}%
  \def\afterantilabe{}%
  \newcommand{\antilabe}[1]{%
    \beforeantilabe\hbox to\firsthalf@antilabe@wd{#1\hfill}\afterantilabe%
  }%
\else%
  \let\get@firsthalf@antilabe@wd\relax%
\fi%
\def\msdata@c{}%
\def\msdata@cR{}%
\newcommand{\msdata}[1]{%
  \leavevmode%
  \unless\ifstopmsdata@inserted@%
    \stopmsdata%
    \led@warning@msdatawithoutstop%
  \fi%
  \global\stopmsdata@inserted@false%
  \unless\ifledRcol%
    \numgdef{\msdata@c}{\msdata@c+1}%
    \ifdef{\hypertarget}{%
      \edlabel{\msdata@c:start:msdata}%
    }{}%
    \protected@write\linenum@out{}{%
      \string\@msd{#1}%
    }%
  \else%
    \numgdef{\msdata@cR}{\msdata@cR+1}%
    \ifdef{\hypertarget}{%
      \edlabel{\msdata@cR:start:msdata}%
    }{}%
    \protected@write\linenum@outR{}{%
      \string\@msd{#1}%
    }%
  \fi%
}%
\newcommand{\stopmsdata}[0]{%
  \leavevmode%
  \unless\ifledRcol%
    \protected@write\linenum@out{}{%
      \string\@stopmsd%
    }%
    \ifdef{\hypertarget}{%
      \edlabel{\msdata@c:end:msdata}%
    }{}%
  \else%
    \protected@write\linenum@outR{}{%
      \string\@stopmsd%
    }%
   \ifdef{\hypertarget}{%
     \edlabel{\msdata@cR:end:msdata}%
   }{}%
  \fi%
  \global\stopmsdata@inserted@true%
}%
\newif\ifstopmsdata@inserted@%
\newcommand{\setmsdataseries}[1]{%
  \gdef\@msdata@series{#1}%
}%
\def\@msdata@series{A}%
\def\ms@data@position{msdata-regular}%
\newcommand{\setmsdataposition}[1]{%
  \gdef\ms@data@position{#1}%
}%
\def\ms@data@label{Ms.}%
\newcommand{\setmsdatalabel}[1]{%
  \gdef\ms@data@label{#1}%
}%
\numdef{\@msd@c}{0}
\numdef{\@msd@cR}{0}
\numdef{\add@msd@c}{0}%
\numdef{\add@msd@cR}{0}%
\def\@msdata@list{}%
\newcommand{\@msd}[1]{%
  \unless\ifledRcol%
    \global\numdef{\@msd@c}{\@msd@c+\@ne}%
    \csgdef{@msdata@\@msd@c @data}{#1}%
    \csxdef{@msdata@\@msd@c @linenumber}{\the\line@num}%
    \csxdef{@msdata@\@msd@c @sublinenumber}{\the\subline@num}%
    \csxdef{@msdata@\@msd@c @abslinenumber}{\the\absline@num}%
    \xright@appenditem{\the\absline@num}\to\actionlines@list%
    \xright@appenditem{-1010}\to\actions@list%
  \else%
    \global\numdef{\@msd@cR}{\@msd@cR+\@ne}%
    \csgdef{@msdata@\@msd@cR @dataR}{#1}%
    \csxdef{@msdata@\@msd@cR @linenumberR}{\the\line@numR}%
    \csxdef{@msdata@\@msd@cR @sublinenumberR}{\the\subline@numR}%
    \csxdef{@msdata@\@msd@cR @abslinenumberR}{\the\absline@numR}%
    \xright@appenditem{\the\absline@numR}\to\actionlines@listR%
    \xright@appenditem{-1010}\to\actions@listR%
 \fi%
}%
\newcommand{\@stopmsd}[0]{%
  \unless\ifledRcol%
     \ifcsundef{@msdata@\@msd@c @stoplinenumber}{%
       \csxdef{@msdata@\@msd@c @stopabslinenumber}{\the\absline@num}%
       \csxdef{@msdata@\@msd@c @stoplinenumber}{\the\line@num}%
       \csxdef{@msdata@\@msd@c @stopsublinenumber}{\the\subline@num}%
     }{}%
  \else%
     \ifcsundef{@msdata@\@msd@cR @stoplinenumberR}{%
       \csxdef{@msdata@\@msd@cR @stopabslinenumberR}{\the\absline@numR}%
       \csxdef{@msdata@\@msd@cR @stoplinenumberR}{\the\line@numR}%
       \csxdef{@msdata@\@msd@cR @stopsublinenumberR}{\the\subline@numR}%
     }%
     {}%
 \fi%
}%
\newcommand{\add@msdata}{%
  \bgroup%
  \normalfont%
  \unless\ifledRcol@%
    \numgdef{\add@msd@c}{\add@msd@c+\@ne}%
      \ifcsdef{@msdata@\add@msd@c @data}{%
        \letcs{\@data}{@msdata@\add@msd@c @data}%
        \edef\l@d@nums{%
          000|% Start page = we don't print it
          \csuse{@msdata@\add@msd@c @linenumber}|% Start line number
          \csuse{@msdata@\add@msd@c @sublinenumber}|% Start subline number
          000|% End page number, we don't print it
          \ifnumless{\csuse{@msdata@\add@msd@c @stopabslinenumber}}{\csuse{@lastabsline@forpage@\the\page@num}}%
            {\csuse{@msdata@\add@msd@c @stoplinenumber}}%End line number if in the same page
            {\csuse{@lastline@forpage@\the\page@num}}%Otherwise, last number of the page
          |%
          \ifnumless{\csuse{@msdata@\add@msd@c @stopabslinenumber}}{\csuse{@lastabsline@forpage@\the\page@num}}%
            {\csuse{@msdata@\add@msd@c @stopsublinenumber}}%End subline number if in the same page
            {\csuse{@lastsubline@forpage@\the\page@num}}%Otherwise, last subnumber of the page
          |%
          \edfont@info%Font
          }%
          \@msd@options@fullpagefalse%
          \if@firstlineofpage%Try if the data are for the full page. If yes, will add options to the list.
            \unless\if@msdata@insertedfrompreviouspage%
              \ifnumless{\csuse{@lastabsline@forpage@\the\page@num}}{\csuse{@msdata@\add@msd@c @stopabslinenumber}+\@ne}%
              {%
              \numdef{\@tmp}{\add@msd@c+\@ne}%
              \ifcsdef{@msdata@\@tmp @abslinenumber}%
                {\ifnumequal{\csuse{@msdata@\@tmp @abslinenumber}}{\csuse{@lastabsline@forpage@\the\page@num}}%
                  {}%
                  {\@msd@options@fullpagetrue}%
                }%
                {\@msd@options@fullpagetrue}%
              }%
             {}%
            \fi%
          \fi%
          \listxadd{\@msdata@list}{%
            \@msd@options@iffullpage%
            \ifluatex%
              \csxdef{footnote@luatextextdir}{\the\textdir}%
              \csxdef{footnote@luatexpardir}{\the\pardir}%
            \fi%
            \csdef{@this@crossref@start}{\add@msd@c:start:msdata}%
             \csdef{@this@crossref@end}{\add@msd@c:end:msdata}%
             \unexpanded{%
               \def\annot@start{}%
               \def\annot@end{}%
             }%
            \noexpand\csuse{v\@msdata@series footnote}{\@msdata@series}{{\expandonce\l@d@nums}{\ms@data@label}{\expandonce\@data}}%
            \reset@msd@options@iffullpage%
         }%
      }%
      {}%
  \else%
    \numgdef{\add@msd@cR}{\add@msd@cR+\@ne}%
      \ifcsdef{@msdata@\add@msd@cR @dataR}{%
        \letcs{\@data}{@msdata@\add@msd@cR @dataR}%
        \edef\l@d@nums{%
          000|% Start page = we don't print it
          \csuse{@msdata@\add@msd@cR @linenumberR}|% Start line number
          \csuse{@msdata@\add@msd@cR @sublinenumberR}|% Start subline number, for now, not used
          000|% End page number, we don't print it
          \ifnumless{\csuse{@msdata@\add@msd@cR @stopabslinenumberR}}{\csuse{@lastline@forpageR@\the\page@numR}}%
            {\csuse{@msdata@\add@msd@cR @stoplinenumberR}}%End line number if in the same page
            {\csuse{@lastline@forpageR@\the\page@numR}}%Otherwise, last number of the page
          |%
          \ifnumless{\csuse{@msdata@\add@msd@cR @stopabslinenumberR}}{\csuse{@lastline@forpageR@\the\page@numR}}%
            {\csuse{@msdata@\add@msd@cR @stopsublinenumberR}}%End subline number if in the same page
            {\csuse{@lastsubline@forpageR@\the\page@numR}}%Otherwise, last number of the page
          000|% End sub line number, for now, not used
          \edfont@info%Font
          }%
          \@msd@options@fullpagefalse%
          \if@firstlineofpageR%
            \unless\if@msdata@insertedfrompreviouspage%
              \ifnumless{\csuse{@lastabsline@forpageR@\the\page@numR}}{\csuse{@msdata@\add@msd@c @stopabslinenumberR}+\@ne}%
              {%
              \numdef{\@tmp}{\add@msd@cR+\@ne}%
              \ifcsdef{@msdata@\@tmp @abslinenumberR}%
                {\ifnumequal{\csuse{@msdata@\@tmp @abslinenumberR}}{\csuse{@lastabsline@forpageR@\the\page@numR}}%
                  {}%
                  {\@msd@options@fullpagetrue}%
                }%
                {\@msd@options@fullpagetrue}%
              }%
             {}%
            \fi%
          \fi%
          \listxadd{\@msdata@list}{%
            \@msd@options@iffullpage%
            \ifluatex%
              \csxdef{footnote@luatextextdir}{\the\textdir}%
              \csxdef{footnote@luatexpardir}{\the\pardir}%
            \fi%
            \csdef{@this@crossref@start}{\add@msd@cR:start:msdata}%
             \csdef{@this@crossref@end}{\add@msd@cR:end:msdata}%
             \unexpanded{%
               \def\annot@start{}%
               \def\annot@end{}%
             }%
            \noexpand\csuse{v\@msdata@series footnote}{\@msdata@series}{{\expandonce\l@d@nums}{\ms@data@label}{\expandonce\@data}}%
            \reset@msd@options@iffullpage%
         }%
      }%
      {}%
  \fi%
  \egroup%
}%
\newif\if@msdata@insertedfrompreviouspage%
\newcommand{\add@msdata@firstlineofpage}{%
  \bgroup%
  \normalfont%
  \unless\ifledRcol@%
    \ifcsdef{@msdata@\add@msd@c @data}{%
    \ifnumless{\the\absline@num-\@ne}{\csuse{@msdata@\add@msd@c @stopabslinenumber}}%
      {%
      \global\@msdata@insertedfrompreviouspagetrue%
      \letcs{\@data}{@msdata@\add@msd@c @data}%
      \edef\l@d@nums{%
          000|% Start page = we don't print it
          \numexpr\the\line@num+\@ne\relax|% Start line number = first line of the page. As \add@msdata@firstlineofpage is called before line number has been incremented, we increment it for printing
          000|% Start subline number, for now, not used
          000|% End page number, we don't print it
          \ifnumless{\csuse{@msdata@\add@msd@c @stopabslinenumber}}{\csuse{@lastabsline@forpage@\the\page@num}}%
            {\csuse{@msdata@\add@msd@c @stoplinenumber}}%End line number if in the same page
            {\csuse{@lastline@forpage@\the\page@num}}%Otherwise, last number of the page
          |%
          000|% End sub line number, for now, not used
          \edfont@info%Font
          }%
          \@msd@options@fullpagefalse%
          \ifnumless{\csuse{@lastabsline@forpage@\the\page@num}}{\csuse{@msdata@\add@msd@c @stopabslinenumber}+\@ne}%We will test if the ms data is for the full page
          {%
          \numdef{\@tmp}{\add@msd@c+\@ne}%
          \ifcsdef{@msdata@\@tmp @abslinenumber}%
            {\ifnumequal{\csuse{@msdata@\@tmp @abslinenumber}}{\csuse{@lastabsline@forpage@\the\page@num}}%
              {}%
              {\@msd@options@fullpagetrue}%
            }%
            {\@msd@options@fullpagetrue}%
          }%
         {}%
         \listxadd{\@msdata@list}{%
          \@msd@options@iffullpage%
          \ifluatex%
            \csxdef{footnote@luatextextdir}{\the\textdir}%
            \csxdef{footnote@luatexpardir}{\the\pardir}%
          \fi%
          \csdef{@this@crossref@start}{\add@msd@c:start:msdata}%
           \csdef{@this@crossref@end}{\add@msd@c:end:msdata}%
           \unexpanded{%
             \def\annot@start{}%
             \def\annot@end{}%
           }%
          \noexpand\csuse{v\@msdata@series footnote}{\@msdata@series}{{\expandonce\l@d@nums}{\ms@data@label}{\expandonce\@data}}%
          \reset@msd@options@iffullpage%
        }%
      }%
      {\global\@msdata@insertedfrompreviouspagefalse}%
  }{}%
  \else%
    \ifcsdef{@msdata@\add@msd@cR @dataR}{%
    \ifnumless{\the\absline@numR-\@ne}{\csuse{@msdata@\add@msd@cR @stopabslinenumberR}}%
          {%
          \global\@msdata@insertedfrompreviouspagetrue%
          \letcs{\@data}{@msdata@\add@msd@cR @dataR}%
          \edef\l@d@nums{%
          000|% Start page = we don't print it
          \numexpr\the\line@numR+\@ne\relax|% Start line number = first line of the page. As \add@msdata@firstlineofpage is called before line number has been incremented, we increment it for printing
          000|% Start subline number, for now, not used
          000|% End page number, we don't print it
          \ifnumless{\csuse{@msdata@\add@msd@cR @stopabslinenumberR}}{\csuse{@lastline@forpageR@\the\page@numR}}%
            {\csuse{@msdata@\add@msd@cR @stoplinenumberR}}%End line number if in the same page
            {\csuse{@lastline@forpageR@\the\page@numR}}%Otherwise, last number of the page
          |%
          000|% End sub line number, for now, not used
          \edfont@info%Font
          }%
          \@msd@options@fullpagefalse%
          \ifnumless{\csuse{@lastabsline@forpageR@\the\page@numR}}{\csuse{@msdata@\add@msd@cR @stopabslinenumberR}+\@ne}%
          {%
          \numdef{\@tmp}{\add@msd@cR+\@ne}%
          \ifcsdef{@msdata@\@tmp @abslinenumberR}%
            {\ifnumequal{\csuse{@msdata@\@tmp @abslinenumberR}}{\csuse{@lastabsline@forpageR@\the\page@numR}}%
              {}%
              {\@msd@options@fullpagetrue}%
            }%
            {\@msd@options@fullpagetrue}%
          }%
         {}%
         \listxadd{\@msdata@list}{%
           \@msd@options@iffullpage%
           \ifluatex%
             \csxdef{footnote@luatextextdir}{\the\textdir}%
             \csxdef{footnote@luatexpardir}{\the\pardir}%
           \fi%
          \csdef{@this@crossref@start}{\add@msd@cR:start:msdata}%
           \csdef{@this@crossref@end}{\add@msd@cR:end:msdata}%
           \unexpanded{%
             \def\annot@start{}%
             \def\annot@end{}%
           }%
           \noexpand\csuse{v\@msdata@series footnote}{\@msdata@series}{{\expandonce\l@d@nums}{\ms@data@label}{\expandonce\@data}}%
           \reset@msd@options@iffullpage%
        }%
     }%
     {\global\@msdata@insertedfrompreviouspagefalse}%
  }{}%
  \fi%
  \egroup%
}%
\newcommand{\insert@msdata}{%
  \def\do##1{##1}%
  \dolistloop{\@msdata@list}%
  \global\let\@msdata@list\relax%
}%
\newif\if@msd@options@fullpage%
\newcommand{\@msd@options@iffullpage}[0]{%
  \if@msd@options@fullpage%
    \noexpand\toggletrue{nonum@}%
    \ifdefvoid{\ms@data@label}%
      {\noexpand\toggletrue{nosep@}}%
      {}%
  \fi%
}%
\newcommand{\reset@msd@options@iffullpage}[0]{%
  \noexpand\togglefalse{nonum@}%
  \noexpand\togglefalse{nosep@}%
}%
\newtoks\@emptytoks

\newtoks\l@denvbody

\newcommand{\addtol@denvbody}[1]{%
  \global\l@denvbody\expandafter{\the\l@denvbody#1}}
\newif\if@edtabular%
\newcommand{\l@dcollect@body}[1]{%
   \ifvmode\else%
     \\%Ensure that we do not have spurious indentation at the very first line, caused by some text before.
   \fi%
  \l@denvbody{\expandafter#1\expandafter{\the\l@denvbody}}%
  \edef\processl@denvbody{\the\l@denvbody\noexpand\end{\@currenvir}}%
  \l@denvbody\@emptytoks \def\l@dbegin@stack{b}%
  \begingroup
    \@edtabulartrue%
    \expandafter\let\csname\@currenvir\endcsname\l@dcollect@@body
    \edef\processl@denvbody{\expandafter\noexpand\csname\@currenvir\endcsname}%
    \processl@denvbody%
    }%

\def\l@dpush@begins#1\begin#2{%
  \ifx\end#2\else b\expandafter\l@dpush@begins\fi}

\def\l@dcollect@@body#1\end#2{%
  \edef\l@dbegin@stack{\l@dpush@begins#1\begin\end
                       \expandafter\@gobble\l@dbegin@stack}%
  \ifx\@empty\l@dbegin@stack
    \endgroup
    \@checkend{#2}%
    \addtol@denvbody{#1}%
  \else
    \addtol@denvbody{#1\end{#2}}%
  \fi
  \processl@denvbody % A little tricky! Note the grouping
}

\newcommand*{\l@dtabnoexpands}{%
  \let\rtab=0%
  \let\ctab=0%
  \let\ltab=0%
  \let\rtabtext=0%
  \let\ltabtext=0%
  \let\ctabtext=0%
  \let\edbeforetab=0%
  \let\edaftertab=0%
  \let\edatleft=0%
  \let\edatright=0%
  \let\edvertline=0%
  \let\edvertdots=0%
  \let\edrowfill=0%
}

\newcommand{\disable@familiarnotes}{%
  \unless\ifnofamiliar@%
    \def\do##1{%
        \csletcs{footnote@@##1}{footnote##1}%
        \expandafter\renewcommand \csname footnote##1\endcsname[1]{%
          \protected@csxdef{@thefnmark##1}{\csuse{thefootnote##1}}%
          \csuse{@footnotemark##1}%
        }%
    }%
    \dolistloop{\@series}%
  \fi%
}%
\newcommand{\restore@familiarnotes}{%
  \unless\ifnofamiliar@%
    \def\do##1{%
        \csletcs{footnote##1}{footnote@@##1}%
    }%
    \dolistloop{\@series}%
  \fi%
}%

\newcommand{\disable@sidenotes}{%
  \let\@@ledrightnote\ledrightnote%
  \let\@@ledleftnote\ledleftnote%
  \let\@@ledsidenote\ledsidenote%
  \let\ledrightnote\@gobble%
  \let\ledleftnote\@gobble%
  \let\ledsidenote\@gobble%
}%
\newcommand{\restore@sidenotes}{%
  \let\ledrightnote\@@ledrightnote%
  \let\ledleftnote\@@ledleftnote%
  \let\ledsidenote\@@ledsidenote%
}%
\newcommand{\disable@endnotes}{%
  \unless\ifnoend@%
    \def\do##1{%
        \csletcs{##1@@endnote}{##1endnote}%
        \expandafter\renewcommand \csname ##1endnote\endcsname[1]{}%
    }%
    \dolistloop{\@series}%
  \fi%
}%
\newcommand{\restore@endnotes}{%
  \unless\ifnofamiliar@%
    \def\do##1{%
        \csletcs{##1endnote}{##1@@endnote}%
    }%
    \dolistloop{\@series}%
  \fi%
}%
\newcommand{\disable@notes}{%
  \disable@sidenotes%
  \disable@familiarnotes%
  \disable@endnotes%
}%
\newcommand{\restore@notes}{%
  \restore@sidenotes%
  \restore@familiarnotes%
  \restore@endnotes%
}%
\let\EDTEXT=\edtext
\newcommand{\xedtext}[2]{\EDTEXT{#1}{#2}}
\let\EDLABEL=\edlabel
\newcommand*{\xedlabel}[1]{\EDLABEL{#1}}
\AtBeginDocument{\let\xedindex\edindex}%
\newcommand{\nulledindex}[2][\jobname]{\@bsphack\@esphack}

\let\@line@@num=\linenum
\newcommand*{\l@dgobbleoptarg}[2][]{\relax}%

\NewExpandableDocumentCommand{\l@secondmandarg}{om}{#2}%
\let\Relax=\relax
\let\NEXT=\next

\newcommand{\l@dmodforedtext}{%
  \let\edtext\relax
  \def\do##1{\global\csletcs{##1footnote}{l@dgobbleoptarg}}%
  \dolistloop{\@series}%
  \let\edindex\nulledindex
  \let\linenum\@gobble}
\newcommand{\l@drestoreforedtext}{%
  \def\do##1{\global\csletcs{##1footnote}{##1@@footnote}}
  \dolistloop{\@series}%
  \let\edindex\xedindex}
\newcommand{\l@dnullfills}{%
  \def\edlabel##1{}%
  \def\edrowfill##1##2##3{}%
}
\newcommand{\l@drestorefills}{%
  \def\edrowfill##1##2##3{\@EDROWFILL@{##1}{##2}{##3}}%
}

\newcommand{\letsforverteilen}{%
  \let\edtext\xedtext
  \let\edindex\xedindex
  \def\do##1{\global\csletcs{##1footnote}{##1@@footnote}}
  \dolistloop{\@series}%
  \let\linenum\@line@@num
  \hilfsskip=\l@dcolwidth%
  \advance\hilfsskip by -\wd\hilfsbox
  \def\edlabel##1{\xedlabel{##1}}}

\newcommand\disablel@dtabfeet{\l@dmodforedtext}%
\newcommand\enablel@dtabfeet{\l@drestoreforedtext}%
\newcount\l@dampcount
  \l@dampcount=1\relax
\newcount\l@dcolcount
  \l@dcolcount=0\relax

\newbox\hilfsbox
\newskip\hilfsskip
\newbox\Hilfsbox
\newcount\hilfscount

\newdimen\dcoli
\newdimen\dcolii
\newdimen\dcoliii
\newdimen\dcoliv
\newdimen\dcolv
\newdimen\dcolvi
\newdimen\dcolvii
\newdimen\dcolviii
\newdimen\dcolix
\newdimen\dcolx
\newdimen\dcolxi
\newdimen\dcolxii
\newdimen\dcolxiii
\newdimen\dcolxiv
\newdimen\dcolxv
\newdimen\dcolxvi
\newdimen\dcolxvii
\newdimen\dcolxviii
\newdimen\dcolxix
\newdimen\dcolxx
\newdimen\dcolxxi
\newdimen\dcolxxii
\newdimen\dcolxxiii
\newdimen\dcolxxiv
\newdimen\dcolxxv
\newdimen\dcolxxvi
\newdimen\dcolxxvii
\newdimen\dcolxxviii
\newdimen\dcolxxix
\newdimen\dcolxxx
\newdimen\dcolerr   % added for error handling

\newcommand{\l@dcolwidth}{\ifcase \the\l@dcolcount \dcoli %???
  \or \dcoli \or \dcolii \or \dcoliii
  \or \dcoliv \or \dcolv \or \dcolvi
  \or \dcolvii \or \dcolviii \or \dcolix \or \dcolx
  \or \dcolxi \or \dcolxii \or \dcolxiii
  \or \dcolxiv \or \dcolxv \or \dcolxvi
  \or \dcolxvii \or \dcolxviii \or \dcolxix \or \dcolxx
  \or \dcolxxi \or \dcolxxii \or \dcolxxiii
  \or \dcolxxiv \or \dcolxxv \or \dcolxxvi
  \or \dcolxxvii \or \dcolxxviii \or \dcolxxix \or \dcolxxx
  \else \dcolerr \fi}

\newcommand*{\stepl@dcolcount}{\advance\l@dcolcount\@ne
  \ifnum\l@dcolcount>30\relax
    \led@err@TooManyColumns
  \fi}

\newcommand{\l@dsetmaxcolwidth}{%
  \ifdim\l@dcolwidth < \wd\hilfsbox
    \l@dcolwidth = \wd\hilfsbox
  \else \relax \fi}

\def\measuremcell #1&{%
    \ifx #1\\ \ifnum\l@dcolcount=0\let\NEXT\relax%
              \else\l@dcheckcols%
                    \l@dcolcount=0%
                    \let\NEXT\measuremcell%
              \fi%
    \else\setbox\hilfsbox=\hbox{$\displaystyle{#1}$}%
      \stepl@dcolcount%
      \l@dsetmaxcolwidth%
      \let\NEXT\measuremcell%
    \fi\NEXT}

\def\measuretcell #1&{%
    \ifx #1\\ \ifnum\l@dcolcount=0\let\NEXT\relax%
              \else\l@dcheckcols%
                    \l@dcolcount=0%
                    \let\NEXT\measuretcell%
              \fi%
    \else\setbox\hilfsbox=\hbox{#1}%
      \stepl@dcolcount%
      \l@dsetmaxcolwidth%
      \let\NEXT\measuretcell%
    \fi\NEXT}

\def\measuremrow #1\\{%
   \ifx #1&\let\NEXT\relax%
   \else\measuremcell #1&\\&\\&%
      \let\NEXT\measuremrow%
   \fi\NEXT}
\def\measuretrow #1\\{%
   \ifx #1&\let\NEXT\relax%
   \else\measuretcell #1&\\&\\&%
     \let\NEXT\measuretrow%
   \fi\NEXT}

\newskip\edtabcolsep
\global\edtabcolsep=10pt

\newcommand{\variab}{\relax}

\newcommand*{\l@dcheckcols}{%
  \ifnum\l@dcolcount=1\relax
  \else
    \ifnum\l@dampcount=1\relax
    \else
      \ifnum\l@dcolcount=\l@dampcount\relax
      \else
        \l@d@err@UnequalColumns
      \fi
    \fi
    \l@dampcount=\l@dcolcount
  \fi}

\newdimen\edfilldimen
\edfilldimen=0pt

\newcounter{addcolcount}
  \renewcommand{\theaddcolcount}{\romannumeral \c@addcolcount}
\def\setmcellright #1&{\def\edlabel##1{}%
       \let\edindex\nulledindex
       \ifx #1\\ \ifnum\l@dcolcount=0%\removelastskip
                     \let\Next\relax%
                \else\l@dcolcount=0%
                      \let\Next=\setmcellright%
                \fi%
       \else%
           \disablel@dtabfeet%
           \stepl@dcolcount%
           \disable@notes%
           \setbox\hilfsbox=\hbox{$\displaystyle{#1}$}%
           \restore@notes%
           \letsforverteilen%
           \hskip\hilfsskip$\displaystyle{#1}$%
           \hskip\edtabcolsep%
           \let\Next=\setmcellright%
       \fi\Next}

\def\settcellright #1&{\def\edlabel##1{}%
       \let\edindex\nulledindex
       \ifx #1\\ \ifnum\l@dcolcount=0%\removelastskip
                    \let\Next\relax%
                \else\l@dcolcount=0%
                      \let\Next=\settcellright%
                \fi%
       \else%
           \disablel@dtabfeet%
           \stepl@dcolcount%
           \disable@notes%
           \setbox\hilfsbox=\hbox{#1}%
           \restore@notes%
           \letsforverteilen%
           \hskip\hilfsskip#1%
           \hskip\edtabcolsep%
           \let\Next=\settcellright%
       \fi\Next}
\def\setmcellleft #1&{\def\edlabel##1{}%
    \let\edindex\nulledindex
    \ifx #1\\ \ifnum\l@dcolcount=0 \let\Next\relax%
              \else\l@dcolcount=0%
                    \let\Next=\setmcellleft%
              \fi%
     \else   \disablel@dtabfeet%
              \stepl@dcolcount%
              \disable@notes%
              \setbox\hilfsbox=\hbox{$\displaystyle{#1}$}%
              \restore@notes%
              \letsforverteilen%
              $\displaystyle{#1}$\hskip\hilfsskip\hskip\edtabcolsep%
              \let\Next=\setmcellleft%
     \fi\Next}

\def\settcellleft #1&{\def\edlabel##1{}%
    \let\edindex\nulledindex
    \ifx #1\\ \ifnum\l@dcolcount=0 \let\Next\relax%
              \else\l@dcolcount=0%
                    \let\Next=\settcellleft%
              \fi%
     \else   \disablel@dtabfeet%
              \stepl@dcolcount%
              \disable@notes%
              \setbox\hilfsbox=\hbox{#1}%
              \restore@notes%
              \letsforverteilen%
              #1\hskip\hilfsskip\hskip\edtabcolsep%
              \let\Next=\settcellleft%
     \fi\Next}
\def\setmcellcenter #1&{\def\edlabel##1{}%
    \let\edindex\nulledindex
    \ifx #1\\ \ifnum\l@dcolcount=0\let\Next\relax%
             \else\l@dcolcount=0%
                   \let\Next=\setmcellcenter%
             \fi%
    \else    \disablel@dtabfeet%
             \stepl@dcolcount%
             \disable@notes%
             \setbox\hilfsbox=\hbox{$\displaystyle{#1}$}%
             \restore@notes%
             \letsforverteilen%
             \hskip 0.5\hilfsskip$\displaystyle{#1}$\hskip0.5\hilfsskip%
             \hskip\edtabcolsep%
             \let\Next=\setmcellcenter%
     \fi\Next}

\def\settcellcenter #1&{\def\edlabel##1{}%
    \let\edindex\nulledindex
    \ifx #1\\ \ifnum\l@dcolcount=0 \let\Next\relax%
              \else\l@dcolcount=0%
                    \let\Next=\settcellcenter%
              \fi%
     \else   \disablel@dtabfeet%
              \stepl@dcolcount%
              \disable@notes%
              \setbox\hilfsbox=\hbox{#1}%
              \restore@notes%
              \letsforverteilen%
              \hskip 0.5\hilfsskip #1\hskip 0.5\hilfsskip%
              \hskip\edtabcolsep%
              \let\Next=\settcellcenter%
     \fi\Next}

\let\NEXT=\relax

\def\setmrowright #1\\{%
    \ifx #1& \let\NEXT\relax
    \else \centerline{\setmcellright #1&\\&\\&}
          \let\NEXT=\setmrowright
    \fi\NEXT}
\def\settrowright #1\\{%
    \ifx #1& \let\NEXT\relax
    \else \centerline{\settcellright #1&\\&\\&}
          \let\NEXT=\settrowright
    \fi\NEXT}

\def\setmrowleft #1\\{%
    \ifx #1&\let\NEXT\relax
    \else \centerline{\setmcellleft #1&\\&\\&}
          \let\NEXT=\setmrowleft
    \fi\NEXT}
\def\settrowleft #1\\{%
    \ifx #1& \let\NEXT\relax
    \else \centerline{\settcellleft #1&\\&\\&}
          \let\NEXT=\settrowleft
    \fi\NEXT}

\def\setmrowcenter #1\\{%
    \ifx #1& \let\NEXT\relax%
    \else \centerline{\setmcellcenter #1&\\&\\&}
          \let\NEXT=\setmrowcenter
     \fi\NEXT}
\def\settrowcenter #1\\{%
    \ifx #1& \let\NEXT\relax
    \else \centerline{\settcellcenter #1&\\&\\&}
          \let\NEXT=\settrowcenter
    \fi\NEXT}

\newcommand{\nullsetzen}{%
    \stepl@dcolcount%
    \l@dcolwidth=0pt%
    \ifnum\l@dcolcount=30\let\NEXT\relax%
          \l@dcolcount=0\relax
    \else\let\NEXT\nullsetzen%
    \fi\NEXT}

\newcommand{\edatleft}[3][\@empty]{%
  \ifx#1\@empty
    \vbox to 10pt{\vss\hbox{$\left#2\vrule width0pt height #3
                           depth 0pt \right. $\hss}\vfil}
  \else
    \vbox to 4pt{\vss\hbox{$#1\left#2\vrule width0pt height #3
                depth 0pt \right. $}\vfil}
  \fi}
\newcommand{\edatright}[3][\@empty]{%
  \ifx#1\@empty
    \vbox to 10pt{\vss\hbox{$\left.\vrule width0pt height #3
                depth 0pt \right#2 $\hss}\vfil}
  \else
    \vbox to 4pt{\vss\hbox{$\left.\vrule width0pt height #3
                depth 0pt \right#2 #1 $}\vfil}
  \fi}

\newcommand{\edvertline}[1]{\vbox to 8pt{\vss\hbox{\vrule height #1}\vfil}}

\newcommand{\edvertdots}[1]{\vbox to 1pt{\vss\vbox to #1%
         {\cleaders\hbox{$\m@th\hbox{.}\vbox to 0.5em{ }$}\vfil}}}

\newcommand{\l@dtabaddcols}[2]{%
  \l@dcheckstartend{#1}{#2}%
  \ifl@dstartendok
  \setcounter{addcolcount}{#1}%
  \@whilenum \value{addcolcount}<#2\relax \do
  {\advance\edfilldimen by \the \csname dcol\theaddcolcount\endcsname
   \advance\edfilldimen by \edtabcolsep
   \stepcounter{addcolcount}}%
  \advance\edfilldimen by \the \csname dcol\theaddcolcount\endcsname
  \fi
}

\newif\ifl@dstartendok
\newcommand{\l@dcheckstartend}[2]{%
  \l@dstartendoktrue
  \ifnum #1<\@ne
    \l@dstartendokfalse
    \led@err@LowStartColumn
  \fi
  \ifnum #2>30\relax
    \l@dstartendokfalse
    \led@err@HighEndColumn
  \fi
  \ifnum #1>#2\relax
    \l@dstartendokfalse
    \led@err@ReverseColumns
  \fi
}

\newcommand*{\edrowfill}[3]{%
  \l@dtabaddcols{#1}{#2}%
  \hb@xt@ \the\l@dcolwidth{\hb@xt@ \the\edfilldimen{#3}\hss}}
\let\@edrowfill@=\edrowfill
\def\@EDROWFILL@#1#2#3{\@edrowfill@{#1}{#2}{#3}}

\newcommand{\leftltab}[1]{%
  \hb@xt@\z@{\vbox{\edtabindent%
  \moveleft\Hilfsskip\hbox{\ #1}}\hss}}

\newcommand{\leftrtab}[2]{%
  #2\hb@xt@\z@{\vbox{\edtabindent%
    \advance\Hilfsskip by\dcoli%
    \moveleft\Hilfsskip\hbox{\ #1}}\hss}}

\newcommand{\leftctab}[2]{%
        \hb@xt@\z@{\vbox{\edtabindent\l@dcolcount=\l@dampcount%
        \advance\Hilfsskip by 0.5\dcoli%
        \setbox\hilfsbox=\hbox{\def\edlabel##1{}%
        \disablel@dtabfeet$\displaystyle{#2}$}%
        \advance\Hilfsskip by -0.5\wd\hilfsbox%
        \moveleft\Hilfsskip\hbox{\ #1}}\hss}%
        #2}

\newcommand{\rightctab}[2]{%
        \setbox\hilfsbox=\hbox{\def\edlabel##1{}%
        \disablel@dtabfeet#2}\l@dampcount=\l@dcolcount%
        #1\hb@xt@\z@{\vbox{\edtabindent\l@dcolcount=\l@dampcount%
        \advance\Hilfsskip by 0.5\l@dcolwidth%
        \advance\Hilfsskip by -\wd\hilfsbox%
        \setbox\hilfsbox=\hbox{\def\edlabel##1{}%
        \disablel@dtabfeet$\displaystyle{#1}$}%
        \advance\Hilfsskip by -0.5\wd\hilfsbox%
        \advance\Hilfsskip by \edtabcolsep%
        \moveright\Hilfsskip\hbox{ #2}}\hss}%
        }

\newcommand{\rightltab}[2]{%
        \setbox\hilfsbox=\hbox{\def\edlabel##1{}%
        \disablel@dtabfeet#2}\l@dampcount=\l@dcolcount%
        #1\hb@xt@\z@{\vbox{\edtabindent\l@dcolcount=\l@dampcount%
        \advance\Hilfsskip by\l@dcolwidth%
        \advance\Hilfsskip by-\wd\hilfsbox%
        \setbox\hilfsbox=\hbox{\def\edlabel##1{}%
        \disablel@dtabfeet$\displaystyle{#1}$}%
        \advance\Hilfsskip by-\wd\hilfsbox%
        \advance\Hilfsskip by\edtabcolsep%
        \moveright\Hilfsskip\hbox{ #2}}\hss}%
        }

\newcommand{\rightrtab}[2]{%
        \setbox\hilfsbox=\hbox{\def\edlabel##1{}%
        \disablel@dtabfeet#2}%
        #1\hb@xt@\z@{\vbox{\edtabindent%
        \advance\Hilfsskip by-\wd\hilfsbox%
        \advance\Hilfsskip by\edtabcolsep%
        \moveright\Hilfsskip\hbox{ #2}}\hss}%
        }

\newcommand{\rtab}[1]{%
  \l@dnullfills
    \def\edbeforetab##1##2{\leftrtab{##1}{##2}}%
    \def\edaftertab##1##2{\rightrtab{##1}{##2}}%
    \measurembody{#1}%
  \l@drestorefills
    \variab
    \setmrowright #1\\&\\%
    \enablel@dtabfeet}

\newcommand{\measurembody}[1]{%
  \disablel@dtabfeet%
  \l@dcolcount=0%
  \nullsetzen%
  \l@dcolcount=0
  \measuremrow #1\\&\\%
  \global\l@dampcount=1}

\newcommand{\rtabtext}[1]{%
 \l@dnullfills
    \measuretbody{#1}%
 \l@drestorefills
    \variab
    \settrowright #1\\&\\%
    \enablel@dtabfeet}

\newcommand{\measuretbody}[1]{%
  \disable@notes%
  \disablel@dtabfeet%
  \l@dcolcount=0%
  \nullsetzen%
  \l@dcolcount=0
  \measuretrow #1\\&\\%
  \restore@notes%
  \global\l@dampcount=1}

\newcommand{\ltab}[1]{%
 \l@dnullfills
    \def\edbeforetab##1##2{\leftltab{##1}{##2}}%
    \def\edaftertab##1##2{\rightltab{##1}{##2}}%
    \measurembody{#1}%
  \l@drestorefills
    \variab
    \setmrowleft #1\\&\\%
    \enablel@dtabfeet}

\newcommand{\ltabtext}[1]{%
  \l@dnullfills
    \measuretbody{#1}%
  \l@drestorefills
    \variab
    \settrowleft #1\\&\\%
    \enablel@dtabfeet}

\newcommand{\ctab}[1]{%
  \l@dnullfills
    \def\edbeforetab##1##2{\leftctab{##1}{##2}}%
    \def\edaftertab##1##2{\rightctab{##1}{##2}}%
    \measurembody{#1}%
  \l@drestorefills
    \variab
    \setmrowcenter #1\\&\\%
    \enablel@dtabfeet}

\newcommand{\ctabtext}[1]{%
  \l@dnullfills
    \measuretbody{#1}%
  \l@drestorefills
    \variab
    \settrowcenter #1\\&\\%
    \enablel@dtabfeet}

\newcommand{\spreadtext}[1]{%\l@dcolcount=\l@dampcount%
   \hb@xt@ \the\l@dcolwidth{\hbox{#1}\hss}}
\newcommand{\spreadmath}[1]{%
  \hb@xt@ \the\l@dcolwidth{\hbox{$\displaystyle{#1}$}\hss}}

\newskip\HILFSskip
\newskip\Hilfsskip

\newcommand{\EDTABINDENT}{%
    \ifnum\l@dcolcount=30\let\NEXT\relax\l@dcolcount=0%
    \else\stepl@dcolcount%
         \advance\Hilfsskip by\l@dcolwidth%
         \ifdim\l@dcolwidth=0pt\advance\hilfscount\@ne
         \else\advance\Hilfsskip by \the\hilfscount\edtabcolsep%
         \hilfscount=1\fi%
         \let\NEXT=\EDTABINDENT%
    \fi\NEXT}%
\newcommand{\edtabindent}{%
    \l@dcolcount=0\relax
    \Hilfsskip=0pt%
    \hilfscount=1\relax
    \EDTABINDENT%
    \hilfsskip=\hsize%
    \advance\hilfsskip -\Hilfsskip%
    \Hilfsskip=0.5\hilfsskip%
    }%

\def\EDTAB #1|#2|{%
    \setbox\tabhilfbox=\hbox{$\displaystyle{#1}$}%
    \setbox\tabHilfbox=\hbox{$\displaystyle{#2}$}%
    \advance\tabelskip -\wd\tabhilfbox%
    \advance\tabelskip -\wd\tabHilfbox%
    \unhbox\tabhilfbox\hskip\tabelskip%
    \unhbox\tabHilfbox}%

\def\EDTABtext #1|#2|{%
    \setbox\tabhilfbox=\hbox{#1}%
    \setbox\tabHilfbox=\hbox{#2}%
    \advance\tabelskip -\wd\tabhilfbox%
    \advance\tabelskip -\wd\tabHilfbox%
    \unhbox\tabhilfbox\hskip\tabelskip%
    \unhbox\tabHilfbox}%
\newbox\tabhilfbox
\newbox\tabHilfbox

\newenvironment{edarrayl}{\l@dcollect@body\ltab}{}
\newenvironment{edarrayc}{\l@dcollect@body\ctab}{}
\newenvironment{edarrayr}{\l@dcollect@body\rtab}{}

\newenvironment{edtabularl}{\l@dcollect@body\ltabtext}{}
\newenvironment{edtabularc}{\l@dcollect@body\ctabtext}{}
\newenvironment{edtabularr}{\l@dcollect@body\rtabtext}{}

\newcommand{\initnumbering@quote}{
    \ifnoquotation@\else
    \renewcommand{\quotation}{\par\leavevmode%
                                \parindent=1.5em%
                                \skipnumbering%
                                \ifautopar%
                                    \vskip-\parskip%
                                \else%
                                    \vskip\topsep%
                                \fi%
                                \global\leftskip=\leftmargin%
                                \global\rightskip=\leftmargin%
                            }
    \renewcommand{\endquotation}{\par%
                             \global\leftskip=0pt%
                                \global\rightskip=0pt%
                                \leavevmode%
                                \skipnumbering%
                                \ifautopar%
                                    \vskip-\parskip%
                                \else%
                                    \vskip\topsep%
                                \fi%
                                }
    \renewcommand{\quote}{\par\leavevmode%
                                \parindent=0pt%
                                \skipnumbering%
                                \ifautopar%
                                    \vskip-\parskip%
                                \else%
                                    \vskip\topsep%
                                \fi%
                                \global\leftskip=\leftmargin%
                                \global\rightskip=\leftmargin%
    }
    \renewcommand{\endquote}{\par%
                                \global\leftskip=0pt%
                                \global\rightskip=0pt%
                                \leavevmode%
                                \skipnumbering%
                                \ifautopar%
                                    \vskip-\parskip%
                                \else%
                                    \vskip\topsep%
                                \fi%
                                }
    \fi
}
\newcommand{\ledsectnotoc}{\let\addcontentsline\@gobblethree}
\newcommand{\ledsectnomark}{%
  \let\chaptermark\@gobble%
  \let\sectionmark\@gobble%
  \let\subsectionmark\@gobble%
}
\catcode`\#=12
\notbool{@noeled@sec}{%
\ifl@dmemoir
  \newcommand\beforeeledchapter{%
  \clearforchapter%
}
\else
  \newcommand\beforeeledchapter{%
  \if@openright%
   \cleardoublepage%
  \else%
   \clearpage%
  \fi%
}
\fi
\def\print@rightmargin@eledsection{%
  \if@eled@sectioning%
    \begingroup%
    \if@RTL%
        \let\llap\rlap%
        \let\leftlinenum\rightlinenum%
        \let\leftlinenumR\rightlinenumR%
        \let\l@drd@ta\l@dld@ta%
        \let\l@drsn@te\l@dlsn@te%
    \fi%
    \hfill\l@drd@ta \csuse{LR}{\l@drsn@te}%
    \endgroup%
  \fi%
}%

\def\print@leftmargin@eledsection{%
  \if@eled@sectioning%
    \leavevmode%
    \begingroup%
    \if@RTL%
        \let\rlap\llap%
        \let\rightlinenum\leftlinenum%
        \let\rightlinenumR\leftlinenumR%
        \let\l@dld@ta\l@drd@ta%
        \let\l@dlsn@te\l@drsn@te%
    \fi%
    \l@dld@ta\csuse{LR}{\l@dlsn@te}%
    \endgroup%
  \fi%
}%

\AtBeginDocument{%

\pretocmd{\M@sect}
  {\let\old@edtext=\edtext%
  \let\edtext=\dummy@edtext@showlemma%
  }
  {}
  {}

\apptocmd{\M@sect}
  {\let\edtext=\old@edtext}
  {}
  {}

\patchcmd{\M@sect}
  { #9}
  { #9%
  \print@rightmargin@eledsection%
  }
  {}
  {}

\patchcmd{\M@sect}
  {\hskip #3\relax}
  {\hskip #3\relax%
  \print@leftmargin@eledsection%
  }
  {}
  {}

\patchcmd{\@mem@old@ssect}
  {#5}
  {#5%
  \print@leftmargin@eledsection%
  }
  {}
  {}

\patchcmd{\@mem@old@ssect}
  {\hskip #1}
  {\hskip #1%
  \print@rightmargin@eledsection%
  }
  {}
  {}

\patchcmd{\scr@startchapter}{\if@openright\cleardoublepage\else\clearpage\fi}{%
  \if@eled@sectioning\else%
    \ifl@dprintingpages\else%
      \if@openright\cleardoublepage\else\clearpage\fi%No clearpage inside a \Pages: will keep critical notes from printing on the title page. Here for scrbook.
    \fi%
  \fi%
  }
  {}
  {}

\patchcmd{\@makechapterhead}
  {#1}
  {\print@leftmargin@eledsection%
    #1%
  \print@rightmargin@eledsection%
  }
  {}
  {}

\patchcmd{\@makechapterhead}% For BIDI
  {\if@RTL\raggedleft\else\raggedright\fi}%
  {\if@eled@sectioning\else%
    \if@RTL\raggedleft\else\raggedright\fi%
  \fi%
  }%
  {}%
  {}%

\patchcmd{\@makeschapterhead}
  {#1}
  {\print@leftmargin@eledsection%
    #1%
   \print@rightmargin@eledsection%
  }
  {}
  {}

\pretocmd{\@sect}
  {\let\old@edtext=\edtext
  \let\edtext=\dummy@edtext@showlemma%
  }
  {}
  {}

\apptocmd{\@sect}
  {\let\edtext=\old@edtext}
  {}
  {}

\pretocmd{\@ssect}
  {\let\old@edtext=\edtext%
  \let\edtext=\dummy@edtext@showlemma%
  }
  {}
  {}

\apptocmd{\@ssect}
  {\let\edtext=\old@edtext}
  {}
  {}

\@ifpackageloaded{nameref}{

  \patchcmd{\NR@sect}
    {#8}
    {#8%
    \print@rightmargin@eledsection%
    }
    {}
    {}

  \patchcmd{\NR@sect}
    {\hskip #3\relax}
    {\hskip #3\relax%
    \print@leftmargin@eledsection%
    }
    {}
    {}

  \patchcmd{\NR@ssect}
    {#5}
    {#5%
    \print@rightmargin@eledsection%
    }
    {}
    {}

  \patchcmd{\NR@ssect}
    {\hskip #1}
    {\hskip #1%
    \print@leftmargin@eledsection%
    }
    {}
    {}
  }%
  {
  \patchcmd{\@sect}
    {#8}
    {#8%
    \print@rightmargin@eledsection%
    }
    {}
    {}

  \patchcmd{\@sect}
    {\hskip #3\relax}
    {\hskip #3\relax%
    \print@leftmargin@eledsection%
    }
    {}
    {}

  \patchcmd{\@ssect}
    {#5}
    {#5%
    \print@rightmargin@eledsection%
    }
    {}
    {}

  \patchcmd{\@ssect}
    {\hskip #1}
    {\hskip #1%
    \print@leftmargin@eledsection%
    }
    {}
    {}
  }%
}%
{}}%
\protect\catcode`\#=6 %Space NEEDS by \catcode
\AtBeginDocument{%
\patchcmd{\chapter}{\clearforchapter}{%
  \if@eled@sectioning\else%
    \ifl@dprintingpages\else%
      \clearforchapter%
    \fi%
  \fi%
  }%
  {}%
  {}%

\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{%
  \if@eled@sectioning\else%
    \ifl@dprintingpages%
      \endgraf%
    \else%
      \if@openright\cleardoublepage\else\clearpage\fi%No clearpage inside a \Pages: will keep critical notes from printing on the title page. Here for classical  classes
    \fi%
  \fi%
  }%
  {}%
  {}%
}%
\newif\if@eled@sectioning%
\notbool{@noeled@sec}{%
\newwrite\eled@sectioning@out
\newcommand{\eledchapter}[2][]{%
  \disable@familiarnotes%
  #2%
  \restore@familiarnotes%
  \ifledRcol%
    \immediate\write\eled@sectioningR@out{%
      \string\eled@chapter{#1}{\unexpanded{#2}}{\the\l@dnumpstartsR}{}{R}
      }%
  \else%
    \immediate\write\eled@sectioning@out{%
      \string\eled@chapter{#1}{\unexpanded{#2}}{\the\pstarts@read@L}{}{}%
      }%
  \fi%
}

\newcommand{\eledsection}[2][]{%
  \disable@familiarnotes%
  #2%
  \restore@familiarnotes%
  \ifledRcol%
    \immediate\write\eled@sectioningR@out{%
      \string\eled@section{#1}{\unexpanded{#2}}{\the\l@dnumpstartsR}{}{R}
      }%
  \else%
    \immediate\write\eled@sectioning@out{%
      \string\eled@section{#1}{\unexpanded{#2}}{\the\pstarts@read@L}{}{}%
      }%
  \fi%
}

\newcommand{\eledsubsection}[2][]{%
  \disable@familiarnotes%
  #2%
  \restore@familiarnotes%
  \ifledRcol%
    \immediate\write\eled@sectioningR@out{%
      \string\eled@subsection{#1}{\unexpanded{#2}}{\the\l@dnumpstartsR}{}{R}
      }%
  \else%
    \immediate\write\eled@sectioning@out{%
      \string\eled@subsection{#1}{\unexpanded{#2}}{\the\pstarts@read@L}{}{}%
      }%
  \fi%
}
\newcommand{\eledsubsubsection}[2][]{%
  \disable@familiarnotes%
  #2%
  \restore@familiarnotes%
  \ifledRcol%
    \immediate\write\eled@sectioningR@out{%
      \string\eled@subsubsection{#1}{\unexpanded{#2}}{\the\l@dnumpstartsR}{}{R}
      }%
  \else%
    \immediate\write\eled@sectioning@out{%
      \string\eled@subsubsection{#1}{\unexpanded{#2}}{\the\pstarts@read@L}{}{}%
      }%
  \fi%
}

\WithSuffix\newcommand\eledchapter*[2][]{%
  \disable@familiarnotes%
  #2%
  \restore@familiarnotes%
  \ifledRcol%
    \immediate\write\eled@sectioningR@out{%
      \string\eled@chapter{#1}{\unexpanded{#2}}{\the\l@dnumpstartsR}{*}{R}
      }%
  \else%
    \immediate\write\eled@sectioning@out{%
      \string\eled@chapter{#1}{\unexpanded{#2}}{\the\pstarts@read@L}{*}{}%
      }%
  \fi%
}

\WithSuffix\newcommand\eledsection*[2][]{%
  \disable@familiarnotes%
  #2%
  \restore@familiarnotes%
  \ifledRcol%
    \immediate\write\eled@sectioningR@out{%
      \string\eled@section{#1}{\unexpanded{#2}}{\the\l@dnumpstartsR}{*}{R}
      }%
  \else%
    \immediate\write\eled@sectioning@out{%
      \string\eled@section{#1}{\unexpanded{#2}}{\the\pstarts@read@L}{*}{}%
      }%
  \fi%
}

\WithSuffix\newcommand\eledsubsection*[2][]{%
  \disable@familiarnotes%
  #2%
  \restore@familiarnotes%
  \ifledRcol%
    \immediate\write\eled@sectioningR@out{%
      \string\eled@subsection{#1}{\unexpanded{#2}}{\the\l@dnumpstartsR}{*}{R}
      }%
  \else%
    \immediate\write\eled@sectioning@out{%
      \string\eled@subsection{#1}{\unexpanded{#2}}{\the\pstarts@read@L}{*}{}%
      }%
  \fi%
}

\WithSuffix\newcommand\eledsubsubsection*[2][]{%
  \disable@familiarnotes%
  #2%
  \restore@familiarnotes%
  \ifledRcol%
    \immediate\write\eled@sectioningR@out{%
      \string\eled@subsubsection{#1}{\unexpanded{#2}}{\the\l@dnumpstartsR}{*}{R}
      }%
  \else%
    \immediate\write\eled@sectioning@out{%
      \string\eled@subsubsection{#1}{\unexpanded{#2}}{\the\pstarts@read@L}{*}{}%
      }%
  \fi%
}
\def\eled@chapter#1#2#3#4#5{%
    \ifstrempty{#4}%
      {%
      \ifstrempty{#1}%
        {%
        \csgdef{eled@sectioning@#3#5}{\let\edtext=\dummy@edtext@showlemma\chapter{#2}}%
        \csgdef{eled@sectmark@#3#5}{\let\edtext=\dummy@edtext{}\chaptermark{#2}}%
        }%Need for \pairs, because of using parbox.
        {%
        \csgdef{eled@sectioning@#3#5}{\let\edtext=\dummy@edtext@showlemma\chapter[#1]{#2}}%
        \csgdef{eled@sectmark@#3#5}{\let\edtext=\dummy@edtext{}\chaptermark{#2}}%Need for \pairs, because of using parbox.
        }%
      }%
      {%
      \ifstrempty{#1}%
        {\csgdef{eled@sectioning@#3#5}{\let\edtext=\dummy@edtext@showlemma\chapter*{#2}}}%
        {\csgdef{eled@sectioning@#3#5}{\let\edtext=\dummy@edtext@showlemma\chapter*[#1]{#2}}}%Bug in LaTeX!
      }%
  \listcsgadd{eled@sections#5@@}{#3}%
    }
\def\eled@section#1#2#3#4#5{%
    \ifstrempty{#4}%
      {\ifstrempty{#1}%
        {%
        \csgdef{eled@sectioning@#3#5}{\section{#2}}%
        \csgdef{eled@sectmark@#3#5}{\let\edtext=\dummy@edtext{}\sectionmark{#2}}%Need for \pairs, because of using parbox.
        }%
        {%
        \csgdef{eled@sectioning@#3#5}{\section[#1]{#2}}%
        \csgdef{eled@sectmark@#3#5}{\let\edtext=\dummy@edtext{}\sectionmark{#1}}%Need for \pairs, because of using parbox.
        }%
      }%
      {\ifstrempty{#1}%
        {\csgdef{eled@sectioning@#3#5}{\section*{#2}}}%
        {\csgdef{eled@sectioning@#3#5}{\section*[#1]{#2}}}%Bug in LaTeX!
      }
  \listcsgadd{eled@sections#5@@}{#3}%
    }
\def\eled@subsection#1#2#3#4#5{%
    \ifstrempty{#4}%
      {\ifstrempty{#1}%
        {%
        \csgdef{eled@sectioning@#3#5}{\subsection{#2}}%
        \csgdef{eled@sectmark@#3#5}{\let\edtext=\dummy@edtext{}\csuse{subsectionmark}{#2}}%Need for \pairs, because of using parbox. \csuse in case of \subsectionmark is not defined (book)
        }%
        {%
        \csgdef{eled@sectioning@#3#5}{\subsection[#1]{#2}}%
        \csgdef{eled@sectmark@#3#5}{\let\edtext=\dummy@edtext{}\csuse{subsectionmark}{#1}}%Need for \pairs, because of using parbox. \csuse in case of \subsectionmark is not defined (book)
        }%
      }%
      {\ifstrempty{#1}%
        {\csgdef{eled@sectioning@#3#5}{\subsection*{#2}}}%
        {\csgdef{eled@sectioning@#3#5}{\subsection*[#1]{#2}}}%Bug in LaTeX!
      }
  \listcsgadd{eled@sections#5@@}{#3}%
    }
\def\eled@subsubsection#1#2#3#4#5{%
    \ifstrempty{#4}%
      {\ifstrempty{#1}%
        {\csgdef{eled@sectioning@#3#5}{\subsubsection{#2}}}%
        {\csgdef{eled@sectioning@#3#5}{\subsubsection[#1]{#2}}}%
      }%
      {\ifstrempty{#1}%
        {\csgdef{eled@sectioning@#3#5}{\subsubsection*{#2}}}%
        {\csgdef{eled@sectioning@#3#5}{\subsubsection*[#1]{#2}}}%Bug in LaTeX!
      }
  \listcsgadd{eled@sections#5@@}{#3}%
    }

}{}
\def\normal@page@break{}
\def\l@prev@pb{}
\def\l@prev@nopb{}
\newcommand{\ledpb}{\write\linenum@out{\string\led@pb}}
\newcommand{\ledpbnum}[1]{\write\linenum@out{\string\led@pbnum{#1}}}
\newcommand{\lednopb}{\write\linenum@out{\string\led@nopb}}
\newcommand{\lednopbnum}[1]{\write\linenum@out{\string\led@nopbnum{#1}}}
\newcommand{\led@pb}{\listxadd{\l@prev@pb}{\the\absline@num}}
\newcommand{\led@pbnum}[1]{\listxadd{\l@prev@pb}{#1}}
\newcommand{\led@nopb}{\listxadd{\l@prev@nopb}{\the\absline@num}}
\newcommand{\led@nopbnum}[1]{\listxadd{\l@prev@nopb}{#1}}
\def\led@pb@setting{before}
\newcommand{\ledpbsetting}[1]{\gdef\led@pb@setting{#1}}
\newcommand{\led@check@pb}{\xifinlist{\the\absline@num}{\l@prev@pb}{\pagebreak[4]}{}}
\newcommand{\led@check@nopb}{%
    \IfStrEq{\led@pb@setting}{before}{%
      \xifinlist{\the\absline@num}{\l@prev@nopb}%
         {\numdef{\abs@prevline}{\the\absline@num-1}%
         \xifinlist{\abs@prevline}{\normal@page@break}%
           {\nopagebreak[4]\enlargethispage{\baselineskip}}%
           {}}%
         {}}%
       {}%
     {}%
    \IfStrEq{\led@pb@setting}{after}{%
      \xifinlist{\the\absline@num}{\l@prev@nopb}{%
        \xifinlist{\the\absline@num}{\normal@page@break}%
          {\nopagebreak[4]\enlargethispage{\baselineskip}}%
            {}%
}%
         {}}%
       {}%
     {}%
}
\newcommand{\check@pb@in@verse}{%
    \ifinstanza\iflednopbinverse\ifinserthangingsymbol% Using stanzas and enabling page breaks in verse control, while on a hanging verse.
        \ifnum\page@num=\last@page@num\else%If we have change page
          \IfStrEq{\led@pb@setting}{before}{%
              \numgdef{\abs@line@verse}{\the\absline@num-1}%
              \ledpbnum{\abs@line@verse}%
            }{}%
          \IfStrEq{\led@pb@setting}{after}{%
              \numgdef{\abs@line@verse}{\the\absline@num-1}%
              \lednopbnum{\abs@line@verse}%
          }{}%
        \fi%
    \fi\fi\fi%
}
\def\Hy@raisedlink@left#1{%
    \ifvmode
        #1%
    \else
        \Hy@SaveSpaceFactor
        \llap{\smash{%
        \begingroup
            \let\HyperRaiseLinkLength\@tempdima
            \setlength\HyperRaiseLinkLength\HyperRaiseLinkDefault
            \HyperRaiseLinkHook
        \expandafter\endgroup
        \expandafter\raise\the\HyperRaiseLinkLength\hbox{%
            \Hy@RestoreSpaceFactor
            #1%
            \Hy@SaveSpaceFactor
        }%
        }}%
        \Hy@RestoreSpaceFactor
        \penalty\@M\hskip\z@\relax
    \fi
}
\ifeledmaccompat@%

  \newcommand{\footnormalX}[1]{\arrangementX[#1]{normal}}%
  \newcommand{\footparagraphX}[1]{\arrangementX[#1]{paragraph}}%
  \newcommand{\foottwocolX}[1]{\arrangementX[#1]{twocol}}%
  \newcommand{\footthreecolX}[1]{\XarrangementX[#1]{threecol}}%

  \unless\ifnocritical@
    \newcommand{\footnormal}[1]{\Xarrangement[#1]{normal}}%
    \newcommand{\footparagraph}[1]{\Xarrangement[#1]{paragraph}}%
    \newcommand{\foottwocol}[1]{\Xarrangement[#1]{twocol}}%
    \newcommand{\footthreecol}[1]{\Xarrangement[#1]{threecol}}%
    \let\hsizetwocol\Xhsizetwocol
    \let\hsizethreecol\Xhsizethreecol
    \let\bhookXnote\Xbhooknote
    \let\boxsymlinenum\Xboxsymlinenum
    \let\symlinenum\Xsymlinenum
    \let\beforenumberinfootnote\Xbeforenumber
    \let\afternumberinfootnote\Xafternumber
    \let\beforeXsymlinenum\Xbeforesymlinenum
    \let\afterXsymlinenum\Xaftersymlinenum
    \let\inplaceofnumber\Xinplaceofnumber
    \let\Xlemmaseparator\lemmaseparator
    \let\afterlemmaseparator\Xafterlemmaseparator
    \let\beforelemmaseparator\Xbeforelemmaseparator
    \let\inplaceoflemmaseparator\Xinplaceoflemmaseparator
    \let\txtbeforeXnotes\Xtxtbeforenotes
    \let\afterXrule\Xafterrule
    \let\numberonlyfirstinline\Xnumberonlyfirstinline
    \let\numberonlyfirstintwolines\Xnumberonlyfirstintwolines
    \let\nonumberinfootnote\Xnonumberinfootnote
    \let\pstartinfootnote\Xpstart
    \let\pstartinfootnoteeverytime\Xpstarteverytime
    \let\onlyXpstart\Xonlypstart
    \let\Xnonumberinfootnote\Xnonumber
    \let\nonbreakableafternumber\Xnonbreakableafternumber
    \let\maxhXnotes\Xmaxhnotes
    \let\beforeXnotes\Xbeforenotes
    \let\boxlinenum\Xboxlinenum
    \let\boxlinenumalign\Xboxlinenumaligm
    \let\boxstartlinenum\Xboxstartlinenum
    \let\boxendlinenum\Xboxendlinenum
    \let\twolines\Xtwolines
    \let\morethantwolines\Xmorethantwolines
    \let\twolinesbutnotmore\Xtwolinesbutnotmore
    \let\twolinesonlyinsamepage\Xtwolinesonlyinsamepage
  \fi

  \unless\ifnofamiliar@
    \let\notesXwidthliketwocolumns\noteswidthliketwocolumnsX
  \fi
  \newcommandx{\parafootsep}[2][1,usedefault]{%
      \Xparafootsep[#1]{#2}%
      \parafootsepX[#1]{#2}
  }%

  \newcommandx{\afternote}[2][1,usedefault]{%
      \Xafternote[#1]{#2}%
      \afternoteX[#1]{#2}%
  }%

  \unless\ifnoend@
    \let\XendXtwolines\Xendtwolines
    \let\XendXmorethantwolines\Xendmorethantwolines
    \let\bhookXendnote\Xendbhooknote
    \let\boxXendlinenum\Xendboxlinenum%
    \let\boxXendlinenumalign\Xendboxlinenumalign%
    \let\boxXendstartlinenum\Xendboxstartlinenum%
    \let\boxXendendlinenum\Xendboxendlinenum%
    \let\XendXlemmaseparator\Xendlemmaseparator
    \let\XendXbeforelemmaseparator\Xendbeforelemmaseparator
    \let\XendXafterlemmaseparator\Xendafterlemmaseparator
    \let\XendXinplaceoflemmaseparator\Xendinplaceoflemmaseparator
  \fi

  \AtBeginDocument{%
    \ifdef\lineref{}{\let\lineref\edlineref}%
  }%

\fi%
\endinput
%%
%% End of file `reledmac.sty'.
