4

I have a tabular environment and some associated text. Now, I don't want to use this text as the caption for various reasons and I want to make sure that they are always on the same page, or that the text at least comes on the same page as the beginning of the table.

Is there any way to force this to occur without specifying exactly how large the table (or related types of content one might be trying to associate) is?

lockstep
  • 250,273
akdom
  • 2,325
  • 2
    I assume by "caption" you're referring to the fact that you want the "associated text" to "go" with the "tabular environment." If this is the case, and the location of the "associated text" is known, boxing them seems to be the option. You can either do this using a minibox environment (with a fixed/full width of \linewidth) or perhaps a varwidth environment from the varwidth package. varwidth is like minipage, but allows to shrink to the content's natural width. If this does not seem to be an option, please elaborate on your requirement. – Werner Nov 03 '11 at 20:27
  • Have a look at my answer to this post – cmhughes Nov 03 '11 at 21:48

2 Answers2

9

Since you're not sure about specifying a width, you can box the tabular content and let LaTeX extract the width. Here is such an approach:

Boxing the tabular is performed using the lrbox environment:

\begin{lrbox}{<box cmd>}
  ...
\end{lrbox}

where <box cmd> is defined using \newsavebox{<box cmd>}. In my MWE (see below), I've used \mytabular. Then you can box the entire contents of the "keep stuff together" inside a minipage of width \linewidth. This way it will stretch the entire text block width, even though you might only use a portion of it.

The width of the boxed tabular \mytabular is accessed using \wd\mytabular (similarly, \ht\mytabular would be the height, while \dp\mytabular would be the depth). With the tabular saved, \usebox{\mytabular} typesets it.

enter image description here

\documentclass{article}
%\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\newsavebox{\mytabular}% Capture a tabular in a box
\begin{document}
\lipsum[1-4]% dummy text
\noindent\begin{minipage}​​​​​​​​​{\linewidth}
  \centering
  \begin{lrbox}{\mytabular}% Store tabular in \mytabular
    \begin{tabular}{llcrr}
      \hline
      Heading 1 & Heading 2 & Title & Heading 3 & Heading 4 \\ \hline
      stuff & stuff & some more stuff & stuff & stuff \\
      stuff & stuff & some more stuff & stuff & stuff \\
      stuff & stuff & some more stuff & stuff & stuff \\ \hline
    \end{tabular}
  \end{lrbox}

  \begin{minipage}{\wd\mytabular}
    \usebox{\mytabular}% Print tabular

    \lipsum[1]% dummy text/"caption"
  \end{minipage} \par
  \kern\textfloatsep% Make this seem like a "float"
​\end{minipage}
\lipsum[5-9]% dummy text
\end{document}
​

Modifications to the width of the "caption" and the vertical gap below the "caption" is possible.

I've commented out the varwidth package since it's use does not seem required here. However, if your request is modified, it might be.

Moriambar
  • 11,466
Werner
  • 603,163
  • 1
    What happens, if it is a very long text and and maybe also a long table? This is one of these questions that cannot be answered unless the constraints are specified. – yannisl Nov 03 '11 at 21:11
  • I agree. It seems very particular to a specific instance of the table. Perhaps using longtable if the table is too long, boxing (as I suggested) if it is not. Also, how is the content around it treated - the left-over white space from page breaking, for example. – Werner Nov 03 '11 at 21:15
  • 1
    Better algo, Combine the minipage+tabular in the lrbox, measure height, if it exceeds the page size, use longtable plus emit message in the log, if it is within say 70% of pageheight float on own page etc. Consider minipage material to be inserted in multicolumn at bottom of tabular. – yannisl Nov 03 '11 at 21:34
2

I assume the "associated text" materials are explanatory in nature, so they should go below the tabular(x,y) environment. If you place them below the \end{tabular} statement but before the \end{table} statement, you'll get exactly what you're looking for.

Mico
  • 506,678