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}
{\normalfont\LARGE\bfseries}{}{0em}{\colorbox{redbg}{\parbox{\textwidth-2\fboxsep}{\textcolor{red}{\thesection\quad#1}}}}
%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}
Best Answer
You need to use the (representation for) counter for subsections and you had the one for sections. You need
\thesubsection
instead of\thesection
inYou 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: