3

I use custom section headings, which for long titles occasionally create an overfull \hbox. Explicit hyphenation does not help. Per this answer I insert \\ to break the section title, but then the first line is not justified and what is worse, the corresponding TOC entry also includes the line break. Is there a better way to address this issue?

Here is the example:

\documentclass{memoir}
\usepackage[margin=20mm]{geometry}
\usepackage{lipsum}
\usepackage{fourier}
\usepackage[explicit]{titlesec}

\makeatletter
\define@key{mosquito}{subtitle}{\def\mosquito@subtitle{#1}}
\newcommand\sectionsubtitlefont{\normalfont\huge}

\titlespacing\section{0pt}{12pt plus 4pt minus 2pt}{-5pt plus 5pt}

\titleformat{\section}
  { \rmfamily \scshape}{}{0em}{{%
  % subtitle
  \ifx\mosquito@subtitle\@empty\else
{\sectionsubtitlefont\mosquito@subtitle}
\vskip\medskipamount
\leaders\vrule width \textwidth\vskip0.4pt
\vskip\medskipamount
\nointerlineskip
  \fi
  % title
  {\HUGE\bfseries{\MakeTextUppercase{#1}}}
  \ifx\mosquito@subtitle\@empty
    \vskip\medskipamount
\leaders\vrule width \textwidth\vskip0.4pt  
\vskip\medskipamount
\nointerlineskip
\else
\vspace{0.15in}
  \fi
}%
  }
\newcommand{\Section}[1][]{%
  \setkeys{mosquito}{subtitle={},#1}%
  \section}
\makeatother

\setsecnumdepth{book}

\begin{document}
\tableofcontents
\vspace{0.4in}

\Section{This is a very long title, whose string overflows (v1)}
\lipsum[4]

\vspace{0.2in}
\Section[subtitle={This is a subtitle}]{This is a very long title, whose string overflows (v2)}
\lipsum[4]

\vspace{0.2in}
\Section{This is a very long title, whose\\ string overflows (v3)}
\lipsum[4]

\end{document}

enter image description here

m0squito
  • 578
  • 7
  • 18
  • Adding \raggedright to the definition of \sectionsubtitlefont? – egreg Feb 21 '14 at 21:58
  • It doesn't seem to change anything. Perhaps I didn't do it right? \newcommand\sectionsubtitlefont{\normalfont\huge\raggedright} – m0squito Feb 21 '14 at 22:18
  • For anyone else finding this, if it is a one off, using \\ to break the heading and the optional first argument of the \section command to provide the toc entry worked for me as a quick fix, i.e. \Section[This is a very long title, whose string overflows (v3)]{This is a very long title, whose\\ string overflows (v3)}. It's not pretty, but if it's just the odd place it may be ok. – zelanix Apr 06 '17 at 09:35

1 Answers1

2

You are using contradictory formatting like scshape and \MakeTextUppercase. You either use one of them. Further, as commented by egreg and Gonzalo Medina, you can use \raggedright just after \scshape:

\titleformat{\section}
  {\rmfamily\scshape\raggedright}{}{0em}{%
  % subtitle
  \ifx\mosquito@subtitle\@empty\else
{\sectionsubtitlefont\mosquito@subtitle}
\vskip\medskipamount
\leaders\vrule width \textwidth\vskip0.4pt
\vskip\medskipamount
\nointerlineskip
  \fi
  % title
  {\HUGE\sloppy\bfseries #1}      %%% No \MakeTextUppercase
  \ifx\mosquito@subtitle\@empty
    \vskip\medskipamount
\leaders\vrule width \textwidth\vskip0.4pt
\vskip\medskipamount
\nointerlineskip
\else
\vspace{0.15in}
  \fi
}%

If you want them to be upper case add \MakeTextUppercase in the place of scshape.

Code:

\documentclass{memoir}
\usepackage[margin=20mm]{geometry}
\usepackage{lipsum}
\usepackage{fourier}
\usepackage[explicit]{titlesec}

\makeatletter
\define@key{mosquito}{subtitle}{\def\mosquito@subtitle{#1}}
\newcommand\sectionsubtitlefont{\normalfont\huge}

\titlespacing\section{0pt}{12pt plus 4pt minus 2pt}{-5pt plus 5pt}

\titleformat{\section}
  {\rmfamily\scshape\raggedright}{}{0em}{%
  % subtitle
  \ifx\mosquito@subtitle\@empty\else
{\sectionsubtitlefont\mosquito@subtitle}
\vskip\medskipamount
\leaders\vrule width \textwidth\vskip0.4pt
\vskip\medskipamount
\nointerlineskip
  \fi
  % title
  {\HUGE\sloppy\bfseries #1}
  \ifx\mosquito@subtitle\@empty
    \vskip\medskipamount
\leaders\vrule width \textwidth\vskip0.4pt
\vskip\medskipamount
\nointerlineskip
\else
\vspace{0.15in}
  \fi
}%
\newcommand{\Section}[1][]{%
  \setkeys{mosquito}{subtitle={},#1}%
  \section}%
\makeatother

\setsecnumdepth{book}

\begin{document}
\tableofcontents
\vspace{0.4in}

\Section{This is a very long title, whose string overflows (v1)}
\lipsum[4]

\vspace{0.2in}
\Section[subtitle={This is a subtitle}]{This is a very long title, whose string overflows (v2)}
\lipsum[4]

\vspace{0.2in}
\Section{This is a very long title, whose\\ string overflows (v3)}
\lipsum[4]

\end{document}

enter image description here

  • 2
    This doesn't solve the problem; change the margins to 40mm (instead of 20mm) and you'll see the problem reappear. The best thing to do is to add \raggedright as in \titleformat{\section} {\rmfamily\scshape\raggedright}{}{0em}{...} – Gonzalo Medina Feb 22 '14 at 04:25
  • @GonzaloMedina Your solution is the best one! – m0squito Feb 22 '14 at 04:38
  • @GonzaloMedina Thanks. I knew that won't solve the problem. But egreg has laready commented about \raggedright above. Hence I thought, he will give an answer. Not to be intrusive :). Now that you caught me, I will add it to the answer. Hope that it will be OK. –  Feb 22 '14 at 06:32