# Images off the page in multicols environment

Before my question, some background information.

I’ve created an algorithm (not latex) which will generate a catalog for a collection of books dynamically. A user supplies information to query an SQL database to retrieve book information and also defines the structure of the catalog in an XML file. Finally, through a series of automated steps, .tex files are generated which are then run through latex to create the catalog PDF file.

Everything works well except for adding images in which through trial and error I’ve discovered that the package picins works easily and well with the multicol package and looks great too. The only trouble I am encountering is that it appears that the picins package does not calculate when an image will appear off the page, so from time to time images appear off the page in the catalog.

Since these catalogs are generated automatically, the less human interaction the better. Adding \columnbreak prior to a paragraph which is textwrapped by parpic, does fix the issue, however performing this manually is not a solution since there are hundreds (perhaps thousands) of books which will make up each catalog.

Surrounding the paragraph and image in a \parbox will keep the image from going off the page; however it creates a lot of white space and adds pages to the catalog. Since this catalog will be printed, extra pages means more money, so white space must be kept to a minimum.

I'm quite stuck on a solution, I aplogize but my questions are broad. I appreciate your suggestions.

Would it make sense / is it possible to calculate the lines remaining in a column and use the ifthen package to insert a \columnbreak automatically?

Is there a better solution than picins which works nicely with the multicol package that will automatically break the column if the image will not fit?

The MWE is as follows:

\documentclass[english]{book}
\usepackage{multicol}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{graphicx}
\usepackage{picins}
\usepackage{blindtext}

\setlength{\columnsep}{0.375in}

\begin{document}
\begin{multicols*}{2}
\noindent\blindtext[5]\\
\pichskip{.25in}\noindent\parpic[sr][r]{\includegraphics[width=2in]{dog.png}\picskip{15}}
\blindtext
\end{multicols*}
\end{document}


Dog image (for testing purposes):

The output:

-

Here is a solution that should fit in with your automation.

The needspace package provides a command \needspace{<length>} which checks to see whether there is <length> left in the column or on the page, otherwise it issues a \break. This allows the text to roll over into another column/page. In that sense you can modify your code to the following:

<preamble>
...
\usepackage{needspace}% http://ctan.org/pkg/needspace
...
\begin{document}
...
\setbox0=\hbox{\includegraphics[width=2in]{dog.png}}% Store image in \box0
\needspace{\ht0}% Need at least the height of \box0
\pichskip{.25in}\noindent\parpic[sr][r]{\usebox0}% Insert image
Lorem ipsum...


This is the output that is generated using this fix:

-
This is an excellent solution. Thank you very much. –  Doug Aug 26 '11 at 14:36