# Horizontally centered figures in twoside margin of book using wrapfig

I would like to put figures in the outer margins of a book. I am using wrapfig to do so, but the figures are aligned differently on even and odd numbered pages. On odd pages, they align based on the edge of the page; on even pages, they align next to the text itself.

Ideally, I would like the figures to be centered in the middle of the margin regardless of even/odd page numbers. How can I accomplish this?

Here is a minimal example.

    \documentclass[10pt]{book}

\usepackage{lipsum}
\usepackage{wrapfig}
\usepackage{tikz}

\setlength{\wrapoverhang}{\marginparwidth}

\begin{document}
\begin{wrapfigure}{o}{0pt}
\begin{tikzpicture}
\draw (0,0)--(3,0)--(3,3)--(0,3)--cycle;
\end{tikzpicture}
\caption{}\end{wrapfigure}
\lipsum[1]

\begin{wrapfigure}{o}{0pt}
\begin{tikzpicture}
\draw (0,0)--(5,0)--(5,5)--(0,5)--cycle;
\end{tikzpicture}
\caption{}\end{wrapfigure}
\lipsum[1]

\clearpage

\begin{wrapfigure}{o}{0pt}
\begin{tikzpicture}
\draw (0,0)--(3,0)--(3,3)--(0,3)--cycle;
\end{tikzpicture}
\caption{}\end{wrapfigure}
\lipsum[1]

\begin{wrapfigure}{o}{0pt}
\begin{tikzpicture}
\draw (0,0)--(5,0)--(5,5)--(0,5)--cycle;
\end{tikzpicture}
\caption{}\end{wrapfigure}
\lipsum[1]

\end{document}

-

One solution would be wrap the figure in a minipage, and use \centering to center the image within the minipage:

\begin{wrapfigure}{o}{0pt}%
\begin{minipage}{\marginparwidth}%
\centering%
\begin{tikzpicture}
...
\end{tikzpicture}
\caption{}
\end{minipage}
\end{wrapfigure}


To simplify this process it is helpful to define a custom environment, but this is not quite straightforward with wrapfigure. Below, I have adapted the solution provided at Defining a custom ‘wrapfig’ environment to define MyWrapFigure:

\documentclass[10pt]{book}

\usepackage{lipsum}
\usepackage{wrapfig}
\usepackage{tikz}

\newenvironment{MyWrapFigure}{%
\wrapfigure{o}{0pt}%
\begin{minipage}{\marginparwidth}%
\centering%
}{%
\end{minipage}%
\endwrapfigure%
}

\setlength{\wrapoverhang}{\marginparwidth}

\begin{document}
\begin{MyWrapFigure}
\begin{tikzpicture}
\draw (0,0)--(3,0)--(3,3)--(0,3)--cycle;
\end{tikzpicture}
\caption{}
\end{MyWrapFigure}
\lipsum[1]

\begin{MyWrapFigure}
\begin{tikzpicture}
\draw (0,0)--(5,0)--(5,5)--(0,5)--cycle;
\end{tikzpicture}
\caption{}
\end{MyWrapFigure}
\lipsum[1]

\clearpage

\begin{MyWrapFigure}
\begin{tikzpicture}
\draw (0,0)--(3,0)--(3,3)--(0,3)--cycle;
\end{tikzpicture}
\caption{}
\end{MyWrapFigure}
\lipsum[1]

\begin{MyWrapFigure}
\begin{tikzpicture}
\draw (0,0)--(5,0)--(5,5)--(0,5)--cycle;
\end{tikzpicture}
\caption{}
\end{MyWrapFigure}
\lipsum[1]
\end{document}

-
Nice solution; rather than using 150pt as the width for the minipage, you could use \marginparwidth which would change if the page dimensions change – cmhughes Nov 10 '11 at 15:44
@cmhughes: Excellent suggestion. Have incorporated that into the updated solution. – Peter Grill Nov 10 '11 at 15:55
Perfect. Thanks for the quick reply. – GregH Nov 10 '11 at 16:33