RMarkdown引用子目录in_header文件失败,求解决方案
问题描述
当pre.sty与RMarkdown文件同目录时,在YAML中配置in_header: pre.sty可正常引入该文件。但将pre.sty移至etc子目录后,配置in_header: etc\pre.sty会触发以下错误:
File etc\pre.sty not found in resource path Error: pandoc document conversion failed with error 99 Execution halted
是否有方法引入子目录中的头文件?或是这属于Bug?尝试多种方式均未成功。
最小示例
示例1(正常工作)
当pre.sty与RMarkdown文件同目录时:
--- output: bookdown::pdf_document2: toc: no fig_align: center fig_height: 4 fig_width: 6 latex_engine: xelatex keep_tex: true includes: in_header: pre.sty fontsize: 11pt --- Min example 1 - this works when pre.sty is stored in same dir as rmarkdown.
示例2(无法工作)
当pre.sty存放在etc子目录时:
--- output: bookdown::pdf_document2: toc: no fig_align: center fig_height: 4 fig_width: 6 latex_engine: xelatex keep_tex: true includes: in_header: etc\pre.sty fontsize: 11pt --- Min example 2 - this does not work when pre.sty is stored in the etc subdir.
pre.sty内容
\usepackage{booktabs}
解决方法
这不是Bug,问题出在路径的转义字符上:
- 在YAML语法中,反斜杠
\是转义字符,单独写etc\pre.sty时,\p会被解析为转义序列,导致路径识别错误。 - 可以通过两种方式修正路径:
- 使用正斜杠作为路径分隔符(跨平台兼容性更好,推荐优先使用):
includes: in_header: etc/pre.sty - 使用双反斜杠(转义后的反斜杠):
includes: in_header: etc\\pre.sty
- 使用正斜杠作为路径分隔符(跨平台兼容性更好,推荐优先使用):
内容的提问来源于stack exchange,提问作者t-student
相关产品推荐
相关产品推荐

