自定义部分标题中的水平盒子过满

自定义部分标题中的水平盒子过满

我使用自定义章节标题,对于长标题,有时会产生过满的 \hbox。显式连字符没有帮助。根据这个回答我插入了\\换行符来分隔章节标题,但第一行却不对齐,更糟糕的是,相应的目录条目也包含换行符。有没有更好的方法来解决这个问题?

以下是示例:

\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}

在此处输入图片描述

答案1

您使用的格式相互矛盾,例如scshape\MakeTextUppercase。您只能使用其中之一。此外,正如 egreg 和 Gonzalo Medina 所评论的那样,您可以\raggedright在 之后使用\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
}%

如果希望它们为大写,请\MakeTextUppercase在 的位置添加scshape

代码:

\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}

在此处输入图片描述

相关内容