# How to interrupt longtable to place float (figure)?

I am writing a document describing a process step by step. After trying with enumerate (which did not look so appealing) I switched to longtable.

This longtable can span more than two full pages.

The problem I ran into is illustrated by the MWE below. Since there is not sufficient space on the first page to place the figure, it is moved after the longtable. Is there a way of placing floats so they interrupt the longtable? The figure should be on the top of page 2.

Or is there a different LaTeX macro that will allow me to typeset the process steps in the same way but will be treated as text (and can thus be interrupted by floats)? Requirement is that the step number can be referenced.

\documentclass[a4paper,10pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{paralist}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{lipsum}

%% Compact list to be used within tabular for step listing
\newenvironment{PCitem}
{
\begin{minipage}[t]{\linewidth}\begin{compactitem}
} {
\end{compactitem}\end{minipage}
}

%% counter step listing, right aligned
\newcounter{PCstep}
\newcommand{\PC}{%
\refstepcounter{PCstep}%
\hfill\thePCstep.}

%% dummy table content
\newcommand{\tablecontent}{
\PC & Doing this & Take care!\vspace{1ex}\\

\PC & Doing that & Doing that is very complicated and is divided into the following steps

\begin{PCitem}
\item Cleaning
\item Making dirty
\item Cleaning, again
\item Making dirty, again
\item oh stop it!
\end{PCitem}\vspace{1ex}\\
}

\begin{document}

\section{Page almost filled, so no space for float}

\lipsum[3-6]

\section{Beginning}

This is some text, and soon there will be a longtable spanning three pages. But before Figure~\ref{fig:label} is inserted, which should be ideally be placed on the next page, effectively interrupting the longtable.

%% Figure
\begin{figure}[!tb] % Here Top Bottom
\centering
\begin{center}
\rule{400pt}{100pt}
\caption{This figure is actually supposed to be placed before the longtable.}
\label{fig:label}
\end{center}
\end{figure}

%% Longtable
\begin{longtable}{p{25pt}>{\textbf\bgroup}p{117pt}<{\egroup}p{240pt}}

& \textbf{\itshape Step} & \textbf{\itshape Details} \vspace{1ex}\endfirsthead

\tablecontent
\tablecontent
\tablecontent
\tablecontent
\tablecontent
\tablecontent
\tablecontent
\tablecontent
\tablecontent

\end{longtable}

\end{document}

-

can't really interupt the longtable but you can put \clearpage before it to stop the float floating past it.

Incidentally the optional argument

[!tb] % Here Top Bottom


increases the chance of the float going to the end of the document as it does not allow p also, despite the comment it doesn't allow here floats (you need to include h) for that.

-
Your solution gets close. I have to put the \clearpage before the figure. If between figure and table, the figure is placed on its own page before the table. Nevertheless, on the first page the space from the end of the text to the end of the page becomes white. Depending on figure size this white space could end up quite large. Then you wrote "can't really interrupt longtable". I reckon it should have been "can't interrupt"? –  user334287 Jul 30 '12 at 8:38
you could use afterpage as suggested by Mico to delay both the figures and the longtable to the next page break to avoid the short page. In that case I would put them in a separate file and just \afterpage{\clearpage\input{file-with-figure-and-longtable}} –  David Carlisle Jul 30 '12 at 8:59
–  David Carlisle Jul 30 '12 at 9:00

This might be a good case for using the afterpage package and its associated command, \afterpage:

...
\afterpage{  % start of 'afterpage' group
\begin{figure}[!t] % force figure to be set right away at top of page
\centering % you don't need both \centering and the 'center' environment.
\rule{400pt}{100pt}
\caption{This figure will now be placed before the longtable.}
\label{fig:label}
\end{figure}

%% Longtable
\begin{longtable}{p{25pt}>{\textbf\bgroup}p{117pt}<{\egroup}p{240pt}}
& \textbf{\itshape Step} & \textbf{\itshape Details} \vspace{1ex}
...
\end{longtable}
} right curly braces to signal end of 'afterpage' group
...


With this setup, you needn't keep track of where on the current page you are when the \afterpage{ ... } stuff is encountered; LaTeX will (happily?) defer typesetting the argument of this command until after the current page is fully finished.

-
Using afterpage appears to have the same affect as David's answer above. The bottom of page 1 stays white and the table starts after the figure on page 2, even though there is space for header and item 1 on page 1. I feel your code is potentially dangerous: (i) I need to keep track of that curly bracket after the table and (ii) if the \afterpage moves to the top of a page (change of content), that whole page will stay white. –  user334287 Jul 30 '12 at 8:47
@user334287 - A major constraint you're encountering is that one cannot break up a longtable with another float. What the \afterpage instruction would do for you -- relative to David's answer -- is to keep you from having to create a potentially unsightly premature linebreak before providing the code for the figure and the longtable; instead, LaTeX is left free to fill up that page with material encountered after longtable is finished. As to your concern that a whole blank page might be inserted if \afterpage is encountered at the top of a page: Not to worry, this won't happen. –  Mico Jul 30 '12 at 10:23
Thank you for that clarification. In my example there is not text after the longtable, thus the empty space. And thank you for the confirmation that longtable cannot be interrupted. Since I cannot have text written after the longtable appear before it I will stick to the tabbing solution I posted below. –  user334287 Jul 30 '12 at 10:48

I changed the code to emulated the longtable with a tabbing environment. Code below. To allow multiline cells a lot of minipages have to be used. The longtable is also included for comparison.

Advantages of tabbing (in my opinion):

• Treated by latex as text, not float
• I can interrupt the tabbing to place figures or other floats without unexpected float (longtable) placement.

• Code looks much more complicated with all the minipages

One problems remains: \nopagebreak does not work for the first line (ie header). To show that uncomment the \\ just before the first figure. Use \\* instead of \nopagebreak.

\documentclass[a4paper,10pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{paralist}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{lipsum}

%% Compact list to be used within tabular for step listing
\newenvironment{PCitem}
{
\begin{minipage}[t]{\linewidth}\begin{compactitem}
} {
\end{compactitem}\end{minipage}
}

%% counter step listing, right aligned
\newcounter{PCstep}
\newcommand{\PC}{%
\refstepcounter{PCstep}%
\begin{minipage}[t]{20pt}\hfill\thePCstep.\hspace{2ex}\end{minipage}}

%% tabbing with fixed tab sizes to imitage longtable, no spacing above/below
\newcommand\mytabs{\hspace{25pt}\=\hspace{117pt}\=\hspace{240pt}}
\newenvironment{mytabbing}[1][\mytabs]
{\setlength{\topsep}{0pt}%
\setlength{\partopsep}{0pt}%
\setlength{\itemsep}{0pt}%
\begin{tabbing}#1\kill}
{\end{tabbing}}

%% dummy table content
\newcommand{\tablecontent}{
\PC & Doing this & Take care!\vspace{1ex}\\

\PC & Doing that with longtable and showing linebreaks in second column& Doing that is very complicated and is divided into the following steps

\begin{PCitem}
\item Cleaning
\item Making dirty
\item Cleaning, again
\item Making dirty, again
\item oh stop it!
\end{PCitem}\vspace{1ex}\\
}

\newcommand{\tabbingcontent}{
\PC\>
\begin{minipage}[t]{109pt}\textbf{Doing this}\end{minipage}\>
\begin{minipage}[t]{232pt}Take care!\end{minipage}\\[1ex]

\PC\>\begin{minipage}[t]{109pt}\textbf{Doing that even much better with tabbing}\end{minipage}\>
\begin{minipage}[t]{232pt}Doing that is very complicated and is divided into the following steps

\begin{PCitem}
\item Cleaning
\item Making dirty
\item Cleaning, again
\item Making dirty, again
\item oh stop it!
\end{PCitem}
\end{minipage}\\[1ex]
}

\begin{document}

\section{Page almost filled, so no space for float}

\lipsum[3-6]

\section{Beginning}

This is some text, and soon there will be a longtable spanning three pages. But before Figure~\ref{fig:label} is inserted, which should be ideally be placed on the next page, effectively interrupting the longtable. Now some more text to make sure that the nopagebreak works as intended. And some more text, just to make the tabbing jump to the next page. Still not enough text, so I will type a bit more useless stuff. This tabbing is very resilient, wonder when it will break. Oh, and one more line.\\
%\\  % uncomment to show nopagebreak problem

%% Figure
\begin{figure}[t] % Here Top Bottom
\centering
\rule{400pt}{100pt}
\caption{With tabbing the listing can be interrupted to place this figure.}
\label{fig:label}
\end{figure}

%% beginning of the longtable-emulation through tabbing. Need a lot of
%% minipages to allow multiline cells.
\begin{mytabbing}
\>\textbf{\itshape Step} \>\textbf{\itshape Details}\\*

\tabbingcontent
\tabbingcontent
\tabbingcontent
\PC\>\begin{minipage}[t]{109pt}\textbf{Figure placement inside the tabbing}\end{minipage}\>\begin{minipage}[t]{232pt}I'm referencing to Figure~\ref{fig:F2}. For the figure code the tabbing is interrupted.\end{minipage}
\end{mytabbing}
\vspace{1ex}
%% Figure
\begin{figure}[t] % Here Top Bottom
\centering
\rule{400pt}{100pt}
\caption{Figure placed inside tabbing}
\label{fig:F2}
\end{figure}
\begin{mytabbing}
\tabbingcontent
\tabbingcontent
\tabbingcontent
\tabbingcontent
\tabbingcontent

\end{mytabbing}

Longtable below for comparison

%% Longtable
\setcounter{PCstep}{0}
\begin{longtable}{p{25pt}>{\textbf\bgroup}p{117pt}<{\egroup}p{240pt}}

& \textbf{\itshape Step} & \textbf{\itshape Details} \vspace{1ex}\endfirsthead

\tablecontent
\tablecontent
\tablecontent
\tablecontent
\PC & Figure placement inside the longtable & I'm referencing to Figure~\ref{fig:F3}. For the figure code the longtable is interrupted.\\[1ex]
\end{longtable}
%% Figure
\begin{figure}[t] % Here Top Bottom
\centering
\rule{400pt}{100pt}
\caption{Figure placed inside longtable}
\label{fig:F3}
\end{figure}
\begin{longtable}{p{25pt}>{\textbf\bgroup}p{117pt}<{\egroup}p{240pt}}
\tablecontent
\tablecontent
\tablecontent
\tablecontent
\tablecontent

\end{longtable}

\end{document}


Note: Footnotes not working properly with this solution.

-