目录标题后的水平线

目录标题后的水平线

当我有 2 页或更多页的目录时,目录标题移动到目录最后一页后出现水平线。是否有任何选项可以强制水平线仅出现在目录的第一页上?当我只有 1 页目录时,输出正常!

我的 MWE:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[hidelinks]{hyperref}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}

\begin{document}
    \tableofcontents
    \begin{tikzpicture}[overlay,remember picture]
    \draw [line width=1pt]
    ($ (current page.north west) + (3cm,-5.5cm) $) -- ($ (current page.north east) + (-1.5cm,-5.5cm) $);
    \end{tikzpicture}
    \clearpage

    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title} 
\end{document}

在此处输入图片描述

使用页眉/页脚编辑:

%%begin preamble
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[hidelinks]{hyperref}
\usepackage{array}
\usepackage{lastpage}
\usepackage{xcolor}
\usepackage{fancyhdr}
\usepackage{tikz}
\usepackage{background}
\usepackage{url}
%\usepackage{showframe}
\usepackage{lipsum}
\usepackage{makecell}
\usepackage[export]{adjustbox}
\usepackage{graphbox}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{bigstrut}
\usepackage{changepage}
\usepackage{titlesec}
\usepackage{etoolbox}
\usepackage{float}
\usepackage{amsmath}
\usepackage{tabto}
\usepackage{textcomp}
%\usepackage{tocloft}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}

%%begin ToC, LoF & LoT renaming 
\renewcommand{\contentsname}{\hfill \textit{Table of Contents}}     %%Table of contents name change
\renewcommand{\listfigurename}{\hfill \textit{List of Figures}}     %%List of figures name change
\renewcommand{\listtablename}{\hfill \textit{List of Tables}}       %%List of tables name change
%%end ToC, LoF & LoT renaming 

