8

I need to turn this

\begin{FOO}
my little pony
\end{FOO}

into this

\bar{my little pony}

I have tried this:

\newenvironment{FOO}{\bar}{}

But this seems to only make \bar apply on the first character, like so:

\bar{m}y little pony
  • 4
    I think the title is misleading. You rather want to have a command acting on the environment body – user31729 Feb 2 '16 at 8:47
  • Doesn't \newenvironment{FOO}{\bar\bgroup}{\egroup} work? – Hagen von Eitzen Feb 2 '16 at 14:30
12

(Admittedly, the example with \color) could be achieved without this, but let us assume \mycmd is more complex for the real case and not a toy 'theory';-))

Collecting the environment body is easiest with environ package and its \NewEnviron command and \BODY.

\documentclass{article}

\usepackage{xcolor}
\usepackage{environ}
\usepackage{blindtext}

\newcommand{\mycmd}[1]{%
  \color{blue}#1%
}

\NewEnviron{FOO}{\mycmd{\BODY}}{}

\begin{document}
\begin{FOO}
my little pony

\blindtext[2]
\end{FOO}

\end{document}

More complex examples might have to use a \expandafter before \BODY.

enter image description here

  • Please note that there might be cases where an \expandafter for the \BODY is necessary before the command \mycmd comes into action – user31729 Feb 2 '16 at 11:36
  • Collecting the environment body is easiest with environ How do you go about it in normal environments? (I can ask a new question if necessary) – MayeulC Jul 23 '18 at 12:45

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.