Page LayoutCalculating the Width of a Section Title

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
onidavin
Posts: 9
Joined: Thu Apr 29, 2010 11:12 pm

Calculating the Width of a Section Title

Post by onidavin »

Hello,

This is perhaps a simple question -- I'm trying to calculate the width of a section title so that I can make a \rule of the appropriate width. I'm using titlesec, and tried doing:

Code: Select all

\newlength{\secwid}
\settowidth{\secwid}{\sectiontitle}
But the width ends up as 0. I can use \thesection instead, and it'll give me the width of the section label. Which is nice, because I'll be able to add that value to the section title value and have the width I need! But getting the section title value is proving very difficult.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Calculating the Width of a Section Title

Post by gmedina »

Hi,

the titlesec package offers the \titlewidth command; refer to the package documentation for further information.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
onidavin
Posts: 9
Joined: Thu Apr 29, 2010 11:12 pm

Re: Calculating the Width of a Section Title

Post by onidavin »

\titlewidth is one of the first things I tried, but it seems to return 0 -- the \rule ends up not drawing at all.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Calculating the Width of a Section Title

Post by gmedina »

onidavin wrote:\titlewidth is one of the first things I tried, but it seems to return 0 -- the \rule ends up not drawing at all.
It seems that you forgot to load titlesec with the calcwidth option. In the following example the section title is centered, with a dotted rule above it (the length of the rule depends on \titlewidth), and the length given by \titlewidth will be typeset just below the tile:

Code: Select all

\documentclass{article}
\usepackage[calcwidth]{titlesec}

\titleformat{\section}[block]
  {\filcenter\large
   \titleline*[c]{\titlerule*[.6pc]{\tiny\textbullet}}%
   \addvspace{6pt}%
   \normalfont\sffamily}
  {\thesection}{1em}{}[\addvspace{10pt}\the\titlewidth]
\titlespacing{\section}
  {5pc}{*2}{*2}[5pc]

\begin{document}

\section{Some short title}
\section{Some not so short title}
\section{Some really long, long title}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
onidavin
Posts: 9
Joined: Thu Apr 29, 2010 11:12 pm

Re: Calculating the Width of a Section Title

Post by onidavin »

Works like a charm. Thanks!
Post Reply