【发布时间】:2019-08-12 23:42:28
【问题描述】:
我正在 Rmarkdown 中写一篇论文,我正在使用 bookdown::html_document2 来编织输出。交叉引用图形和表格适用于常见的 Bookdown 语法,例如\@ref(tab:example_table)。但是,不能交叉引用章节章节/标题:\@ref(introduction) 不起作用。我最终会得到??,这很奇怪,因为链接指向正确的标签:/example_path/example_filename.html#introduction。我正在处理单个文档。
我检查了源代码,章节标题的标签 ID 也是正确的。当我使用 Pandoc 的本机语法引用章节部分/标题时,它确实有效:[introduction] 有效。但是,这将打印介绍部分/标题的标签,而不是标题本身,因此“介绍”而不是带有大写“I”的“介绍”。一个明显的解决方法是使用[Introduction][introduction],但这违背了我的情况下交叉引用的目的。我也尝试给我的章节章节/标题自定义标签,但这也行不通。它也不适用于bookdown::pdf_document2。
根据“使用 R Markdown 创作书籍”中的this 部分,我使用的语法应该可以工作,所以我在这里缺少什么?
我正在使用 Pandoc 版本2.7.3。
编辑:
删除会话信息,因为它与问题无关。
编辑:
@RalfStubner 请求了一个 mre(我应该首先提供 - 感谢 Ralf 的建议!):
---
title: "Document Title"
author: "Jono3030"
date: "Last updated: `r Sys.time()`"
output:
bookdown::html_document2:
number_sections: no
bookdown::pdf_document2:
keep_tex: no
number_sections: no
bookdown::word_document2: default
---
# Introduction
This is the introduction
# Chapter_one
This is chapter one.
# Chapter_two
This is chapter two. Trying to reference the Introduction.
This does not work: \@ref(introduction)
This works: [Introduction][introduction]
This does not result in the title but in the tag: [introduction]
这是我收到的错误消息:
Output created: mre.html
Warning message:
The label(s) introduction not found
但标签似乎根据html正确分配:
<div id="introduction" class="section level1">
<h1>Introduction</h1>
<p>This is the introduction</p>
</div>
这里是href:
<p>This does not work: <a href="#introduction"><strong>??</strong></a></p>
<p>This works: <a href="#introduction">Introduction</a></p>
<p>This does not result in the title but in the tag: <a href="#introduction">introduction</a></p>
在创建 mre 的过程中,我还注意到 \@ref(introduction) 如果未禁用部分编号,则其行为会有所不同。在这种情况下,\@ref(introduction) 返回节的编号,即“1”。
mre:
---
title: "Document Title"
author: "Jono3030"
date: "Last updated: `r Sys.time()`"
output:
bookdown::html_document2: default
bookdown::pdf_document2:
keep_tex: no
number_sections: no
bookdown::word_document2: default
---
# Introduction
This is the introduction
# Chapter_one
This is chapter one.
# Chapter_two
This is chapter two. Trying to reference the Introduction.
This does work - returns number: \@ref(introduction)
This works: [Introduction][introduction]
This does not result in the title but in the tag: [introduction]
【问题讨论】:
-
我很确定我以前有过这个工作。也许应该是
\@ref(#introduction)? -
感谢@Marius 的评论!我忘了提到我尝试过
\@ref(#introduction),在此之前我在 html 和 pdf 文档中将输出按字面意思呈现为“@ref(#introduction)”。 -
如何渲染文档?您实际上是在使用 bookdown 和
render_book还是别的什么? -
好电话。我正在使用以下命令从 Vim 中渲染文档:
autocmd Filetype rmd map <F5> :w<CR> :!echo<space>"require(rmarkdown);<space>render('<c-r>%')"<space>\|<space>R<space>--vanilla<enter>。但是,根据您的评论,我尝试使用render_book("filename.Rmd", "bookdown::html_document2")直接呈现文档,但我仍然得到相同的结果,即?? -
请提供minimal reproducible example。您的 YAML 标头包含许多(可能)不相关的内容。
标签: r r-markdown pandoc bookdown cross-reference