TransWikia.com

Why can't I add a split environment in a newenvironment?

TeX - LaTeX Asked by KNOB Personal on June 28, 2021

I wanted a simple environment for making lists of equations, so I made one that includes an equation and a split environment. The code won’t compile and the error seems unrelated. The most pressing thing I noticed was the "You can’t use `end’ in internal vertical mode." The problem is definitely the split environment.
Here is a MWE.

documentclass{article}
usepackage{amsmath}

newenvironment{env}
    {begin{equation}begin{split}}
    {end{split}end{equation}}

begin{document}
begin{env}
    x
end{env}
end{document}

If I remove the split environment from env, everything compiles just fine. Additionally, If the following example compiles just fine. What is the problem then?

documentclass{article}
usepackage{amsmath}

begin{document}
begin{equation}
begin{split}
    x
end{split}
end{equation}
end{document}

Here is the LaTeX error upon running the MWE.

math.tex|11 error| LaTeX Error: begin{split} on input line 9 ended by end{env}.
math.tex|12 error| LaTeX Error: begin{equation} on input line 9 ended by end{document}.
math.tex|12 error| Missing $ inserted.
math.tex|12 error| Missing } inserted.
math.tex|12 error| Missing } inserted.
math.tex|12 error| Missing } inserted.
math.tex|12 error| Missing cr inserted.
math.tex|12 error| Missing { inserted.
math.tex|12 error| Missing { inserted.
math.tex|12 error| Extra }, or forgotten $.
math.tex|12 error| Missing $ inserted.
math.tex|12 error| Missing } inserted.
math.tex|12 error| You can't use `end' in internal vertical mode.
math.tex|12 error| LaTeX Error: begin{equation} on input line 9 ended by end{document}.
math.tex|12 error| Missing } inserted.
math.tex|12 error| Missing $ inserted.
math.tex|12 error| Display math should end with $$.
math.tex|| Emergency stop.
math.tex|| ==> Fatal error occurred, no output PDF file produced!

One Answer

AMS alignments need to grab their body so you can not hide the end code in a macro (as documented in the amsmath documentation) but if you use NewDocumentEnvironment (in current latex or the xparse package for older latex you can make the new environment grab the environment body first so that end{split} is visible to begin{split}

documentclass{article}
usepackage{amsmath}

%usepackage{xparse} % not needed in current latex

NewDocumentEnvironment{env}{+b}
    {begin{equation}begin{split}#1end{split}end{equation}}
    {}

begin{document}
begin{env}
    x
end{env}
end{document}

Correct answer by David Carlisle on June 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP