如何设置使用多列和 tocloft 的目录第二行无缩进

如何设置使用多列和 tocloft 的目录第二行无缩进

背景::大家好,我是这里的新手。我有一个目录,正在使用 \tableofcontents 在 Rnw 文件中生成。我正在使用 multicols 包将此目录格式化为两列。我还使用 tocloft 包格式化目录:除其他外,我已关闭 tocloft 左侧的自动章节编号 - 因此,目录由(理想情况下)左对齐的条目列表组成,每个条目都有一个右对齐的页码,全部分为两列。

困难:在较长的目录中,如果生成 2 行或更多行文本,则第 2 行及以上行会缩进,以便它们不与第 1 行的左边距对齐。例如:

两个目录条目的图像,第二个条目在第二行缩进

这个论坛上有关于如何删除目录中第 2 行及以上行的缩进以便它们与章节/页码左对齐的答案(关于目录,与我的不同,页码在左侧)。这不是我想要的;我希望第 2 行及以上行的文本与目录条目的第 1 行对齐。

这篇文章的其余部分包括我的原始代码、我第一次尝试解决该问题以及第二次尝试。

原始代码:(我试图只包含本节所需的内容,所以我希望我没有删减太多外部读者需要的内容)

% Start the "Preamble."
\documentclass{article}
% use the LaTeX 'tocloft' package to adjust toc formatting.
\usepackage{tocloft}
% Enter remaining LaTeX preamble commands.
\title{This is my report}
\author{H.S.}
\date{May, 2023}
\begin{document}
% Insert a toc title, then a two-column list of contents that is hyperlinked.
\hypersetup{linktoc=all} % make page numbers also links
\renewcommand{\contentsname}{Contents} % give title of TOC
\renewcommand{\cftaftertoctitle}{\thispagestyle{empty}} % omit p# from footer
\renewcommand{\cftsecafterpnum}{\vskip35pt} % add dbl space beween entries
\renewcommand{\cftsecfont}{\LARGE} % adjust toc entries' font size
\renewcommand{\cfttoctitlefont}{
  \fontsize{35pt}{35pt}\selectfont % adjust toc title font size
  \bfseries % make toc title font bold
  \def\numberline#1{} % eliminate tocloft's auto section numbers
\begin{hyphenrules}{nohyphenation} % stop hyphenation
\setlength{\columnsep}{50pt} % set width of gap between columns
\begin{multicols}{2} % use two columns
\tableofcontents % list section titles as toc entries
\end{multicols}
\end{hyphenrules}
\clearpage
\section{Section One Title}
\clearpage
\section{Section Two Title, Super Long Example to Produce a Break so You Can See the Error}
\clearpage
\section{Section Three Title}
\clearpage
\end{document}

首次尝试修复:我尝试了@tocrmarg(来自 tocloft 手册:“为了将目录中的(分段)标题等排版得参差不齐,没有连字符”,所以我也在这个版本中删除了 \hyphenrules 行):

% Start the "Preamble."
\documentclass{article}
% use the LaTeX 'tocloft' package to adjust toc formatting.
\usepackage{tocloft}
% Enter remaining LaTeX preamble commands.
\title{This is my report}
\author{H.S.}
\date{May, 2023}
\begin{document}
% Insert a toc title, then a two-column list of contents that is hyperlinked.
\hypersetup{linktoc=all} % make page numbers also links
\renewcommand{\contentsname}{Contents} % give title of TOC
\renewcommand{\cftaftertoctitle}{\thispagestyle{empty}} % omit p# from footer
\renewcommand{\cftsecafterpnum}{\vskip35pt} % add dbl space beween entries
\renewcommand{\cftsecfont}{\LARGE} % adjust toc entries' font size
\makeatletter
  \renewcommand{\@tocrmarg}{2.55em plus 1fil}
\makeatother
\renewcommand{\cfttoctitlefont}{
  \fontsize{35pt}{35pt}\selectfont % adjust toc title font size
  \bfseries % make toc title font bold
  \def\numberline#1{}} % eliminate tocloft's auto section numbers
%\begin{hyphenrules}{nohyphenation} % stop hyphenation
\setlength{\columnsep}{50pt} % set width of gap between columns
\begin{multicols}{2} % use two columns
\tableofcontents % list section titles as toc entries
\end{multicols}
%\end{hyphenrules}
\clearpage
\section{Section One Title}
\clearpage
\section{Section Two Title, Super Long Example to Produce a Break so You Can See the Error}
\clearpage
\section{Section Three Title}
\clearpage
\end{document}

没有改变。

第二次尝试修复:我尝试用 \raggedright 命令包装目录条目。

% Start the "Preamble."
\documentclass{article}
% use the LaTeX 'tocloft' package to adjust toc formatting.
\usepackage{tocloft}
% Enter remaining LaTeX preamble commands.
\title{This is my report}
\author{H.S.}
\date{May, 2023}
\begin{document}
% Insert a toc title, then a two-column list of contents that is hyperlinked.
\hypersetup{linktoc=all} % make page numbers also links
\renewcommand{\contentsname}{Contents} % give title of TOC
\renewcommand{\cftaftertoctitle}{\thispagestyle{empty}} % omit p# from footer
\raggedright{
  \renewcommand{\cftsecafterpnum}{\vskip35pt} % add dbl space beween entries
  \renewcommand{\cftsecfont}{\LARGE} % adjust toc entries' font size
  \renewcommand{\cfttoctitlefont}{
    \fontsize{35pt}{35pt}\selectfont % adjust toc title font size
    \bfseries % make toc title font bold
    \def\numberline#1{}} % eliminate tocloft's auto section numbers
  \begin{hyphenrules}{nohyphenation} % stop hyphenation
  \setlength{\columnsep}{50pt} % set width of gap between columns
  \begin{multicols}{2} % use two columns
  \tableofcontents % list section titles as toc entries
  \end{multicols}
  \end{hyphenrules}}
\clearpage
\section{Section One Title}
\clearpage
\section{Section Two Title, Super Long Example to Produce a Break so You Can See the Error}
\clearpage
\section{Section Three Title}
\clearpage
\end{document}

这绝对不是我想要的——这在条目中的单词之间添加了空格(就像它试图使文本对齐一样),并且一些页码甚至比其他页码更靠右。

有什么想法吗?谢谢!:)

答案1

% Start the "Preamble."
\documentclass[english]{article}
% it seems this document uses environment hyphenrules from babel
\usepackage{babel}

%it uses multicols
\usepackage{multicol}


% already default
% \renewcommand{\contentsname}{Contents} % give title of TOC


% use the LaTeX 'tocloft' package to adjust toc formatting.
\usepackage{tocloft}

\renewcommand{\cftaftertoctitle}{\thispagestyle{empty}} % omit p# from footer
\renewcommand{\cftsecafterpnum}{\vskip35pt} % add dbl space beween entries
\renewcommand{\cfttoctitlefont}{%
   \fontsize{35pt}{35pt}\selectfont % adjust toc title font size
   \bfseries % make toc title font bold
}

% This is problematic because tocloft executes this in a group with
% the toc entry.  So the \baselineskip change enactment which requires a \par never
% happens.
% \renewcommand{\cftsecfont}{\LARGE} % adjust toc entries' font size
% this \cftsecfont can NOT be conveniently used for changing font size

% so crazy workaround
\renewcommand{\cftsecfont}{\LARGE\aftergroup \LARGE} % adjust toc entries' font size

% it would be THAT MUCH easier actually redefining \l@section ourself
\setlength{\cftsecnumwidth}{0pt}
\renewcommand\numberline[1]{}

% Enter remaining LaTeX preamble commands.
\title{This is my report}
\author{H.S.}
\date{May, 2023}

% it uses hyperref
\usepackage{hyperref}
\hypersetup{linktoc=all,% make page numbers also links
            colorlinks=true,% get rid of red frames (I would actually used
                            % hidelinks option for duration of
                            % tableofcontents)
            linkcolor=blue,
            }

\begin{document}

% Insert a toc title, then a two-column list of contents that is hyperlinked.

% \begin{hyphenrules}{nohyphenation} % stop hyphenation
% but why would you do that? especially in two columns
\setlength{\columnsep}{50pt} % set width of gap between columns
\begin{multicols}{2} % use two columns
\tableofcontents % list section titles as toc entries
\end{multicols}
% \end{hyphenrules}

% left-over from testing things
% \fontsize{35pt}{45pt}\selectfont 

\clearpage
\section{Section One Title}
\clearpage
\section{Section Two Title, Super Long Example to Produce a Break so You Can See the Error}
\clearpage
\section{Section Three Title}
\clearpage
\end{document}

请仔细考虑代码注释以获得解释。

在此处输入图片描述

相关内容