# Wrapfig - why is figure placed in margins?

I've created a wrapfig environment

\newenvironment{usefulterms}{
\begin{wrapfigure}{l}[0pt]{0.05\textwidth}
\vspace{-15pt}
\includegraphics[width = 0.05\textwidth]{abc.jpg}
\vspace{-25pt}
\end{wrapfigure}
\noindent{\textbf{Useful Terms}} \\
\newline}
{\newline}


The problem is that even thought I specify an overhang of 0pt, it is still placed in the margin;

Why is this command being "disobeyed"?

Thanks

Nick

• Can you provide an MWE (minimal working example)? – Guido Aug 14 '12 at 8:52

This is caused by the fact that material inside an environment is placed inside a group, which confuses wrapfig. Page 1 of the package documentation, says

If you put a wrapfigure in a parbox or a minipage, or any other type of grouping, the text wrapping should end before the group does.

• Thanks. I guess the work-around would be to specify this kind of thing as a command, and not an environment. – Nick Riches Aug 14 '12 at 11:05

I know I'm late on this, but note that it is possible to create an environment which contains a working wrapfigure environment.

\documentclass[]{article}

\usepackage{environ}% to collect the body of the environment
\usepackage{etoolbox}% for \AfterEndEnvironment hook
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{showframe}% to show that they don't go into the margin

\AfterEndEnvironment{afterend}{\AfterEnd}

\newcommand{\setafterend}[1]{%
\gdef\AfterEnd{%
\begin{wrapfigure}{l}[0pt]{2cm}%
\includegraphics[width=2cm]{example-image}%
\end{wrapfigure}%
#1%
}%
}
\NewEnviron{afterend}%
{%
\expandafter\setafterend\expandafter{\BODY}%
}

\begin{document}
\begin{afterend}
foo
\end{afterend}
\begin{afterend}
bar
\end{afterend}
\end{document}