1

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 ?enter image description here

  • What \documentclass are you using? – Werner Dec 12 '16 at 19:58
  • I'm using the article class – Morten Dec 12 '16 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 '16 at 21:32
  • There are no numbers. – Morten Dec 12 '16 at 21:43
1

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}

enter image description here

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

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

enter image description here

2

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 ~:

enter image description here

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.