1

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

2
  • You can use a `\\` inside the section title.
    – Bernard
    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?
    – johnny7
    Jul 24, 2021 at 15:19

1 Answer 1

1

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}

enter image description here

5
  • Works like a charm, thanks! Is the first raggedright (before \LARGE) obsolete in this case?
    – johnny7
    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.
    – campa
    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?
    – johnny7
    Jul 24, 2021 at 15:52
  • @johnny7 Use \parbox[t]{12cm}{...}.
    – campa
    Jul 24, 2021 at 15:55
  • Perfect, thanks, I really appreciate it!
    – johnny7
    Jul 24, 2021 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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