1

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.

  • 1
    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
2

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
\renewcommand{\contentsname}{Table of Contents}
%% babel
%\addto\captionsenglish{%
%  \renewcommand{\contentsname}{Table of Contents}%
%}

\begin{document}

\tableofcontents

\section*{fgh fgh, fgh}
vbn

\section{zxc zxc, zxc}
qwe

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

\end{document}

enter image description here

|improve this answer|||||
  • 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
0

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

|improve this answer|||||

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.