Environments – How to Define New Environment That Wraps Around linenomath in LaTeX

environmentsmath-mode

In order to assign proper line number for displayed equation, I need to wrap each displayed equation around

\begin{linenomath*}
\end{linenomath*}

I would like to define a new environment that wrap these two lines around

\begin{align*}
\end{align*}

etc. Here is my unsuccessful attempt to implement this:

\documentclass[12pt,letterpaper]{amsart}

\usepackage{amssymb,lineno}
\linenumbers

\newenvironment{myeqn}
    {
      \begin{linenomath*}
        \begin{align}
        \end{align}
      \end{linenomath*}
    }

\begin{document}

What I want

\begin{linenomath*}
  \begin{align}
    x^n + y^n &= z^n
\label{one}
  \end{align}
\end{linenomath*}

What I got

\begin{myeqn}
  x^n + y^n &= z^n
\label{two}
\end{myeqn}

\end{document}

What's the right way to implement this? MANY THANKS!

Best Answer

This sort of works; at least, the output is the same (albeit disputable).

\documentclass[12pt,letterpaper]{amsart}

\usepackage{amssymb,lineno}
\linenumbers

\newenvironment{myeqn}
 {\csname linenomath*\endcsname\align}
 {\endalign\csname endlinenomath*\endcsname}

\begin{document}

What I want
\begin{linenomath*}
  \begin{align}
    x^n + y^n &= z^n
\label{one}
  \end{align}
\end{linenomath*}

What I got

\begin{myeqn}
  x^n + y^n &= z^n
\label{two}
\end{myeqn}

\end{document}

enter image description here

When you wrap align in some other environment, you need to use the “inner form”, not with \begin and \end.


A generic environment where the desired one is given as argument.

\documentclass[12pt,letterpaper]{amsart}

\usepackage{amssymb,lineno}
\linenumbers

\NewDocumentEnvironment{linenodisplay}{mb}
 {\begin{linenomath*}\begin{#1}#2\end{#1}\end{linenomath*}}
 {}

\begin{document}

Here is \texttt{align}
\begin{linenodisplay}{align}
  x^n + y^n &= z^n \\
  x^n + y^n &= z^n
\end{linenodisplay}
and here is \texttt{align*}
\begin{linenodisplay}{align*}
  x^n + y^n &= z^n \\
  x^n + y^n &= z^n
\end{linenodisplay}
Also \texttt{equation}
\begin{linenodisplay}{equation}
a=b
\end{linenodisplay}
and \texttt{equation*}
\begin{linenodisplay}{equation*}
a=b
\end{linenodisplay}

\end{document}

You might need to add \usepackage{xparse} if your LaTeX release is earlier than 2020-10-01.

enter image description here

Related Question