# Conflict between titlesec and colortbl

I'm getting a conflict between titlesec and colortbl that I can't seem to fix.

The minimal working example is below. As soon as I put a \section in, pdflatex errors with ! Missing number, treated as zero. <to be read again> } l.9 \section{First Section}

In order to make it work I have to either:

• Not use sections (obviously not feasible)
• Not use my section style (but the journal requires it!)
• Not use coloured tables (but I want to!)

Can you help me make this document with my section heading style included and coloured cells in the table?

Thanks, Sven.

Edited below to include the full set of titlesec and titleformat command I use

\documentclass{article}

\usepackage{colortbl}
\usepackage{textcase}
\usepackage[explicit]{titlesec}
\titleformat{\section}
{\normalfont\bfseries\filcenter}{\thesection}{1em}{\MakeTextUppercase{#1}}
\titlespacing*{\subsection}{0pt}{1em}{0em}
\titlespacing*{\subsubsection}{0pt}{1em}{0em}
\titleformat*{\subsection}{\bfseries}
\titleformat*{\subsubsection}{\bfseries}

\begin{document}

\section{First Section}
\subsection{a sub Sec}
If I define a section (by uncommenting above)... it fails.
\subsubsection{Again}

If I then do one of:
\begin{itemize}
\item{Remove the table (and colortbl package}
\item{Remove my section formatting above}
\end{itemize}
... my doc will compile, but I won't have the style I want.

\begin{table}[!ht]
\begin{tabular}{ |c|l|l } \hline
\rowcolor{red} A    & B     & C     \\ \hline
Hello World         & other     & \cellcolor{blue}stuff\\ \hline
\end{tabular}
\caption{How can I get colored table cells and keep my section heading format?}
\end{table}

\end{document}


For reference in case of package conflicts, here is the list of other packages I'm using in my complete document (but don't seem to affect this particular bug):

\usepackage[left=18mm,top=20mm,right=18mm,bottom=25mm,includehead,nofoot,headheight=0pt,headsep=5mm]{geometry}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{dcolumn}
\usepackage{hyperref}
\usepackage{tabularx}
\usepackage{subfig}
\usepackage{url}
\usepackage[capitalise,noabbrev]{cleveref}
\usepackage{epstopdf}
\usepackage[absolute]{textpos}

-
Please call package hyperref as last package ... –  Kurt Mar 15 '13 at 2:46
@sven It seems the problem lies with your use of \MakeUppercase in \titleformat*. I get no error if I remove it. –  Jubobs Mar 15 '13 at 2:48
@Jubobs: Thanks for helping find that. Just out of curiosity, is it normal for this problem to only occur after I put in the colortbl package? Because my document would be created without error even with the MakeUppercase issue you identified, as long as it didn't have colortbl too... –  sven Mar 15 '13 at 3:39

There's no conflict between the packages; the problem is that \MakeUppercase requires an argument; you can overcome this by using the extended syntax for \titleformat and the explicit option for titlesec to have explicit access to the title:

\documentclass{article}
\usepackage{colortbl}
\usepackage{textcase}
\usepackage[explicit]{titlesec}
\titleformat{\section}
{\normalfont\bfseries\filcenter}{\thesection}{1em}{\MakeTextUppercase{#1}}

\begin{document}

\section{First Section}

\begin{table}[!ht]
\centering
\begin{tabular}{ |c|l|l } \hline
\rowcolor{red} A    & B     & C     \\ \hline
Hello World         & other     & \cellcolor{blue}stuff\\ \hline
\end{tabular}
\caption{Problem solved!}
\end{table}

\end{document}


Notice also that I used \MakeTextUppercase from the textcase package (it's safer than the standard \MakeUppercase in titles).

After the edit to the original question, here's now the code with the necessary modifications for the other sectional units:

\documentclass{article}
\usepackage{colortbl}
\usepackage{textcase}
\usepackage[explicit]{titlesec}

\titleformat{\section}
{\normalfont\normalsize\bfseries\filcenter}{\thesection}{1em}{\MakeTextUppercase{#1}}
\titleformat{\subsection}
{\normalfont\normalsize\bfseries}{\thesubsection}{1em}{#1}
\titlespacing*{\subsection}{0pt}{1em}{0em}
\titlespacing*{\subsubsection}{0pt}{1em}{0em}

\begin{document}

\section{Test Section}
\subsection{Test Subsection}
\subsubsection{Test Subsubsection}
\begin{table}[!ht]
\centering
\begin{tabular}{ |c|l|l } \hline
\rowcolor{red} A    & B     & C     \\ \hline
Hello World         & other     & \cellcolor{blue}stuff\\ \hline
\end{tabular}
\caption{Problem solved!}
\end{table}

\end{document}


-
Thanks Gonzalo, that indeed fixes the MWE. Can I trouble you to help me "safe-ify" the following set of section adjustments: \usepackage{textcase} \usepackage[explicit]{titlesec} \titleformat{\section} {\normalfont\bfseries\filcenter}{\thesection}{1em} {\MakeTextUppercase{#1}} \titleformat*{\subsection}{\bfseries} \titleformat*{\subsubsection}{\bfseries} –  sven Mar 15 '13 at 3:28
Argh, sorry the comment didn't format how I intended. I'm no longer getting a problem with the MWE I posted, but there's something in my full document that still won't compile properly even after fixing the titlesec. I'll try and identify it enough to recreate in the MWE... sorry, might take a few minutes.... –  sven Mar 15 '13 at 3:36
@sven mo problem; code in comments looses formatting (adding back-ticks helps a little). –  Gonzalo Medina Mar 15 '13 at 3:48
Ok Gonzalo, I've edited my question to include all section formatting that I do... I left in the original stuff that was working without the colortbl (I understand that using the explicit option means they're bad... I just couldn't get it to work) –  sven Mar 15 '13 at 3:51
@sven please see my updated answer. –  Gonzalo Medina Mar 15 '13 at 3:57