Increase margin in section titles with titlesec

I use the titlesec package to define section titles and use the format

\titleformat{\section}[display]
{\raggedright\LARGE}{\thesection}{1ex}{}


The font is quite large, so I would prefer to have a wider margin on the right so that I don't get

This is a long section title

but rather

This is a long

section title

How can I achieve that for all section titles? My question may be similar to this, but parbox seem to be intended for a centered box and it also does not provide a general formatting: Set margins / make box for the section title with titlesec

• You can use a \\ inside the section title. Jul 24, 2021 at 15:13
• But I'd have to in every section title manually and choose the location of the linebreak? Couldn't I let it automatically break the line if the title would be longer than xx cm? Jul 24, 2021 at 15:19

The last argument of \titleformat can contain code whose last macro takes the section title as argument. You can exploit this e.g. to put everything in a \parbox of given width.

\documentclass{article}

\usepackage{titlesec}
\titleformat{\section}[display]%
{\raggedright\LARGE}%
{\thesection}%
{1ex}% vertical space after label
{\sectionformat}% before code
[\vspace{1ex}]% after code (add some mor vertical space)
\newcommand*{\sectionformat}[1]{\parbox{5cm}{\raggedright#1}}

\begin{document}

\section{This is a long section title}
Bla bla bla

\end{document}


• Works like a charm, thanks! Is the first raggedright (before \LARGE) obsolete in this case? Jul 24, 2021 at 15:34
• @johnny7 Oh, yes, I believe so. It would make a difference if you defined the label in a strange way, but in this case it makes no difference. Jul 24, 2021 at 15:36
• I tried the same procedure for subsections, for which I don't use the display option: \titleformat{\subsection} {\Large}{\thesubsection}{1ex}{\subsectionformat} [\vspace{1cm}] \newcommand*{\subsectionformat}[1]{\parbox{12cm}{\raggedright#1}} But this doesn't work as expected because the subsection number is not aligned with the first line of the title, it is centered between the two lines. Any ideas? Jul 24, 2021 at 15:52
• @johnny7 Use \parbox[t]{12cm}{...}. Jul 24, 2021 at 15:55
• Perfect, thanks, I really appreciate it! Jul 24, 2021 at 15:57