This is a follow up to my post here: Graphics – Images off the page in multicolumn environment
Using C#, XML, SQL, and LaTeX, I've managed to create a system which retrieves database information and fills LaTeX commands to automatically create nicely typeset catalogs to send to customers. The beauty of this system is that it is completely hands off, no LaTeX code modification is necessary after all of the commands are written.
The catalog uses the multicol
environment since each section within the catalog may have a different number of columns. I found that text-wrapping images in the multicol
environment works best when using the picins
package. My previous post uses the needspace
package to automatically break a column to ensure that the image does not go off the page. The current trouble is that the needspace
package leaves the catalog with undesirable whitespace which not only diminishes some esthetic appeal, but also accumulates and adds extra pages to the catalog.
The following code:
\documentclass{book}
\usepackage{multicol}
\setlength{\columnsep}{0.375 in}
\usepackage{picins}
\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{color}
\usepackage[includeheadfoot, top=.25in, bottom=.25in, left=.5in, right=.5in]{geometry}
\usepackage{needspace}
\usepackage[english]{babel}
\usepackage{blindtext}
\newcommand{\ItemImage}[5]%
{\pichskip{#5 in}\parpic[#2][#3]{\includegraphics[width=#4 in]{#1.png}}}
\newcommand{\ItemDescription}[1]%
{\noindent\color{black}#1\newline}
\begin{document}
\begin{multicols*}{2}
Book Title Number One\\
This is the subtitle of the first book\\
First Name, Last Name\\
\ItemImage{book1}{sr}{r}{1.25}{.125}
\ItemDescription{\blindtext[3] I was often using any of the available “lorem ipsum” generators on the web while testing different things in Latex until I discovered that the Latex distribution provides packages generating blind text, which is definitely more convenient. With just a few lines of code}
000-0-00000-000-0,Hb,\$19.95\\
Book Series\\
6 x 9,100,2012\\
\EndRule
\needspace{20\baselineskip}
Book Title Number Two\\
This is the subtitle of the second book\\
First Name,Last Name\\
\ItemImage{book1}{sr}{r}{1.25}{.125}
\ItemDescription{\blindtext}
000-0-00000-000-0,Hb,\$19.95\\
Book Series\\
10 x 12,500,2012\\
\EndRule
\end{multicols*}
\end{document}
creates something similar to:
The ideal situation would be (this graphic wasn't made from LaTeX):
So in the ideal situation, rather than the image being placed before the book description:
*Put Image Here* This would be the description starting in column 1. This line is in
column 1 also. This line can't fit in column 1 and has moved to column 2
The image would be placed within the book's description:
This would be the description starting in column 1. This line is in column 1. *Put
Image Here* This line can't fit in column 1 and has moved to column 2
Since the text is automatically placed from the database, there would need to be some way for LaTeX to automatically figure out where to correctly place the image. Floating is what comes to mind, however text wrapping in a multicol
environment is essential in which picins does the best job and doesn't float.
My ultimate question is:
Is there a way to calculate the lines remaining in a column, compare that value to the lines an image takes up, and place the image dynamically within a block of text at the point where the text breaks to the next column?
A rather round about way to asking a question but I'm very open to suggestions and points in the right direction to how to achieve the ideal situation shown above.
Best Answer
Here is a not too perfect concept. It uses
pgfpagesnodes
to determine whether there is enough room to place the picture. Special thanks to Garbage Collector for making me aware of this question. An unsolved problem is just too tempting.,
as a separator, which isn't ideal. So I asked a question how to change it, and thanks to egreg, it now uses|
(but nearly any character is possible).\picskip{0}
, the subdivisions of the descriptions should not be to short. To avaoid pushing the picture to the very end, they should not be to long. Five sentences of the length of my sample sentence should be okay (with square images of 1.25 inch).Code
Result