在 r bookdown 中更改章节颜色(pdf 输出)

Changing Chapter colors in r bookdown (pdf output)

在常规的 bookdown 格式中,每一章都以粗体开头:

第 1 章

章节标题

我正在尝试将 "Chapter 1" 的颜色从黑色改为深灰色。我对 LaTeX 完全陌生,但是根据其他 Whosebug 问题将一些代码拼凑在一起,以自定义颜色。我创建了一个 mystyles.sty 文件,其中包含:

\usepackage{titlesec}
\usepackage{xcolor}

\definecolor{battleshipgrey}{rgb}{0.52, 0.52, 0.51}
\titleformat{\thechapter}
{\color{battleshipgrey}\normalfont\Large\bfseries}
{\color{battleshipgrey}\chapter}{1em}{}

我的 YAML header 是:

title: "My Title"
author: "Me"
date: ""
output: pdf_document
bibliography: [bib.bib]
documentclass: book
geometry: left=4cm, right=3cm, top=2.5cm, bottom=2.5cm
link-citations: yes
classoption: openany
biblio-style: apalike
subparagraph: true

我有一个 _output.yml,代码如下:

bookdown::pdf_book:
  includes:
    in_header: mystyles.sty
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: no
mainfont: Bookman

我在 mystyles.sty 文档中指定的错误是什么?目前,颜色没有任何变化。我试过指定:

\titleformat{\chapter}
{\color{battleshipgrey}\normalfont\Large\bfseries}
{\color{battleshipgrey}\thechapter}{1em}{}

但这会使章节编号和标题变成灰色,但章节标题的格式会更改为:

1 章标题

您可以尝试以下使用 mystyes.sty 中的 xcolorsectsty 包的方法:

\usepackage{xcolor}
\usepackage{sectsty}
\definecolor{battleshipgrey}{rgb}{0.52, 0.52, 0.51}
\chapterfont{\color{battleshipgrey}}  % sets colour of chapters                                                                                

无论如何,这似乎对我有用,并且会生成没有数字的灰色章节标题。

ekstroem 让我走上了使用 sectsty 包的正确轨道。我尝试使用 sectsty 包中的 \chapternumberfont,但没有用。我认为 Bookdown 没有将章节编号的名称设置为这个特定名称。我找到了一个可行的解决方法,方法是将整个章节设置为灰色,然后将章节标题设置为黑色:

\usepackage{xcolor}
\usepackage{sectsty}
\definecolor{battleshipgrey}{rgb}{0.52, 0.52, 0.51}
\chapterfont{\color{battleshipgrey}}
\chaptertitlefont{\color{black}}