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

14
  • 1
    sidewaystable is a float like table so can never break over a page, use the landscape enviornment from the lscape or pdflscape package. May 26, 2020 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, 2020 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. May 26, 2020 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 May 26, 2020 at 18:30
  • 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. tex.stackexchange.com/questions/334246/… May 26, 2020 at 21:24

3 Answers 3

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .