[Tex/LaTex] Wrapfigure-Minipage Woes: How to correctly typeset wrapped text boxes within a \parcolumns environment modified for a two-sided document

double-sidedminipageparcolumnswrapfigure

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 %
    \vbadness\@M%
    \splittopskip\z@skip%
    \checkoddpage\ifoddpage
      \count@\z@%
      \loop\ifnum\count@<\pc@columncount%
        \advance\count@\@ne%
        \my@placeboxes@body%
      \repeat%
    \else
      \count@\pc@columncount%
      \loop\ifnum\count@>\z@%
        \my@placeboxes@body%
        \advance\count@\m@ne%
      \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}

Example, p.1
Example, p.2

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.

Best Answer

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:

wrapfig problem

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.

enter image description here

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