1

I have several long tables in my document, some of which are in landscape mode. When a table is "landscaped" using sidewaystable, although the table is in the next full page, the text continues as usual until it fills the previous page (see first image). This is the expected behaviour, identical to normal tables or figures --for which Latex finds "the best position" and the text before and after the table/figure continues as usual.

As my tables are long, I need to use landscape from the package pdflscape or lscape. However, when using either of them, the text after the table is not continuing as usual to fill the previous page (see second image).

Is there any way around this? I'd like to be able to obtain the same behaviour of sidewaystable (actually, the usual behaviour) for my long landscaped tables.

MWE (change the comment from the \begin and \end{landscape} to those of sidewaystable to exchange the result):

\documentclass[11pt, a4paper, oneside]{report}
\usepackage[left=4cm,right=2cm,top=2.5cm,bottom=2.5cm]{geometry}
\usepackage{xltabular}
\usepackage{pdflscape}
\usepackage{lipsum}
\usepackage{rotating}

\begin{document}

\setlength{\parskip}{6pt}
\lipsum[23]

\begin{sidewaystable}
% \begin{landscape}
 \setlength\LTcapwidth{\linewidth} % I need that for long captions
{\small
\begin{xltabular}{\linewidth}{@{} XXXX @{}}
\caption{{\normalsize This is a caption.\label{table:bla}}}\\
\hline
Column 1 & Column 2 & Column 3 & Column 4 \\
\hline
\endfirsthead
\multicolumn{4}{c}%
{\footnotesize{\tablename\ \thetable\ -- \textit{Continued from previous page}}} \\ \hline
Column 1 & Column 2 & Column 3 & Column 4 \\
\hline
\endhead
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\end{xltabular}}
% \end{landscape}
\end{sidewaystable}

\lipsum[1]

\lipsum[2]

\lipsum[3]

\end{document}

Result using sidewaystable (the table does not break to change the page but the text behaves "correctly"): sidewaystable

Result using landscape (the table does break to change the page but the text behaves "wrongly"): enter image description here

Zarko
  • 296,517
Miquel
  • 103
  • 9
  • 1
    sidewaystable is a float like table so can never break over a page, use the landscape enviornment from the lscape or pdflscape package. – David Carlisle May 26 '20 at 18:01
  • @DavidCarlisle Yes, that's what I do. However, the text is then broken at the position of the table in the code. What I want is the text to behave "normally", i.e., continue until it fills the page. Here I only use sidewaystable to show the behaviour I want the text to have. – Miquel May 26 '20 at 18:25
  • 2
    well that's the choice. floats do not split. Personally I'd remove the longtable run latex so that I see the good page break then re-insert the table at that point so you get a natural page break. A bit of effort but fewer keystrokes than it takes to write this comment. – David Carlisle May 26 '20 at 18:28
  • also please remove all the mis-placed \\ after lipsum that tex is warning about Underfull \hbox (badness 10000) in paragraph at lines 10--11 – David Carlisle May 26 '20 at 18:30
  • Oh, I haven't thought of that idea, thanks. For me it is a good solution, as I only have 3 or 4 of such tables. But isn't there any other option? In a document with tenths of such tables, that must be quite annoying... – Miquel May 26 '20 at 20:08
  • I don't understand why the \\ are mis-placed, neither what it means this warning (my document has quite a few of these...) :-S How to finish the paragraph, then? – Miquel May 26 '20 at 20:10
  • 1
    \\ does not finish the paragraph that is the problem, it should never be used at the end of a paragraph it should (vary rarely) be used to force a line break mid-paragraph. A paragraph is ended by a blank line in the source. https://tex.stackexchange.com/questions/334246/what-does-the-phrase-underfull-hbox-badness-10000-in-paragraph-actually-mea/334249#334249 – David Carlisle May 26 '20 at 21:24
  • @DavidCarlisle I thought \ + blank line was the standard way to end a paragraph leaving space after it... So I see one has to use \setlength{\parskip}{XXpt} for that, right? Maybe you could add a line or two in your answer (the link) saying that? I think many non-experts use \ + blank line in order to have this extra space between paragraphs --at least this is the case within the people I know. (I would comment this directly in your other answer but I don't have enough reputation) – Miquel May 27 '20 at 08:08
  • perhaps this is a better answer for you https://tex.stackexchange.com/questions/82664/when-to-use-par-and-when-newline-or-blank-lines/82666#82666 – David Carlisle May 27 '20 at 08:10
  • I've also seen it, thans :-) – Miquel May 27 '20 at 08:14
  • Does the use of \+blank line have something to do with Latex leaving isolated lines either at the beginning or the end of a page? Does it consider them to be 2 lines? – Miquel May 27 '20 at 08:22
  • yes \\ followed by a blank line is typeset like \\zzz followed by a blank line, the line with zzz (or nothing) is a line of the paragraph not vertical space so is never dropped. and as it is a line of the paragraph the widow/orphan counting counts that line, similarly the finalhyphendemerits guard agaianst hyphenating the penultimate line of a paragraph applies to teh wrong line. "Badness 10000" is TeX's maximum level of badness. If you get that warning then something is very wrong with the document. – David Carlisle May 27 '20 at 08:40
  • Thanks. I think many things are very wrong in my doc XD Let's see what happens after I change this... – Miquel May 27 '20 at 08:49
  • @DavidCarlisle coming back to your idea of removing the table, run latex to see where the page breaks and re-insert the table at that point; how would you code breaking the paragraph which "contains the table in between" so that the last line in the page spans for the whole line width, and the next line is not indented (especially if the last word of the page is hyphenated (broken between the pages))? – Miquel May 27 '20 at 17:02

3 Answers3

3

It's been three years since the question was posted, so I hope it is still useful for someone out there. The problem can be solved by using the package "afterpage", in which the command forces the execution of the code passed to the argument.

I hope that the website will remain available for a while, so I'm sharing the link where Juanjo first solved the problem here . Additionally, I'm providing the minimal example that was shared:


    \documentclass{article}
    \usepackage{array,longtable,lipsum,afterpage,pdflscape}
    \begin{document}
    \lipsum[1]
    \afterpage{
    \begin{landscape}
    \begin{longtable}{|c|>{\raggedright}p{0.7\linewidth}|}
      \hline
      A & \lipsum[1] \tabularnewline \hline
      B & \lipsum[2] \tabularnewline \hline
      C & \lipsum[3] \tabularnewline \hline
      D & \lipsum[4] \tabularnewline \hline
    \end{longtable}
    \end{landscape}
    }
    \lipsum[2-8]
    \end{document}

1

There doesn't seem to be a way to solve this problem, and I only found a dodgy workaround:

As @DavidCarlisle says in the comments of the question, you can run the code without the table to check where you have to introduce the table. What you want is to break the paragraph in your code in the same place where the latex breaks the page. To do that you can use \linebreak so that the last sentence in the page is justified, introduce the table, and start the rest of the text with \noindent.

However, this leads to having a blank page before the table. Therefore, instead of adding \linebreak where the last line of the page ends, you have to add it where the previous line ends:

Place <code>\linebreak</code> after "mi."

See MWE:

\documentclass[11pt, a4paper, oneside]{report}
\usepackage[left=4cm,right=2cm,top=2.5cm,bottom=2.5cm]{geometry}

\usepackage{xltabular}
\usepackage{pdflscape}
\usepackage{lipsum}
\usepackage{rotating}

\begin{document}

\setlength{\parskip}{6pt}
\lipsum[2-7]

Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor loremnon justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi. \linebreak % As seen when running the code without the table, here is where the previous to the last line of the page ends.

\begin{landscape}
\setlength\LTcapwidth{\linewidth}
{\small
\begin{xltabular}{\linewidth}{@{} XXXX @{}}
\caption{{\normalsize This is a caption.\label{table:bla}}}\\
\hline
Column 1 & Column 2 & Column 3 & Column 4 \\
\hline
\endfirsthead
\multicolumn{4}{c}%
{\footnotesize{\tablename\ \thetable\ -- \textit{Continued from previous page}}} \\ \hline
Column 1 & Column 2 & Column 3 & Column 4 \\
\hline
\endhead
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5]   \\ \hline
\end{xltabular}}
\end{landscape}

