使用bookdown+memoir LaTeX类撰论文,导出Word遇files2[[format]]错误求助
我来帮你分析下这个问题的解决思路——你遇到的Error in files2[[format]] : attempt to select less than one element in get1index错误,一般是bookdown在处理Word输出格式时,找不到有效的配置项,或是和LaTeX专用的memoir类产生了冲突。结合你的_output.yml配置,给你几个具体的排查和修复步骤:
移除过时的pandoc过滤器参数
新版本的pandoc已经将参考文献处理功能内置,不再需要单独的pandoc-citeproc过滤器。如果你的pandoc版本≥2.11,这个旧过滤器会导致解析错误,把--filter pandoc-citeproc替换成内置的--citeproc参数。将参考文献配置移至主文档YAML头部
不要在_output.yml的pandoc_args里指定参考文献和CSL文件,而是把这些配置放到你的主Rmd文件的YAML头部,这样bookdown能更好地跨格式处理参考文献,避免路径解析问题:--- title: "你的论文标题" author: "你的名字" bibliography: [./bib/packages.bib, ./bib/thesis.bib] csl: ./csl/apa6.csl ---之后在
_output.yml的word_document2配置里,删掉--bibliography和--csl相关的参数。隔离memoir类对Word输出的影响
memoir是LaTeX专用的文档类,Word输出完全不需要加载它。你可以在配置里明确区分:- 在
_output.yml的pdf_book配置中,添加documentclass: memoir,确保仅PDF输出使用该类; - 不要在
word_document2配置中添加任何LaTeX相关的文档类设置。
- 在
检查文件路径的有效性
确认./csl/apa6.csl、./bib/packages.bib这些相对路径是正确的(相对于项目根目录)。可以在R控制台用file.exists("./csl/apa6.csl")验证路径是否存在,必要时改用绝对路径测试。更新依赖包和pandoc
旧版本的bookdown可能对word_document2的支持存在bug,先更新bookdown:install.packages("bookdown")同时检查pandoc版本(用
rmarkdown::pandoc_version()查看),建议升级到2.11及以上版本。
修改后的_output.yml示例参考:
bookdown::pdf_book: includes: in_header: latex/preamble.tex # defines style and latex options before_body: latex/before_body.tex # defines cover page latex_engine: xelatex # lualatex or xelatex citation_package: none # needs to be "none" in order to use the csl file keep_tex: true # keeps the .tex file dev: "cairo_pdf" toc: false # deactivates default table of contents highlight: pygments # code highlighting pandoc_args: [ "--csl", "./csl/apa6.csl" ] # specifies the csl file to be used documentclass: memoir # 明确PDF输出使用memoir类 bookdown::word_document2: pandoc_args: [ "--citeproc" ] # 使用内置参考文献处理 # 如需自定义Word样式,取消注释下面的行 # reference_doc: "./assets/2018-05-17-aim1-draft.docx"
内容的提问来源于stack exchange,提问作者Ladislas Nalborczyk

