Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
2  
This is not really "identity", because it encloses the contents in a group. But there's nothing of this kind in the LaTeX kernel. –  egreg May 31 '12 at 14:17
2  
From the text in your environment: do you really mean "without being changed" because if so that sounds more like the verbatim environment. –  Frank Mittelbach May 31 '12 at 14:29
3  
Because \begin{foo} uses \foo and \end{foo} uses \endfoo if it exists, it is OK but usually not recommend to use a macro name as environment, i.e. you could simply use \begin{relax} .. \end{relax} or \begin{empty} .. \end{empty} and take advantage of \relax and \empty. Both are basically no-op macros, which \empty expands to nothing but \relax is not expandable and represents a lower-level no-op. –  Martin Scharrer May 31 '12 at 14:30
    
@FrankMittelbach: No, I don't mean verbatim. I'll edit. –  krlmlr May 31 '12 at 14:33
    
@MartinScharrer: Thanks. The empty environment works for me, relax didn't work as LaTeX complained that \endrelax is undefined. –  krlmlr May 31 '12 at 14:42

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
Lovely. -- Am I the first who needs this functionality? Perhaps we can package this, and add an \identity command, too? –  krlmlr May 31 '12 at 15:11
    
@user946850: there is already \@firstofone which is like \identity but has a @ in its name :-(. I can package it if you want. Can you give some information about your exact use case for a descriptive summary? –  Martin Scharrer May 31 '12 at 15:14
    
"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. This package defines the \verb!\identity! command and the \verb!identity! environment to be used as parameters for macros that accept macros or environment names." -- Something along these lines? –  krlmlr May 31 '12 at 15:29

There's not a predefined environment that does nothing. You can emulate it with

\begin{empty}
Text
\end{empty}

but this will add unwanted spaces. So probably

\newenvironment{identity}
  {\ignorespaces}
  {\ignorespacesafterend}

would be what you need. Beware that this will add a level of grouping so it's not really "do nothing".

share|improve this answer
    
I have completely forgotten about the extra spaces. Thank you! –  krlmlr May 31 '12 at 15:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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