[Tex/LaTex] n “identity” (or “no-op”) environment that simply uses its contents unaltered

environmentsmacros

In the functional programming paradigm, it is common to have functions accept other functions as parameters. This paradigm can be followed in LaTeX, too: A macro can accept parameters which themselves can be macros or environment names. For this use case, the equivalent of an "identity function" is missing in LaTeX: A macro and an environment that behaves as if it wasn't there.

Basically, I am looking for something along these lines:

\documentclass{article}
\begin{document}

\newenvironment{identity}{}{}
\begin{identity}
  This will be parsed as if not surrounded by anything.
\end{identity}
\end{document}

I'm reluctant to define this environment if something similar already exists in standard LaTeX or in any "standard" package. How is "identity" called in LaTeX?

Best Answer

In addition to egreg's answer:

You can make the environment more transparent if you cancel the group it adds. However, you need to define the environment name in the end-code to make the end-test happy. Note that this will not give you a correct warning message if you use either \begin{identity} or \end{identity} without the other. Instead you will get a different environment name in the warning message.

\documentclass{article}

\makeatletter
\newenvironment{identity}
    {\endgroup\ignorespaces}
    {\begingroup\def\@currenvir{identity}\ignorespacesafterend}
\makeatother

\begin{document}

\newcommand\test{Grouping!}

\begin{identity}

    Some text ...
    \renewcommand\test{No Grouping!}

    This is the environment ``\csname @currenvir\endcsname''.
    %\tracingonline=1
    %\showgroups
\end{identity}

Grouping? \test

\end{document}

The test file shows that there is no effective grouping for the content.

Related Question