# dynamic title size using titlesec

Is there a way to dynamically size titles as part of a \titleformat (from the titlesec package) definition? I'd like to scale titles of different lengths to be the same width (relative to \textwidth) by only changing fontsize.

The answers to this question describe how to resize text in a similar way, but I'm not sure how this could be used to redefine/extend the section titles. (e.g. as a part of \titleformat*{\subsection} ...)

• Did you try to use \resizebox? – Sigur May 21 '15 at 10:13
• @Sigur not sucessfully. how would it work? – zzkt May 21 '15 at 11:00
• For instance, you'd want that \section{Abc} and \section{Abc def ghi jkl} typeset the text filling the same width? – egreg May 21 '15 at 15:14
• @egreg. correct. – zzkt May 22 '15 at 8:24
• It doesn't seem like a good idea. – egreg May 22 '15 at 8:39

## 1 Answer

It doesn't seem like a good idea.

\documentclass{article}
\usepackage{titlesec,graphicx}

\titleformat{\section}
{\Large\bfseries}
{\thesection}
{1em}
{\resizetitle}

\newcommand{\resizetitle}[1]{\resizebox{\sectionwidth}{!}{#1}}
\newlength{\sectionwidth}
\AtBeginDocument{%
\setlength{\sectionwidth}{.3\textwidth}%
}

\begin{document}

\section{Abc}

\section{Def ghi klm}

\section{Lorem ipsum dolor sit amet}

\end{document}


One could scale also the numbers, but the overall result doesn't seem worthy a try.

• thanks for providing an answer, despite your reservations. it works as expected. – zzkt May 22 '15 at 8:57