[英]Cross-referencing in a single-file produced with `bookdown::html_document2`
[英]Cross-referencing bookdown::html_document2 not working
我正在 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
。
根据“Authoring Books with R Markdown”中的这一部分,我使用的语法应该有效,所以我在这里遗漏了什么?
我正在使用 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>
在创建\\@ref(introduction)
的过程中,我还注意到\\@ref(introduction)
如果未禁用部分编号,则行为会有所不同。 在这种情况下, \\@ref(introduction)
返回节的编号,即“1”。
先生:
---
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]
R-Markdown/bookdown 中的部分标题具有实际文本(例如Hello World
)和 ID。 该 ID 可能是自动的(示例中的hello-world
)或手动定义的(例如Hello World {#hello-world-section}
)。 有几种方法可以交叉引用一个部分:
\\@ref(ID)
,即\\@ref(hello-world)
或\\@ref(hello-world-section)
。 如果该部分已编号,这将生成对该部分的数字引用。 否则会产生错误/警告。
[section text]
,即[Hello World]
。 您可以通过实际文本引用一节。 部分标题的实际文本也将是链接文本。
[link text][section text]
,即[see above][Hello World]
。 您仍然使用部分文本,但使用了备用链接文本。
[link text](#ID)
,即[see above](#hello-world)
或[see above](#hello-world-section)
。 在这里,您将使用部分 ID 并指定实际的链接文本。
请注意,没有指定 ID 并自动获取实际文本的模式! 此外,您从不指定 ID 括号,而是始终在括号中。
引用 id 不能在 id 中使用_
:例如, {#hello_world}
\\@ref(hello_world)
{#hello_world}
和\\@ref(hello_world)
不起作用,但{#helloworld}
和\\@ref(helloworld)
应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.