[Tex/LaTex] Overfull hbox in a custom section title

sectioning

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

Best Answer

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