# Wrapfigure-Minipage Woes: How do I correctly typeset wrapped text boxes within a \parcolumns environment modified for a two-sided document?

This is a follow-up question concerning an experimental chapter layout I'm trying to implement for a novel I'm writing.

So. My initial problem of finding a way to modify the innards of the parcolumns package so that it alternates the order of the columns with each page break (producing a layout with outer/inner columns instead of left/right ones) has been solved, with many thanks to alexurba. However: in implementing the working modification of the \pc@placeboxes macro, whenever the page breaks switch from odd to even, the placement of the minipages inside wrapfigure environments intended to be placed along the inner margin of the outer column cause problems for the inner column. I'll let the code and images below speak for themselves:

\documentclass[11pt,letterpage,twosides]{bookest}
%
\geometry{textheight=9in,vmarginratio=1:1,outermargin=1.5in,innermargin=.5in,%
marginparwidth=1.35in,marginparsep=.1in}
\usepackage{parcolumns}
\usepackage{marginnote}
\usepackage{wrapfig}
\usepackage{lipsum}
\usepackage{changepage}
\strictpagecheck
%
\newcommand{\warfe}{\begin{wrapfigure}{o}[.15in]{1.5in} \begin{minipage}[t]{1.4in} %
\noindent \Large}
\newcommand{\warfo}{\begin{wrapfigure}{i}{1.5in} \begin{minipage}[t]{1.45in} %
\noindent \Large}
\newcommand{\wraf}{\end{minipage} \vspace{-.28in} \end{wrapfigure}}
\newcommand{\stm}{\marginnote}
%
\makeatletter
%
\def\pc@placeboxes{%
\global\let\@tempa\relax%
\hb@xt@\linewidth{%
\vfuzz30ex %
\splittopskip\z@skip%
\checkoddpage\ifoddpage
\count@\z@%
\loop\ifnum\count@<\pc@columncount%
\my@placeboxes@body%
\repeat%
\else
\count@\pc@columncount%
\loop\ifnum\count@>\z@%
\my@placeboxes@body%
\repeat%
\fi
}%
\@tempa%
}
%
\def\my@placeboxes@body{%
\expandafter\ifvoid\csname pc@column@\number\count@\endcsname%
\hskip\csname pc@column@width@\number\count@\endcsname%
\else%
\expandafter\setbox\expandafter\@tempboxa%
\expandafter\vsplit\csname pc@column@\number\count@\endcsname%
to \dp\strutbox%
\vbox{\unvbox\@tempboxa}%
\fi%
\expandafter\ifvoid\csname pc@column@\number\count@\endcsname%
\else%
\global\let\@tempa\pc@placeboxes%
\fi%
\ifnum\count@>\z@%
\strut%
\hfill%
\ifpc@rulebetween%
\vrule%
\hfill%
\fi%
\fi%
}
%
\makeatother
%
\begin{document}
%
%\chapter{Sample Experimental Chapter Layout}
%[Code and JPEGs ommitted.]
%
\chapter{My Wrapfigure-Minipage Woes}
%
\begin{parcolumns} %
[sloppy=true, %
sloppyspaces=true, %
nofirstindent=true, %
colwidths={1=3.5in, %
2=2.8in}]{2}
%
\colchunk[1]{\Large \indent \stm{\normalsize \textbf{Sample margin note, always to be on %
the outer margin. \\ ---------- \\ The typesetting of this page is entirely correct, with %
no wrapfigure/minipage environments flying around as in the following page...}} \lipsum[10]%
\lipsum[11] \lipsum[12] \stm{\textbf{\normalsize As one can see quite clearly, the %
typesetting of this page with switched columns is troubled, to put it lightly. \\ %
---------- \\ While the wrapfigure-minipage boxes themselves are placed correctly, another%
phantom empty box (of the same width) is inserted, flush with the edge of the page at the %
inner margin, pushing the text of the larger column over into the box set in the smaller %
column. \\ ---------- \\ What gives, friends?\\}} \lipsum[13] \lipsum[14]}
%
\colchunk[2]{\small \indent \warfo{Sample  wrapped text box, using a minipage inside a %
wrapfig environment.} \wraf \lipsum[14] \warfo{Note: no problems on the odd pages...} \wraf%
\lipsum[15] \warfe{Another sample wrapped text box, placed along the inner side of the \\ %
column.} \wraf \lipsum[16]  \warfe{Yet another text box.}\wraf \lipsum[17]}
%
\end{parcolumns}
%
\end{document}


I'll be taking a closer look at the implementation sections of the package documentation for wrapfig and parcolumns, reading against the modification of \pc@placeboxes to see if I can make any sense of what exactly might be going on here. I'll update this if I get anywhere near the realm of progress.

UPDATE:

