2

In Portuguese, the name of "Table of Contents" is "Sumário". So, I'd like to get the word "Sumário" in the center of the page.

I know how to put the title in the center:

\documentclass{article}
\usepackage{tocloft}
\renewcommand{\contentsname}{\hfill\bfseries\Large Contents\hfill}   
\renewcommand{\cftaftertoctitle}{\hfill}
\begin{document}
\tableofcontents
\end{document}

I also know how to replace "Table of Contents" by "Sumário":

\documentclass{article}
\usepackage[english,brazil]{babel} 
\begin{document}
\tableofcontents
\end{document}

However, these two solutions does not work together. When using both, we obtain the word "Sumário", but not in the center.

What could I do?

Remark: I'm also using \usepackage{tocloft} and \usepackage[english,brazil]{babel} for other purposes in the documment. So, they cannot be removed from the preamble (i.e. the solution have to be compatible with both).

  • Just out of curiosity, are you trying to follow ABNT rules? – Joseph May 22 '18 at 18:04
  • @Joseph Exactly! And it's not easy in Latex. Fortunately, there are users who can help. – Pedro May 22 '18 at 19:12
  • Pedro, there is a class (and another package) that do that already: try out abntex2, if it's an article, just do \documentclass[article]{abntex2}. – Joseph May 22 '18 at 19:37
3

Simply merging your solutions doesn't work because babel defers the definition of the section headers after the \begin{document}. You can overcome this by inserting the \renewcommand{\contentsname} after \begin{document}:

\documentclass{article}
\usepackage[english,brazil]{babel}
\usepackage{tocloft}
\begin{document}
\renewcommand{\contentsname}{\hfill\bfseries\Large Sum\'ario\hfill\null}
\tableofcontents
\end{document}

As remco pointed out in the comments, the redefinition of \contentsname happens when you switch the language, for example, with \selectlanguage. So after every language-switching command you have to re-reset the \contentsname.

Another alternative is to add the redefinitions to \captionsbrazil:

\documentclass{article}
\usepackage[english,brazil]{babel}
\usepackage{tocloft}
\addto\captionsbrazil{\renewcommand{\contentsname}{\hfill\bfseries\Large Sum\'ario\hfill\null}}
\begin{document}
\tableofcontents
\end{document}

This would make the new definition of \contentsname as the "default". But I don't know about the robustness of this, because the \captions<language> macro is supposed to have only the names of the headers...

  • That's what I thought as well. But that failed after changing the language with \selectlanguage (which seems to reset the \contentsname), so the redefinition(s) should be done just before the \tableofcontents. – remco May 22 '18 at 16:56
  • @remco Yes, the definitions are made when babel executes \captions<language>. And the language switching commands, like \selectlanguage do that. I'll add a note in my answer :) – Phelype Oleinik May 22 '18 at 17:03
  • @PhelypeOleinik If I use fancyhdr to put the page number at top right (as explained here), the "sumário" goes to the left (not totally). Is there any solution? – Pedro May 22 '18 at 20:06
  • 1
    @Pedro If you look carefully, the previous solution had \hfill\bfseries\Large Contents\hfill and a \cftaftertoctitle (which I suppose is inserted after the title) which was \hfill. This roughly translates to \hfill Contents\hfill\hfill, which makes the space on the right twice as large as the one on the left. Something was removing one of these \hfills before you used \pagestyle{fancy}, and stopped. I changed the definition of \contentsname to contain both the \hfills and removed the \cftaftertoctitle which was causing the difference. – Phelype Oleinik May 22 '18 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.