3

I would like to create an environment with its own line numbers, that is to say:

(1) the lines within this environment are not counted by lineno

(2) it has a continuous line numbering itself, with displayed equations numbered

Thus, say this environment has the name MYenv, I would like this:

\documentclass{article}

\usepackage{lineno} % here I didn't use [mathlines]

\begin{document} \linenumbers

TEXT

\begin{MYenv}
    TEXT \[x\] TEXT
\end{MYenv}

TEXT

\[y\]

\begin{MYenv}
    TEXT
\end{MYenv}

\end{document}

to produce something like

1    | TEXT
MY1  | TEXT
MY2  | x
MY3  | TEXT
2    | TEXT
     | y
MY4  | TEXT

How should one define such an environment?

Jinwen
  • 8,518

1 Answers1

1

The code below defines a conditional \ifLNturnsON to record whether line numbers have been turned on in the context. Inside MYenv it uses the counter myLN to store the line numbers in this environment. It also stores the value of counter linenumber in recordLN, and restores the normal line numbers after MYenv, so that the main text and the environment MYenv have separate line numbers.

As for the numbering for displayed math, it uses a patch from this answer. The environment MYenv numbers displayed math with \linenumberdisplaymath, no matter whether [mathlines] option has been turned on in the main text. Thus in the example below, you can see that displayed math has been numbered (\[x\]) inside MYenv, but not in the main text.

enter image description here

\documentclass{article}

\usepackage{lineno}

\newif\ifLNturnsON

\newcounter{recordLN} \newcounter{myLN} \setcounter{myLN}{1}

\renewcommand\makeLineNumber{\hss\scriptsize\ttfamily \LineNumber~$|$\hspace{1em}}%

\newenvironment{MYenv} {% \setcounter{recordLN}{\value{linenumber}}% \setcounter{linenumber}{\value{myLN}}% \LNturnsONfalse% \ifLineNumbers\LNturnsONtrue\fi\nolinenumbers% \linenumbers% \renewcommand\makeLineNumber{\hss\scriptsize\ttfamily MY \LineNumber~$|$\hspace{1em}}% \linenumberdisplaymath% number displayed math } {% \par% \ifLNturnsON\linenumbers\fi% \setcounter{myLN}{\value{linenumber}}% \setcounter{linenumber}{\value{recordLN}}% }

\usepackage{etoolbox} \RequirePackage{mathtools}

%% From https://tex.stackexchange.com/a/461192 % Patch 'normal' math environments: \newcommand\linenomathpatch[1]{% \cspreto{#1}{\linenomath}% \cspreto{#1}{\linenomath}% \cspreto{end#1}{\endlinenomath}% \cspreto{end#1}{\endlinenomath}% } % Patch AMS math environments: \newcommand\linenomathpatchAMS[1]{% \cspreto{#1}{\linenomathAMS}% \cspreto{#1}{\linenomathAMS}% \csappto{end#1}{\endlinenomath}% \csappto{end#1}{\endlinenomath}% } % Define \linenomathAMS depending on whether 'mathlines' option is provided \expandafter\ifx\linenomath\linenomathWithnumbers \let\linenomathAMS\linenomathWithnumbers % The following line gets rid of an extra line numbers at the bottom: \patchcmd\linenomathAMS{\advance\postdisplaypenalty\linenopenalty}{}{}{} \else \let\linenomathAMS\linenomathNonumbers \fi

\linenomathpatch{equation} \linenomathpatchAMS{gather} \linenomathpatchAMS{multline} \linenomathpatchAMS{align} \linenomathpatchAMS{alignat} \linenomathpatchAMS{flalign}

\usepackage[skip=0pt]{parskip}

\begin{document} \linenumbers

TEXT

\begin{MYenv}
    TEXT \[x\] TEXT
\end{MYenv}

TEXT

\[y\]

\begin{MYenv}
    TEXT
\end{MYenv}

\end{document}

Jinwen
  • 8,518