在我的报告中,我想有一个简短的目录,因此我使用 multicol 将其打印成两列以节省空间。但顶部的对齐方式如图所示。
我更喜欢第二列的第一行与第一列的目录中的第一个条目对齐。总之,第 3 节应移至第 2 节下方的左列,第 4 节应与第 1 节保持在同一行。
我找到了一些解决方案,但它们在某种程度上非常手动,你必须自己编写哪个部分出现在哪一列。到目前为止,这里一切都是自动发生的 :)
我迄今为止的代码:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
%%%%%% additional packages
\usepackage{multicol}
\usepackage{etoolbox}
%%%%%%
\setcounter{tocdepth}{1} %set depth of printed table of contets.
\makeatletter
\patchcmd{\l@section}
{\hfil}
{\leaders\hbox{\normalfont$\m@th\mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfill}
{}{}
\makeatother %print dots in sections in toc.
\begin{document}
\begin{multicols}{2}
\tableofcontents
\end{multicols}
\section{Introduction}%%%%%%%%%%Introduction
\subsection{bla}
\subsubsection{blabla}
\section{Approach}%%%%%%%%%%Approach
\subsection{bla}
\subsubsection{blabla}
\section{Experimental results}%%%%%%%%%%Experimental results
\subsection{bla}
\subsubsection{blabla}
\section{Discussion}%%%%%%%%%%Discussion
\subsection{bla}
\subsubsection{blabla}
\section{Summary}%%%%%%%%%%Summary
\subsection{bla}
\subsubsection{blabla}
\end{document}
非常感谢,SemtexB
答案1
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
%%%%%% additional packages
\usepackage{multicol,etoolbox}
%%%%%%
\setcounter{tocdepth}{2} %set depth of printed table of contets.
\makeatletter
\patchcmd{\l@section}
{\hfil}
{\leaders\hbox{\normalfont$\m@th\mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfill}
{}{}
\renewcommand\tableofcontents{%
\begin{multicols}{2}[\section*{\contentsname
\@mkboth{%
\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}]%
\@starttoc{toc}%
\end{multicols}%
}
\makeatother %print dots in sections in toc.
\begin{document}
\tableofcontents
\section{zz}
aa
\section{bbbb}
\subsection{xgxgxgxg}
aaa
\subsection{xgxgxgxg}
aaa
\subsection{xgxgxgxg}
aaa
\subsection{xgxgxgxg}
\end{document}
答案2
一个更短但更隐蔽的解决方案:
\documentclass{article}
\usepackage{multicol}
\AtBeginDocument{\addtocontents{toc}{\protect\begin{multicols}{2}}}
\AtEndDocument{\addtocontents{toc}{\protect\end{multicols}}}
\begin{document}
\tableofcontents
\section{fee}
\subsection{foo}
\subsection{bar}
\section{fie}
\section{fo}
\section{fum}
\end{document}
答案3
这是一个老话题,但我遇到了同样的问题,并找到了一个我认为更简单的解决方案,所以...只需删除目录标题,然后在 \tableofcontents 之前手动添加它。您的代码将如下所示:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
%%%%%% additional packages
\usepackage{multicol}
\usepackage{etoolbox}
%%%%%%
\setcounter{tocdepth}{1} %set depth of printed table of contets.
\makeatletter
\patchcmd{\l@section}
{\hfil}
{\leaders\hbox{\normalfont$\m@th\mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfill}
{}{}
\makeatother %print dots in sections in toc.
\begin{document}
\section*{Contents} % As suggested by TeXnician
\renewcommand{\contentsname}{}
\begin{multicols}{2}
\tableofcontents
\end{multicols}
\section{Introduction}%%%%%%%%%%Introduction
\subsection{bla}
\subsubsection{blabla}
\section{Approach}%%%%%%%%%%Approach
\subsection{bla}
\subsubsection{blabla}
\section{Experimental results}%%%%%%%%%%Experimental results
\subsection{bla}
\subsubsection{blabla}
\section{Discussion}%%%%%%%%%%Discussion
\subsection{bla}
\subsubsection{blabla}
\section{Summary}%%%%%%%%%%Summary
\subsection{bla}
\subsubsection{blabla}
\end{document}