\noindent  Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesquea nulla. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculusmus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursusluctus mauris. % \noindent makes the rest of the paragraph start without an indent, as if it would actually continue as normal.

\lipsum[2-7]

\end{document}

This is quite dodgy because 1) one has to do everything by hand, which may be very impractical for a long document with plenty of such tables and every time the spacing before the table is changed by any minor editing, it is likely you have to re-place the \linebreak and \noindent; 2) it can not even be done in the place where it is supposed to be --at the end of the page-- but one line above to avoid introducing a blank page.

I'm sure there must be a better way, but no one is giving any better way to do it. So, better having a dodgy workaround than nothing.

Miquel
  • 103
  • 9
1

Well, I overlooked this question when it was asked ...

  • for such situations have been defined afterpage package, which enable to fill otherwise empty spaces before landscape environments and other similar constructs, which start new pages in document (see also answer of @Erick Garcia, +1). -Your table can be simply, and to my opinion more aesthetical write by use of the tabularray package which employ booktabs package (loaded as its library). _ For nicer formatting of text in cells is added package microtype:
\documentclass[11pt, a4paper, oneside]{report}
\usepackage[hmargin={4cm,2cm}, vmargin=2.5cm]{geometry}

\usepackage{microtype} \usepackage{tabularray} \UseTblrLibrary{booktabs} \usepackage{pdflscape} \usepackage{afterpage} \usepackage{lipsum}

\begin{document} \lipsum[1-5]

\afterpage{ \begin{landscape} %\leavevmode\vfil \begin{longtblr}[ caption=This is a caption, label={table:bla} ]{ colspec = {@{} *{4}{X[j]} @{}}, row{2-Z}= {rowsep=4pt}, rowhead = 1 } \toprule Column 1 & Column 2 & Column 3 & Column 4 \ \midrule % table body \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] \ \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] \ \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] \ \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] \ \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] \ \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] \ \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] \ \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] & \lipsum[2][3-5] \ \bottomrule \end{longtblr} \end{landscape} }

\lipsum[6-12]

\end{document}

enter image description here

enter image description here

Zarko
  • 296,517