1

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
\addcontentsline{toc}{section}{#1}
\sectionstyle{#1}
\strut%removes too low hrule when letter of section title extends into baseline
{\color{blue}\hrule width 1.5cm height 2pt}
\par\nobreak\addvspace{1ex}\@afterheading}
\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?

2
  • 2
    Please edit your question to include a MWE, so we can test and help easier.
    – koleygr
    Commented Aug 24, 2017 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} Commented Aug 24, 2017 at 14:10

3 Answers 3

1

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
\addcontentsline{toc}{section}{#1}
\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}
\par\nobreak\addvspace{1ex}\@afterheading}
\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.

1

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
\addcontentsline{toc}{section}{#1}
\sectionstyle{\underline{#1\rule[-2pt]{0pt}{0pt}}}
\par\nobreak\addvspace{1ex}\@afterheading}
\makeatother

\begin{document}

\maketitle

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

\end{document}

enter image description here

1

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
\addcontentsline{toc}{section}{#1}
\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}
\par\nobreak\addvspace{1ex}\@afterheading}
\makeatother

\begin{document}

\maketitle

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

\end{document}

enter image description here

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
\addcontentsline{toc}{section}{#1}
\sectionstyle{\cusuline{#1}}%
\par\nobreak\addvspace{1ex}\@afterheading}
\makeatother

\begin{document}

\maketitle

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

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .