# Error with \uppercase in titlesec's label field

I'm trying to use titlesec and format my chapters so the label 'Chapter' is rendered in ALLCAPS. If I use \MakeUppercase I get the desired result.

\titleformat
{\chapter} % command
[display] % shape
{\bfseries\Large\itshape} % format
{\sffamily\huge\MakeUppercase\chaptertitlename{ }\thechapter} % label
{0.5ex} % sep
{
\rule{\textwidth}{1pt}
\vspace{1ex}
\centering
} % before-code
[
\vspace{-0.5ex}%
\rule{\textwidth}{0.3pt}
] % after-code


But if I use \uppercase, I get an Missing { inserted. \chapter{The First Chapter} error.

\titleformat
{\chapter} % command
[display] % shape
{\bfseries\Large\itshape} % format
{\sffamily\huge\uppercase\chaptertitlename{ }\thechapter} % label
{0.5ex} % sep
{
\rule{\textwidth}{1pt}
\vspace{1ex}
\centering
} % before-code
[
\vspace{-0.5ex}%
\rule{\textwidth}{0.3pt}
] % after-code


I'm using pdfTeX 3.14159265-2.6-1.40.19 (TeX Live 2018/W32TeX). I get the same error if I use MikTeX 2.9.7100 x64. This is on Windows 7.

I can figure out why can't I use \uppercase. I'm asking this so if anyone else has this problem, they won't spend a lot of time debugging this.

Thank you!

My Whole document is

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{titlesec}

\titleformat
{\chapter} % command
[display] % shape
{\bfseries\Large\itshape} % format
{\sffamily\huge\uppercase\chaptertitlename{ }\thechapter} % label
{0.5ex} % sep
{
\rule{\textwidth}{1pt}
\vspace{1ex}
\centering
} % before-code
[
\vspace{-0.5ex}%
\rule{\textwidth}{0.3pt}
] % after-code

\begin{document}

\author{TeXstudio Team}
\title{Simple Book Example}
\date{January 2013}

\frontmatter
\maketitle
\tableofcontents

\mainmatter

\chapter{The First Chapter}

And now for something completely new

\chapter{The Second Chapter}

\backmatter
% bibliography, glossary and index would go here.

\end{document}


latex provides \MakeUppercase because \uppercase will destroy lots of text, almost all text if you are using UTF-8 with non ascii letters. However the specific error here is

\uppercase\chaptertitlename


The braces are not optional, it has to be

\uppercase{\chaptertitlename}


hence the missing brace error that you show.

But as noted above this will break the UTF-8 encoding of any text using accented letters as \uppercase simply uses the ascii uppercase table and applies it to the UTF-8 bytes, which will produce something that is not valid UTF-8.

Try this:

\documentclass{article}

\newcommand{\test}{{abc}def}

\begin{document}

\uppercase\test

\end{document}


The output is

Funny? A bit.

The fact is that \uppercase is a TeX primitive that requires { after it. To this end, it expands tokens after it; any \relax token or space token is ignored and expansion is resumed. This expansion process ends as soon as another unexpandable token is found: if it is {, TeX proceeds to execute \uppercase; otherwise, it raises an error, inserts { and rereads that token.

This should explain the funny output above: the expansion of \test results in {abc}def, so {abc} is processed by \uppercase and we get “ABCdef”.

To the contrary, \MakeUppercase is a macro with one argument.

Avoid omitting braces around arguments. And avoid \uppercase with LaTeX.