格式化目录标题

格式化目录标题

例如,我希望目录标题使用粗体 Arial 字体并将颜色设为红色,同时我还想将此标题更改为例如“目录的另一个标题”。

Mico 给出的解决方案在以下代码中不起作用。需要做哪些更改?

% Sources :
%   1) http://tex.stackexchange.com/questions/35825/pretty-table-of-contents
%   2) http://tex.stackexchange.com/questions/35903/formatting-the-title-of-the-toc

\documentclass{book}
    \usepackage{xcolor}
    \usepackage{framed}
%   \usepackage{helvet}

    \definecolor{myred}{RGB}{160,0,0}
    \definecolor{myyellow}{RGB}{169,121,69}

    \renewenvironment{leftbar}{%
        \def\FrameCommand{{\color{myyellow}\vrule width 2pt depth 6pt} \hspace{10pt}}%
        \MakeFramed {\advance\hsize-\width \FrameRestore}%
    }%
    {\endMakeFramed}

    \makeatletter
        \def\@chapter[#1]#2{%
            \ifnum \c@secnumdepth >\m@ne
                \if@mainmatter
                    \refstepcounter{chapter}%
                    \typeout{\@chapapp\space\thechapter.}%
                    \addtocontents{toc}%
                    {%
                        {\protect\parbox{4.5em}{\hfill\Huge\color{myred}\bfseries\thepage}}%
                        \protect\hspace*{.5em}
                        \protect\parbox{\dimexpr\linewidth-4.3em\relax}{%
                            \protect\begin{leftbar}
                            {\scshape\small\chaptername~\thechapter}\\\sffamily#1%
                            \protect\end{leftbar}%
                        }
                    }%
                \else
                    \addcontentsline{toc}{chapter}{#1}%
                \fi
            \else
                \addcontentsline{toc}{chapter}{#1}%
            \fi
            \chaptermark{#1}%
            \addtocontents{lof}{\protect\addvspace{10\p@}}%
            \addtocontents{lot}{\protect\addvspace{10\p@}}%
            \if@twocolumn
                \@topnewpage[\@makechapterhead{#2}]%
            \else
                \@makechapterhead{#2}%
                \@afterheading
            \fi%
        }
    \makeatother

% << WARNING ! >>
%
% \renewcommand{\contentsname} does not work with babel.
% With babel we have to use 
%    \addto\captionsenglish{\def\contentsname{...}}
% or
%    \addto\extrasenglish{\def\contentsname{...}}.
    \renewcommand{\contentsname}{\sffamily \textcolor{\color{myred}}{Another title for the table of contents}}


\begin{document}

\tableofcontents

\chapter{Beginning to learn design with \LaTeX}

\chapter{Beginning to learn design with HTML}
\setcounter{page}{13}% just for the example

\chapter{Beginning to learn design with HTML and some other text to span more than one line in the ToC}

\chapter{Another chapter....}
\setcounter{page}{244}% just for the example

\end{document}

答案1

您可以在不使用任何特殊包的情况下实现所需的功能(字体更改除外)。您可以简单地重新定义,\tableofcontents如 中所述book.cls。在下面的示例代码中,我展示了此重新定义的样子;请随意添加其他功能,例如标有“ ”的行中的字体% change here the format

\documentclass{book}
    \usepackage{xcolor}
    \usepackage{framed}

    \definecolor{myred}{RGB}{160,0,0}
    \definecolor{myyellow}{RGB}{169,121,69}

    \renewenvironment{leftbar}{%
        \def\FrameCommand{{\color{myyellow}\vrule width 2pt depth 6pt} \hspace{10pt}}%
        \MakeFramed {\advance\hsize-\width \FrameRestore}%
    }%
    {\endMakeFramed}

    \makeatletter
        \def\@chapter[#1]#2{%
            \ifnum \c@secnumdepth >\m@ne
                \if@mainmatter
                    \refstepcounter{chapter}%
                    \typeout{\@chapapp\space\thechapter.}%
                    \addtocontents{toc}%
                    {%
                        {\protect\parbox{4.5em}{\hfill\Huge\color{myred}\bfseries\thepage}}%
                        \protect\hspace*{.5em}%
                        \protect\parbox{\dimexpr\linewidth-5em\relax}{%
                            \protect\begin{leftbar}
                            {\scshape\small\chaptername~\thechapter}\\\sffamily#1%
                            \protect\end{leftbar}%
                        }\par\noindent%
                    }%
                \else
                    \addcontentsline{toc}{chapter}{#1}%
                \fi
            \else
                \addcontentsline{toc}{chapter}{#1}%
            \fi
            \chaptermark{#1}%
            \addtocontents{lof}{\protect\addvspace{10\p@}}%
            \addtocontents{lot}{\protect\addvspace{10\p@}}%
            \if@twocolumn
                \@topnewpage[\@makechapterhead{#2}]%
            \else
                \@makechapterhead{#2}%
                \@afterheading
            \fi%
        }
\AtBeginDocument{\renewcommand\contentsname{Another title for the Table of Contents}}
\renewcommand\tableofcontents{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \chapter*{{\color{myred}\contentsname}% change here the formatting
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \@starttoc{toc}%
    \if@restonecol\twocolumn\fi
    }
    \makeatother

% << WARNING ! >>
%
% \renewcommand{\contentsname} does not work with babel.
% With babel we have to use 
%    \addto\captionsenglish{\def\contentsname{...}}
% or
%    \addto\extrasenglish{\def\contentsname{...}}.


\begin{document}

\tableofcontents

\chapter{Beginning to learn design with \LaTeX}

\chapter{Beginning to learn design with HTML}
\setcounter{page}{13}% just for the example

\chapter{Beginning to learn design with HTML and some other text to span more than one line in the ToC}

\chapter{Another chapter....}
\setcounter{page}{244}% just for the example

\end{document}

我还纠正了代码中的一些虚假空格和一些长度\parbox

在此处输入图片描述

答案2

评论:我在下面写了答案原帖作者提供了更多关于他/她需要什么的信息。与此同时,Gonzalo Medina 提供了更广泛(更好!)的答案,解决了需要考虑的长长目标清单。:-)

我认为没有必要使用该titlesec软件包;但是,根据您的描述,您将需要(x)colorhelvet软件包(假设您愿意使用海尔维提卡代替宋体...)——不要忘记(至少)运行代码两次:

\documentclass{article}
\usepackage{xcolor}
\usepackage{helvet}
\renewcommand{\contentsname}{\sffamily\textcolor{red}{Another title for the table of contents}}
\begin{document}
\tableofcontents
\section{Header of first and only section}
\end{document}

在此处输入图片描述

答案3

如果你正在使用KOMA 脚本类并且不仅想要目录的标题而且想要红色字体的所有标题,您可以使用:

\usepackage[T1]{fontenc}% uarial is not available in OT1
\usepackage{uarial}% see http://ctan.org/pkg/urw-arial
\usepackage{xcolor}% see http://ctan.org/pkg/xcolor
\addtokomafont{disposition}{\color{red}}

\renewcaptionname在这种情况下,可以使用以下方法更改文本:

\AtBeginDocument{%
  \renewcaptionname{english}\contentsname{Another title for the table of contents}%
}

如果只有章节标题应为红色,部分标题应为蓝色,而所有其他标题仍应为黑色,则可以使用

\usepackage[T1]{fontenc}% uarial is not available in OT1
\usepackage{uarial}% see http://ctan.org/pkg/urw-arial
\usepackage{xcolor}% see http://ctan.org/pkg/xcolor
\addtokomafont{chapter}{\color{red}}
\addtokomafont{section}{\color{blue}}

如果只有目录应该是红色的,你可以使用

\BeforeTOCHead[toc]{%
  \addtokomafont{chapter}{\color{red}}% with scrartcl replace "chapter" by "section"
}

以下是使用 scrartcl 的 MWE:

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{uarial}% see http://ctan.org/pkg/urw-arial
\usepackage{xcolor}% see http://ctan.org/pkg/xcolor
\BeforeTOCHead[toc]{%
  \addtokomafont{section}{\color{red}}% with scrartcl replace "chapter" by "section"
}
\AtBeginDocument{%
  \renewcaptionname{english}\contentsname{Another title for the table of contents}%
}

\usepackage{blindtext}% for demonstration only

\begin{document}
\tableofcontents
\blinddocument
\end{document}

KOMA-Script 示例第一页结果的上半部分

相关内容