# Simple nested environment problem

I thought this would be simple, and it may be but I'm missing something. The following (non-functional code) gives this error:

./mwe-environments.tex:28: LaTeX Error: \begin{innerii} on input line 28 ended by \end{inneri}.

What am I missing? I'd like outer to begin inneri, have innerii conclude inner1 and, finally have \end{outer} finish innerii. (This is a bare-bones MWE -- there is a lot going in in the environments.)

\documentclass{article}

\usepackage{xparse}
\usepackage{calc}

\NewDocumentEnvironment{myouter}{}{%
\begin{inneri}
}{%
\end{innerii}
}

\NewDocumentEnvironment{inneri}{}{%
\bfseries
}{%
}

\NewDocumentEnvironment{innerii}{}{%
\end{inneri}
\itshape
}{%
%
}

\begin{document}

\begin{myouter}
Bla bla bla
\begin{innerii}
BLA bLA bLA
\end{myouter}

\end{document}

• anything is possible but it's just wrong to try to make \begin{innerii} BLA bLA bLA \end{myouter} be valid it defeats the entire point of the environment syntax with matching named group delimiters. – David Carlisle Oct 16 '17 at 20:09
• What's the purpose of having a \begin without the matching \end? This seems like an XY question. – egreg Oct 16 '17 at 21:10
• I may be guilty of over-simplifying the issue. The goal was to give the easiest code with the fewest keystrokes to non-TeX users. \begin{myouter} starts the formatting, \begin{innerii} formats another section and \end{myouter} completes the formatting with a touch of sicut erat in principio for good measure. I'm sure there are other ways to do this, but having started down that path I was determined to persevere. – sgmoye Oct 17 '17 at 0:06

As David says, doing this is not recommended as it breaks the LaTeX \begin{env}...\end{env} paradigm that everyone expects. This said, here is another way to fix the issue: rather than using \begin{inneri} and \end{inneri} inside the environments just use \inneri and \endinneri. This stops LaTeX from looking ahead and searching for an \end{inneri}. The full code:

\documentclass{article}

\usepackage{xparse}
\usepackage{calc}

\NewDocumentEnvironment{myouter}{}{%
\inneri
}{%
\end{innerii}
}

\NewDocumentEnvironment{inneri}{}{%
\bfseries
}{%
}

\NewDocumentEnvironment{innerii}{}{%
\endinneri
\itshape
}{%
%
}

\begin{document}

\begin{myouter}
Bla bla bla
\begin{innerii}
BLA bLA bLA
\end{myouter}

\end{document}

• Tacky of me to switch solutions, but this is perfect. Thanks. – sgmoye Oct 16 '17 at 20:46
• @sgmoye I don't mind either way but the outputs are different, they don't just remove the error. Andrew's version removes the grouping so the declarations from \inneri stay in scope after \endinneri – David Carlisle Oct 16 '17 at 20:50
• @sgmoye the worrying thing if you accept either solution is that you might actually use this syntax in a document! – David Carlisle Oct 16 '17 at 20:50
• @sgmoye Some people waut a while before accepting solutions for this reason. Either way you should give David an up-vote. – user30471 Oct 16 '17 at 20:50
• @Andrew if you put the grouping back you'll reset \@currenvir and then the solutions will be the same (latex isn't looking ahead for these environments as it does for newenv based ones) – David Carlisle Oct 16 '17 at 20:54

LaTeX is very flexible but if you bend it this much, it might snap, but this runs without error.

\documentclass{article}

\usepackage{xparse}
\usepackage{calc}

\makeatletter
\NewDocumentEnvironment{myouter}{}{%
\begin{inneri}%
}{%
% please don't do this in any public package
% it completely breaks latex syntax and any editors, syntax
% highlighters or humans trying to read the document.
\def\@currenvir{innerii}%
\end{innerii}%
}

\NewDocumentEnvironment{inneri}{}{%
\bfseries
}{%
}

\NewDocumentEnvironment{innerii}{}{%
% please don't do this in any public package
% it completely breaks latex syntax and any editors, syntax
% highlighters or humans trying to read the document.
\def\@currenvir{inneri}%
\end{inneri}%
\itshape
}{%
%
}
\makeatother
\begin{document}

\begin{myouter}
Bla bla bla
\begin{innerii}
BLA bLA bLA
\end{myouter}

\end{document}