# Wrong section numbers when using background package

I am using background package to add current section number to the background of each page. But it seems the section number is wrong. The MWE is as follows:

\documentclass[12pt]{article}
\usepackage{lipsum}
\usepackage{background}
\SetBgContents{\arabic{section}}
\SetBgAngle{0}
\begin{document}
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\end{document}


The section numbers in the first and second pages should be 3 and 6 respectively, but it shows 4 and 7. What is the problems with it?

• Which alternative do you want? E.g., do you want the background number to correspond to the section number that's correct at the start of the page? – Mico Jul 11 '14 at 5:29
• @Mico Sorry, I have edited my question to clarify it. In fact, I want the section number that's at the end of the page. – Z.H. Jul 11 '14 at 5:34

The problem here deals with the output routine and the fact that it doesn't coincide with the gathered text at the point of page shipout (it's asynchronous). One is typically grabbing information too late, since TeX has already gather text that will form part of the following page (stepping counters and such as it's gobbling the input stream). One way around this is to tap into LaTeX's mark mechanism...

The following quote is taken from the fancyhdr documentation (section 10 Dictionary style headers):

Dictionaries and concordances usually have a header containing the first word defined on the page or both the first and the last words. This can easily be accomplished with fancyhdr and LaTeX's mark mechanism. Of course if you use the marks for dictionary style headers, you cannot use them for chapter and section information, so if there are also chapters and sections present, you must redefine the \chaptermark and \sectionmark to make them harmless:

\renewcommand{\chaptermark}[1]{}
\renewcommand{\sectionmark}[1]{}


Now you do a \markboth{#1}{#1} for each dictionary or concordance entry #1 and use \rightmark for the first entry defined on the page and \leftmark for the last one.

Using a similar approach, I've redefined \sectionmark to write \markboth{\thesection}{\thesection}. Now \rightmark will be the first section number, and \leftmark will be the last section number:

\documentclass[12pt]{article}
\usepackage{lipsum,background}
\renewcommand{\sectionmark}[1]{\markboth{\thesection}{\thesection}}
\SetBgContents{\leftmark}
\SetBgAngle{0}
\begin{document}
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\end{document}


If you want to use the headers for your regular \leftmark-\rightmark setting then, of course, redefining \sectionmark would not be as helpful. One could switch to using titleps which provides "switches" to use a number of additional marks. Here's an example that defines a page style main which sets the header to contain the section number & name, yet still refers to the last section in the background using the \bottitlemarks switch:

\documentclass[12pt,twoside]{article}
\usepackage{lipsum,background,titleps}
\SetBgContents{\bottitlemarks\thesection}
\SetBgAngle{0}
\newpagestyle{main}{
{\thepage}{}{\thesection~\sectiontitle}% Even
}
\pagestyle{main}
\begin{document}
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\end{document}


If you're accustomed to fancyhdr, consider reading titleps for fancyhdr users.

• @Z.H.: See the updated answer. I suggested using titleps which provides an interface to make use of other/more marks. – Werner Jul 11 '14 at 14:34

Inspired by Werner's answer, I have the following answer which directly uses \marks command in etex, for resolving the conflict between header sections and background sections.

\documentclass[12pt]{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\usepackage{etex,background,lipsum}
\newmarks\mymark
\renewcommand{\sectionmark}[1]{\marks\mymark{\arabic{section}}}
\SetBgContents{\botmarks\mymark}
\SetBgAngle{0}
\begin{document}
\chapter{Some Name}
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\section{Name}
\lipsum[6]
\section{Name}
\lipsum[8]
\section{Name}
\lipsum[4]
\end{document}