%%begin upper case in section and toc
\ifdefined\texorpdfstring\else\newcommand{\texorpdfstring}[2]{#1}\fi
\let\oldsection\section

\makeatletter
\renewcommand{\section}{\@ifstar{\oldsection*}{\@Section}}
\newcommand{\@sSection}[2][]{%
    \oldsection*{\texorpdfstring{\MakeUppercase{#2}}{#2}}}
\newcommand{\@Section}[2][]{%
    \def\shortsec{#1}%
    \ifx\shortsec\empty%
    \oldsection{\texorpdfstring{\MakeUppercase{#2}}{#2}}
    \else%
    \oldsection[\texorpdfstring{\MakeUppercase{#1}}{#1}]{\texorpdfstring{\MakeUppercase{#2}}{#2}}
    \fi}
\makeatother
%%end upper case in section and toc

%%begin section/subsection/subsubsection modifying
\AtBeginEnvironment{adjustwidth}{%
    \titleformat{\section}{\Large\bfseries}{\parbox[t]{2cm}{\thesection}}{-0.5pt}{\parbox[t]{\dimexpr\textwidth-2cm}}
    \titleformat{\subsection}{\large\bfseries}{\parbox[t]{2cm}{\thesubsection}}{-0.5pt}{\parbox[t]{\dimexpr\textwidth-2cm}}
    \titleformat{\subsubsection}{\normalsize\bfseries}{\parbox[t]{2cm}{\thesubsubsection}}{-0.5pt}{\parbox[t]{\dimexpr\textwidth-2cm}}
    \titleformat{\subsubsubsection}{\normalsize\bfseries}{\parbox[t]{2cm}{\thesubsubsubsection}}{-0.5pt}{\parbox[t]{\dimexpr\textwidth-2cm}}
    \titleformat{\paragraph}{\normalsize\bfseries}{\parbox[t]{2cm}{\theparagraph}}{-0.5pt}{\parbox[t]{\dimexpr\textwidth-2cm}}
    \titleformat{\subparagraph}{\normalsize\bfseries}{\parbox[t]{2cm}{\thesubparagraph}}{-0.5pt}{\parbox[t]{\dimexpr\textwidth-2cm}}

    \titlespacing*{\section}{-2cm}{0.6cm}{0.6cm}
    \titlespacing*{\subsection}{-2cm}{0.4cm}{0.4cm}
    \titlespacing*{\subsubsection}{-2cm}{0.2cm}{0.2cm}
    \titlespacing*{\subsubsubsection}{-2cm}{0.2cm}{0.2cm}
    \titlespacing*{\paragraph}{-2cm}{0.2cm}{0.2cm}
    \titlespacing*{\subparagraph}{-2cm}{0.2cm}{0.2cm}
}
%%end section/subsection/subsubsection modifying

%%begin new command for generating table of contents, list of figures and list of tables
\newcommand\tocloflot{%
    \clearpage
    \tableofcontents
    \addtocontents{toc}{\string\hrule}
    \clearpage
    \csname phantomsection\endcsname
    \listoffigures
    \addtocontents{lof}{\string\hrule\par\bigskip}
    \addcontentsline{toc}{section}{List of Figures}
    \clearpage
    \csname phantomsection\endcsname
    \listoftables
    \addtocontents{lot}{\string\hrule\par\bigskip}
    \addcontentsline{toc}{section}{List of Tables}
    \clearpage
}

%\renewcommand{\cftaftertoctitle}{\\\rule{\linewidth}{1pt}\vspace*{ex}}
%\renewcommand{\cftafterloftitle}{\\\rule{\linewidth}{1pt}\vspace*{3ex}}
%\renewcommand{\cftafterloftitle}{\\\rule{\linewidth}{1pt}\vspace*{3ex}}

%%end new command for generating table of contents, list of figures and list of tables





\usepackage{graphicx}
\graphicspath{{./images/}}      %%images are kept in a folder named images under the directory of the main document

\usepackage{geometry}
\geometry{paper=a4paper}                                                    %%odabir veličine papira
\geometry{bindingoffset=1.5cm,hmargin={1.5cm,1.5cm},vmargin={1cm,1cm}}      %%podešavanje margina
\geometry{includehead=true,headheight=3cm,headsep=0.5cm}                        %%podešavanje headinga
\geometry{includefoot=false}                                                %%podešavanje footinga

\setlength{\parindent}{0cm}     %%paragraf uvodni razmak

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}  %%delete default header line

\newcommand{\thenextpage}{\number\numexpr\thepage+1\relax}      %%new command for next page reference

\renewcommand\cellset{\renewcommand{\arraystretch}{0.7}}        %%changing linespacing on a single row of a table

%%begin page borders
\SetBgOpacity{100.0}    %% Select opacity
\SetBgScale{1}          %% Select scale factor of logo
\SetBgAngle{0}          %% Select rotation of logo
\SetBgColor{black}      %% Select color
\SetBgContents{
    \begin{tikzpicture}[overlay,remember picture]
    \draw [line width=1pt,rounded corners=0pt]
    ($ (current page.north west) + (2.5cm,-1cm) $)
    rectangle
    ($ (current page.south east) + (-1cm,1cm) $);

    \draw [line width=1.5pt]
    ($ (current page.north west) + (2.5cm,-14.85cm) $) -- ($ (current page.north west) + (1cm,-14.85cm) $);

    \node[anchor=north] at ($(current page.north west)+(1.5cm,-1cm)$) {\rotatebox{90}{\makecell[r]{\scriptsize\selectfont Reproduction or any use not conformity with the intended\\
                \scriptsize\selectfont application is not permissible.}}};
    \node[anchor=south] at ($(current page.south west)+(1.5cm,1cm)$) {\rotatebox{90}{\makecell[l]{\scriptsize\selectfont Nachdruck oder Verwendung, die nicht der beabsichtigten\\
                \scriptsize\selectfont Anwendung entspricht, ist nicht zulässig.}}};

    \end{tikzpicture}
}
%%end page borders

%%begin firstpage footer
\fancypagestyle{firstpage}{%
    \fancyhead[CE,CO,LE,LO,RE,RO]{} %% clear out all headers
    \fancyfoot[CE,CO,LE,LO,RE,RO]{} %% clear out all footers
    \fancyhf{}                      %% clear default for head and foot
    \fancyfoot[C]{%
        \begin{tikzpicture}[remember picture,overlay]
        \node[anchor=south west,inner sep=0pt] at ($(current page.south west)+(2.5cm,1cm)$) {
            \begin{tabular}{
                @{}p{3cm}@{}
                @{}p{2.92cm}@{}|
                @{}>{\centering \scriptsize}p{2.3cm}@{}|
                @{}>{\centering \scriptsize}p{2.3cm}@{}|
                @{}>{\centering \scriptsize}p{2.3cm}@{}|
                @{}>{\centering \scriptsize}p{2.3cm}@{}|
                @{}p{1.5cm}@{}
                @{}p{0.8cm}@{}
            }
            \cline{3-8}
            & 
            & 
            & 
            & 
            & 
            & \multicolumn{2}{c}{hh}
            \bigstrut\\ \cline{3-8}
            & 
            & 
            & 
            & 
            & 
            & \multicolumn{2}{c}{hhh}
            \bigstrut\\ \cline{3-8}
            & 
            & 
            & 
            & 
            & 
            & \multicolumn{2}{c}{}
            \bigstrut\\ \cline{3-8}
            & 
            & 
            & 
            & 
            & 
            & \multicolumn{2}{c}{jjj}
            \bigstrut\\ \cline{3-8}
            & 
            & 
            & 
            & 
            & 
            & \multicolumn{2}{c}{jjj}
            \bigstrut\\ \hline
            \multirow{-2.3}{=}{jjj}
            & gg
            & \multicolumn{3}{l}{gg}
            \bigstrut\\ \hline
            \multirow{-3}{=}{g}
            & gg
            & \multicolumn{3}{l}{g}
            \bigstrut\\ \hline
            \multicolumn{2}{@{}l|}{g}
            & \raggedright g
            & \multicolumn{3}{@{}l|}{g}
            & g
            & g
            \bigstrut\\ \cline{7-8} 
            \multicolumn{2}{c|}{ggg}
            & \centering hhh
            & \multicolumn{3}{c|}{ggg}
            & gg
            & g
            \bigstrut\\ \hline
            \multicolumn{2}{@{}l|}{gg}
            & \raggedright gg
            & \multicolumn{3}{@{}l|}{gg}
            & gg:
            & gg
            \bigstrut\\ \cline{7-8} 
            \multicolumn{2}{c|}{ggg}
            & \centering ggg
            & \multicolumn{3}{c|}{gg}
            & gg
            & gg
            \bigstrut\\
            \end{tabular}%
        };
        \end{tikzpicture}
    }
}
%%end firstpage footer

%%begin default header for all pages; except first page style
\fancyhead[CE,CO,LE,LO,RE,RO]{} %% clear out all headers
\fancyfoot[CE,CO,LE,LO,RE,RO]{} %% clear out all footers
\fancyhf{}                      %% clear default for head and foot
\fancyhead[C]{%
    \begin{tikzpicture}[remember picture,overlay]
    \node[anchor=north west,inner sep=0pt] at ($(current page.north west)+(2.5cm,-1cm)$) {
        \begin{tabular}{
            @{}>{\centering}p{4cm}@{}|
            @{}p{5.95cm}@{}|
            @{}p{2.50cm}@{}|
            @{}p{2.50cm}@{}|
            @{}>{\scriptsize}p{1.50cm}@{}
            @{}>{\footnotesize}p{1.00cm}@{}
        }
        gg
        & hh
        & \multicolumn{2}{@{}l|}{hhh}
        & hh
        & hh
        \bigstrut\\ \cline{5-6}
        & \centering hh
        & \multicolumn{2}{c|}{hh}
        & hhhh
        & hh
        \bigstrut\\ \cline{2-6}
        & hh
        & hh
        & hh
        & hh
        & hh
        \bigstrut[b]\\ \cline{5-6}
        & \centering hh
        & \centering hh
        & \centering hh
        & hh
        & hh
        \bigstrut\\ \hline
        \end{tabular}%
    };
    \end{tikzpicture}
}

%%end default header for all pages; except first page style

%%end preamble
\begin{document}
    %%titlepage begin
    \sloppy     %% sprijecava da text ode van margine
    \thispagestyle{firstpage}       %%\fancypagestyle{firstpage}

    %%begin podešavanje margina, headinga i footinga za početnu stranicu
    \newgeometry{bindingoffset=1.5cm,hmargin={1.5cm,1.5cm},vmargin={1cm,1cm},
        includehead=false,
        includefoot=true,footskip=10cm
    }
    %%end podešavanje margina, headinga i footinga za početnu stranicu

    \vspace*{6cm}                   %%6cm vertical offset
    \begin{center}
        \Huge Some random text\\
        \large Some random text\\~\\
        \Huge Some random text\\
        \large Some random text
    \end{center}
    \vspace*{\fill}                 %%fill page with empty space
    \clearpage  
    %%titlepage end

    \restoregeometry                %%restore to default header for all pages; except first page style
    \tocloflot

    \begin{adjustwidth}{2cm}{}

        \section{Section}
            \lipsum
            \lipsum
            \lipsum
            \lipsum 
    \end{adjustwidth}
\end{document}

答案1

起初我试图将 tikzpicture 移到前面,但虽然它不占用任何空间(与 相同\hbox{}),但仍导致\tableofcontents向下移动一行,

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[hidelinks]{hyperref}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usepackage{everypage}
\usepackage{showframe}% debugging tool

\begin{document}
    \AddThispageHook{\begin{tikzpicture}[overlay,remember picture]
    \draw [line width=1pt]
    ($ (current page.north west) + (3cm,-5.5cm) $) -- ($ (current page.north east) + (-1.5cm,-5.5cm) $);
    \end{tikzpicture}}
    \tableofcontents
    \clearpage

    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title} 
\end{document}

如果您不想使用 tocloft,您可以使用\addtocontents进行格式化。

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[hidelinks]{hyperref}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usepackage{showframe}% debugging tool

\begin{document}
    \tableofcontents
    \clearpage
    %\addtocontents{toc}{\hrule height1pt}
    \addtocontents{toc}{\vskip-\ht\strutbox\makebox[\textwidth][c]{\rule{\dimexpr\textwidth+2cm}{1pt}}\par}

    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title}
    \section{title} 
\end{document}

答案2

既然一个简单的规则很容易获得,为什么要用大锤来获得它呢tocloft

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{tocloft}
\usepackage{multido}

\usepackage[hidelinks]{hyperref}
\renewcommand{\cftaftertoctitle}{\\\rule{\linewidth}{1pt}\vspace*{3ex}}
\begin{document}

 \tableofcontents
 \clearpage

\multido{\i = 1 + 1}{27}{\section{title} \lipsum[\i]}

\end{document} 

在此处输入图片描述

相关内容