# Autosizing two-column layouts

In preparing a solutions manual for a textbook, I quite often wanted to place some text and an associated figure side-by-side as part of the solution. What I ended up doing was either using multicolumn mode or else (more often) a two-column table. The problem with the latter solution is that it required tuning to get the widths of the columns correct so that the entire thing looked right. What I'd like is a more automatic way to accomplish this. Specifically, I want two columns, with text in one and a figure in the other; I'd like the column containing the figure to be sized so that the figure just fits in the column, and the column containing the text to be left- and right-justified.

There was a somewhat related conversation in comp.text.tex recently ("spring margin", Jun 8) in which someone suggested the memoir class with \leftspringright, but at a quick read that didn't seem to actually do what I'd like.

Any suggestions?

Here is an example. The following tex:

$\,$\newline
\begin{tabular}{m{2.8in}m{3.2in}}
The point on the unit circle associated with $-13\pi/3$ is
$(1/2,-\sqrt{3}/2)$, so
$\cot (-13\pi/3) = -1/\sqrt{3}=-\sqrt{3}/3.$
&
\begin{center}
\includegraphics[scale=0.75]{1_3_13.pdf}
\end{center}
\end{tabular}


generates the following pdf snippet: The document has several hundred such images, and different spacing is required in general depending on the size of the image so as not to exceed the right margin.

My preamble looks like this:

\documentclass[twoside]{book}
\usepackage[margin=1in,twoside]{geometry}
\usepackage{fancyhdr}
\usepackage{amstext}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{array}
\usepackage{multicol}


so I'm not using anything particularly unusual.

• You should comment on why some of the answers provided do not do what you want so that they might be improved. – TH. Sep 12 '10 at 12:00
• Don't ask me how I ended up on this one :), but since you did not accept any of the answers, may I suggest you to use TikZ for this purpose? See the introduction parts of Sec.2,3,4. By the way, you can still use your external figures too. – percusse Dec 3 '11 at 1:59

I suspect that tabularx (also available within memoir) might be able to handle this.

Something like this might be the basis for a solution:

\begin{tabularx}{\textwidth}{Xc}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Maecenas in felis sapien.
& \fbox{\parbox[t]{2cm}{Simulated Figure}}
\end{tabularx}

\begin{tabularx}{\textwidth}{Xc}
Phasellus consectetur, sapien nec mollis euismod,
sapien sapien posuere justo, nec elementum sapien nisl vitae metus.
Ut nisl lacus, tincidunt eget fringilla eget, eleifend sit amet ipsum.
& \fbox{\parbox[t]{4cm}{Simulated Figure}}
\end{tabularx}


The segment \fbox{\parbox[t]{4cm}{Simulated Figure}} stands in place of your figures.

There may be problems with this, depending on the exact nature of your figure. If you'd like to post an example, we may be able to help you better.

• I posted some example text and output as an edit to the original question. – rogerl Sep 9 '10 at 13:50

Here's a new/fresh answer after @rogerl's clarification.

It is possible to measure the figure, and adapt the table dimensions accordingly.

Here's a (slightly elephantine} proof of concept:

\newsavebox{\figbox}
\newlength{\myfigwid}
\newlength{\myfigheight}
\newlength{\myotherside}
\sbox{\figbox}{\includegraphics[scale=1.2]{1_3_13.pdf}}
\settowidth{\myfigwid}{\usebox{\figbox}}
\settoheight{\myfigheight}{\usebox{\figbox}}
\setlength{\myotherside}{\textwidth}
\begin{tabular*}{\textwidth}{m{\myotherside}c}
\parbox[c]{\myotherside}{
The point on the unit circle associated with $-13\pi/3$ is
$(1/2,-\sqrt{3}/2)$, so
$\cot (-13\pi/3) = -1/\sqrt{3}=-\sqrt{3}/3.$}
&
\parbox[c][\myfigheight][c]{\myfigwid}{\usebox{\figbox}}
\end{tabular*}


Just edited the solution to give slightly better centering (I think...)

It is semantically better to flow the text and figures as normal writing (as suggested by TH) rather than placing it in a tabular environment; you can use the wrapfig environment. This makes it also possible to flow the figures both left or right, which will improve the overall look of the page.

Here is a solution:

\begin{wrapfigure}[10]{r}[-1.5em]{0pt}
\includegraphics[width=2.0in]{./graphics/p93.pdf}
\end{wrapfigure}

\quad  % dummy to let wrapfig start before
% actual paragraph

