19

I'm getting odd behavior using package tocloft to format my table of contents and center the titles "Contents", "List of Figures", and "List of Tables". Using the code suggested in the tocloft documentation, I tried to center "Contents" with:

\renewcommand{\cfttoctitlefont}{\hfill\Large}
\renewcommand{\cftaftertoctitle}{\hfill}

but instead of centering, this right justifies the title, even though the exact same code works to center the list of figures and list of tables titles.

In my minimal working example, I can get this working by replacing the first \hfill with \hfil, but in my actual document that fix causes the title to be somewhere in between left and center. I am trying to understand why \cfttoctitlefont is not working the same as \cftlottitlefont and \cftloftitlefont, so that I can address the problem in my actual document.

Minimal working example:

\documentclass{article}
\usepackage{tocloft}
\renewcommand{\cfttoctitlefont}{\hfill\Large}
\renewcommand{\cftaftertoctitle}{\hfill}
\renewcommand{\cftlottitlefont}{\hfill\Large}
\renewcommand{\cftafterlottitle}{\hfill}

\begin{document}
\tableofcontents
\listoftables
\section{Section}
\begin{table}[h]
\centering
\begin{tabular}{ l | c || r }
\hline
1 & 2 & 3 \\ 
\hline
\end{tabular}
\caption[Example table]{This is a table.} 
\end{table}
\end{document}

Thank you!!

Corentin
  • 9,981
Katie R
  • 385

3 Answers3

21

There's indeed something in the example code of the tocloft package that's not quite correct about how to get the string "Contents" centered on a line. Anyway, you can achieve your objective by issuing the commands

\renewcommand{\contentsname}{\hfill\bfseries\Large Contents\hfill}   
\renewcommand{\cftaftertoctitle}{\hfill}

Note the \hfill inserted after "Contents". If you would rather have the string "Contents" be set in normal size and normal weight, you'd use \normalfont\normalsize instead of \bfseries\Large.

A full MWE, applying these changes to the header of the List of Tables as well:

enter image description here

\documentclass{article}
\usepackage{tocloft}
\renewcommand{\contentsname}{\hfill\bfseries\Large Contents\hfill}   
\renewcommand{\cftaftertoctitle}{\hfill}
\renewcommand{\listtablename}{\hfill\bfseries\Large List of Tables} % no \hfill after "List of Tables"...
%%% using the command "\renewcommand{\cftlottitlefont}{\hfill\bfseries\Large}" works too...
\renewcommand{\cftafterlottitle}{\hfill}

\begin{document}
\tableofcontents
\listoftables
\section{Section}
\begin{table}[h]
\centering
\begin{tabular}{| l | c | r |}
\hline
1 & 2 & 3 \\ 
\hline
\end{tabular}
\caption[Example table]{This is a table.} 
\end{table}
\end{document}
Mico
  • 506,678
  • Perfect! Worked for my actual document as well. – Katie R Nov 06 '13 at 17:24
  • 1
    The 10th upvote on this answer, on 2018-08-16, earned me badges 1,110 and 1,111 [!] on this site. Many thanks!!! – Mico Aug 16 '18 at 21:57
  • This almost worked for me, but if the List of Tables is included in the TOC, it takes the same centered bold text. Do you know how to make it suck that i can keep the contents of the TOC as it is? If the question is not clear let me know and i will clarify – Austin Benny Mar 23 '22 at 14:35
  • @AustinBenny - I'm not sure I understand what "the list of tables [being] included in the ToC entails. Pleaese post a new query, in which you'd explain what exactly you're doing and what you are looking to achieve. Incidentally, by posting a new query, far more people will get to see what you're looking to achieve, thus considerably raising the odds that someone will be able to come up with a solution. – Mico Mar 23 '22 at 14:40
  • 1
    @Mico got it. Here is the question: https://tex.stackexchange.com/questions/638149/how-to-center-the-list-of-tables-title-in-such-a-way-that-the-section-list-of-t – Austin Benny Mar 23 '22 at 14:51
  • You don't want to modify the formatting internally to \contentsname itself, in case you need it in some other place (especially the toc itself). However, the bug mentioned in the original post must have been fixed, because the code works flawlessly. – Vincent Krebs Feb 05 '23 at 00:36
8

Actually your solution is almost correct. The reason why it is not working is the fact that LaTeX removes the horizontal space that comes at the end of a line. This answer further elaborates on this fact but to prevent this either you need to insert a dummy text -- e.g., \null{}, \mbox{}, \hfill -- after the last \hfill:

\renewcommand{\cfttoctitlefont}{\hfill\Large}
\renewcommand{\cftaftertoctitle}{\hfill\hfill} %Note the second \hfill, it could have been any dummy text

and Mico's solution effectively does that by ending \contentsname with an \hfill and simultaneously setting \cftaftertoctitle to \hfill.

Or, you could use \hspace*{\fill} so that your code becomes:

\renewcommand{\cfttoctitlefont}{\hspace*{\fill}\Large}
\renewcommand{\cftaftertoctitle}{\hspace*{\fill}}

and Mico's solution becomes:

\renewcommand{\contentsname}{\hspace*{\fill}\bfseries\Large Contents\hspace*{\fill}}   
%\renewcommand{\cftaftertoctitle}{\hspace*{\fill}} % Alternatively, you can delete the last \hspace*{\fill} from the above line and uncomment this line.

This last \contentsname definition works with both tocloft package and the original LaTeX toc command as well.

Halil Sen
  • 277
  • For me, \renewcommand{\cfttoctitlefont}{\hfill\bfseries\huge} \renewcommand{\cftaftertoctitle}{\hfill} works perfectly (I do not need to add any dummy text after the second \hfill). – Vincent Krebs Feb 05 '23 at 00:30
1

Simply using the following works too:

 \renewcommand{\contentsname}{\centering Contents}
  • 1
    This only works if you're not using tocloft. – Werner May 06 '16 at 17:07
  • If you need \contentsname in the toc, for example, you will get the issue in https://tex.stackexchange.com/questions/638149/how-to-center-the-list-of-tables-title-in-such-a-way-that-the-section-list-of-t. – Vincent Krebs Feb 05 '23 at 00:32