在Markdown中设置自定义章节编号

时间:2018-08-26 15:06:10

标签: header r-markdown customization

我正在创建一个R Markdown文档,该文档输出PDF文档。我的YAML标头如下:

---
title: "Introduction"
author: "John Doe
date: "August 26, 2018"
mainfont: Pancetta Pro
documentclass: book
output:
  pdf_document:
    number_sections: true
    df_print: kable
    fig_caption: yes
    fig_width: 6
    highlight: tango
    includes:
      in_header: preamble.tex
    latex_engine: xelatex
geometry: headheight=25pt, tmargin=25mm, bmargin=20mm, innermargin=20mm, outermargin=20mm
---

preamble.tex文件中,我要使用以下LaTeX命令(它们只是修改标题的显示方式):

\renewcommand{\chaptermark}[1]{\markright{#1}}
\renewcommand{\sectionmark}[1]{}
\renewcommand{\subsectionmark}[1]{}    
\usepackage{titlesec}
\titleformat{\chapter}[hang]{\Huge}{\bfseries\thechapter}{0.2pt}{\thicklines\thehook}[\vspace{0.5em}]

但是,当这些最后几行包含在preamble.tex中时,在编织R Markdown文件时出现错误:

! Argument of \paragraph has an extra }.
<inserted text> 
\par 
l.1290 \ttl@extract\paragraph
Error: Failed to compile Template.tex

我不知道为什么它无法运行。 preamble.tex文件的内容如下:

% !TeX program = lualatex
\usepackage{relsize} % To make math slightly larger.

\newcommand{\thehook}{%
 \hspace{.5em}%
 \setlength{\unitlength}{1em}%
 \raisebox{-.5em}{\begin{picture}(.4,1.7)
  \put(0,0){\line(1,0){.2}}
  \put(.2,0){\line(0,1){1.7}}
  \put(.2,1.7){\line(1,0){.2}}
 \end{picture}}%
 \hspace{0.5em}%
} %This creates the "hook" symbol at the beginning of each chapter.

\usepackage{anyfontsize}
\usepackage{fontspec}
\setmainfont{Pancetta Pro}

%   We set the font for the chapters:
\newfontfamily\chapterfont{Pancetta Pro}

%   And now for the sections:
\newfontfamily\sectionfont{Pancetta Pro}


\usepackage{fancyhdr}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhead[RO]{\large\sffamily\rightmark\thehook\textbf{\thepage}}
\fancyhead[LE]{\large\sffamily\textbf{\thepage}\thehook\rightmark}

\fancypagestyle{plain}{%
 \fancyhf{}
}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markright{#1}}
\renewcommand{\sectionmark}[1]{}
\renewcommand{\subsectionmark}[1]{}

\fontsize{12}{20}\selectfont

\usepackage{titlesec}
\titleformat{\chapter}[hang]{\Huge}{\bfseries\thechapter}{0.2pt}{\thicklines\thehook}[\vspace{0.5em}]

当排除上一个代码中的最后6行时,没有错误,并且pdf被创建。

1 个答案:

答案 0 :(得分:2)

如果要将titlesecrmarkdown一起使用,则必须添加

subparagraph: yes

到您的YAML标头,请参见several other answers


rmarkdown使用的默认LaTeX类是article,没有章节。您应该添加

documentclass: report

documentclass: book

到您的YAML标头。