8

This is a follow-up question to How to print section titles like tab-list?.

I want to insert section titles somewhere in the background in every pages. (It could be chapter titles, well)

I've searched around but did'nt find anything other then the current section title.

MWE-1:

\documentclass{article}
\usepackage{lipsum}

\begin{document}

    \section{The first section}
    \lipsum[1-3]

    \section{The second section}
    \lipsum[1-3]

    \section{The third section}
    \lipsum[1-3]

    \section{The fourth section}
        Hi, I'm the 4th.
        The title of the 2nd section is... ?? Huh??

    \section{The fifth section}
    \lipsum[1-3]

    \section{The sixth section}
    \lipsum[1-3]

    \section{The seventh section}
    \lipsum[1-3]

\end{document}

MWE-2:

In practice, I need to typeset actual section titles in the following tab buttons:

\documentclass{article}
\usepackage[a5paper,vmargin=2cm]{geometry}
\usepackage{background}
\usepackage{etoolbox}
\usepackage{totcount}
\usepackage{lipsum}

% to have access to the total number of sections
\regtotcounter{section}

% every section starts on a new page
\pretocmd{\section}{\clearpage}{}{}

% auxiliary lengths for the height of the frame and the width of each tab
\newlength\frameheight
\newlength\avgtabwidth

% style for the section tabs
\tikzset{tab/.style={
    text width=\avgtabwidth,anchor=south,
    draw=gray,thick,rectangle,rounded corners=12pt,rotate=270,
    align=center,text height=20pt,
    text depth=25pt,inner sep=0pt,fill=gray!20,
    yshift=-18pt,font=\sffamily\LARGE}}
% style for the current section tab
\tikzset{selectedtab/.style={tab,color=white,fill=gray!90}}

% the page number is showed in the background material
\pagestyle{empty}

