【问题标题】:Remove section numbering in pdf_document删除 pdf_document 中的章节编号
【发布时间】:2021-02-09 12:30:20
【问题描述】:

我正在尝试使用 RMarkdown 将文档编织成 pdf 文档。我想默认删除部分编号。我认为number_section:FALSE 会这样做,但事实并非如此。我已精简为 MWE,但我仍在获取部分编号。

在编织的report.tex 文件中,我使用{-} 专门标记为没有编号的部分标记为\section*{...},而其他部分标记为\section{...},这就是您完成编号的方式一些 LaTeX 中的部分,但是我希望将所有部分编织为\section*{...},而不必在每个部分(或子部分)中指定{-}

我正在使用 pandoc 版本2.11.2、RStudio 版本1.4.1103 和R 版本4.0.3

这是我的源文件:

report.Rmd:

---
output: 
  pdf_document:
    template: template.tex
    keep_tex: TRUE
    number_sections: FALSE
---
# Abstract {-}

# Introduction

## A Subsection

# A Second Section

template.tex:

\documentclass{article}

\usepackage{hyperref}

\begin{document}

$body$

\end{document}

RMarkdown 输出中的 pandoc 调用显示为(我添加了换行符以使其更易于阅读),请注意它不包含 --number-sections,它通常用于告诉 pandoc 强制编号,默认情况下 pandoc 应该不要给他们编号。

"C:/Program Files/RStudio/bin/pandoc/pandoc" 
+RTS -K512m -RTS report.utf8.md 
--to latex 
--from markdown+autolink_bare_uris+tex_math_single_backslash 
--output report.tex 
--lua-filter "C:\Users\MichaelBarrowman\Documents\R\R-4.0.3\library\rmarkdown\rmarkdown\lua\pagebreak.lua" 
--lua-filter "C:\Users\MichaelBarrowman\Documents\R\R-4.0.3\library\rmarkdown\rmarkdown\lua\latex-div.lua" 
--self-contained
--template template.tex 
--highlight-style tango 
--pdf-engine pdflatex 

这将编织到以下report.tex 文件:

\documentclass{article}

\usepackage{hyperref}

\begin{document}

\hypertarget{abstract}{%
\section*{Abstract}\label{abstract}}
\addcontentsline{toc}{section}{Abstract}

\hypertarget{introduction}{%
\section{Introduction}\label{introduction}}

\hypertarget{a-subsection}{%
\subsection{A Subsection}\label{a-subsection}}

\hypertarget{a-second-section}{%
\section{A Second Section}\label{a-second-section}}

\end{document}

呈现为report.pdf,如下所示:

【问题讨论】:

    标签: r latex r-markdown pandoc


    【解决方案1】:

    您的问题似乎是您使用的是自定义 LaTeX 模板,但没有在该模板中添加任何内容来处理打开或关闭部分编号。 作为参考,以下是 pandoc 默认 LaTeX 模板如何处理此问题:

    $if(numbersections)$
    \setcounter{secnumdepth}{$if(secnumdepth)$$secnumdepth$$else$5$endif$}
    $else$
    \setcounter{secnumdepth}{-\maxdimen} % remove section numbering
    $endif$
    

    (见第 306--310 行 here)。

    因此,如果您想处理此问题使用您自己的自定义模板,您必须自己处理(甚至可能只是通过复制粘贴默认模板的功能)。

    当我这样编辑你的template.tex 时:

    \documentclass{article}
    
    \usepackage{hyperref}
    
    $if(numbersections)$
    \setcounter{secnumdepth}{$if(secnumdepth)$$secnumdepth$$else$5$endif$}
    $else$
    \setcounter{secnumdepth}{-\maxdimen} % remove section numbering
    $endif$
    
    
    \begin{document}
    
    $body$
    
    \end{document}
    

    我得到了我认为你想要的结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 2021-08-26
      • 2011-04-10
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多