[Tex/LaTex] How to center the table of contents title using tocloft

horizontal alignmenttable of contentstocloft

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!!

Best Answer

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}