【问题标题】:What causes a pandoc document conversion error when blockquote follows header当blockquote跟随标题时导致pandoc文档转换错误的原因
【发布时间】:2025-12-23 08:50:12
【问题描述】:

我在 RStudio 中使用 knitr,我正在寻找从 rmarkdown 文件创建文档时出现奇怪错误的解释。例如,我有一个文件 pdf-test.Rmd:

---
title: "PDF knit error"
output: pdf_document
---

##Headers

> ###Quote 1
This results in an error; if the blockquote symbol ('>') in 
preceeding line is removed, no error

> ###Quote 2
This line is fine

当我尝试使用 Knit PDF 按钮创建 pdf 时,输出如下:

|……………………………………………………………………………………………………………………………… .............| 100% 没有R代码的普通文本 处理文件:pdf-test.Rmd 输出文件:pdf-test.knit.md /usr/bin/pandoc +RTS -K512m -RTS pdf-test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pdf-test.pdf --template /home/jcoliver/R /x86_64-pc-linux-gnu-library/3.3/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable 'geometry:边距=1英寸 ! LaTeX 错误:出了点问题——可能是缺少 \item。 有关说明,请参阅 LaTeX 手册或 LaTeX Companion。 键入 H 以获得即时帮助。 ... l.94 \end{报价} pandoc:生成 PDF 时出错 错误:pandoc 文档转换失败,错误 43 执行停止

如上所述,从第一个实例中删除块引号字符 (>) 会使错误消失(尽管所需的格式也是如此)。

---
title: "PDF knit error"
output: pdf_document
---

##Headers

###Quote 1
No error here

> ###Quote 2
This line remains fine

并且没有来自 pandoc/LaTeX 的投诉:

|……………………………………………………………………………………………………………………………… .............| 100% 没有R代码的普通文本 /usr/bin/pandoc +RTS -K512m -RTS pdf-test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pdf-test.pdf --template /home/jcoliver/R /x86_64-pc-linux-gnu-library/3.3/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable 'geometry:边距=1英寸 处理文件:pdf-test.Rmd 输出文件:pdf-test.knit.md 创建的输出:pdf-test.pdf

我也可以通过将块引用的标题级别更改为H4或更高(H5,H6等)来避免错误,而将第一个块引用的标题级别更改为H1或H2仍然会导致错误.

那么为什么会出现这个错误呢?为什么我不能在标题之后立即使用 H3 的块引用?请注意,我在标题行尝试了不同的标题级别(#Headers##Headers###Headers)、##Headers 行之后的不同间距以及不同的输出格式(即 HTML),但错误总是发生.

一些系统细节:

  • Ubuntu 16.04
  • pandoc 1.17.2(1.16.0.2 也出现错误)
  • R 3.3.1
  • RStudio 0.99.489

【问题讨论】:

    标签: r latex rstudio knitr pandoc


    【解决方案1】:

    您想在节标题之后直接引用节标题?这听起来确实很特别,看起来pandoc 无法处理这个问题。

    正如eipi10's answer 中所提议的,一种解决方案是在标题和引用之间添加something。但是,我认为您应该在文档中添加白色填充文本。例如,当您从 PDF 复制文本时,填充符变为可见。

    相反,只需添加一个(空)框:\mbox{}。然后,为了避免由于我们引入的额外行而导致过多的垂直空格,添加一些负垂直空格:\vspace*{-1cm}

    ---
    output: pdf_document
    ---
    
    ##Headers
    
    \mbox{}\vspace*{-1cm}
    
    > ### Quoted Section
    
    Foobar.
    

    【讨论】:

      【解决方案2】:

      我不确定为什么会发生错误,但这里有一个 hack,它允许您在 H2 标头之后立即将 H3 标头放在块引号中,而没有中间文本。

      基本的想法是你在标题之间添加一些文本,但是你将它的颜色设置为白色(你还需要在标题中声明\usepackage{color}才能工作) .然后,因为这也会在标题之间增加太多空间,所以使用\tiny 使文本非常小,并使用\vspace*{-\baselineskip} 减少行之间的空间。 (我最初尝试使用\phantom{aaa} 添加幻像文本,但仍然导致相同的错误,所以我切换到“真实”文本,但呈现与背景相同的颜色(即白色)。)

      ---
      title: "PDF knit error"
      output: 
        pdf_document:
          number_sections: no
      header-includes:
        - \usepackage{color}
      ---
      
      ##Headers
      
      \vspace*{-\baselineskip}
      \tiny
      \begin{itemize}
      \color{white}
      \item Some text  
      \end{itemize}
      \normalsize
      \vspace*{-\baselineskip}
      
      > ###Quote 1  
      This results in an error; if the blockquote symbol ('>') in 
      preceeding line is removed, no error
      
      > ###Quote 2  
      This line is fine
      

      【讨论】:

        最近更新 更多