3

I made a table wider than \textwidth in pdflscape in LaTeX, so I put it in a new geometry with \newgeometry. After the table, I restored the geometry with restoregeometry. But there is a blank before the table. Is there a way to omit the blank before the table or another way to put the table center in pdflscape?

\documentclass{article}
\usepackage{lipsum,tabularray,afterpage,geometry,pdflscape}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\pagestyle{headings}
\begin{document}
\section{New section}
\lipsum{1-6}
\afterpage{\newgeometry{left=2cm,right=2cm,bottom=0.5cm,top=0.5cm}
    \pagestyle{empty}
    \begin{landscape}
        \begin{longtblr}{
                colspec={X[l,3em] X[c,4em] X[c,4em] X[c,3.5em] X[c,3em] X[c,3em] X[c,4em] X[c,2em] X[c,3em] X[c,3em] X[c,4em] X[c,4em] X[c,4em] X[c,3.5em] X[c,4em] X[c,3em] X[c,6em] X[c,3.2em]},colsep=1pt,rowhead=1
            }
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        \end{longtblr}
    \end{landscape}
    }\restoregeometry
\lipsum{7-12}
\end{document}

enter image description here

I changed codes to :

\afterpage{\newgeometry{left=2cm,right=2cm,bottom=0.5cm,top=0.5cm}
    \begin{landscape}
    \pagestyle{empty}
        \begin{longtblr}{
                colspec={X[l,3em] X[c,4em] X[c,4em] X[c,3.5em] X[c,3em] X[c,3em] X[c,4em] X[c,2em] X[c,3em] X[c,3em] X[c,4em] X[c,4em] X[c,4em] X[c,3.5em] X[c,4em] X[c,3em] X[c,6em] X[c,3.2em]},colsep=1pt,rowhead=1
            }
            \newgeometry{left=2cm,right=2cm,bottom=0.5cm,top=0.5cm}
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        \end{longtblr}\restoregeometry
    \end{landscape}
    }

The blank was removed, but the next page after the table is abnormal. enter image description here

Y. zeng
  • 1,885
  • As a quick clarification request: (1) Do you wish to remove the space on the 1st page shown on your screenshot (the portrait page)? I am struggling in understanding what extra space you are referring to. (2) You notified that you decided to place the table in pdflscape in order to make it wider. Are you looking for a solution that will place the table on a portrait page, but to make the table wider than \textwidth? Thank you very much! – Miriam Briskman Jun 02 '22 at 01:58
  • @MiriamBriskman Thanks for your reply. (1) Yes, I hope to remove the space on the 1st page; (2) My table will contain too many contents which are not suited for portrait page. In this case, I replaced the contents with just numbers. – Y. zeng Jun 02 '22 at 02:06

1 Answers1

4

EDIT: Y.zeng noted that, after the \afterpage command, paragraphs continue beyond the bottom margin.

To make the pages after the landscape mode return to their 'normal' display, without any paragraphs continuing beyond the bottom margin, add the macro call \clearpage right before the closing } of the \afterpage macro.

The decision to use \clearpage is based on the explanation brought in the answer at https://tex.stackexchange.com/a/88658/256551.

Here is the full, correctly-working code:

\documentclass{article}
\usepackage{lipsum,tabularray,afterpage,geometry,pdflscape}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\pagestyle{headings}
\begin{document}
\section{New section}
\lipsum{1-6}
\afterpage{
    \newgeometry{left=2cm,right=2cm,bottom=0.5cm,top=0.5cm}
    \begin{landscape}
    \pagestyle{empty}
        \begin{longtblr}{
                colspec={X[l,3em] X[c,4em] X[c,4em] X[c,3.5em] X[c,3em] X[c,3em] X[c,4em] X[c,2em] X[c,3em] X[c,3em] X[c,4em] X[c,4em] X[c,4em] X[c,3.5em] X[c,4em] X[c,3em] X[c,6em] X[c,3.2em]},colsep=1pt,rowhead=1
            }
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        \end{longtblr}
    \end{landscape}\restoregeometry
    \clearpage % <------ Add this macro call
    }
\lipsum{7-12}

\lipsum{7-12} \end{document}

Then, the PDF pages will look like: enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

