如何中断longtable来放置浮点数(数字)?

如何中断longtable来放置浮点数(数字)?

我正在写一份文档,逐步描述一个过程。在尝试使用enumerate(看起来不太吸引人)后,我改用了longtable

这张长表可以跨越整整两页以上。

下面的 MWE 说明了我遇到的问题。由于第一页上没有足够的空间放置图形,因此图形被移到了长表之后。有没有办法放置浮动元素,使它们中断长表?图形应该位于第 2 页的顶部。

或者是否有不同的 LaTeX 宏可以让我以相同的方式排版流程步骤,但会将其视为文本(因此可以被浮点数打断)?要求是可以引用步骤编号。

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

答案1

无法真正中断长表,但你可以将其放在\clearpage它前面以阻止浮点数浮过它。

顺便说一下可选参数

[!tb] % Here Top Bottom

增加了浮点数到达文档末尾的机会,因为它也不允许p,尽管注释中说这里不允许浮点数(您需要包含h)。

答案2

这可能是使用该afterpage包及其相关命令的一个很好的例子\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}
\endfirsthead
...
\end{longtable}
} right curly braces to signal end of 'afterpage' group
...

通过这种设置,您不需要在遇到内容时跟踪当前页面的位置\afterpage{ ... };LaTeX 将(高兴地?)推迟排版此命令的参数,直到当前页面完全完成之后。

答案3

我修改了代码以模拟longtable环境tabbing。代码如下。为了允许多行单元格,必须使用大量小页面。还包括 longtable 以供比较。

制表符的优点(我认为):

  • 被 latex 视为文本,而不是浮动
  • 我可以中断制表符来放置数字或其他浮点数,而不会出现意外的浮点数(长表)放置。

制表符的缺点:

  • 由于包含所有小页面,代码看起来更加复杂

仍存在一个问题:\nopagebreak 对第一行(即标题)不起作用。要显示该内容,请取消注释\\第一个图之前的注释。使用\\*而不是\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}

笔记:使用此解决方案时脚注无法正常工作。

相关内容