16

Im trying to alter the colors on my section, subsection and subsubsection.

I do so using the titlesec page:

\titleformat*{\section}{\Large\bfseries\sffamily\color{red}}
\titleformat*{\subsection}{\large\bfseries\sffamily\color{red}}

But the problem is i have to enter the font and font size also. I am using the fontenc package:

\usepackage[T1]{fontenc}

But i cant seem to find out what font and size is being used for the section, subsection and subsubsection.

These 2 values i mean: \Large\bfseries\sffamily

I tried to look in the .sty but that wasent very helpful.

2
  • What document class do you use?
    – lockstep
    Feb 5, 2011 at 12:10
  • report, and Martins answer looks like rocket sience to me. Is there no easier way?
    – Toroo
    Feb 5, 2011 at 12:18

2 Answers 2

10

The second argument of the \titleformat*{\<name>}{<code>} is stored in a macro called \ttlf@<name> which (default) definition can be printed to the command line with \show\ttlf@<name>, after a \makeatletter of course.

\makeatletter
\show\ttlf@section

\ttlf@section:
macro:->\ttlh@hang {\normalfont \Large \bfseries }{\@seccntformat {section}}{\z
@ }{\ttl@passexplicit }{}. 

\show\ttlf@subsection

\ttlf@subsection:
macro:->\ttlh@hang {\normalfont \large \bfseries }{\@seccntformat {subsection}}
{\z@ }{\ttl@passexplicit }{}. 

\show\ttlf@subsubsection    

\ttlf@subsubsection:
macro:->\ttlh@hang {\normalfont \normalsize \bfseries }{\@seccntformat {subsubs
ection}}{\z@ }{\ttl@passexplicit }{}. 

Therefore the correct settings for the requested title formats are:

\titleformat*{\section}{\normalfont\Large\bfseries\color{red}}
\titleformat*{\subsection}{\normalfont\large\bfseries\color{red}}
\titleformat*{\subsubsection}{\normalfont\normalsize\bfseries\color{red}}

Just for completeness here also the settings for \paragraph and \subparagraph:

\titleformat*{\paragraph}{\normalfont\normalsize\bfseries\color{red}}
\titleformat*{\subparagraph}{\normalfont\normalsize\bfseries\color{red}}
4
  • I got it, guess it was too early. Thx
    – Toroo
    Feb 5, 2011 at 12:26
  • @Toroo: I added the final result now for more clarity for you and other people which want to do the same thing. Feb 7, 2011 at 17:22
  • Hi, any idea how to tweak this s.t it does not mess up numbering ? Just removing the asterix after \titleformat does not work for me. Thx !
    – AnaK
    Apr 29, 2016 at 12:50
  • @AnaK: What is the exact problem with the numbering in your document? May 3, 2016 at 16:38
7

This is more a comment on Martin's answer, but it's too long, so I'm putting it as a community wiki answer.

For more user-friendliness, you can define a \addtotitleformat macro which allows you to only write

\addtotitleformat*{\subsection}{\sffamily\color{blue}}

without having to repeat the other formatting information each time. Here's the code:

\documentclass{article}

\usepackage{xcolor}
\usepackage{titlesec}

\makeatletter
\newcommand*\@secondofsix[6]{#2}
\newcommand{\addtotitleformat}{%
  \@ifstar{\addtotitleformat@star}{\addtotitleformat@nostar}}
\newcommand\addtotitleformat@nostar[2]{%
  \PackageError{titlesec}{non starred form of \string\addtotitleformat\space not supported}{}}
\newcommand\addtotitleformat@star[2]{%
  \expandafter\expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter\@currentsection@font
  \expandafter\expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter{%
    \expandafter\expandafter\expandafter\@secondofsix
       \csname ttlf@\expandafter\@gobble\string#1\endcsname}%
  \titleformat*{#1}{\@currentsection@font#2}%
}
\makeatother

\addtotitleformat*{\section}{\Huge\color{red}}
\addtotitleformat*{\subsection}{\sffamily\color{blue}}

\begin{document}

\section{Section title}

\subsection{Section title}

\end{document}
5
  • Here my \multexpafter would be handy ... Feb 5, 2011 at 14:54
  • @Hendrik: Yes, it would allow just to type \multexpafter3\def\multexpafter3\@currentsection@font\multexpafter4{\multexpafter2\@secondofsix. Are you planning on making a package out of your code? It would then be much easier to use than to have to copy the code each time. Feb 5, 2011 at 17:02
  • I've never ever been close to writing a package, so if you have a package where you think it would be worth to include, please do (and just acklowledge the author in the code). Feb 5, 2011 at 17:07
  • @Hendrik: As \multexpafter is a helper macro, a standalone package would be preferable for it. Doing a package out of your code is just a matter of copying it to a .tex file, making a three-line .sty file which inputs it and then a short pdf file explaining how to use it. If you want, I can do that for you but as author of the code, you should be the package maintainer since you're the one who knows it best. Feb 5, 2011 at 21:49
  • Thanks for those explanations. Now it seems, however, that Bruno should write a package that includes his \MultiExpandAfter. Feb 13, 2011 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .