How to underline complete headings in moderncv?

The headings of each section in the following moderncv template are underlined with \hrule of a fixed width of 1.5cm:

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}
\definecolor{color1}{rgb}{0,0,1}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\sectionstyle{#1}
\strut%removes too low hrule when letter of section title extends into baseline
{\color{blue}\hrule width 1.5cm height 2pt}
\makeatother

\begin{document}

\maketitle

\section{Education}
\section{Key Skills}
\section{Scholarships}

\end{document}


How can I change this and underline the entire headings of each caption?

• Please edit your question to include a MWE, so we can test and help easier. – koleygr Aug 24 '17 at 13:35
• How about \sectionstyle{\underline{#1\rule[-2pt]{0pt}{0pt}}} and get rid of the \strut and {\color{blue}\hrule width 1.5cm height 2pt} – Steven B. Segletes Aug 24 '17 at 14:10

Here is a solution:

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}
\definecolor{color1}{rgb}{0,0,1}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\newlength{\mylength}
\newsavebox{\mybox}

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\sectionstyle{#1}
\strut%removes too low hrule when letter of section title extends into baseline
\savebox{\mybox}{\bfseries\large #1}
\settowidth{\mylength}{\usebox{\mybox}}
{\color{blue}\hrule width \mylength height 2pt}
\makeatother

\begin{document}

\maketitle

\section{Education}
\section{Key Skills}
\section{Scholarships}

\end{document}


I think you don't really need savebox for this and can be done easier... You can do it your way by calculating section width your way.

Maybe this (as suggested in my comment)?

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}
\definecolor{color1}{rgb}{0,0,1}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\sectionstyle{\underline{#1\rule[-2pt]{0pt}{0pt}}}
\makeatother

\begin{document}

\maketitle

\section{Education}
\section{Key Skills}
\section{Scholarships}

\end{document}


The following does print a rule which is as wide as the section title:

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}
\definecolor{color1}{rgb}{0,0,1}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\setbox0\hbox{\sectionstyle{#1}}
\usebox0
\strut%removes too low hrule when letter of section title extends into baseline
{\color{blue}\hrule width \wd0 height 2pt}
\makeatother

\begin{document}

\maketitle

\section{Education}
\section{Key Skills}
\section{Scholarships}

\end{document}


Another possibility utilizing the ulem package:

\documentclass[11pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}
\definecolor{color1}{rgb}{0,0,1}
\usepackage[normalem]{ulem}
\usepackage[UKenglish]{babel}
\usepackage[scale=0.8]{geometry}
\firstname{}
\familyname{}

\newcommand*{\cusuline}{%
\bgroup\markoverwith{\rule[-1ex]{0.25ex}{2pt}}\ULon}%

\makeatletter
\renewcommand\sectionfont{\bfseries\large}
\renewcommand*{\section}[1]{%
\par
\phantomsection{}% reset the anchor for hyperrefs
\sectionstyle{\cusuline{#1}}%