如何通过章节编号获取章节标题?

如何通过章节编号获取章节标题?

这是如何打印像标签列表这样的部分标题?

我想在每一页的背景中插入章节标题。(也可以是章节标题)

我搜索了一下,除了当前章节标题之外没有找到任何其他内容。

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:

实际上,我需要在以下选项卡按钮中排版实际的部分标题:

\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}

答案1

这是 MWE 的稍微修改后的版本,它展示了如何以自动方式插入部分名称:

在此处输入图片描述

\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}

这些变化包括:

  1. 修改\section为自动插入表单的标签sec:\thesection;以及
  2. sec:<num>检索使用标签的部分的名称nameref

当然,这种\section修改并不是最理想的,因为它不允许使用星号*变体。但是,在您的实施中可能不需要这样做,因为您无论如何都会以标签的形式列出章节标题。无论如何,如果需要,可以改进/更改这一点。

相关内容