# Change color of sections

I want to change the section style in my latex document, so the section text are diveded in two colors as shown in the picture. The text color should change after 3 leters from green to black. How do I do that ?

• What \documentclass are you using?
– Werner
Dec 12, 2016 at 19:58
• I'm using the article class Dec 12, 2016 at 21:19
• How would the colouring work with numbered/unnumbered sections? Can you provide an image that shows this (or are you only using \section*)?
– Werner
Dec 12, 2016 at 21:32
• There are no numbers. Dec 12, 2016 at 21:43

Here is a solution using the titlesec package to redefine the \section* headings.

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{titlesec}
\titleformat{name=\section,numberless}{\normalfont\Large\bfseries}{}{0em}{\colorthree}
\newcommand\colorthree[1]{\colorthreeX#1}
\newcommand\colorthreeX[3]{{\color{ForestGreen}#1#2#3}}
\usepackage{blindtext}
\begin{document}
\section*{Education}
\blindtext
\section*{Experience}
\blindtext
\end{document}


If you also want a horizontal line, define \section* as

\usepackage[explicit]{titlesec}
\titleformat{name=\section,numberless}{\normalfont\Large\bfseries}{}{0em}{\colorthree{#1} \hrulefill}


The following etoolbox patch updates \@sect to colour the first three letters of all sectional titles. Spaces are gobbled, so you'll have to use ~:

\documentclass{article}

\usepackage{etoolbox,xcolor}
\newcommand{\seccolfmt}[3]{\textcolor{green!80!black}{#1#2#3}}
\makeatletter
\patchcmd{\@sect}% <cmd>
{#8}% <search>
{\seccolfmt #8}% <replace>
{}{}% <success><failure>
\makeatother

\setcounter{secnumdepth}{0}% Don't number \section, \subsection, ...

\begin{document}

\section{Education}
\subsection{Experience}
\section{A~section}

\end{document}