3

I have a question on my MWE, well rather WE, because minimal isn't really applicable to it anymore...

Let me first tell you what my expected output is. So for a twoside document, I need the first pages of the document (title page, abstract, table of contents, etc.) to be single sided which means that the content should be printed to the right page (verso) and then followed by an empty page (recto). For the end of the document, I need the references to start on the verso. The same applies to every section of the appendix.

I was able to get all this working by defining some custom commands like \generateblanks and redefining \cleardoublepage. The problem I still have is, that - logically - the blank pages are counted. Because of several reasons I want the page output to only consider the pages with content. I could simply add \addtocounter{page}{-1} to the respective commands that generate the blank pages (\generateblanks and \cleardoublepage) but as this modifies the actual page counter, TeX will get confused when parsing the document. This will lead to a malfunction of the \cleardoublepage.

The idea would be to only modify the page number that is output. While researching I found this thread on TeX.SE. Here, David proposed to define a blankpages counter which is increased when a blank page is inserted. Then, redefining the \thepage command to subtract the value of blankpages from the page counter, would lead to the printing of the desired page number, while still keeping track of the "real" page number.

Unfortunately, this does not work in my case. TeX is highly unimpressed by these commands and simply appears to ignore them. I suspect that this is caused by the use of the \pagenumbering{} command which seems to redefine \thepage too.

What would be your approach to this issue? Would it be possible to define a second page counter that is increased every time the actual page counter is increased? This would allow me to mess around with that second counter, without disturbing TeX.


(M)WE

\documentclass[twoside]{article}

% %%%%%%%%
% Packages
% %%%%%%%%

\usepackage{lipsum}
\usepackage{appendix}
\usepackage{fancyhdr}
\usepackage{xparse}
\usepackage{atbegshi}
\usepackage{geometry}
\usepackage[backend = biber]{biblatex}



% %%%%%%%%%%%%%%%%%%%%%%%%%%
% Set internal two-side Bool
% %%%%%%%%%%%%%%%%%%%%%%%%%%

\newbool{twosideDocument}
{\makeatletter
\if@twoside
    \global\setbool{twosideDocument}{true}
\else
    \global\setbool{twosideDocument}{false}
\fi
\makeatother}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Accentuated geometry to easily
% distinguish between left and right
% (even / odd) pages.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\geometry{%
    a4paper,%
    top = 3cm,%
    bottom = 3.5cm,%
    outer = 2.5cm,%
    nomarginpar,%
    showframe = false%
}