Hope this helps!


Your 2nd version of \afterpage, as you specified it in your question, should remove all spaces on all the portrait pages.

The full code should be:

\documentclass{article}
\usepackage{lipsum,tabularray,afterpage,geometry,pdflscape}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\pagestyle{headings}
\begin{document}
\section{New section}
\lipsum{1-6}
\afterpage{\newgeometry{left=2cm,right=2cm,bottom=0.5cm,top=0.5cm}
    \begin{landscape}
    \pagestyle{empty}
        \begin{longtblr}{
                colspec={X[l,3em] X[c,4em] X[c,4em] X[c,3.5em] X[c,3em] X[c,3em] X[c,4em] X[c,2em] X[c,3em] X[c,3em] X[c,4em] X[c,4em] X[c,4em] X[c,3.5em] X[c,4em] X[c,3em] X[c,6em] X[c,3.2em]},colsep=1pt,rowhead=1
            }
            \newgeometry{left=2cm,right=2cm,bottom=0.5cm,top=0.5cm}
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18\\
        \end{longtblr}\restoregeometry
    \end{landscape}
    }
\lipsum{7-12}
\end{document}

and the PDF pages look as follows: enter image description here enter image description here enter image description here enter image description here

Is this the format you were looking to get? Thank you!

Miriam Briskman
  • 479
  • 2
  • 11
  • 1
    Hello, thanks for your reply. If you change \lipsum{7-12} to \lipsum{7-12}\lipsum{7-12}, you will find the page after the table is weird at the bottom. – Y. zeng Jun 02 '22 at 02:38
  • @Y.zeng, thank you very much for your important concern. I will look into this issue now, and will edit the answer accordingly should I find that the change works. Thank you very much! – Miriam Briskman Jun 02 '22 at 02:39
  • @Y.zeng, I believe a solution was found! Just add \clearpage before the closing brace } of the \afterpage macro. The answer was added to include this solution. Please let us know if this solution works. Thank you very much for your attention! – Miriam Briskman Jun 02 '22 at 03:13
  • 1
    There is an sutomatic \clearpage at the beginning and end of both landscape and \newgeometry-\restoregeometry. After all, any floats in the queue will have a different width. – John Kormylo Jun 02 '22 at 03:24
  • @JohnKormylo, thank you very much for noting this. I based my answer on the idea at https://tex.stackexchange.com/a/88658/256551 (I should probably add this link to my answer.) – Miriam Briskman Jun 02 '22 at 03:28
  • 1
    @MiriamBriskman Although from my knowledge I can't understand what you said totally, you hele me a lot and help me resolve the problem. Thanks very much! – Y. zeng Jun 02 '22 at 03:32
  • @Y.zeng, thank you very much for your kind words! – Miriam Briskman Jun 02 '22 at 03:34
  • 1
    Interestingly, \leavevmode\vspace{-\baselineskip} also works. – John Kormylo Jun 02 '22 at 04:08
  • Based on page #42 of the doc on geometry at https://ctan.mirrors.hoobly.com/macros/latex/contrib/geometry/geometry.pdf, the \restoregeometry macro is defined as \newcommand{\restoregeometry}{\clearpage\Gm@restore@pkg\Gm@changelayout}. So it seems, as you suspected, that \clearpage is present at the beginning of the definition, but not at its end. Thank you very much, @JohnKormylo, for raising this idea! – Miriam Briskman Jun 02 '22 at 04:13
  • @MiriamBriskman Hello, I have found another question: the next page after the table has not page number if page number was put at the bottom of pages. – Y. zeng Jun 02 '22 at 04:30
  • @Y.zeng, thank you very much for your response. It looks like the new question you raised deserves to become an independent question. First, check if you can find any online similar questions that may help in solving yours. If you can't find anything helpful, create another question here on the website. Good luck! – Miriam Briskman Jun 02 '22 at 04:36
  • @MiriamBriskman Thanks very much. Let me create a new one! – Y. zeng Jun 02 '22 at 04:38
  • @Y.zeng, thank you. Good luck! – Miriam Briskman Jun 02 '22 at 04:39
  • @MiriamBriskman Thanks! Have a good day ahead! – Y. zeng Jun 02 '22 at 04:41