10

Wouldn't it be nice if we have a command to end an environment in a generic way? Example:

\begin{itemize}
   \item Apple
   \item Banana
\endenv       % replaces / is an alternative to, \end{itemize} or any other environments.

so that if I change my mind, I can just replace the first line with, for instance, \begin{enumerate}, without changing the closing part. This is also useful when one changes from normal to starred environment, etc.

This feature would serve as an alternative to the normal closing, which will still be available. And this hypothetical \endenv can be restricted to work only for 'normal' environment, and will not work for complex ones such as verbatim, frame, listings.

Should we have that \endenv? Is there already a package for this feature?

Edit:

To future visitors: from the accepted answer below one can see that this is not a very good idea. If one still demands the presumed convenience, this kind of thing should/can be delegated to the editor.

  • 3
    This has been discussed before, search the archives. In general I do not think it is a good idea as it make the code less readable. As @Sigur mentions, might be an idea to see who an editor can help. Mine (Emacs+auctex) can fx change the current env (both start and end) by a simple key combination. – daleif Nov 18 '14 at 13:16
  • 1
    See tex.stackexchange.com/q/205314/15925 for something very similar. – Andrew Swann Nov 18 '14 at 13:17
  • 4
    I would hate such an idea because if the body of the environment is long and I can only see the last part of the environment, then I cannot tell which environment is closed by the command. It's almost as bad as programming without environments: now all macro bodies are closed by a parenthesis. BTW: Do you just want a YES/NO answer? If correct, what to gain? If incorrect, then what is your real question? – user10274 Nov 18 '14 at 13:19
  • 1
    Nesting of environments would get messed up pretty fast when looking at the code. Or in other words: where am i? – Johannes_B Nov 18 '14 at 13:39
  • 1
    TeX is powerful enough to change its own syntax. You're suggesting a more brace-like notation for the contents of an environment than an XML-like notation which explicit begin and end markers. Yes, it would work and some might like it that way, but it's basically a non-LaTeX syntax and experienced LaTeXers would be (a) confused and (b) offended. – Matthew Leingang Nov 18 '14 at 17:00
16

It's trivial to define, just

\makeatletter
\def\endenv{\expandafter\end\expandafter{\@currenvir}}
\makeatother

This will work for any normal environment that is closed by expanding \end. Special environments that parse ahead to find their end (verbatim, tabularx, align, ...) will not work with this command,

But you should not use it. One of the main aims of the environment syntax is to make grouping explicit in the source. If you want a syntax with an anonymous close then {} is available, cf

\parbox{1cm}{hello}

or

\begin{minipage}{1cm}
hello
\end{minipage}

but please not

\begin{minipage}{1cm}
hello
\endenv

Similar considerations led </> close tag (which worked in the reference SGML syntax) not to be included in XML as it adds little real functionality and makes reading and processing the markup harder.

  • 4
    you might mention that there are some environments that must look for an exact match -- verbatim, for example. and also the confusion that would result from using such a technique with nested environments. (i don't even want to go there!) – barbara beeton Nov 18 '14 at 13:33
  • 2
    That is a typical beginner question. – Sigur Nov 18 '14 at 13:40
  • 3
    @Sigur typical beginner answer, I've not been using tex long. – David Carlisle Nov 18 '14 at 13:42
  • 1
    @DavidCarlisle. That is a fine point you wrote there. Thanks! This \endenv may not be a good idea afterall. By the way, in your code above (first block, last line), shouldn't it be \makeatother? – fajar Nov 18 '14 at 13:57
  • 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.