我正在尝试更改章节标题的背景颜色。我希望整个页面顶部(包括顶部和右侧边距)变为灰色,直到标题规则为止。
目前它们看起来像这样:
\documentclass[10pt,twoside,openright]{book}
\usepackage{titlesec}
\titleformat{\chapter}[display]{\LARGE\sffamily\bfseries\raggedright}
{\leftskip2cm {\Large\textmd{\chaptertitlename~\thechapter}}}
{0cm}{\leftskip2cm}[\vspace{4mm} {\color{dgray}\titlerule[.6mm]} \enlargethispage{-\parskip}]
用于\enlargethispage
为标题编号留出一些额外空间,标题编号仅在章节第一页的页面底部显示。我尝试使用环境framed
或,但colorbox
迄今为止尚未成功。
答案1
由于按照您的要求,我的其他答案做了大量更改,因此我决定撰写一份新答案,而不是编辑旧答案。请看这里。
我从这里获取了\blap
和的宏定义:\tlap
google 群组
\documentclass[10pt,twoside,openright]{book}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{eso-pic}
\usepackage{geometry}
\newcommand\chapterBox{%
\put(0,0){%
\parbox[b][\paperheight]{\paperwidth}{%
\color{blue}\rule{\paperwidth}{8.1cm}
\vfill
}
}}
\def\tlap#1{\vbox to 0pt{\vss#1}}
\def\blap#1{\vbox to 0pt{#1\vss}}
\titleformat{\chapter}[display]
{\LARGE\sffamily\bfseries\raggedright}
{}
{0cm}
{\AddToShipoutPicture*{\chapterBox}
\tlap{\parbox[b]{\linewidth}{%
\Large\textmd{\chaptertitlename~\thechapter}\newline
\Huge\bfseries #1
}}
}
[{\color{red}\titlerule[.6mm]}\enlargethispage{-\parskip}]
\begin{document}
\chapter{My chapter}
text
\chapter{My extremely long second chapter heading}
\chapter{My last and even longer chapter heading that is so long, it does actually span three lines}
text
\clearpage
text
\end{document}
答案2
这是一个使用 TikZ 的解决方案(通过tikzpagenodes
)。在这里,未编号的章节(ToC、LoF、LoT、\chapter*)将按预期运行,使用键numberless
from titlesec
(在示例代码中,我将其从 更改openright
为openany
仅适用于商品):
\documentclass[10pt,twoside,openany,draft]{book}
\usepackage{tikzpagenodes}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}
\titleformat{\chapter}[display]
{\LARGE\sffamily\bfseries\raggedright}
{}{0cm}
{\begin{tikzpicture}[remember picture,overlay]
\fill[gray!20]
(current page.north west) rectangle ([yshift=-7cm]current page.north east);
\node[inner sep=0pt,anchor=south west] (title) at
([yshift=-6.5cm]current page text area.west|-current page.north)
{\parbox[t]{2cm}{\mbox{}}%
\parbox[t]{\dimexpr\linewidth-2cm\relax}{\raggedright#1}};
\node[inner sep=0pt,anchor=north west] (label) at
([yshift=-4cm]current page text area.west|-current page.north)
{\hspace*{2cm}{\Large\textmd{\chaptertitlename~\thechapter}}};
\node[inner sep=0pt,anchor=north west] at ([yshift=-7cm]current page.north west)
{\color{gray}\rule{\paperwidth}{.6mm}};
\end{tikzpicture}%
}
[\enlargethispage{-\parskip}]
\titleformat{name=\chapter,numberless}[display]
{\LARGE\sffamily\bfseries\raggedright}
{}{0cm}
{\begin{tikzpicture}[remember picture,overlay]
\fill[gray!20]
(current page.north west) rectangle ([yshift=-7cm]current page.north east);
\node[inner sep=0pt,anchor=south west] (title) at
([yshift=-6.5cm]current page text area.west|-current page.north)
{\parbox[t]{2cm}{\mbox{}}%
\parbox[t]{\dimexpr\linewidth-2cm\relax}{\raggedright#1}};
\node[inner sep=0pt,anchor=north west] at ([yshift=-7cm]current page.north west)
{\color{gray}\rule{\paperwidth}{.6mm}};
\end{tikzpicture}%
}
[\enlargethispage{-\parskip}]
\titlespacing*{\chapter}
{0pt}{50pt}{10pt}
\begin{document}
\tableofcontents
\chapter{Test chapter}
\lipsum[4]
\chapter[Test chapter]{Test chapter with a really long title just for the example}
\lipsum[4]
\chapter*{Test unnumbered chapter}
\end{document}
一些放大的图像:
编号章节:
答案3
我尝试了一下,结果比我想象的要难。不过,这是我的建议:
您可能需要调整标有 %* 的行的长度以适合您的页面几何形状。
另外,因为我手头没有好看的深灰色,所以我就用了蓝色。当然你可以blue
用 来替换dgray
,但我还是保留原样,这样其他人也可以编译代码。
我对我不需要这样做感到有点惊讶\ClearShipoutPicture
,但无论如何......如果彩色框也出现在其他页面上,您可能希望将其包含在最后一个参数中\titleformat
。
\documentclass[10pt,twoside,openright]{book}
\usepackage{titlesec}
\usepackage{color}
\usepackage{eso-pic}
\usepackage{geometry}
\newcommand\chapterBox{%
\put(0,0){%
\parbox[b][\paperheight]{\paperwidth}{%
\color{blue}\rule{\paperwidth}{7.5cm} %*
\vfill
}
}}
\titleformat{\chapter}[display]
{\LARGE\sffamily\bfseries\raggedright}
{\leftskip2cm\Large\textmd{\chaptertitlename~\thechapter}}
{0cm}
{\AddToShipoutPicture*{\chapterBox}\leftskip2cm}
[\vspace{4mm}{\color{red}\titlerule[.6mm]}\enlargethispage{-\parskip}]
\begin{document}
\chapter{My chapter}
text
\clearpage
text
\end{document}
答案4
以 Carstens 的回答为基础,我开始改变自己的风格。我现在使用一个大的章节编号,后面跟着一个垂直居中的标题,两者都是白色的,背景是灰色的。
确实需要进行一些修改,如果我知道无数个选项的话,这些修改本来可以更容易地完成。
由于标题的长度差异,我决定使用两种不同的字体大小。一种用于“目录”、“简介”和“结论”等短标题,另一种用于较长的多行章节标题。感谢你们的帮助!
\usepackage[explicit]{titlesec}
\usepackage{eso-pic}
\definecolor{lgray}{gray}{.5}
\newcommand{\bsf}{\sffamily\bfseries}
\newcommand\chapterBox{%
\put(0,0){%
\parbox[b][\paperheight]{\paperwidth}{%
\color{lgray}\rule{\paperwidth}{75mm}\vspace{-5mm}
\color{black}\rule{\paperwidth}{.5mm}
\vfill }}}
\def\tlap#1{\vbox to 0pt{\vss#1}}
%\def\blap#1{\vbox to 0pt{#1\vss}}
\newcommand{\chapternum}{%
\ifnum\value{chapter}>0
\thechapter%
\else
{ }%
\fi}
\newcommand{\chaptersize}{%
\ifnum\value{chapter}<2
\fontsize{.75cm}{.75cm}\selectfont%
\else
\ifnum\value{chapter}>5
\fontsize{.75cm}{.75cm}\selectfont%
\else
\fontsize{.64cm}{.64cm}\selectfont%
\fi
\fi}
\titleformat{\chapter}[display]
{\sffamily}{\vspace{-9mm}}{0mm}{\vspace*{-10mm}%
\AddToShipoutPicture*{\chapterBox}
\tlap{\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} m{2cm} m{9.65cm}}
\color{white}\fontsize{3cm}{3cm}\selectfont\bsf{\chapternum} &\parbox[c][4cm] {9.66cm}
{\color{white}\chaptersize\bsf\raggedright{#1}}
\end{tabular*}}}
[\enlargethispage{-\parskip}]
编辑:我现在大大简化了代码。下面在白色标题周围创建了一个深灰色边框。其中章节号采用大字体,而章节标题采用旁边的小字体。
\usepackage[explicit]{titlesec}
\usepackage{eso-pic}
\newcommand{\bsf}{\sffamily\bfseries}
\newcommand{\chapternum}{
\ifnum\value{chapter}>0
\thechapter \fi}
\newcommand{\chaptersize}{
\ifnum\value{chapter}<2
\fontsize{.75cm}{.75cm}\selectfont%
\else
\ifnum\value{chapter}>5
\fontsize{.75cm}{.75cm}\selectfont%
\else
\fontsize{.64cm}{.75cm}\selectfont%
\fi
\fi}
\titleformat{\chapter}[display]
{}{}{0mm}
{\AddToShipoutPicture*{\parbox[b][\paperheight]{\paperwidth}{ \vspace{22mm}
\color{black!50}\rule{\paperwidth}{45mm}\vspace{-\baselineskip}
\color{black} \rule{\paperwidth}{.3mm}\vspace{\dimexpr-45mm-\baselineskip}
\color{black} \rule{\paperwidth}{.3mm}
\parbox[c][45mm]{\paperwidth}{
\begin{tabular}{m{2cm} m{2.2cm} m{10cm}}
& \color{white}\fontsize{35mm}{35mm}\selectfont\bsf{\chapternum} & \parbox[c][45mm]{10cm}
{ \color{white}\chaptersize\bsf\raggedright{#1}}
\end{tabular}} \vfill }}}
[\vspace{-2mm}\enlargethispage{-\parskip}]