tags:

views:

295

answers:

2

For some reason I thought it would be nice to have inline figures (i.e. no floats) just like the equation environment. They would have to be numbered, as I want to be able to refer to them later on. I've come up with two attempts, but both have their shortcomings. I'm hoping for some feedback that can sort me out.

The first attempt uses 3 minipages (see the code below). This looks nice as the figure number is aligned vertically with the middle of the figure. However, as the width of the figure approaches the width of the page, things start to break down. Also, it doesn't behave very nicely at pagebreaks.

The second attempt uses the equation environment with a different label. Apart from the fact that I don't know if this is a sensible thing to do, it produces extra whitespace in the beginning of the next paragraph. It also doesn't align the label vertically in the center, but puts it on the bottom.

Here's an example of both attempts:

\documentclass{article}
\usepackage{pgf,tikz}
\usepackage{lipsum}

%
% Attempt 1
%
% Uses 3 minipages.
% Breaks if figure is wide, and at the bottom of a page.
%

\usepackage{calc}
\newlength{\figlabelwidth} % width of label
\newlength{\imgwidth} % max. width of figure

\newenvironment{inlinefig1}
{
 \refstepcounter{figure} % increase figure number
 \begin{center} % don't know if this is necessary
 \setlength{\figlabelwidth}{\widthof{(Fig. \thefigure)}} 
 \setlength{\imgwidth}{\textwidth - \figlabelwidth - \figlabelwidth} 
 \setlength{\imgwidth}{0.9\imgwidth} % to be on the safe side
 \begin{minipage}{\figlabelwidth}\makebox[\figlabelwidth]{}\end{minipage} % ghost minipage for centering
 \hfill
 \begin{minipage}{\imgwidth}\begin{center} % minipage for figure
}
{
 \end{center}\end{minipage}
 \hfill
 \begin{minipage}{\figlabelwidth}(Fig. \thefigure)\end{minipage} % minipage for label
 \end{center}
}

%
% Attempt 2
%
% Uses an equation environment with relabeled labels.
% Label is not centered vertically, and produces extra whitespace in the paragraph after it.
%

\def\theoldequation{\theequation} % save the old equation format

\newenvironment{inlinefig2}
{
 \refstepcounter{figure} % increase figure number
 \def\theequation{Fig. \arabic{figure}} % switch to figure numbering
 \begin{equation}
}
{
 \end{equation}
 \def\theequation{\theoldequation} % reset to old equation label format
 \addtocounter{equation}{-1} % correct the equation numbering
}

\begin{document}
\noindent \lipsum[1]
\begin{inlinefig1}
 \begin{tikzpicture}
  \draw (0,0) grid +(12,2);
 \end{tikzpicture}
\end{inlinefig1}
\lipsum[2]
\begin{inlinefig2}
 \begin{tikzpicture}
  \draw (0,0) grid +(12,2);
 \end{tikzpicture}
\end{inlinefig2}
\lipsum[3]
\end{document}

Do you guys have any better ideas, or suggestions to fix the any of the shortcomings? Thanks!

+3  A: 

If you use the "float" package, you can specify H as a placement, which makes it appear exactly "here".

Svante
A: 
\newbox\inlinefigbox
\newenvironment{inlinefig3} 
{
\refstepcounter{figure} % increase figure number 
\setbox\inlinefigbox=\hbox\bgroup
}
{
\egroup
\hbox to \hsize{\hfil \box \inlinefigbox \hss (Fig. \arabic{figure})}
}
Alexey Malistov