# Automatically scale vertical rule to fit the text height

I'm trying to modify the style of the sections in moderncv package. Specifically, I want sections to have a vertical rule on their right side of the height of the section text. Below is the non-working code, where the height of the rule is hardcoded:

\documentclass[11pt,sans]{moderncv}
\moderncvstyle{casual}
\moderncvcolor{blue}
\reversemarginpar
\usepackage[paper=letterpaper,
marginparwidth=1.2in,
marginparsep=.05in,
margin=1in,
includemp,
scale=0.75]{geometry}
\setlength{\parindent}{0in}

\renewcommand{\section}[1]{\pagebreak[3]%
\vspace{1.3\baselineskip}%
\noindent\llap{\scshape\smash{\parbox[t]{%
\marginparwidth}{%
\hyphenpenalty=10000\raggedright%
\sectionfont{\textcolor{color1}{#1 \rule{1pt}{18pt}}}}}}%
\vspace{-\baselineskip}\par}

% personal data
\name{Harry}{Potter}
\title{Resumé}
\phone[mobile]{+777~(0)~777-777777}

\usepackage[utf8]{inputenc}
\title{}
\begin{document}

\makecvtitle

\section{Technology Summary}

\subsection{Languages}
ActionScript, C, Clojure, Common Lisp, Emacs Lisp, Erlang, Forth, Haskell,
Haxe, Java, JavaScript, Mercury, OCaml, PHP, Prolog, Python, Ruby, Scala,
Scheme, Shell.

\end{document}


I think I need to reference the \parbox declared earlier somehow, but I don't know how to do that.

Here's the image to illustrate the problem:

• Please make a compilable example out of it! – user31729 Oct 13 '15 at 15:36
• @ChristianHupfer it was basically it, updated to have some fake data. – wvxvw Oct 13 '15 at 15:41
• You want the rule to be of same height as the entire text or only one line? – user11232 Oct 13 '15 at 15:43
• @HarishKumar as the text of the section (it can span several lines). I'll add an image to make it stand out. – wvxvw Oct 13 '15 at 15:44

Here's one option using the tikzmark library from TikZ (since internal calculations are performed, the code needs two or three runs for the rules to reach their final positions):

\documentclass[11pt,sans]{moderncv}
\moderncvstyle{casual}
\moderncvcolor{blue}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\reversemarginpar
\usepackage[paper=letterpaper,
marginparwidth=1.2in,
marginparsep=.05in,
margin=1in,
includemp,
scale=0.75]{geometry}
\setlength{\parindent}{0in}

\newcounter{tmp}

\renewcommand{\section}[1]{\pagebreak[3]%
\stepcounter{tmp}%
\par\vspace{1.3\baselineskip}%
\noindent\llap{\scshape\smash{\parbox[t]{%
\dimexpr\marginparwidth-5pt\relax}{%
\hyphenpenalty=10000\raggedright%
\sectionfont{\textcolor{color1}{\tikzmark{start-\thetmp}#1\hfill\tikzmark{end-\thetmp}}}}}}%
\tikz[remember picture,overlay]
\draw[color1,line width=1pt] ([xshift=5pt,yshift=2ex]{pic cs:end-\thetmp}|-{pic cs:start-\thetmp}) -- ([xshift=5pt]{pic cs:end-\thetmp});
\vspace{-\baselineskip}\par}

\usepackage[utf8]{inputenc}
\firstname{John}
\familyname{Doe}
\title{}
\begin{document}

\section{A test section}
\cventry{2008--2010}{Lecturer}{Some Place}{Some Country}{}{Some responsabilities}

\section{Another test section}
\cventry{2008--2010}{Lecturer}{Some Place}{Some Country}{}{Some responsabilities}

\section{Yet another test section with a longer title}
\cventry{2008--2010}{Lecturer}{Some Place}{Some Country}{}{Some responsabilities}

\end{document}


The result:

• There's a bit of a problem with this code, though it doesn't look like it's the tikz problem (or so I think), the box containing the sections is actually too wide, so, for some section titles the text runs on top of the rule. Would you happen to know how to remedy this? – wvxvw Oct 13 '15 at 18:02
• @wvxvw Sure; this is easy. I have to leave now but later I will provide the necessary adjustment. – Gonzalo Medina Oct 13 '15 at 18:29
• @wvxvw Please see my updated answer. Now there's at least 5pt between the titles and the rules. – Gonzalo Medina Oct 13 '15 at 19:12
• Thanks! Although in the end, I had to make the box even smaller and shift the ruler by some small negative value to the left, since what happened is that the whole box would overlap with a regular paragraph text (you don't see it in your example since you don't have paragraph text following the section). – wvxvw Oct 14 '15 at 7:21

You can just use a tabular:

\documentclass[11pt,sans,draft]{moderncv}
\moderncvstyle{casual}
\moderncvcolor{blue}
\reversemarginpar
\usepackage[paper=letterpaper,
marginparwidth=1.3in,
marginparsep=.05in,
margin=1in,
includemp,
scale=0.75]{geometry}
\setlength{\parindent}{0in}

\renewcommand{\section}[1]{\pagebreak[3]%
\vspace{1.3\baselineskip}%
\noindent\raisebox{-0\height}[0pt][0pt]{\llap{%
\color{color1}\renewcommand\arrayrulewidth{1pt}%
\begin{tabular}[t]{@{}>{\hyphenpenalty=10000\raggedright}p{\marginparwidth}|}
\textcolor{color1}{\sectionfont{#1}}
\end{tabular}
}}%
\vspace{-\baselineskip}\par}

% personal data
\name{Harry}{Potter}
\title{Resumé}
\phone[mobile]{+777~(0)~777-777777}

\usepackage[utf8]{inputenc}
\title{}
\begin{document}

\makecvtitle

\section{A test section}
\cventry{2008--2010}{Lecturer}{Some Place}{Some Country}{}{Some responsabilities}

\section{Another test section}
\cventry{2008--2010}{Lecturer}{Some Place}{Some Country}{}{Some responsabilities}

\section{Yet another test section with a longer title}
\cventry{2008--2010}{Lecturer}{Some Place}{Some Country}{}{Some responsabilities}

\end{document}


• I'm getting Use of \@array doesn't match its definition. I think it doesn't like the line with @{}> in it, but I don't know what it does, so I can't try something else instead. – wvxvw Oct 13 '15 at 17:00
• Isn't \usepackage{array} missing? – egreg Oct 13 '15 at 19:50
• @egreg: No, it is indirectly loaded so no need of explicitely putting it. The class file has \RequirePackage[table]{xcolor} – user11232 Oct 13 '15 at 23:05