2

According to the rules for written work at my university, a table or figure cannot divide a paragraph. In the following MWE, the table doesn't fit on the first page and is therefore placed on the second page. This is fine, but the following paragraph is split by the positioning of the table. Ideally, the table should appear after this split paragraph, or the split paragraph should appear after the table. How can I do this in general, as this situation is often repeated in my text?

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{blindtext}
\begin{document}
\blindtext[3]\par

Text text \textbf{The table should appear here, avoiding splitting the paragraph. According to the rules for my work, paragraphs should be complete, either before or after tables and figures.}\par

\begin{table}[h!] \centering \caption{The caption} \begin{tabular}{|c|c|} \hline & \ \hline 1 & 7 \ \hline 2 & 77 \ \hline 3 & 777 \ \hline 4 & 777 \ \hline 5 & 7777 \ \hline \end{tabular} \end{table}

\blindtext\par

\end{document}

enter image description here

  • 1
    I don't know a way to do this automatically. What I would do is, after the text is final, move the input for the table to after the paragraph that is being split, and use the [h] option. Start at the beginning of the file, and reprocess after every change -- the vertical spacing will change (more extra space in the middle of a page than at top or bottom), so paragraphs that were broken in an earlier run may not break after rearrangement. Also consider the use of \enlargethispage and \looseness if only one or two lines are broken to a new page. – barbara beeton Sep 20 '23 at 21:20
  • duplicate of this https://tex.stackexchange.com/questions/246155/how-to-place-floats-between-paragraphs/246315#246315 – David Carlisle Sep 20 '23 at 23:26
  • 1
    Most likely that rule from your university does not apply to top/bottom tables not closely stucked text but only to tables really within paragraphs on the same page (that indeed is a horrible place). IMHO the top/bottom floats are better than even between paragraphs, and a naked eye, this is the usual for the 95% of good scientific journals tha I saw. Just ask before to mess too munch with table positions. – Fran Sep 21 '23 at 09:25
  • 1
    If you want the table "here" at all costs, you can use the [H] option with the float package, but if it doesn't fit, the table have no more option that move to the next page, so leaving a horrible space on the previous page, since you do not allow to fill it with the text after the table. Therefore, it wll be your work rewrite the text to avoid the gaps. – Fran Sep 21 '23 at 09:29
  • Note that [h] is actually implemented as [ht] (you should get a warning about this) because too many people were using [h] and complaining when all their figures were not showing up until the next \clearpage. Also, [h!] rartely differs from [h]; it does NOT mean "try harder." – John Kormylo Sep 21 '23 at 14:53

1 Answers1

2

This creates command \partable to put the table in front of the paragraph if both will not fit on the page.

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{blindtext}

% Place the paragrpah first it there is enougth room for both. \makeatletter \newif\ifparfirst

\newcommand{\partable}[2]% #1 = paragraph, #2 = table {\bgroup\par% use local registers \setbox7=\vbox{#1}% measure height \sbox9{\parbox[b]{\columnwidth}{\def@captype{table}#2}}% \parfirsttrue \ifdim\pagegoal=\maxdimen% new page \else \setlength{\dimen7}{\dimexpr \pagegoal-\pagetotal}% remaining space on page \setlength{\dimen9}{\dimexpr \ht7 + \dp7 + \ht9 + \dp9 + \intextsep}% \ifdim\dimen7<\dimen9 \parfirstfalse \fi \fi \ifparfirst \unvbox7 \begin{table}[h]\usebox9\end{table} \else \begin{table}[h]\usebox9\end{table} \unvbox7 \fi %\the\dimen7\par\the\dimen9\par\the\dimexpr \ht9 + \dp9 + \intextsep\relax \par\egroup} \makeatother

\begin{document} \blindtext[3]\par

\partable{Text text \textbf{The table should appear here, avoiding splitting the paragraph. According to the rules for my work, paragraphs should be complete, either before or after tables and figures.}}% {\centering \caption{The caption} \begin{tabular}{|c|c|} \hline & \ \hline 1 & 7 \ \hline 2 & 77 \ \hline 3 & 777 \ \hline 4 & 777 \ \hline 5 & 7777 \ \hline \end{tabular} }

\blindtext\par

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120