如何自定义章节标题以r markdown中的字母开头

时间:2018-01-25 03:03:47

标签: r r-markdown

虽然我们可以通过在YAML标题中添加选项number_sections: true来自动化数字部分,但我想知道我们是否可以更改r markdown中的标题样式。例如,我们可以使用下面的字母来定制它吗?

  

:一种。 A节

     
    

A.1小节

         
      

A.1.1小节1

             

A.1.2小节1

    
  
     

B中。 B节

     
    

B.1小节

         
      

B.1.1子小节

    
  

1 个答案:

答案 0 :(得分:1)

回答pdf_document

将此行保存在外部文件中(例如inheader.tex):

\renewcommand{\thesection}{\Alph{section}}

并将文件插入文档的标题中:

---
title: "Lettered sections"
output: 
  pdf_document:
    number_sections: true
    includes:
      in_header: inheader.tex
---

回答html_document格式

TL;博士

编织此Rmd文件:

---
title: "Lettered sections"
output: html_document
---

```{css, echo=FALSE}
.header-section-number {
  display: none;
}

body {
  counter-reset: counter-level-1;
}

h1:not(.title) {
  counter-increment: counter-level-1;
  counter-reset: counter-level-2;
}

h1:not(.title)::before{
  content: counter(counter-level-1, upper-alpha) ". ";
}

h2 {
  counter-increment: counter-level-2;
  counter-reset: counter-level-3;
}

h2::before {
  content: counter(counter-level-1, upper-alpha) "." counter(counter-level-2) " ";
}

h3 {
  counter-increment: counter-level-3;
}

h3::before {
  content: counter(counter-level-1, upper-alpha) "." counter(counter-level-2) "." counter(counter-level-3) " ";
}
```

# Section

## Subsection

### Sub subsection

### Sub subsection

## Subsection

# Section

## Subsection

### Sub subsection

## Subsection

<强>说明
数字部分是原生pandoc选项。似乎pandoc没有为分层标题定制提供任何支持。

因此,HTML输出有三个选项:

  • 深入研究pandoc并开发一个新作家,因为等级标题被声明为整数see here, line #105。请注意,relevant recent issue为了便于标题自定义。
  • 使用CSS修改HTML呈现。
  • 使用Javascript修改HTML元素。这可能是toc: true
  • 所必需的

此答案提供了使用CSS自定义分层标题的示例。建议将所有CSS代码(第7至39行)保存到扩展名为.css的外部文件中,并使用此YAML标头将其包含在HTML报告中:

---
title: "Lettered sections"
output: 
  html_document:
    css: "filename.css"
---

附加说明
可以使用除数字或字母之外的其他计数器,请参阅here以获取列表 您还可以使用@counter-style定义自己的一组计数器。