\mathsubparagraph{Parallelogram.}%
\index{Area of parallelogram, to find}%
\index{Parallelogram, to find area of}%
---To find the area of a parallelogram,
multiply its
length, or \textit{base} as it is
called, by its height, or
\textit{altitude} as it is called, or
expressed in the simple
form of an algebraic
equation.---
$A = b \times h$

\begin{wrapfigure}[9]{l}[-1.5em]{0pt}
\includegraphics[width=1.5in]{./graphics/p94a.pdf}
\end{wrapfigure}

\mathsubparagraph{Triangle.}%
\index{Area of triangle, to find}%
\index{Triangle, to find area of}%
---To find the area of a triangle when
the base and altitude are given,
multiply its base by its altitude
and divide by $2$, or
$A=\frac{bh}{2}$
}


You might need to experiment a little at first, but if your text and figures are more or less the same width and amount of text, the work involved is not that much.

The above code gives me this:

• I fully agree that this is semantically cleaner, but OP said "hundreds of figures"; I don't know whether he's prepared to do the individual number-of-lines tuning for each one... – Brent.Longborough Sep 11 '10 at 7:44
• @Brent there are no real short-cuts here, one could wrap some of the commands in macros to lessen the amount of work. At the end one should also produce the figures with TikZ which would take quite a bit of time also. – Yiannis Lazarides Sep 11 '10 at 8:18
• I admit I'm more than a little surprised that there is no short-cut available. I can't believe I'm the first person (or even the hundredth) to have this problem. – rogerl Sep 11 '10 at 12:42
• yes, I agree there are no (or at least very few) shortcuts. But I think that it would be possible to do a semantically clean wrapper based on "my" method (ie sizing the box first). I don't have spare time to do that now, but I'll think about it a bit more and hope to repost something in a few days. Clearly there will be cases where the text and the figure have unfavourable relative sizes, but I think my approach works in a generic way and avoids hand-tuning except in the most recalcitrant cases. – Brent.Longborough Sep 11 '10 at 18:49

And, finally, here's another possible answer, though I accept this may not be exactly what you want: scale all the figures to the same width, using, for example,

\includegraphics[width=4cm]{1_3_13.pdf}

If the figure sizes are within reasonable limits, I think this may be aesthetically more appealing, but I accept that your mileage may vary considerably from mine...

• That would be wonderful, but sadly I cannot do that because I don't have control over the figure sizes and the publisher does have requirements on the final apparent size of the figures. – rogerl Sep 11 '10 at 12:43

How about using wrapfig to place your graphics on the right and then have your text flow around it? Something like

\usepackage{wrapfig}
\newbox\wfigbox
\newcommand*\wfig[3][]{%
\setbox\wfigbox\hbox{\includegraphics[#1]{#2}}%
\begin{wrapfigure}{r}{\wd\wfigbox}%
\box\wfigbox
\caption{#3}%
\end{wrapfigure}%
}


Then you use it like this.

\wfig[scale=2]{image-name}{Caption here}


The optional argument is the same as the optional argument to \includegraphics.

OK, as vaguely promised, here's my final (!?) suggestion.

The ncctools bundle includes, within the nccfloats package, the \sidefig command. Here's a complete example based on \sidefig:

\documentclass[a4paper,twoside,12pt]{memoir}
\usepackage{nccfloats}
\usepackage{graphicx}
\newsavebox{\sbsfigbox}
\newlength{\sbsfigwid}
\newcommand{\sidebyside}[2]{
\sbox{\sbsfigbox}{#1}                              % Get figure into a box
\settowidth{\sbsfigwid}{\usebox{\sbsfigbox}}       % Measure the box
\sidefig[c](\sbsfigwid){\usebox{\sbsfigbox}}{#2}   % Build everything
}
\begin{document}
\section{1.3.3}
\sidebyside{\includegraphics{1_3_13.pdf}}{%
The point on the unit circle associated with $-13\pi/3$ is
$(1/2,-\sqrt{3}/2)$, so
$\cot (-13\pi/3) = -1/\sqrt{3}=-\sqrt{3}/3.$
}
\end{document}

• Rather than answering 4 times, you can edit an old answer. – TH. Sep 12 '10 at 12:02
• I've just spotted that \sidefloat places the figure on the outside, or `\sidefloat* places it on the inside, rather than left or right. Is this OK? – Brent.Longborough Sep 12 '10 at 12:15
• @TH: Yes. I decided consciously to make separate answers, to reduce the clutter in any single answer. This is in part a consequence of progressive discovery of the OP's requirements and my understanding of them. – Brent.Longborough Sep 12 '10 at 12:27