13

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.

3 Answers 3

12
\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

12
\newlength{\charwidth}
...
\settowidth{\charwidth}{\S}

Or if package calc is used, it supports \widthof inside calculations (\setlength, \addtolength).

0
5

For the problem at hand, one could also use a combination of \hrulefill and \raisebox:

\documentclass{article}

\usepackage{lipsum}

\newcommand{\breaknote}{%
  \vspace*{-0.5ex}%
  \par\noindent\hrulefill\raisebox{-0.5ex}{\S}\hrulefill\par
  \vspace{0.5ex}%
}

\begin{document}

\breaknote

\lipsum[1]

\breaknote

\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 .