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

3
在常规的bookdown格式中,每个章节都以粗体字开头:

第一章

章节标题

我想将“第一章”的颜色更改为深灰色,而不是黑色。我完全不懂LaTeX,但根据其他Stackoverflow问题组合了一些代码来自定义颜色。我创建了一个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头部是:

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 章节标题

2个回答

1

ekstroem 帮助我正确使用了 sectsty 包。我尝试使用 sectsty 包中的 \chapternumberfont,但没有成功。我认为 Bookdown 没有将章节编号名称设置为这个特定的名称。我找到了一个有效的解决方法,即将整个章节设置为灰色,然后将章节标题设置为黑色:

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

1
您可以尝试以下方法,使用mystyes.sty中的xcolorsectsty包:
\usepackage{xcolor}
\usepackage{sectsty}
\definecolor{battleshipgrey}{rgb}{0.52, 0.52, 0.51}
\chapterfont{\color{battleshipgrey}}  % sets colour of chapters                                                                                

那在我的端上似乎可以工作,并且会生成不带编号的灰色章节标题。

非常抱歉回复晚了,我有一段时间无法在这个项目上工作。对于我来说,这会将“第1章”和“章节标题”都设置为灰色,而我只想让它适用于“第1章”部分。我将研究sectsty包,看看能否进一步解决问题。 - kneijenhuijs
我尝试使用sectsty包中的\chapternumberfont,但没有成功。我认为Bookdown没有将章节编号的名称设置为这个特定的名称。我找到了一个解决方法,通过将整个章节设置为灰色,然后将章节标题设置为黑色:\chapterfont{\color{battleshipgrey}} \chaptertitlefont{\color{black}} - kneijenhuijs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接