# How to draw a rule without knowing its specific width?

I am trying to improve my CV which I am writing using LaTeX. I want to show section headings something like this

But I do not know how to;

1. Add white space next to the section title, which is equal to what appears before the text as shown in the example figure.

2. Add a rule exactly the same as what is appeared in the margin, starting right after the text and ending at the end of the textwidth size. This is hard for me, because the size of the rule is different based on the text size which occupies part of the text width of the paper.

Here is the MWE which makes part of the problem.

%pdfLaTeX
\documentclass{article}

\usepackage{marginnote}
\usepackage{color}
\definecolor{darkcandyapplered}{rgb}{0.64, 0.0, 0.0}

\newcommand{\stitle}[2] {%
\noindent\textcolor{darkcandyapplered}{\large \bf #1}%
\reversemarginpar\marginpar%
{\raggedleft \textcolor{darkcandyapplered}{\rule{2.35cm}{8.5pt}} \\*[-.8pc]}%
\\[#2\baselineskip]}

\begin{document}

\stitle{Education}{0}

\end{document}


You could use the sectioning commands, with the help of titlesec:

\documentclass{article}
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{lipsum}

\definecolor{darkcandyapplered}{rgb}{0.64, 0.0, 0.0}

\titleformat{\section}
{\Large\bfseries}
{}
{0pt}
{\betweenrules}

\titlespacing*{\section}
{0pt}% space at left
{1ex}% space above
{2ex}% space below

\newcommand{\betweenrules}[1]{%
\makebox[0pt][r]{\color{darkcandyapplered}\vrule width 2cm height 1.5ex \hspace{1em}}%
#1% the title
\hspace{1em}%
\color{darkcandyapplered}%
}

\begin{document}

\lipsum[3]

\section{Education}

\lipsum[1-2]

\end{document}


Here is a simple solution, without marginnote: I just borrowed a code from @David Carlisle. Also a sans-serif font would look nicer with such heavy rules, in my opinion, and cabin matches them particularly well. You also might consider making rules the same height as lower case letters, instead of higher case:

\documentclass{article}
\usepackage[showframe, reversemp]{geometry}
\usepackage{cabin}
\usepackage{color}
\definecolor{darkcandyapplered}{rgb}{0.64, 0.0, 0.0}

\makeatletter
\makeatother

\newcommand{\stitleA}[2] {%
\noindent\textcolor{darkcandyapplered}{\large\sffamily\bfseries\llap{\rule{\marginparwidth}{8.4pt}\hspace*{\marginparsep}}\hspace*{-\fontdimen2\font}#1\hskip\marginparsep\thickrulefill[8.4pt]}%
\\[#2\baselineskip]}
\newcommand{\stitle}[2] {%
\noindent\textcolor{darkcandyapplered}{\large\sffamily\bfseries%
\llap{\rule{\marginparwidth}{1ex}\hspace*{\marginparsep}}\hspace*{-\fontdimen2\font}#1\hskip\marginparsep\thickrulefill}%
\\[#2\baselineskip]}

\begin{document}

\stitle{Education}{0}

\stitleA{Education}{0}

\end{document}


• I'd define a new command, not redefine \hrulefill. – egreg Oct 4 '15 at 17:58
• @egreg: Done. I took the opportunity to illustrate a second style. – Bernard Oct 4 '15 at 18:48
• Isn't it theoretically a problem in the first style that you switch to sffamily and bfseries after using font properties? – Hagen von Eitzen Oct 4 '15 at 20:05
• @Hagen von Eitzen: you're right. I simplified the code on the spot and forgot to take care of this point. It's corrected now. Thank you for pointing it. – Bernard Oct 4 '15 at 20:25
• I think that the horizontal white space after the text is not always the same as the white space before the text for different text value... It may be an error with my vision... Also, in some cases, the first letter of title comes outside the frame... – Enthusiastic Engineer Oct 4 '15 at 20:40
\documentclass{article}
\usepackage[showframe, reversemp]{geometry}
\usepackage{cabin}
\usepackage{xcolor}
\usepackage{xhfill}
\definecolor{darkcandyapplered}{rgb}{0.64, 0.0, 0.0}
\newcommand\stitle[1]{%
\noindent\textcolor{darkcandyapplered}{%
\hspace*{\dimexpr-\marginparwidth-\marginparsep}%
\rule{\marginparwidth}{2ex}%
\hspace{\marginparsep}\textbf{\textsf{#1}}}\hspace{\marginparsep}%
\xhrulefill{darkcandyapplered}{2ex}\par}
\begin{document}

\stitle{Education}

\end{document}


Put the title in a box and use the width of that box. For the rule in margin use \marginparwidth instead of 2.35cm.

\documentclass{article}

\usepackage{marginnote}
\usepackage{color}
\definecolor{darkcandyapplered}{rgb}{0.64, 0.0, 0.0}

\newsavebox{\mytitle}
\newcommand{\stitle}[1] {%
\sbox{\mytitle}{\textcolor{darkcandyapplered}{\large\bfseries#1}}%
\noindent\textcolor{darkcandyapplered}{%
\usebox{\mytitle}\hspace{\marginparsep}%
\rule{\dimexpr\textwidth-\marginparsep-\wd\mytitle\relax}{8.5pt}}%
\reversemarginpar\marginpar%
{\raggedleft \textcolor{darkcandyapplered}{\rule{\marginparwidth}{8.5pt}}}
}% \\*[-.8pc]}%
%\\[#2\baselineskip]}

\begin{document}

\stitle{Education}%{0}

\end{document}


Also, I didn't understand the need for second argument and those negative spaces, so I removed them. They just produce bad boxes.

Another option is to use the linegoal package and \linegoal gives the remaining width of the line:

%pdfLaTeX
\documentclass{article}

\usepackage{marginnote}
\usepackage{color}
\definecolor{darkcandyapplered}{rgb}{0.64, 0.0, 0.0}

\usepackage{linegoal}

\newcommand{\stitle}[1] {%
\noindent\textcolor{darkcandyapplered}{%
\large\bfseries#1\hspace{\marginparsep}%
\rule{\linegoal}{8.5pt}}%
\reversemarginpar\marginpar%
{\raggedleft \textcolor{darkcandyapplered}{\rule{\marginparwidth}{8.5pt}}}
}% \\*[-.8pc]}%
%\\[#2\baselineskip]}

\begin{document}

\stitle{Education}%{0}

\end{document}

• Sometimes, I need to add a \noindent syntax for the paragraph after... – Enthusiastic Engineer Oct 4 '15 at 20:50
• Also, how can I add to the space between the next line and the title line? Currently there is no space between the title line and the next line... – Enthusiastic Engineer Oct 4 '15 at 21:01

FWIW, here is a ConTeXt solution. ConTeXt already has a command \textrule that does almost exactly what is needed. \textrule{#1} starts at the left edge of text area, draws a rule of a fixed width, leaves a fixed distance, typesets #1, leaves a fixed distance, and draws a rule until the remaining available width.

\textrule starts at the edge of text area, so to get the desired result, all we need to do is change the \leftskip so that the left edge of text area is the same as left edge of the left margin. This can be done using \startnarrower.

Combining these two, here is the complete code:

\setuplayout
[
cutspace=1in,
backspace=1in,
width=middle,
leftmargin=0.5in,
rightmargin=0.5in,
]

\definecolor[darkcandyapplered][r=0.64]

\setuptextrules
[
width=\leftmarginwidth, distance=\leftmargindistance,
rulecolor=darkcandyapplered, rulethickness=1.5ex,
style=\bfb,
]

\definenarrower
[rulenarrower]
[
left=-\the\dimexpr\leftmargindistance+\leftmarginwidth\relax,
default=left,
right=0pt,
]

\define[1]\titlecommand
{\startrulenarrower\textrule{#1}\stoprulenarrower}

\showframe % To see the page boundaries

\starttext

\titlecommand{Education}

\input ward

\stoptext


which gives