\ifbool{twosideDocument}{%
    \geometry{%
        inner = 4.5cm%
    }%
}{%
    \geometry{%
        inner = 2.5cm%
    }%
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%
% Define a custom command to
% generate blank pages
% %%%%%%%%%%%%%%%%%%%%%%%%%%

\newbool{nextblank}
\newcounter{blankpages}

\newcommand{\generateblanks}{%
    \ifbool{nextblank}%
    {%
        \thispagestyle{empty}%
        \setcounter{blankpages}{1}
        \mbox{}%
        \clearpage%
        \global\setbool{nextblank}{false}
    }%
    {%
        \thispagestyle{noheader}
        \global\setbool{nextblank}{true}
    }%
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Redefine the cleardoublepage command
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\makeatletter
\renewcommand*{\cleardoublepage}{%
    \clearpage%
    \ifbool{twosideDocument}{%
        \ifodd%
            \c@page%
        \else%
            \hbox{}%
            \thispagestyle{empty}%
            \setcounter{blankpages}{1}
            \hbox{}%
            \newpage%
            \if@twocolumn%
                \hbox{}%
                \newpage%
            \fi%
        \fi%
    }{}%
}
\makeatother



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define different page styles
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%

\fancypagestyle{noheader}{
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \ifbool{twosideDocument}{
        \fancyfoot[LE, RO]{\thepage}
    }{
        \fancyfoot[R]{\thepage}
    }
}

\fancypagestyle{general}{
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \ifbool{twosideDocument}{
        \fancyhead[LE,RO]{\itshape\nouppercase{\rightmark}}
        \fancyfoot[LE,RO]{\thepage}
    }{
        \fancyhead[R]{\itshape\nouppercase{\rightmark}}
        \fancyfoot[R]{\thepage}
    }
}

\fancypagestyle{appendix}{%
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \ifbool{twosideDocument}{
        \fancyhead[LE,RO]{\itshape\nouppercase{\leftmark}}
        \fancyfoot[LE,RO]{\thepage}
    }{
        \fancyhead[R]{\itshape\nouppercase{\leftmark}}
        \fancyfoot[R]{\thepage}
    }
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define some page numbering commands
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\pagenumberAlph}{%
    \newpage%
    \thispagestyle{empty}%
    \pagestyle{empty}%
    \pagenumbering{Alph}%
}

\newcommand{\pagenumberRoman}{%
    \newpage%
    \global\setbool{nextblank}{true}%
    \ifbool{twosideDocument}{%
        \AtBeginShipout{\generateblanks}%
    }{}%
    \thispagestyle{noheader}%
    \pagestyle{noheader}%
    \pagenumbering{roman}%
}

\newcommand{\pagenumberArabic}{%
    \newpage%
    \global\let\generateblanks\relax % Relax the blank pages function
    \thispagestyle{general}%
    \pagestyle{general}%
    \pagenumbering{arabic}%
    \setcounter{section}{0}%
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Redefine some commands to start them
% on the verso page
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\let\tableofcontentsCopy\tableofcontents
\renewcommand{\tableofcontents}{%
    \cleardoublepage%
    \tableofcontentsCopy%
    \clearpage%
}

\let\printbibliographyCopy\printbibliography
\renewcommand{\printbibliography}{%
    \cleardoublepage%
    \addcontentsline{toc}{section}{\refname}%
    \pagenumbering{Roman}%
    \printbibliographyCopy%
}

\AtBeginEnvironment{appendices}{%
    \cleardoublepage%
    \thispagestyle{appendix}%
    \pagestyle{appendix}%
    \let\oldSection\section%
    \renewcommand{\section}[1]{%
        \cleardoublepage%
        \oldSection{#1}%
    }%
}



% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Redefine the \thepage command in order 
% to output the desired page number
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand\thepage{\the\numexpr\value{page}-\value{blankpages}\relax}



% %%%%%%%%%%%%%%%
% Add a Reference
% %%%%%%%%%%%%%%%

\begin{filecontents}{references.bib}
    @misc{Test,
        author = {John Doe},
        title = {{This is a test title}},
        year = {2019},
        url = {https://example.com},
        urldate = {2019-05-21}
    }
\end{filecontents}
\addbibresource{references.bib}



% %%%%%%%%%%%%%%%%%
% Add document info
% %%%%%%%%%%%%%%%%%

\title{Test Document}
\author{John Doe}
\date{\today}



% %%%%%%%%%%%%%%%%%%
% Start the document
% %%%%%%%%%%%%%%%%%%

\begin{document}

    \pagenumberAlph
    \maketitle

    \pagenumberRoman
    \begin{abstract}
        \lipsum[1-2]
    \end{abstract}

    \tableofcontents

    \pagenumberArabic
    \section{Foo}
    \lipsum[1-4]\cite{Test}

    \section{Bar}
    \lipsum[5-9]

    \printbibliography

    \begin{appendices}
        \section{Foo}
        \lipsum[1]

        \section{Bar}
        \lipsum[2-10]
    \end{appendices}

\end{document}
  • 1
    well it is certainly possible, but imho you should take your convulated code as an indication that something is wrong with the numbering plan. As a reader I simply hate such creative page numbering. Page numbers should help me to find stuff, I don't want to read the manual first to understand them. Why not simply start with one and count through? – Ulrike Fischer May 24 at 21:56
  • @UlrikeFischer I don't think that this is complicated to understand. Basically, it's according to general rules. Using lowercase Roman letters for the introductory pages is very common. The same is true for the uppercase Roman numbering of the appendix. As far as the white pages are concerned, it's true that this seems unusual, but it's very funny with one-sided printed pages (by the way also standard), if the number sequence is 1, 3, 5 etc.. This looks like pages are missing. So, I would say that this is exactly what makes the document more readable. – Sam May 24 at 22:33
  • if you want to print oneside then use oneside and not twoside as class option and don't insert blank pages which are for twoside layout. But in twoside printing the back of pages count. And regarding the roman numbering: that was needed before the computers for the typesetting process to allow the insertion of table of contents etc, today it is useless; but if you want to keep it: this works fine with \pagenumbering. – Ulrike Fischer May 24 at 22:44
  • As I already have explained, the general document is and should be double-sided. Only the introductory pages and the appendix should be single-sided as it is also common in many books and publications. Also, a thesis should – at least at my university – be typeset like this. Regarding your comment on the roman page numbering, I would strongly disagree. It helps ta have a clean structure in the document. Everybody knows that the main document is numbered in roman letters, all the other necessary gibberish is clearly separated. – Sam May 24 at 23:03
  • But let's agree, that we have a different opinion on this topic, which is totally fine. It's a personal preference whether to use both Roman and Arabic numbers or just count straight through the document. The question of doing or not doing this belongs in another forum and should not be discussed here. Here the programmatic aspects that allow such a project in TeX would be of interest, regardless of whether and why one wants this. – Sam May 24 at 23:10
1

Here is a solution. The idea is to use a counter like abspage from zref package, module : abspage.

\xpatchcmd{\@outputpage}{\count\z@}{\c@abspage}{}{} with this margins and headers are correct and we can define a command \cleartoright correctly.

\newcommand*{\cleartoright}{%
    \clearpage%
    \ifodd\c@abspage\else
        \hbox{}
        \addtocounter{page}{-1}
        \thispagestyle{empty}
        \clearpage
    \fi}

Here is the complete code

\documentclass[twoside]{article}

\usepackage{appendix}
\usepackage{biblatex}
\usepackage{fancyhdr}
\usepackage{xpatch}
\usepackage[user,abspage]{zref}

\fancypagestyle{noheader}{
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \fancyfoot[R]{\thepage}
}

\fancypagestyle{general}{
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \fancyhead[LE,RO]{\itshape\nouppercase{\rightmark}}
    \fancyfoot[LE,RO]{\thepage}
}

\fancypagestyle{appendix}{%
    \fancyhf{} % Clean fields
    \renewcommand{\headrulewidth}{0.4pt}
    \renewcommand{\footrulewidth}{0.4pt}
    \fancyhead[LE,RO]{\itshape\nouppercase{\leftmark}}
    \fancyfoot[LE,RO]{\thepage}
}

\usepackage{mwe}

\makeatletter 
\newcommand*{\cleartoright}{%
    \clearpage%
    \ifodd\c@abspage\else
        \hbox{}
        \addtocounter{page}{-1}
        \thispagestyle{empty}
        \clearpage
    \fi}
\xpatchcmd{\@outputpage}{\count\z@}{\c@abspage}{}{}
\makeatother
\xapptocmd{\appendices}{\newpage\thispagestyle{appendix}\pagestyle{appendix}}{}{}
\xapptocmd{\endappendices}{\newpage}{}{}
\xpretocmd{\section}{\presection}{}{\err}
\newcommand*{\Romanpages}{%
\cleartoright
\pagenumbering{roman}
\thispagestyle{noheader}
\pagestyle{noheader}
\let\presection\cleartoright}
\newcommand*{\Arabicpages}{%
\clearpage % or something
\pagenumbering{arabic}
\thispagestyle{general}
\pagestyle{general}
\let\presection\empty}

\begin{filecontents}{references.bib}
    @misc{Test,
        author = {John Doe},
        title = {{This is a test title}},
        year = {2019},
        url = {https://example.com},
        urldate = {2019-05-21}
    }
\end{filecontents}
\addbibresource{references.bib}

\title{Test Document}
\author{John Doe}
\date{\today}

\begin{document}
\maketitle
%------------------------
\Romanpages
%------------------------
\begin{abstract}
\lipsum[1-2]
\end{abstract}

\tableofcontents
%------------------------
\Arabicpages
%------------------------
\blinddocument
\lipsum
\cite{Test}
\blinddocument

%------------------------
\Romanpages
%------------------------
\printbibliography

\begin{appendices}
\section{Foo}
\lipsum[1]

\section{Bar}
\lipsum[2-10]
\end{appendices}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.