[Tex/LaTex] Horizontal line within a line of text

moderncvrules

You may know that in moderncv there is a horizontal line at the left of each section. I want to print this at the right of the section title too, just like this:

Mockup of desired output

My current code is as follows:

\documentclass[10pt,a4paper]{moderncv}
\moderncvtheme[blue]{classic}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage[scale=0.92]{geometry}

\setlength{\hintscolumnwidth}{1.2cm}
\usepackage{helvet}

\firstname{\Huge Johnson}
\familyname{Green\vspace{-3mm}}

\title{\normalsize \color{black}Expertise in xxx and xxx.}
\mobile{\color{black}{\textbf{(987) 1234 5678}}}
\email{johnson.green@example.com}

\begin{document}
\maketitle

\section{\textbf{\textsc{Objective}}}
Seeking the XXX position in YYY that ... 

\section{\textbf{\textsc{Education}}}
\cvitem{2010 - 2014}{ Ph.D, Z University}
\cvitem{2006 - 2010}{ B.A, M University}
\end{document}

Best Answer

You can redefine \section to add the rule:

\documentclass{moderncv}
\moderncvstyle{classic}
\moderncvcolor{blue}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage[scale=0.92]{geometry}
\usepackage{helvet}

\setlength{\hintscolumnwidth}{1.2cm}
\newlength\TitleWd
\newlength\LeftRuleWd

\makeatletter
\RenewDocumentCommand{\section}{sm}{%
  \settowidth\TitleWd{\sectionstyle{#2}}%
\setlength\LeftRuleWd{%
  \dimexpr\textwidth-\TitleWd-2\separatorcolumnwidth-\hintscolumnwidth\relax}%
  \par\addvspace{2.5ex}%
  \phantomsection{}% reset the anchor for hyperrefs
  \addcontentsline{toc}{section}{#2}%
  \parbox[t]{\hintscolumnwidth}{\strut\raggedleft\raisebox{\baseletterheight}{\color{color1}\rule{\hintscolumnwidth}{0.95ex}}}%
  \hspace{\separatorcolumnwidth}%
  \parbox[t]{\TitleWd}{\strut\sectionstyle{#2}}%
  \hspace{\separatorcolumnwidth}%
  \parbox[t]{\LeftRuleWd}{\strut\raggedleft\raisebox{\baseletterheight}{\color{color1}\rule{\LeftRuleWd}{0.95ex}}}%  
  \par\nobreak\addvspace{1ex}\@afterheading}% to avoid a pagebreak after the heading
\makeatother

\name{John}{Doe}

\begin{document}

\section{Objective}
Test text and some more text and some more text and some more text and some more text and some more text and some more text and some more text and some more text and some more text and some more text and some more text
\section{Another title}
Test text and some more text and some more text and some more text and some more text and some more text and some more text and some more text and some more text and some more text and some more text and some more text

\end{document}

enter image description here