# \hrule underlining of subsection titles

I want an underlining of my subsection titles - a horizontal line that extends the whole page width like a \hrule.

I tried the answer from this question which is to add these lines:

\titleformat{\subsection}
{\normalfont\Large\bfseries}{\thesection}{1em}{}[{\titlerule[0.8pt]}]


where I have changed \section to \subsection.

But this totally messes up all my subsection titles! I already have some color put on them and this is removed. Also, their subsection numbering (i.e. 5.1) disappears and only the section part of the numbering is displayed (i.e. 5).

How can I make this work? And is there a way to add this horizontal line below the title without overwriting existing formatting of the subsections?

My code structure

\documentclass[11pt,a4paper]{article}

\usepackage[utf8x]{inputenc}    %Character set
\usepackage[explicit]{titlesec}
\usepackage{xcolor,lipsum}
...

%Setting subsection and subsubsection title color
\definecolor{red}{RGB}{163, 25, 25}
\subsectionfont{\color{red}}
\subsubsectionfont{\color{red}}

%Setting section title bg color
\definecolor{redbg}{RGB}{235, 214, 214}
\titleformat{\section}

%Underlining ruler for subsections
\titleformat{\subsection}
{\normalfont\Large\bfseries}{\thesection}{1em}{}[{\titlerule[0.8pt]}]

\begin{document}

\section
Text text text
\subsection
Text text text

\end{document}


You need to use the (representation for) counter for subsections and you had the one for sections. You need \thesubsection instead of \thesection in

\titleformat{\subsection}
{\normalfont\Large\bfseries}{\thesection}{1em}{}[{\titlerule[0.8pt]}]


You can introduce the color specification in the second mandatory argument (affects number and title) or in the last mandatory argument (only affects the title). A complete example; notice that I changed the color to Red (upper-case) since red is already a standard LaTeX color:

\documentclass[11pt,a4paper]{article}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}

%Setting section title bg color
\definecolor{redbg}{RGB}{235, 214, 214}
%Setting subsection and subsubsection title color
\definecolor{Red}{RGB}{163, 25, 25}

\titleformat{\section}
{\normalfont\LARGE\bfseries}
{}
{0em}

%Underlining ruler for subsections
\titleformat{\subsection}
{\normalfont\Large\bfseries\color{Red}}
{\thesubsection}
{1em}
{#1}
[{\titlerule[0.8pt]}]

\begin{document}

\section{A test section}
Text text text
\subsection{A test subsection}
Text text text

\end{document}


• Thank you. This is a working answer to my question. Unfortunately I found out that there is an issue with a version of the titlesec that I am already using. I have this line in the preamble as well: \usepackage[explicit]{titlesec} I use it to be able to add a background to section titles. When this is on, there is no text in my subsection titles when I use your answer here. Only the numbering and line i correct color. – Steeven Jun 21 '15 at 19:00
• I have added the code with the package and the code lines for putting a background on the section titles to the code example in the question. – Steeven Jun 21 '15 at 19:04
• @Steeven simply add #1 in the fifth mandatory argument in my definition of \titleformat for subsections. When I arrive home I'll update the code in my answer. – Gonzalo Medina Jun 21 '15 at 19:13
• I have no clue what just happened - but that worked... Thank you very much. – Steeven Jun 21 '15 at 19:20
• @Steeven You're welcome. I've updated my answer. What happened is that when using the explicit option for titlesec the titles must be given explicitly with #1. – Gonzalo Medina Jun 22 '15 at 0:22