Форматирование ненумерованных глав в оглавлении

У меня есть документ с нумерованными и ненумерованными главами. Чтобы отличить их друг от друга в оглавлении, я бы хотел, чтобы ненумерованные главы были выделены курсивом. Мой MWE работает с заголовком главы - как я могу отформатировать соответствующий номер страницы курсивом?

Кроме того, возможно ли центрировать запись части 1?

\documentclass[a4paper, 12pt]{report}

\usepackage[titles]{tocloft}

\begin{document}
\tableofcontents

\part{Part 1}

\chapter{Numbered chapter}

\chapter*{Unnumbered chapter}
\addcontentsline{toc}{chapter}{\textit{Unnumbered chapter}}

\end{document}

person user2568648    schedule 03.01.2017    source источник


Ответы (1)


Вы можете написать то, что естественно сделано \addcontentsline вручную, используя \addtocontents{toc}:

введите описание изображения здесь

\documentclass{report}

\usepackage[titles]{tocloft}

\begin{document}

\tableofcontents

\chapter{Numbered chapter}

\chapter*{Unnumbered chapter}
\addtocontents{toc}
  {\protect\contentsline{chapter}{\textit{Unnumbered chapter}}{\textit{\thepage}}}

\end{document}

Вышеупомянутое должно работать для \chapter, поскольку они обычно устанавливаются на новой странице, и поэтому \thepage приведет к правильному значению. Однако это не работает с hyperref.

В качестве альтернативы определите новый тип записи ToC с именем chapterstar:

\documentclass{report}

\usepackage[titles]{tocloft}
\usepackage{etoolbox}

\makeatletter
\let\l@chapterstar\l@chapter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\l@chapterstar}{\cftchapfont}{\cftchapstarfont}{}{}% Insert starred chapter font
\patchcmd{\l@chapterstar}{#2}{\cftchapstarpagefont #2}{}{}% Insert starred chapter page number font
\makeatother

\newcommand{\cftchapstarfont}{\cftchapfont\itshape}
\newcommand{\cftchapstarpagefont}{\cftchappagefont\itshape}

\begin{document}

\tableofcontents

\chapter{Numbered chapter}

\chapter*{Unnumbered chapter}
\addcontentsline{toc}{chapterstar}{Unnumbered chapter}

\end{document}

Приведенное выше решение работает с hyperref и является более общим.

person Werner    schedule 04.01.2017