目录编号不正确

目录编号不正确

我试图根据页码(在右侧的页脚中)获取目录,但奇怪的是,它从错误的开始计数。它显示的是第 3 页而不是第 2 页,第 4 页而不是第 3 页。我尝试过代码的位置,但似乎没有任何帮助。

这是我的代码:

\documentclass[11pt, A4]{article}
\usepackage[a4paper,top=2cm,hmargin=2cm,bottom=2.9cm,]{geometry}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage[utf8]{inputenc}
\usepackage{titletoc}
\usepackage[T1]{fontenc}
\usepackage{sectsty}
\usepackage{float}
\usepackage{atbegshi}
\makeatletter
\renewcommand{\@seccntformat}[1]{}
\makeatother
\renewcommand\numberline[1]{}
\setlength\headheight{48pt}
%\lhead{\includegraphics[width=5cm]{Afbeeldingen/logo.jpg}}
\rhead{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\contentsname}{Inhoudsopgave}
\chapterfont{\color[RGB]{227,24,24}}
\sectionfont{\color[RGB]{227,24,24}}
\subsectionfont{\color[RGB]{227,24,24}}
\definecolor{bcsred}{RGB}{227,24,24}
\newcommand\Footer{%
\begin{tikzpicture}[remember picture, overlay]
    \node[yshift=1.75cm] at (17, 24.5) {};
    \draw [line width=0.55mm] (-0.5,-23.5) -- (16.5,-23.5);
    \node[yshift=1.75cm, text=red] at (1.5, -26) {Test};
    \node[yshift=1.75cm, text=gray] at (15, -26) {2019-03-21} ;
    \node[fill=black, text=white, yshift=1.75cm] at (17, -25.5)             {\thepage};
    \fill[bcsred]
    (current page.south west) -- (current page.south east) --
    ([yshift=-30pt]current page.south east|-current page text area.south east) --
    ([yshift=-30pt]current page.south west|-current page text area.south west) -- cycle;
\end{tikzpicture}%
}
\pagestyle{fancy}
\cfoot{}
\AtBeginShipout{\Footer}
\AtBeginShipoutFirst{}
\begin{document}
\begin{titlepage}
\begin{center}
\line(1,0){300}\\ 
[0.25in]
\huge\bfseries Test \\ 
[2mm]
\line(1,0){300}\\ 
[1.5cm] 
\fontfamily{Arial}\selectfont
\textsc\LARGE Test\\ 
[1cm]
\textsc\Large Test \\ 
[12cm] 
\end{center} 
\end{titlepage}
\setcounter{page}{2}
%\newpage
\tableofcontents
\newpage
\section{Test}
\newpage
\section{Alerts}
\end{document}

答案1

问题中的代码在调用期间使用页码\AtBeginShipout。这很棘手,因为页面计数器在不同时间被不同的宏和钩子修改,这使得在页面输出期间很难始终获得正确的值。

作为替代方案,您可以使用在宏中显示和增加的自定义计数器\Footer,该计数器负责框中的页码,独立于实际页码计数器。“实际”页码计数器仍用于目录中的数字。

部分代码:

\newcounter{mypagenumber}
\setcounter{mypagenumber}{1}
\newcommand\Footer{%
\begin{tikzpicture}[remember picture, overlay]
    \node[yshift=1.75cm] at (17, 24.5) {};
    \draw [line width=0.55mm] (-0.5,-23.5) -- (16.5,-23.5);
    \node[yshift=1.75cm, text=red] at (1.5, -26) {Test};
    \node[yshift=1.75cm, text=gray] at (15, -26) {2019-03-21} ;
    \node[fill=black, text=white, yshift=1.75cm] at (17, -25.5) {\themypagenumber};
    \fill[bcsred]
    (current page.south west) -- (current page.south east) --
    ([yshift=-30pt]current page.south east|-current page text area.south east) --
    ([yshift=-30pt]current page.south west|-current page text area.south west) -- cycle;
\end{tikzpicture}%
\stepcounter{mypagenumber}
}

结果:

在此处输入图片描述

接下来的页面也显示正确的数字,与目录一致(即 2、3 等,未在屏幕截图中显示)。

请注意,页脚宏存在一个不同的问题,即在末尾添加一个只有页眉和页脚的空白页,但这很可能与页码计数器问题无关。

相关内容