[Tex/LaTex] Retrieve length of a character

characterslengthsrules

I'm creating a custom command which basically prints an horizontal line, a character and another horizontal line. The code would be

\newcommand{\breaknote}{\noindent\rule[.5ex]{.5\textwidth}{.4pt}\S\rule[.5ex]{.5\textwidth}{.4pt}}

Obviously, this goes further than the text margins, throwing a Overful \hbox error.

The question is: is there a way to get the width of a single character and then use this number or store it in a variable like, say, \charwidth? What I want to achieve is something similar to

\newcommand{\breaknote}{\noindent\rule[.5ex]{.5\textwidth - .5\charwidth}{.4pt}\S\rule[.5ex]{.5\textwidth - .5\charwidth}{.4pt}}

This way it would fit like a charm. I am aware that I could look at the error string and get from there the actual text overflow (in pts) thus reading the character length but I was wondering if there's something cleaner.

Best Answer

\documentclass{article}
\usepackage{calc}
\usepackage{lipsum}
\usepackage[showframe]{geometry}
\newlength{\charwidth}
\setlength{\charwidth}{\widthof{\S}}
\newcommand{\breaknote}{\par\noindent\rule[.5ex]{.5\textwidth - .5\charwidth}{.4pt}\S\rule[.5ex]{.5\textwidth - .5\charwidth}{.4pt}\par}

\begin{document}
\lipsum[1]
\breaknote
\lipsum[2]

\end{document}

enter image description here

Related Question