# Defining a custom ‘wrapfig’ environment

[ This is an updated version of http://stackoverflow.com/questions/3233031/latex-defining-a-custom-wrapfig-environment ]

The wrapfig package interacts badly with the setup and teardown done by \begin and \end. Concretely, this is not typeset correctly:

\documentclass{article}
\usepackage{wrapfig}
\newenvironment{pullquote}
{\begin{wrapfigure}{o}{2in}\large}
{\end{wrapfigure}}
\begin{document}
\begin{pullquote}
Chicken chicken
\end{pullquote}
Chicken chicken chicken chicken,
chicken chicken chicken chicken.
Chicken chicken, chicken chicken
chicken chicken chicken chicken
chicken chicken chicken chicken.
\end{document}


The "pull quote" is displaced downward to the end of the document (or to the next wrapfigure, if any) and I get a warning about collisions between wrapping environments in the log file.

But this is typeset correctly (note the dirty-trick use of the internal macros defined by \newenvironment):

\documentclass{article}
\usepackage{wrapfig}
\newenvironment{pullquote}
{\begin{wrapfigure}{o}{2in}\large}
{\end{wrapfigure}}
\begin{document}
\pullquote
Chicken chicken
\endpullquote
Chicken chicken chicken chicken,
chicken chicken chicken chicken.
Chicken chicken, chicken chicken
chicken chicken chicken chicken
chicken chicken chicken chicken.
\end{document}


I am looking for concrete advice on how to fix this apparent bug in wrapfig, which AFAICT is unmaintained.

-

One suggestion: shift the "dirty trick" inside the definition. This seems to work for me:

\documentclass{article}

\usepackage{wrapfig}
\newenvironment{pullquote}
{\wrapfigure{o}{2in}\large}
{\endwrapfigure}
\begin{document}

\begin{pullquote}
Chicken chicken
\end{pullquote}
Chicken chicken chicken chicken,
chicken chicken chicken chicken.
Chicken chicken, chicken chicken
chicken chicken chicken chicken
chicken chicken chicken chicken.
\end{document}


I noticed from the source that the wrapfigure environment is not itself defined using \newenvironment but simply a \def\wrapfigure{\wrapfloat{figure}} and \let\endwrapfigure\endwrapfloat. I don't know if this has anything to do with the bad behaviour, though.

-
This is actually a standard rule of thumb: If you want to define an environment newenv that wraps an oldenv, you should use \oldenv and \endoldenv in the definition of \newenvironment{newenv}{...}{...}. The \begin and \end commands do some preparation to start/end the environment, which are not needed more than once. – Juan A. Navarro Aug 5 '10 at 11:34
@Juan A. Navarro: That's useful to know! *quietly goes back through all his old style files and removes lots of \begin and \ends before anyone notices* I did notice that (La)TeX will complain about unmatched \begin and \ends, but not if they're replaced by the requisite commands. – Andrew Stacey Aug 5 '10 at 11:49
That works quite well, and gets me out of trying to debug the package! Thank you. @Juan: I didn't know that rule of thumb either; is that why the internal macros have names with no @ in them? – Zack Aug 5 '10 at 17:45
Well I don't know if that is precisely the reason, but it looks like a sensible one! – Juan A. Navarro Aug 5 '10 at 18:22