Graphics, Figures & TablesInline figures with equation-like numbering

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
TANutma
Posts: 3
Joined: Fri Mar 05, 2010 11:06 am

Inline figures with equation-like numbering

Post by TANutma »

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 a minimal example of both attempts:

Code: Select all

\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!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Inline figures with equation-like numbering

Post by gmedina »

Hi,

perhaps some of the features provided by the caption package could be useful for you:

Code: Select all

\documentclass{book}
\usepackage{tikz}
\usepackage{caption}
\usepackage{lipsum}

\DeclareCaptionType[within=chapter]{myfig}[Fig.]
\DeclareCaptionFormat{myformat}{(#1)}
\captionsetup[myfig]{format=myformat}

\begin{document}

\noindent \lipsum[1]

\noindent \begin{minipage}{\textwidth}
   \begin{tikzpicture}
      \draw (0,0) grid +(12,2);
   \end{tikzpicture}
  \captionof{myfig}{}
\end{minipage}

\noindent \lipsum[1]

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
TANutma
Posts: 3
Joined: Fri Mar 05, 2010 11:06 am

Re: Inline figures with equation-like numbering

Post by TANutma »

Thanks! But I would really like the label to go to the right of the image, and be vertically centered. As far as I know this isn't possible with the caption package, is it?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Inline figures with equation-like numbering

Post by gmedina »

To put the caption beside the object and vertically centered, you could use the floatrow package.

Edit: a full example:

Code: Select all

\documentclass{book}
\usepackage{caption}
\usepackage{floatrow}
\usepackage{lipsum}

\DeclareNewFloatType{myfig}{placement=H,name=Fig.}
\DeclareCaptionLabelFormat{myformat}{(#1~#2)}
\captionsetup[myfig]{labelsep=none,labelformat=myformat}
\floatsetup[myfig]{capposition=beside,margins=centering,
  capbesideposition={right,center},floatwidth=sidefil,capbesidewidth=1.2cm}


\newenvironment{myfigure}
  {\setlength\floatsep{0pt}\setlength\textfloatsep{0pt}\setlength\intextsep{5pt}
    \begin{myfig}}
  {\end{myfig}}

\begin{document}

\noindent \lipsum[1]
\begin{myfigure}
  \rule{10cm}{3cm}
  \caption{}
\end{myfigure}
\noindent \lipsum[1]
\begin{myfigure}
  \rule{1cm}{3cm}
  \caption{}
\end{myfigure}
\noindent \lipsum[1]

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
TANutma
Posts: 3
Joined: Fri Mar 05, 2010 11:06 am

Re: Inline figures with equation-like numbering

Post by TANutma »

Thanks for the help! This is just what I wanted. Unfortunately, the floatrow package doesn't play nicely when you temporarily change the margins of the page. When placed in a \begin{adjustwidth}{2em}{2em} command of the 'changepage' package, the inline figures behave as if \textwidth hasn't been changed, resulting in an overful hbox. Any attempts on my part to fix this behaviour have failed ...
But thanks again for the help, gmedina!
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Inline figures with equation-like numbering

Post by gmedina »

You're welcome. One possible solution for the problem with the changepage package would be to define the myfigure environment with a mandatory argument controlling capbesidewidth.

An example:

Code: Select all

\documentclass{book}
\usepackage{caption}
\usepackage{floatrow}
\usepackage{lipsum}
\usepackage{changepage}

\DeclareNewFloatType{myfig}{placement=H,name=Fig.}
\DeclareCaptionLabelFormat{myformat}{(#1~#2)}
\captionsetup[myfig]{labelsep=none,labelformat=myformat}
\floatsetup[myfig]{capposition=beside,margins=centering,
  capbesideposition={right,center},floatwidth=sidefil,capbesidewidth=1.2cm}

\newenvironment{myfigure}[1]
  {\setlength\floatsep{0pt}\setlength\textfloatsep{0pt}\setlength\intextsep{5pt}
    \thisfloatsetup{capbesidewidth=#1}
    \begin{myfig}}
  {\end{myfig}}

\begin{document}

\noindent \lipsum[1]
\begin{myfigure}{1.2cm}
  \rule{10cm}{3cm}
  \caption{}
\end{myfigure}
\noindent \lipsum[1]
\begin{adjustwidth}{2em}{2em}
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\begin{myfigure}{3cm}
  \rule{1cm}{3cm}
  \caption{}
\end{myfigure}
\end{adjustwidth}
\noindent \lipsum[1]

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply