将章节标题拆分为两行或更多行第 2 部分

将章节标题拆分为两行或更多行第 2 部分

这篇文章是根据找到的帖子构建的这里List of Algorithms。请帮我开发一个解决方案,将像和这样的“无数”章节List of Matlab Codes显示为:

  • 第 1 行列表
  • 第 2 行算法

而不是显示为:

  • 第 1 行 Al- 列表
  • 第 2 行算法

  • 第 1 行列表
  • 第 2 行 Matlab 代码

而不是显示为:

  • 第 1 行材料清单
  • 2 号线实验室代码

这不会影响其他章节标题的当前显示方式。也就是说,单词不会像“Mat-lab”那样分成两行。

我尝试调整代码text width=.72\paperwidth,但仍然无法在不破坏其他章节标题的情况下找到令人满意的解决方案。

我基本上不希望章节标题中的单词被拆分。感谢您的时间和帮助。

以下是代码:

\documentclass{book}
\usepackage{tikz}
\usepackage{fourier}
\usepackage{listings}
\usepackage{algorithm2e}

\usepackage[explicit,calcwidth]{titlesec}
\usepackage{lipsum}

\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueii}{RGB}{63,200,244}
\definecolor{myblueiii}{RGB}{199,234,253}

\renewcommand\thechapter{\arabic{chapter}}

\newcommand\chapnumfont{%
  \fontsize{60}{72}\selectfont\color{myblueii}\scshape\bfseries%
}

\newcommand\chapnamefont{%
  \normalfont\color{white}\scshape\small\bfseries
}

\titleformat{\chapter}
  {\normalfont\huge\filleft}
  {}
  {0pt}
  {%
  \begin{tikzpicture}[remember picture,overlay]
  \fill[myblueiii]
    (current page.north west) rectangle ([yshift=-10cm]current page.north east);
  \node[
      fill=mybluei,
      text width=2\paperwidth,
      rounded corners=6cm,
      text depth=13cm,
      anchor=center,
      inner sep=0pt] at (current page.north east) (chaptop)
    {\thechapter};%
    {};
  \node[
      text width=.7\paperwidth,
      align=right,
      font=\chapnumfont,
      anchor=south east,
      inner sep=0pt,
      outer sep=0pt] (chapnum) at ([xshift=-20pt]chaptop.south)
    {#1};
    {};
  \node[
      anchor=south,
      inner sep=0pt] (chapname) at ([yshift=2pt]chapnum.south)
  {};
  \end{tikzpicture}%
  }

\titleformat{name=\chapter,numberless}
  {\normalfont\huge\filleft}
  {}
  {0pt}
  {\begin{tikzpicture}[remember picture,overlay]
  \fill[myblueiii]
    (current page.north west) rectangle ([yshift=-10cm]current page.north east);
  \node[
      fill=mybluei,
      text width=2\paperwidth,
      rounded corners=6cm,
      text depth=13cm,
      anchor=center,
      inner sep=0pt] at (current page.north east) (chaptop)
    {};%
  \node[
      text width=0.7\paperwidth,
      align=right,
      font=\chapnumfont,
      anchor=south east,
      inner sep=0pt,
      outer sep=0pt] (chapnum) at ([xshift=-40pt]chaptop.south)
    {#1};
    {};
  \node[
      anchor=south,
      inner sep=0pt] (chapname) at ([yshift=2pt]chapnum.south)
  {};
  \end{tikzpicture}%
  }

\renewcommand{\lstlistingname}{Matlab Code}
\renewcommand{\lstlistlistingname}{List of \lstlistingname s\\}

\begin{document}

\pagestyle{empty}

\tableofcontents

\lstlistoflistings

\listofalgorithms

\chapter{Problem}

\lipsum[1]
\section{Problem Statement 1}
\lipsum[1]

\renewcommand{\chaptername}{Appendix}
\renewcommand{\leftmark}{\sffamily\normalsize\bfseries \appendixname~\thechapter}
\appendix
\chapter{References}

\end{document} 

答案1

嗯,我不确定您是否想自动实现这一点。

因为只需要更改章节标题,所以您可以使用例如命令\mbox{...}。命令\mbox不允许对包含的单词进行连字符连接...。另一种可能性是只添加\\,但您可能会遇到目录或标题问题,因为您不希望出现换行符。或者,如果您使用 hyperref,您将收到一条错误消息,提示\\不允许将其作为书签中的标记。

因此您可以执行以下操作:

\chapter{List of \mbox{Matlab} Codes}

如果您只想禁止使用连字符Matlab

或者你可以使用:

\chapter[List of Algorithms]{List of \mbox{Algorithms}}

然后[...]用于页眉和目录,并{...}用于打印章节标题。

完整的 MWE:

\documentclass{book}
\usepackage{tikz}
\usepackage{fourier}

\usepackage[explicit,calcwidth]{titlesec}
\usepackage{lipsum}

\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueii}{RGB}{63,200,244}
\definecolor{myblueiii}{RGB}{199,234,253}

\renewcommand\thechapter{\arabic{chapter}}

\newcommand\chapnumfont{%
  \fontsize{60}{72}\selectfont\color{myblueii}\scshape\bfseries%
}

\newcommand\chapnamefont{%
  \normalfont\color{white}\scshape\small\bfseries
}

\titleformat{\chapter}
  {\normalfont\huge\filleft}
  {}
  {0pt}
  {%
  \begin{tikzpicture}[remember picture,overlay]
  \fill[myblueiii]
    (current page.north west) rectangle ([yshift=-10cm]current page.north east);
  \node[
      fill=mybluei,
      text width=2\paperwidth,
      rounded corners=6cm,
      text depth=13cm,
      anchor=center,
      inner sep=0pt] at (current page.north east) (chaptop)
    {\thechapter};%
    {};
  \node[
      text width=.7\paperwidth, 
      align=right, 
      font=\chapnumfont,
      anchor=south east,
      inner sep=0pt,
      outer sep=0pt] (chapnum) at ([xshift=-20pt]chaptop.south)
    {#1};
    {};
  \node[
      anchor=south,
      inner sep=0pt] (chapname) at ([yshift=2pt]chapnum.south)
  {};
  \end{tikzpicture}%
  }

\titleformat{name=\chapter,numberless}
  {\normalfont\huge\filleft}
  {}
  {0pt}
  {\begin{tikzpicture}[remember picture,overlay]
  \fill[myblueiii]
    (current page.north west) rectangle ([yshift=-10cm]current page.north east);
  \node[
      fill=mybluei,
      text width=2\paperwidth,
      rounded corners=6cm,
      text depth=13cm,
      anchor=center,
      inner sep=0pt] at (current page.north east) (chaptop)
    {};%
  \node[
      text width=1\paperwidth,
      align=right,
      font=\chapnumfont,
      anchor=south east,
      inner sep=0pt,
      outer sep=0pt] (chapnum) at ([xshift=-40pt]chaptop.south)
    {#1};
    {};
  \node[
      anchor=south,
      inner sep=0pt] (chapname) at ([yshift=2pt]chapnum.south)
  {};
  \end{tikzpicture}%
  }

\begin{document}

\pagestyle{empty}

\tableofcontents

\listoffigures

\listoftables

\chapter{Problem}

\lipsum[1]
\section{Problem Statement 1}
\lipsum[1]

\chapter{List of \mbox{Matlab} Codes}
\section{Algorithm 1}
\lipsum[1]

\chapter[List of Algorithms]{List of \mbox{Algorithms}}
\section{Algorithm 1}
\lipsum[1]

\renewcommand{\chaptername}{Appendix}
\renewcommand{\leftmark}{\sffamily\normalsize\bfseries \appendixname~\thechapter}
\appendix
\chapter{References}

\end{document} 

结果:

一页的结果

更新:

如果你想自动执行此操作,你可以使用以下行

\hyphenpenalty=10000
\exhyphenpenalty=10000

抑制章节名称中的连字符。请参阅以下 MWE 以了解如何添加它:

\documentclass{book}
\usepackage{tikz}
\usepackage{fourier}

\usepackage[explicit,calcwidth]{titlesec}
\usepackage{lipsum}

\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueii}{RGB}{63,200,244}
\definecolor{myblueiii}{RGB}{199,234,253}

\renewcommand\thechapter{\arabic{chapter}}

\newcommand\chapnumfont{%
  \fontsize{60}{72}\selectfont\color{myblueii}\scshape\bfseries%
}

\newcommand\chapnamefont{%
  \normalfont\color{white}\scshape\small\bfseries
}

\titleformat{\chapter}
  {\normalfont\huge\filleft}
  {}
  {0pt}
  {% <==============================================================
  \hyphenpenalty=10000
  \exhyphenpenalty=10000
  \begin{tikzpicture}[remember picture,overlay]
  \fill[myblueiii]
    (current page.north west) rectangle ([yshift=-10cm]current page.north east);
  \node[
      fill=mybluei,
      text width=2\paperwidth,
      rounded corners=6cm,
      text depth=13cm,
      anchor=center,
      inner sep=0pt] at (current page.north east) (chaptop)
    {\thechapter};%
    {};
  \node[
      text width=.7\paperwidth, 
      align=right, 
      font=\chapnumfont,
      anchor=south east,
      inner sep=0pt,
      outer sep=0pt] (chapnum) at ([xshift=-20pt]chaptop.south)
    {#1};
    {};
  \node[
      anchor=south,
      inner sep=0pt] (chapname) at ([yshift=2pt]chapnum.south)
  {};
  \end{tikzpicture}%
  }

\titleformat{name=\chapter,numberless}
  {\normalfont\huge\filleft}
  {}
  {0pt}
  {% <======================================================================
  \hyphenpenalty10000
  \exhyphenpenalty10000
  \begin{tikzpicture}[remember picture,overlay]
  \fill[myblueiii]
    (current page.north west) rectangle ([yshift=-10cm]current page.north east);
  \node[
      fill=mybluei,
      text width=2\paperwidth,
      rounded corners=6cm,
      text depth=13cm,
      anchor=center,
      inner sep=0pt] at (current page.north east) (chaptop)
    {};%
  \node[
      text width=1\paperwidth,
      align=right,
      font=\chapnumfont,
      anchor=south east,
      inner sep=0pt,
      outer sep=0pt] (chapnum) at ([xshift=-40pt]chaptop.south)
    {#1};
    {};
  \node[
      anchor=south,
      inner sep=0pt] (chapname) at ([yshift=2pt]chapnum.south)
  {};
  \end{tikzpicture}%
  }

\begin{document}

\pagestyle{empty}

\tableofcontents

\listoffigures

\listoftables

\chapter{Problem}

\lipsum[1]
\section{Problem Statement 1}
\lipsum[1]

\chapter{List of Matlab Codes}
%\chapter{List of \mbox{Matlab} Codes}
\section{Algorithm 1}
\lipsum[1]

\chapter{List of Algorithms}
%\chapter[List of Algorithms]{List of \mbox{Algorithms}}
\section{Algorithm 1}
\lipsum[1]

\renewcommand{\chaptername}{Appendix}
\renewcommand{\leftmark}{\sffamily\normalsize\bfseries \appendixname~\thechapter}
\appendix
\chapter{References}

\end{document} 

结果如上所示......

相关内容