如何在Quarto书中插入单词”Chapter”,后跟章节编号?

huangapple go评论82阅读模式
英文:

How to insert the word "Chapter" followed by the chapter number in a Quarto book?

问题

我试图在RStudio中使用Quarto创建一个HTML书籍。我将书的每一章设置为单独的.qmd文件,并在_quarto.yml文件中列出了所有章节。默认情况下,Quarto将章节标题格式化为"X MyTitle",其中X是章节编号(例如,1、2、3...),MyTitle是标题(h1)(在每个章节的.qmd文件开头使用# MyTitle定义)。我想改变这种行为,使每个章节自动编号,并显示为"第X章:MyTitle"。在使用knitrbookdown时,通过修改yaml文件,这很容易实现,例如:

book_filename: "machine-learning-causal-inference-with-bookdown"
language:
  ui:
    chapter_name: "第"

然而,在Quarto中似乎不再有效(我尝试过,它不起作用,也不会被R Studio的IDE IntelliSense/自动补全识别)。我查看了Quarto书籍选项,但未找到简单的方法来“重构”章节标题的呈现方式。我希望能够复制来自已修改章节标题的示例Quarto书籍的代码,但遗憾的是,文档中提供的所有示例都指向使用默认章节标题实现的书籍(例如R for Data Science、Python for Data Analysis和Visualization Curriculum)。

所以,有没有人知道一个相对简单的方法来实现这个目标?如果没有,我认为我可能可以通过自定义层叠样式表(CSS)文件来实现,尽管有些痛苦,但考虑到我所期望的章节标题定制是多么普遍,我无法想象在Quarto中没有已经有一个简单而直接的处理方法。

英文:

I am attempting to create an HTML book in RStudio using Quarto. I have set up each chapter of the book as a separate .qmd file and have all the chapters listed in my _quarto.yml file. By default, Quarto formats chapter headings as "X MyTitle" where X is the chapter number (e.g. 1, 2, 3, ...) and MyTitle is the heading (h1) (defined by # MyTitle at the beginning of each chapter's .qmd file). I would like to change this behavior so that each Chapter is automatically numbered and displayed as "Chapter X: MyTitle." This was straight forward when using knitr with bookdown by modifying the yaml file to include something like:

   book_filename: "machine-learning-causal-inference-with-bookdown"
   language:
     ui:
       chapter_name: "Chapter "

However, this does not appear to be valid in Quarto now (I tried this and it didn't work, nor do the options seem to be recognized by the IntelliSense/autocompletion in R Studio's IDE). I reviewed the quarto book options, but couldn't find an easy way to "restructure" the way the chapter headers are presented. I had hoped to replicate code from a sample Quarto book that had modified chapter headings, but sadly, all the examples provided in the documentation point to books that use the default chapter heading implementation (e.g. R for Data Science, Python for Data Analysis, and Visualization Curriculum).

So does anyone know a relatively straight forward way to accomplish this? If not, I figured, I can probably accomplish this, albeit somewhat painfully, with a custom Cascading Style Sheet (CSS) file, but given how common my desired chapter heading customization likely is, I can't imagine there's not already a simple and straight-forward way for handling this in Quarto.

答案1

得分: 3

你可以将以下内容添加到你的CSS文件中:

.chapter-number::before {
  content: "章节:";
}

你也可以通过YAML自定义章节名称,例如:

chapters:
  - text: "我自己的章节名称"
    file: 01-chapter.qmd
英文:

You could add this to your css file:

.chapter-number::before {
  content: "Chapter: ";
}

You can customize your chapter names in the YAML like this btw:

  chapters:
    - text: "My very own chapter name"
      file: 01-chapter.qmd

huangapple
  • 本文由 发表于 2023年4月6日 19:31:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定