当出现连续的短节,或章节之间没有文字变化时,就像这里一样,
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\lipsum[6]
\section{Section 4}
\lipsum[7]
\end{document}
我最终得到了如下目录:
我的问题是,是否可以告诉目录除非页码发生变化,否则不显示任何内容?为了得到类似的东西:
编辑:所有解决方案在各自的 MWE 上都运行良好,但在我的论文上却不行。我需要花一些时间才能在加载的数十个包中找到哪个包负责此问题。Heiko 的解决方案在我的论文中产生了最好的结果,但我在新的目录中遇到了以下问题:
标签恢复正常,而这在 MWE 上不会发生。
答案1
请参阅附录以了解hyperref
版本。
这是一个tocloft
解决方案。我修补了 的定义,以便将\cftXfillnum
其#1
替换为\clrize{#1}
。然后我定义\clrize
来检查是否与#1
全局定义的前一个页面条目匹配。如果是,则将其设为灰色,否则,将其保留为黑色并更新全局保存的页面条目。
编辑使用\xpatchcmd
而不是指定完整的\renewcommand
。
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{tocloft,xcolor,xpatch}
\xpatchcmd\cftchapfillnum{#1}{\clrize{#1}}{}{}
\xpatchcmd\cftsecfillnum{#1}{\clrize{#1}}{}{}
\def\svpage{0}
\def\clrize#1{\ifnum#1=\svpage\relax\textcolor{black!20}{#1}\else#1\gdef\svpage{#1}\fi}
\usepackage{lipsum}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\lipsum[6]
\section{Section 4}
\lipsum[7]
\end{document}
附录:
OP 询问了hyperref
兼容性。经过一番折腾,我发现宏#1
中\cftXfillnum
不是数字,而是hyperref
某种命令。因此,我必须解析该命令,以便提取页码作为#4
该新命令的参数。
\documentclass[10pt,a4paper]{report}
\usepackage[linktoc=all]{hyperref}
\usepackage[utf8]{inputenc}
\usepackage{tocloft,xcolor,xpatch}
\usepackage{lipsum}
\xpatchcmd\cftchapfillnum{#1}{\clrize#1\relax#1}{}{}
\xpatchcmd\cftsecfillnum{#1}{\clrize#1\relax#1}{}{}
\def\svpage{0}
\def\clrize#1#2#3#4#5\relax{\ifnum#4=\svpage\relax%
\color{black!30}\else\gdef\svpage{#4}\fi}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\lipsum[6]
\section{Section 4}
\lipsum[1-42]
\section{Section 5}
\lipsum[7]
\end{document}
答案2
LaTeX 用于格式化目录中的条目。第二个参数是页码。因此,可以将其定义为宏并在下次调用时进行比较。\l@<section>
以下示例也适用于hyperref
。
此外,只\l@...
应重新定义那些实际出现在目录中的命令。否则,出现在章节级别、未放入目录中的页码将不会出现在目录中同一页的下一个条目中。
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\makeatletter
\def\toc@page{}
% Chapters should have a page number.
% \l@chapter is redefined to remember the page number
\let\saved@l@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{%
\def\toc@page{#2}%
\saved@l@chapter{#1}{#2}%
}
\let\saved@l@section\l@section
\renewcommand*{\l@section}[2]{%
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\def\toc@page{#2}%
\expandafter\@secondoftwo
\fi
{%
\saved@l@section{#1}{}%
}{%
\saved@l@section{#1}{#2}%
}%
}
\let\saved@l@subsection\l@subsection
\renewcommand*{\l@subsection}[2]{%
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\saved@l@subsection{#1}{}%
}{%
\saved@l@subsection{#1}{#2}%
}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\subsection{Subsection A}
\lipsum[6]
\section{Section 4}
\lipsum[7]
\end{document}
带有灰色页码
将页码改为灰色,只需要进行小小的修改:
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{color}
\definecolor{tocpagegray}{gray}{.5}
\makeatletter
\def\toc@page{}
% Chapters should have a page number.
% \l@chapter is redefined to remember the page number
\let\saved@l@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{%
\def\toc@page{#2}%
\saved@l@chapter{#1}{#2}%
}
\let\saved@l@section\l@section
\renewcommand*{\l@section}[2]{%
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\def\toc@page{#2}%
\expandafter\@secondoftwo
\fi
{%
\saved@l@section{#1}{\textcolor{tocpagegray}{#2}}%
}{%
\saved@l@section{#1}{#2}%
}%
}
\let\saved@l@subsection\l@subsection
\renewcommand*{\l@subsection}[2]{%
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\saved@l@subsection{#1}{\textcolor{tocpagegray}{#2}}%
}{%
\saved@l@subsection{#1}{#2}%
}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\subsection{Subsection A}
\lipsum[6]
\section{Section 4}
\lipsum[7]
\end{document}
带有灰色页码和灰色圆点
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{color}
\definecolor{tocpagegray}{gray}{.5}
\makeatletter
\def\toc@page{}
% Chapters should have a page number.
% \l@chapter is redefined to remember the page number
\let\saved@l@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{%
\def\toc@page{#2}%
\saved@l@chapter{#1}{#2}%
}
\let\saved@l@section\l@section
\renewcommand*{\l@section}[2]{%
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\def\toc@page{#2}%
\expandafter\@secondoftwo
\fi
{%
\begingroup
\let\leaders\toc@leaders@gray
\saved@l@section{#1}{\textcolor{tocpagegray}{#2}}%
\endgroup
}{%
\saved@l@section{#1}{#2}%
}%
}
\let\saved@l@subsection\l@subsection
\renewcommand*{\l@subsection}[2]{%
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\begingroup
\let\leaders\toc@leaders@gray
\saved@l@subsection{#1}{\textcolor{tocpagegray}{#2}}%
\endgroup
}{%
\saved@l@subsection{#1}{#2}%
}%
}
\let\org@leaders\leaders
\newcommand*{\toc@leaders@gray}[2]{%
\org@leaders\hbox{\textcolor{tocpagegray}{#2}}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\subsection{Subsection A}
\lipsum[6]
\section{Section 4}
\lipsum[7]
\end{document}
删除不必要的点
如果省略页码,则指向不存在的页码的点不再需要。以下示例删除了它们。hyperref
这次加载了包。
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage[colorlinks]{hyperref}
\makeatletter
\def\toc@page{}
% Chapters should have a page number.
% \l@chapter is redefined to remember the page number
\let\saved@l@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{%
\def\toc@page{#2}%
\saved@l@chapter{#1}{#2}%
}
\let\saved@l@section\l@section
\renewcommand*{\l@section}[2]{%
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\def\toc@page{#2}%
\expandafter\@secondoftwo
\fi
{%
\begingroup
\let\leaders\@gobbletwo
\saved@l@section{#1}{}%
\endgroup
}{%
\saved@l@section{#1}{#2}%
}%
}
\let\saved@l@subsection\l@subsection
\renewcommand*{\l@subsection}[2]{%
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\begingroup
\let\leaders\@gobbletwo
\saved@l@subsection{#1}{}%
\endgroup
}{%
\saved@l@subsection{#1}{#2}%
}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\subsection{Subsection A}
\lipsum[6]
\section{Section 4}
\lipsum[7]
\end{document}
带有不必要的点并在新页面上带有页码
以下修改设置标签以检测目录条目所在的页面。然后检查页码是否已更改,这表示目录中有新页面。然后设置页码,即使页码已出现在上一页。
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage[colorlinks]{hyperref}
\usepackage{zref-abspage}
\makeatletter
\def\toc@page{}
\def\toc@abspage{}
\newcounter{toc@abspage}
\renewcommand*{\thetoc@abspage}{%
ta@\the\value{toc@abspage}%
}
\newcommand*{\toc@abspage@label}{%
\zref@labelbyprops{\thetoc@abspage}{abspage}%
}
\newcommand*{\toc@abspage@check}{%
\stepcounter{toc@abspage}%
\edef\toc@abs@page{\zref@extractdefault{\thetoc@abspage}{abspage}{}}%
\zref@refused{\thetoc@abspage}%
\ifx\toc@abs@page\toc@abspage
\else
\def\toc@page{}%
\let\toc@abspage\toc@abs@page
\fi
}
% Chapters should have a page number.
% \l@chapter is redefined to remember the page number
\let\saved@l@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{%
\toc@abspage@check
\def\toc@page{#2}%
\saved@l@chapter{#1}{#2\toc@abspage@label}%
}
\let\saved@l@section\l@section
\renewcommand*{\l@section}[2]{%
\toc@abspage@check
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\def\toc@page{#2}%
\expandafter\@secondoftwo
\fi
{%
\begingroup
\let\leaders\@gobbletwo
\saved@l@section{#1}{\toc@abspage@label}%
\endgroup
}{%
\saved@l@section{#1}{#2\toc@abspage@label}%
}%
}
\let\saved@l@subsection\l@subsection
\renewcommand*{\l@subsection}[2]{%
\toc@abspage@check
\def\toc@new@page{#2}%
\ifx\toc@new@page\toc@page
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\begingroup
\let\leaders\@gobbletwo
\saved@l@subsection{#1}{\toc@abspage@label}%
\endgroup
}{%
\saved@l@subsection{#1}{#2\toc@abspage@label}%
}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\addtocontents{toc}{\protect\newpage}
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\subsection{Subsection A}
\lipsum[6]
\section{Section 4}
\lipsum[7]
\end{document}
答案3
您可以尝试以下操作:
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\newcount\tocpgnum
\let\contentslineori=\contentsline
\def\contentsline#1#2#3{%
\ifnum\tocpgnum=#3
\contentslineori{#1}{#2}{}%
\else
\contentslineori{#1}{#2}{#3}%
\tocpgnum=#3
\fi
}
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\lipsum[6]
\section{Section 4}
\lipsum[7]
\end{document}
答案4
这里展示如何使用包来处理这个问题etoc
。
为了简单起见,我使用了 etoc 手册部分中的技术another compatibility mode
;否则,需要设置更完整的样式(对于灰色引导点,可以像这里一样复制\toc@leaders@gray
方法从Heiko 的回答)。手册中给出了设置etoc
样式的示例,例如Part I, section 6 A second example
。
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{hyperref}
\hypersetup{linktoc=all}
\usepackage{etoc}
\usepackage{color}
\definecolor{tocpagegray}{gray}{.5}
\makeatletter
\let\savedchapterline\l@chapter
\let\savedsectionline\l@section
\let\savedsubsectionline\l@subsection
\def\saved@previouspage {0}
\etocsetstyle{chapter}
{}
{\xdef\HcN@zz{\etocthepage}}
{\ifx\HcN@zz\saved@previouspage
\savedchapterline{\numberline{\etocnumber}\etocname}
{\textcolor{tocpagegray}{\etocpage}}%
\else
\savedchapterline{\numberline{\etocnumber}\etocname}
{\etocpage}%
\fi
\global\let\saved@previouspage\HcN@zz
}
{}%
\etocsetstyle{section}
{}
{\xdef\HcN@zz{\etocthepage}}
{\ifx\HcN@zz\saved@previouspage
\savedsectionline{\numberline{\etocnumber}\etocname}
{\textcolor{tocpagegray}{\etocpage}}%
\else
\savedsectionline{\numberline{\etocnumber}\etocname}
{\etocpage}%
\fi
\global\let\saved@previouspage\HcN@zz
}
{}%
\etocsetstyle{subsection}
{}
{\xdef\HcN@zz{\etocthepage}}
{\ifx\HcN@zz\saved@previouspage
\savedsubsectionline{\numberline{\etocnumber}\etocname}
{\textcolor{tocpagegray}{\etocpage}}%
\else
\savedsubsectionline{\numberline{\etocnumber}\etocname}
{\etocpage}%
\fi
\global\let\saved@previouspage\HcN@zz
}
{}
\makeatother
\begin{document}
\tableofcontents
\chapter{Chapter 1}
\lipsum[1]
\section{Section 1}
\lipsum[2]
\section{Section 2}
\lipsum[3-5]
\section{Section 3}
\subsection{Subsection A}
\lipsum[6]
\section{Section 4}
\lipsum[7]
\end{document}