1

I was trying to make a section style with uppercase fonts and color. But when I use \MakeUppercase like

\makeatletter
\renewcommand{\@seccntformat}[1]{{\textcolor{purple}{\csname the#1\endcsname}\hspace{1em}}}                    
\renewcommand{\section}{\@startsection{section}{1}{\z@}
{-4ex \@plus -1ex \@minus -.4ex}
{1ex \@plus.2ex }
{\normalfont\large\sffamily\bfseries\MakeUppercase\color{purple}}}

The following errors are shown

Argument of \@declaredcolor has an extra }. \section{}
  • 1
    Welcome to TeX.SE! Try swapping \MakeUppercase and \color{purple} on the last line: {\normalfont\large\sffamily\bfseries\color{purple}\MakeUppercase}. All of \normalfont, \large, \sffamily, \bfseries, and \color{purple} are switches (i.e. their effect last until the current scope ends) while \MakeUppercase is a macro that takes one argument, so the way the sectioning commands work, the command which takes one argument must be last. – Phelype Oleinik Feb 12 at 18:54
1

If you swap the order of \MakeUppercase and \color{purple} it works. Internally, the sectioning commands do (approximately) this:

<formatting>{\@seccntformat{<number>} <title>}

where <formatting> is the last argument of \@startsection, <number> is the section number and <title>, you guessed, the title :)

If we do like you suggested, \section{hello} becomes:

\normalfont\large\sffamily\bfseries\MakeUppercase\color{purple}{1 hello}

(\@seccntformat is not important right now). Try running the above and you'll see the same error. That happens because \MakeUppercase grabs \color as argument and when \color tries to do its thing it won't find the droids color name it was looking for and chaos will ensue. But if you swap the order it works :)

\normalfont\large\sffamily\bfseries\color{purple}\MakeUppercase{1 hello}

But now you'll get another error:

! Package xcolor Error: Undefined color `PURPLE'.

but why?! Remember the \@seccntformat back then? You had:

<formatting>{\@seccntformat{<number>} <title>}

when <formatting> contains \MakeUppercase and \@seccntformat contains \textcolor{purple}{<number>}, then you have:

\MakeUppercase{\textcolor{purple}{<number>} <title>}

which will \MakeUppercase before \textcolor is executed:

{\textcolor{PURPLE}{<NUMBER>} <TITLE>}

Yes, the NUMBER too :)

But you don't need that. The counter is used after the \color{purple}, so it will already be coloured. You can remove that and the number will be purple too. If you take that out, your definition of \@seccntformat is identical to LaTeX's one, so you can remove it altogether.

After all this the output is:

enter image description here

MWE:

\documentclass{article}

\usepackage{xcolor}

\makeatletter
% \renewcommand{\@seccntformat}[1]{\csname the#1\endcsname\hspace{1em}} == default :)
\renewcommand{\section}{\@startsection{section}{1}{\z@}
{-4ex \@plus -1ex \@minus -.4ex}
{1ex \@plus.2ex }
{\normalfont\large\sffamily\bfseries\color{purple}\MakeUppercase}}
\makeatother

\begin{document}
\pagestyle{empty}

\section{Hello}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\end{document}

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.