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:
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.
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
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:
\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}
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p