# Have a command acting on the environment body

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
-
I think the title is misleading. You rather want to have a command acting on the environment body – Christian Hupfer Feb 2 at 8:47
Welcome to TeX.SE! – Mico Feb 2 at 8:49
Doesn't \newenvironment{FOO}{\bar\bgroup}{\egroup} work? – Hagen von Eitzen Feb 2 at 14:30

(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.

-
Please note that there might be cases where an \expandafter for the \BODY is necessary before the command \mycmd comes into action – Christian Hupfer Feb 2 at 11:36