# Why does redefining \contentsname conflict with redefining \section when there is a \tableofcontents?

I am trying to modify the title of table of content and make every title of sections uppercase. But strange compilation errors occurred.

The .tex file looks like the following one:

\documentclass{article}
\usepackage{szx}
\begin{document}
\section*{fgh}
vbn
\tableofcontents
\section{zxc}
qwe
\end{document}


And I have tried 3 different szx.sty file, neither of them works.

The first one uses xparse package:

\ProvidesPackage{szx}

\usepackage{xparse}
\RequirePackage{titlecaps}

\let\@szx@oldsection\section
\newcommand{\@szx@sectionstar}[1]{\@szx@oldsection*{\titlecap{#1}}}
\newcommand{\@szx@section}[1]{\@szx@oldsection{\titlecap{#1}}}
\RenewDocumentCommand{\section}{s}{\IfBooleanTF{#1}{\@szx@sectionstar}{\@szx@section}}

\let\@szx@TocName\contentsname
\renewcommand*{\contentsname}{\centering{\@szx@TocName}}


The second one uses \DeclareRobustCommand:

\ProvidesPackage{szx}

\RequirePackage{titlecaps}

\let\@szx@oldsection\section
\DeclareRobustCommand{\@szx@sectionstar}[1]{\@szx@oldsection*{\titlecap{#1}}}
\DeclareRobustCommand{\@szx@section}[1]{\@szx@oldsection{\titlecap{#1}}}
\DeclareRobustCommand{\section}{\@ifstar\@szx@sectionstar\@szx@section}

\let\@szx@TocName\contentsname
\DeclareRobustCommand*{\contentsname}{\centering{\@szx@TocName}}


The last one uses makerobust package:

\ProvidesPackage{szx}

\RequirePackage{makerobust}
\RequirePackage{titlecaps}

\let\@szx@oldsection\section
\newcommand{\@szx@sectionstar}[1]{\@szx@oldsection*{\titlecap{#1}}}
\newcommand{\@szx@section}[1]{\@szx@oldsection{\titlecap{#1}}}
\MakeRobustCommand{\@szx@sectionstar}
\MakeRobustCommand{\@szx@section}
\renewcommand{\section}{\@ifstar\@szx@sectionstar\@szx@section}
\MakeRobustCommand{\section}

\let\@szx@TocName\contentsname
\renewcommand*{\contentsname}{\centering{\@szx@TocName}}
\MakeRobustCommand{\contentsname}


The error messages were the same and looks like:

Use of \titlecap doesn't match its definition. \tableofcontents

Argument of \@firstoftwo has an extra }. \tableofcontents

Paragraph ended before \@firstoftwo was complete. \tableofcontents

Something's wrong--perhaps a missing \item. \tableofcontents

Undefined control sequence. \tableofcontents

Illegal parameter number in definition of \the@@@string. \tableofcontents

Undefined control sequence. \tableofcontents

Incomplete \iffalse; all text was ignored after line 12.

• All definitions are wrong, as they no longer allow an optional argument to \section. Using a formatting command in the code for \contentsname is definitely a bad idea. – egreg Nov 4 '16 at 9:07
• @egreg Could you help me centering the caption of table of content while all section titles are capitalized? – Mr. Ree Nov 4 '16 at 9:17
• @egreg Thanks, you inspired me to re-search the solutions instead of writing my own blindly. – Mr. Ree Nov 4 '16 at 9:33

You need to be a bit more careful in doing those redefinitions.

Moreover the \titlecap command seems to conflict with \centering. In any case, adding formatting instructions to \contentsname is wrong. If you want a capitalized name for the contents, define it explicitly.

\documentclass{article}
\usepackage{titlecaps}
\usepackage{etoolbox}
\usepackage{xparse}

\let\latexsection\section
\RenewDocumentCommand{\section}{som}{%
\IfBooleanTF{#1}
{%
\latexsection*{\titlecap{#3}}%
}%
{
\IfNoValueTF{#2}
{%
\latexsection{\titlecap{#3}}%
}%
{%
\latexsection[\titlecap{#2}]{\titlecap{#3}}%
}%
}%
}
\patchcmd{\tableofcontents}
{\section}
{\latexsection}
{}{}
\patchcmd{\tableofcontents}
{\contentsname}
{\centering\contentsname}
{}{}
\makeatother

%% no babel
%% babel
%}

\begin{document}

\tableofcontents

\section*{fgh fgh, fgh}
vbn

\section{zxc zxc, zxc}
qwe

\section[sh]{zxc zxc, zhc}
qwe

\end{document}


• Thank you for mentioning \patchcmd, that is a brand new thing to me through all the searches I have done. – Mr. Ree Nov 4 '16 at 11:40

With @egreg's suggestion, I searched for another way of centering the caption of table of content.

Just replace the re-definition of \contentsname with

\renewcommand*{\contentsname}{\hfill\@szx@TocName\hfill\hfill}

will solve the conflict.

P.S.

To capitalize section title: https://tex.stackexchange.com/a/129682/101988