Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 11 down vote accepted
\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

share|improve this answer
\newlength{\charwidth}
...
\settowidth{\charwidth}{\S}

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.