In tinkering around with my code, I've discovered that if I use the {i} placement specifier for the wrapfigures on the even pages instead of {o}, this problem vanishes, and all is well except that the wrapfigures are on the wrong side of the column on the even pages. Thus, this problem seems to be specific to the wrapfigure not having any wrapped text between it and the adjacent column (or something to that effect, relating to having nothing but the space between columns separating the wrapfigures from the inner column).

Given this development, I'm now wondering about these two possibilities:

a) Might be possible to issue a series of commands to move the problemed wrapfigures to the right and have the adjacent wrapping text in the column move to the left after the initial typesetting run in which parcolumns first places the columns and boxes on the page?

b) Looking at the documentation for parcolumns has reminded me that there's plenty that the package does after the \pc@placeboxes process (i.e., \pc@alloccolumns, \pc@setcolumnwidths, \pc@setsinglecolwidth, and \pc@setcolumnwidth, as described on pp. 11-13 of the parcolumns documentation). In the (likely) event that the solution wouldn't necessarily have anything to do with setting the wrapfigures on the wrong side of the column (on even pages) and then flipping the positions of the text wrapping around the wrapfigures with that of the wrapfigure-minipage boxes themselves, perhaps the answer lies within the code for one of these post-\pc@placeboxes macros?

Also (for reference, if anyone needs it): alexurba has updated the answer to my original question with a more detailed explanation of how the \pc@placeboxes modification works, so check that out as well if you like.

-

Although there was indeed a bug in my patch to the parcolumns package in the thread about alternating columns, it turned out that the wrapfigure problem here is an incompatibility between the parcolumns and wrapfig packages. You can see that from the following example

\documentclass{article}
\usepackage{parcolumns}
\usepackage{wrapfig}
\usepackage{lipsum}

\newcommand*{\dummypic}[1][dummy picture]{%
\setlength{\fboxsep}{0pt}%
\fbox{\begin{minipage}[t][2cm][c]{2cm}
\centering #1
\end{minipage}}%
}

\begin{document}
\begin{parcolumns}{2}

\colchunk[1]{%
\begin{wrapfigure}{l}{22mm}
\dummypic[\emph{left}]
\end{wrapfigure}\par
\lipsum[4]\par
\begin{wrapfigure}{r}{22mm}
\dummypic[\emph{right}]
\end{wrapfigure}\par
\lipsum[5]
}

\colchunk[2]{%
\begin{wrapfigure}{l}{22mm}
\dummypic[\emph{left}]
\end{wrapfigure}\par
\lipsum[7]\par
\begin{wrapfigure}{r}{22mm}
\dummypic[\emph{right}]
\end{wrapfigure}\par
\unskip\lipsum[8]
}

\end{parcolumns}
\end{document}


which compiles to:

The reason is that parcolumns inserts an \hfill between the two columns. wrapfigure, on the other hand, works by changing the width of the paragraph next to the figure. The \hfill will always shift the narrowed paragraph to the page margin.

The problem can be fixed by restricting the columns to their natural width, for example using an \hbox to <column width> {...}. I have implemented this into the small patch package parcolsx provided in the answer to the question about alternating columns. I do not know if that is the most elegant way to solve the problem, but it works.

I will for now not re-post the code here. If that is desired I (or someone else) can do it later.

-
Excellent!! Functionality trumps elegance as far as I'm concerned--I can finally start working on filling up those colchunks with material from my typewritten novel draft now. Seriously, thank you so very much for all your work on this! I'll update when I can get a chance to test it. –  Isaac Jul 26 '12 at 1:50
Ok, I've implemented your new package in my initial .tex file for the example pages, and all is well except for the separation between the columns on even pages being about 1/3rd that of the separation on the odd pages. What could be tweaked in your parcolsx package to widen the separation on the even pages? This is a minor and trivial issue which pales in comparison to the major problems you've already tackled, though. (Preemptively: Don't apologize!) Again, many thanks for all the work you've put in on this. –  Isaac Jul 26 '12 at 3:29
@Isaac, that sounds like another bug with the separator. I am sure it is easily fixed, but that will have to wait until after working hours. –  alexurba Jul 26 '12 at 7:15
@Isaac - I just had a quick look and it seems that just two lines were interchanged (the order of a counter increment and the placing of the column separator). I just updated the code of the package in the other thread. Please carefully check everything. Maybe you could do me the favor and check also features of the parcolumns package that you do not currently use. If everything works correctly I will think about sending the patch to the parcolumns author. –  alexurba Jul 26 '12 at 8:29
Okay, according to the features described in the documentation for parcolumns, the only ones I wasn't using were the optional distance=[x] parameter for adjusting the distance separating the columns (2em, unless otherwise specified), as well as rulebetween=true. I adjusted the column widths to allow for various increases of the distance separating the columns with rulebetween=true, and everything's golden. Well done, sir. My most sincere thanks and gratitude for all your help. Send Mr. J. Sauer my regards! ;-) –  Isaac Jul 26 '12 at 22:03