2

I'm editing a custom documentclass .cls file and would like to set section titles centered, bold and underlined.

\def\section{%
  \@startsection{section}{1}{\z@}
  {\baselineskip}{\baselineskip}
  {\centering\bfseries\underbar}}

However, \centering and \underbar does not appear to be compatible. With \underbar, section titles are underlined but not centered; without \underbar, section titles are centered but not underlined.

How to set section titles centered, bold and underlined? I've never edited a documentclass before and the solution may be trivial.

2

\underbar is not a switch, like centering, and it takes an argument, so you cannot use it the way you're trying to.

The first option is to mess with LaTeX's \@sect, which is probably more cumbersome than it's worth.

I think your best shot it titlesec with the explicit option:

\usepackage[explicit]{titlesec}
\titleformat\section[block]%
  {\bfseries\centering}
  {\thesection}
  {\baselineskip}
  {\underbar{#1}}

but as dexteritas pointed out in the comments, this will fail miserably for long section titles that require a line break. \underbar typesets its argument in an \hbox, therefore the linebreak doesn't happen. So for long section titles \underbar cannot be used.

An alternative that can handle line breaks is the \ul command from the soul package:

\usepackage{soul}
\usepackage[explicit]{titlesec}
\titleformat\section[block]%
  {\bfseries\centering}
  {\thesection}
  {\baselineskip}
  {\ul{#1}}
  • There is a problem with: \section{A very long title, witch is longer than one line and therefore needs a linebreak}. – dexteritas May 11 '18 at 12:54
  • 1
    @dexteritas Indeed. Thanks! I added a solution that handles line breaks :) – Phelype Oleinik May 11 '18 at 13:03

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.