# With afterpage the number of each section is not continuous

The following minimum example (MWE) based on this question: Section doesn't start from Landscape orientation page, includes a numbered section as the first instruction after the afterpage instruction that is followed by the landscape environment. As a result, the sections are numbered 1 - 3 - 2 - 4. That is, the sequential order is not preserved.

I know that the author of the afterpage package, David Carlisle, states that this is a preliminary version and that it is not particularly robust.

I do not understand what is the problem and less, how to solve it. I would like to know if someone can tell me if this numbering problem can be easily corrected and how or if on the contrary, it is better to abandon the use of afterpage.

This is my MWE:

% !TeX TS-program = LuaLaTex
% !TeX encoding   = UTF-8
% !TeX spellcheck = en_US

\documentclass{article}
\usepackage{graphicx}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{lipsum}

\begin{document}
\title{Afterpage \& landscape para evitar página prematura}
\author{Zarko en TeX SE\thanks{https://tex.stackexchange.com/questions/437696/section-doesnt-start-from-landscape-orientation-page}}
\date{23 jun 2018}
\maketitle

\section{Sección A: El paquete afterpage}

This package implements a command, \verb|\afterpage|, that causes the commands specified in its argument to be expanded after the current page is output.

\lipsum[1-2]

\afterpage{%
\begin{landscape}
\section{Sección B, Uso de landscape con un título de sección}
\lipsum[1]
\begin{table}[htb]
\caption{My landscape table}
\begin{tabular*}{\linewidth}{|@{\extracolsep{\fill}}*{8}{c|}}
\hline
1   &   2   &   3   &  4   &   5   &   6   &   7   &   8   \\
1   &   2   &   3   &  4   &   5   &   6   &   7   &   8   \\
\hline
\end{tabular*}
\end{table}
\lipsum[1-2]
\end{landscape}
}

\section{Sección C, que debe ocupar el lugar de la tercera sección}
\lipsum[1]
\begin{figure}[htb]
\centering
\includegraphics{example-image-duck}
\caption{image which follows landscape table}
\end{figure}

\lipsum[1]

\section{Sección D, para confirmar la continuidad de la numeración de las secciones}

\lipsum[2]
\end{document}

• You should reserve the landscape-mode pages for tables only -- no other text, including no \section directives. Running text should be typeset exclusively in portrait mode. – Mico Jun 28 '18 at 5:58
• @zarko I was refering to the section numbering. I accept Mico criteria, that its preferable to avoid sectioning commands inside \afterpage{ ... }. – djnavas Jun 28 '18 at 7:34
• you are explictly moving the first section heading after the second via \afterpage so the ordering is as I would expect. – David Carlisle Jun 28 '18 at 9:06
• there is no reason to use afterpage (or landscape) here – David Carlisle Jun 28 '18 at 9:09

In my opinion, you're abusing the machinery of the afterpage package if you place both running text and tabular material in the scope of \afterpage{...}. Landscape mode should be used solely for extra-wide tables and figures. In contrast, all running text should be typeset exclusively in portrait mode.

Of course, if you need to make a call-out to a landscape-mode table or figure, you should by all means do so. But that's no different than making call-outs (cross-references) to portrait-mode items, be they equations, figures, tables, footnotes, etc.

Hence, I'd rewrite the second section of your sample document like this:

\section{Sección B, Uso de landscape sin título de sección}

\lipsum[1]

\bigskip\hrule\smallskip
A call-out to table \ref{tab:landscape}
\smallskip\hrule\bigskip

\afterpage{%  % note that the argument of \afterpage contains only a 'table'
\begin{landscape}
\begin{table}[htb]
\caption{My landscape table} \label{tab:landscape}
\begin{tabular*}{\linewidth}{|@{\extracolsep{\fill}}*{8}{c|}}
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8   \\
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8   \\
\hline
\end{tabular*}
\end{table}
\end{landscape}
}  % end of scope of \afterpage directive

\lipsum[2-3]


With this setup, the section numbering will once more be correct, i.e., consecutive.

• Your solution works, but I remark that its afterpage that does not like sectioning commands. I agree that restricting the use of afterpage only for figures or pictures is reasonable, provided that I get the pages full, when the landscape environment is called within \afterpage{ ... }. – djnavas Jun 28 '18 at 7:32
• @djnavas - Placing sectioning commands inside the scope of \afterpage is, basically, asking for trouble. It goes against the basic purpose of the afterpage package. – Mico Jun 28 '18 at 8:19