我的章节使用罗马数字。但当它们显示在目录中时,章节标签会超出左边距。我尝试使用命令修复它dottedcontents
,但超链接会从章节标签中消失。
为了说明目的,我将超链接颜色设置为蓝色。
我的 MWE:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{titlesec}
\usepackage[rightlabels]{titletoc}
%\dottedcontents{section}[1.5em]{}{1.5em}{2em}
\renewcommand{\thesection}{\Roman{section}.}
\titleformat{\section}{\sffamily\bfseries\Large\color{red}}{\Roman{section}.}{0.15cm}{}{}
\renewcommand{\thesubsection}{\Alph{subsection}.}
\titleformat{\subsection}{\sffamily\bfseries\large\color{red}}{\Roman{section}.\Alph{subsection}.}{0.10cm}{}{}
\usepackage[colorlinks, linkcolor=blue, unicode]{hyperref}
\begin{document}
\tableofcontents
\section{First part}
\subsection{First subsection}
\subsection{Second subsection}
\subsection{Third subsection}
\section{Second part}
\subsection{First subsection}
\subsection{Second subsection}
\subsection{Third subsection}
\section{Third part}
\subsection{First subsection}
\subsection{Second subsection}
\subsection{Third subsection}
\end{document}
当我取消注释时\dottedcontents{section}[1.5em]{}{1.5em}{2em}
,它给了我这个:
标签不再超出左边距但这些点很奇怪,没有格式,更重要的是,部分标签上没有超链接。我应该怎么做?
编辑
\dottedcontents{section}[1.5em]{}{1.5em}{2em}
用替换\titlecontents{section}[2.2em]{\addvspace{10pt}\bfseries}{\contentslabel{1.75em}}{\hspace{-1.75em}}{\hfill\contentspage}
可以得到更令人满意的结果,但部分标签的超链接仍然损坏......
答案1
您可以使用包tocbasic
代替titletoc
:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{titlesec}
\usepackage{tocbasic}
\DeclareTOCStyleEntries[numwidth=2em]{tocline}{section,subsection}
\makeatletter
\renewcommand*{\numberline@prefix}{\hfill} % emulation of titletoc's
\renewcommand*{\numberline@postfix}{\enskip} % rightlabel
\makeatother
\renewcommand{\thesection}{\Roman{section}.}
\titleformat{\section}{\sffamily\bfseries\Large\color{red}}{\Roman{section}.}{0.15cm}{}{}
\renewcommand{\thesubsection}{\Alph{subsection}.}
\titleformat{\subsection}{\sffamily\bfseries\large\color{red}}{\Roman{section}.\Alph{subsection}.}{0.10cm}{}{}
\usepackage[colorlinks, linkcolor=blue, unicode]{hyperref}
\begin{document}
\tableofcontents
\section{First part}
\subsection{First subsection}
\subsection{Second subsection}
\subsection{Third subsection}
\section{Second part}
\subsection{First subsection}
\subsection{Second subsection}
\subsection{Third subsection}
\section{Third part}
\subsection{First subsection}
\subsection{Second subsection}
\subsection{Third subsection}
\end{document}
或者不使用内部命令,也许对小节条目进行更好的缩进:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{titlesec}
\usepackage{tocbasic}
\DeclareTOCStyleEntry[numwidth=2em,entrynumberformat=\labelright]{tocline}{section}
\newcommand*{\labelright}[1]{\hspace*{0pt plus 1filll}#1\ }
\DeclareTOCStyleEntry[indent=2em,numwidth=1.5em]{tocline}{subsection}
\renewcommand{\thesection}{\Roman{section}.}
\titleformat{\section}{\sffamily\bfseries\Large\color{red}}{\Roman{section}.}{0.15cm}{}{}
\renewcommand{\thesubsection}{\Alph{subsection}.}
\titleformat{\subsection}{\sffamily\bfseries\large\color{red}}{\Roman{section}.\Alph{subsection}.}{0.10cm}{}{}
\usepackage[colorlinks, linkcolor=blue, unicode]{hyperref}
\begin{document}
\tableofcontents
\section{First part}
\subsection{First subsection}
\subsection{Second subsection}
\subsection{Third subsection}
\section{Second part}
\subsection{First subsection}
\subsection{Second subsection}
\subsection{Third subsection}
\section{Third part}
\subsection{First subsection}
\subsection{Second subsection}
\subsection{Third subsection}
\end{document}