\AtBeginDocument{
    % calculation of the width for each tab
    \setlength\frameheight{\dimexpr\textheight+2cm\relax}
    \ifnum\totvalue{section}>0
        \setlength\avgtabwidth{\dimexpr\frameheight/\totvalue{section}\relax}
    \fi

    % the main part; as background material we place the border, 
    % the section (current and other) tabs and the page number 
    \backgroundsetup{scale=1, color=black, angle=0, opacity=1, contents={
        \begin{tikzpicture}[remember picture, overlay]
            \node[inner sep=0pt, text width=\the\dimexpr\textwidth+1.5cm] 
                at (current page.center) (border) 
                {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
            \foreach \valsection in {0,...,\numexpr\totvalue{section}-1\relax} {
                \node[\ifnum\thesection<\numexpr\valsection+1 tab\relax
                        \else \ifnum\thesection>\numexpr\valsection+1 tab\relax
                        \else selectedtab\fi\fi, 
                      xshift=(0.5+\valsection)*\avgtabwidth]
                    at (border.north east) (tab-\valsection)
                    {Section \the\numexpr\valsection+1\relax};
            }
            \node[draw=gray, line width=2pt, rectangle, rounded corners=10pt,
                  inner sep=0pt, text width=\the\dimexpr\textwidth+1.5cm\relax, fill=white] 
                at (current page.center) {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
            \node[font=\LARGE\sffamily,fill=white] 
                at (border.south) {\makebox[3em][c]{\thepage}};
        \end{tikzpicture}
    }}
}


\begin{document}

\section{Test Section One}
\lipsum[1-3]
\section{Test Section Two}
\lipsum[1-3]
\section{Test Section Three}
\lipsum[1-3]
\section{Test Section Four}
\lipsum[2]

\end{document}
5
  • By "in the background" do you mean section headings (normally in the header, sometimes in footer --- \pagestyle{headings}) or a way to retrieve the section title in cross-referencing (like with the nameref)?
    – henrique
    Oct 10, 2012 at 3:39
  • You can reference the section heading using nameref. See Get the title instead of number of a referenced chapter? Is that what you're after? If so, this question would be considered a duplicate.
    – Werner
    Oct 10, 2012 at 3:43
  • @henrique: I'm using the background package. Please see the example in my previous question tex.stackexchange.com/questions/75637/….
    – Lenik
    Oct 10, 2012 at 4:22
  • @Werner: I can't use \label, the section number is auto calculated, (e.g., in a \for loop) It's just like to implement a simple table of contents, or navigator bar myself. But \nameref could work if sections and chapters are automatically labelled. (what's the auto ref name? Maybe sec:1 or chap:2? can I \for\chapindex... \nameref{chap:\chapindex}?)
    – Lenik
    Oct 10, 2012 at 4:24
  • @Werner: Now we have 2 MWEs.
    – Lenik
    Oct 10, 2012 at 5:02

1 Answer 1

8

Here is a slightly modified version of your MWE that shows how to insert the names of sections in an automated fashion:

enter image description here

\documentclass{article}
\usepackage[a5paper,vmargin=2cm]{geometry}  % http://ctan.org/pkg/geometry
\usepackage{background}                     % http://ctan.org/pkg/background
\usepackage{etoolbox}                       % http://ctan.org/pkg/etoolbox
\usepackage{totcount}                       % http://ctan.org/pkg/totcount
\usepackage{lipsum}                         % http://ctan.org/pkg/lipsum
\usepackage{nameref}                        % http://ctan.org/pkg/nameref

% Automatically insert `\label` with every `\section`
\let\oldsection\section
\renewcommand{\section}[2][\relax]{%
    \ifx#1\relax
      \oldsection{#2}%
    \else
      \oldsection[#1]{#2}%
    \fi%
    \label{sec:\thesection}%
}

% to have access to the total number of sections
\regtotcounter{section}

% every section starts on a new page
\pretocmd{\section}{\clearpage}{}{}

% auxiliary lengths for the height of the frame and the width of each tab
\newlength\frameheight
\newlength\avgtabwidth

% style for the section tabs
\tikzset{tab/.style={
    text width=\avgtabwidth,anchor=south,
    draw=gray,thick,rectangle,rounded corners=12pt,rotate=270,
    align=center,text height=20pt,
    text depth=25pt,inner sep=0pt,fill=gray!20,
    yshift=-18pt,font=\sffamily\LARGE}}
% style for the current section tab
\tikzset{selectedtab/.style={tab,color=white,fill=gray!90}}

% the page number is showed in the background material
\pagestyle{empty}

\AtBeginDocument{
    % calculation of the width for each tab
    \setlength\frameheight{\dimexpr\textheight+2cm\relax}
    \ifnum\totvalue{section}>0
        \setlength\avgtabwidth{\dimexpr\frameheight/\totvalue{section}\relax}
    \fi

    % the main part; as background material we place the border, 
    % the section (current and other) tabs and the page number 
    \backgroundsetup{scale=1, color=black, angle=0, opacity=1, contents={
        \begin{tikzpicture}[remember picture, overlay]
            \node[inner sep=0pt, text width=\the\dimexpr\textwidth+1.5cm] 
                at (current page.center) (border) 
                {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
            \foreach \valsection in {0,...,\numexpr\totvalue{section}-1\relax} {
                \node[\ifnum\thesection<\numexpr\valsection+1 tab\relax
                        \else \ifnum\thesection>\numexpr\valsection+1 tab\relax
                        \else selectedtab\fi\fi, 
                      xshift=(0.5+\valsection)*\avgtabwidth]
                    at (border.north east) (tab-\valsection)
                    {\nameref{sec:\the\numexpr\valsection+1\relax}};
            }
            \node[draw=gray, line width=2pt, rectangle, rounded corners=10pt,
                  inner sep=0pt, text width=\the\dimexpr\textwidth+1.5cm\relax, fill=white] 
                at (current page.center) {\rule{0pt}{\dimexpr\textheight+2cm\relax}};
            \node[font=\LARGE\sffamily,fill=white] 
                at (border.south) {\makebox[3em][c]{\thepage}};
        \end{tikzpicture}
    }}
}


\begin{document}

\section[First]{Test Section One}
\lipsum[1-3]
\section[Second]{Test Section Two}
\lipsum[1-3]
\section[Third]{Test Section Three}
\lipsum[1-3]
\section[Fourth]{Test Section Four}
\lipsum[2]

\end{document}

The changes include:

  1. Modified \section to automatically insert a label of the form sec:\thesection; and
  2. Retrieve the name of the section labelled sec:<num> using nameref.

The \section modification is, of course, sub-optimal, since it doesn't allow for starred * variants. However, this may not be needed in your implementation, since you list the section titles in the form of tabs anyway. Regardless, this can be improved/changed, if needed.

1
  • Great! I've just reformatted the MWE source, so I edited yours to make "diff" output clean. And I made a small change on the \section to get a short title name.
    – Lenik
    Oct 10, 2012 at 5:21

You must log in to answer this question.

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