简体   繁体   English

交叉引用 bookdown::html_document2 不起作用

[英]Cross-referencing bookdown::html_document2 not working

I am working on a paper in Rmarkdown and I am using bookdown::html_document2 to knit the output.我正在 Rmarkdown 中写一篇论文,我正在使用bookdown::html_document2来编织输出。 Cross-referencing figures and tables works with the common Bookdown syntax eg \\@ref(tab:example_table) .交叉引用图形和表格使用常见的 Bookdown 语法,例如\\@ref(tab:example_table) Cross-referencing chapter sections/titles does not, however: \\@ref(introduction) doesn't work.但是,交叉引用章节章节/标题不起作用: \\@ref(introduction)不起作用。 I'll end up with ??我会结束?? which is strange since the link points to the correct tag: /example_path/example_filename.html#introduction .这很奇怪,因为链接指向正确的标签: /example_path/example_filename.html#introduction I'm working in a single document.我正在处理单个文档。

I checked the source code and the tag id's for the chapter titles are correct as well.我检查了源代码,章节标题的标签 ID 也是正确的。 It does work when I reference the chapter section/title with Pandoc's native syntax: [introduction] works.当我使用 Pandoc 的原生语法引用章节部分/标题时,它确实有效: [introduction]有效。 This will, however, print the tag for the introduction section/title and not the title itself so 'introduction' instead of 'Introduction' with a capital 'I'.但是,这将打印介绍部分/标题的标签,而不是标题本身,因此“介绍”而不是带有大写“I”的“介绍”。 An obvious work around is to use [Introduction][introduction] but this defeats the purpose of cross-referencing in my case.一个明显的解决方法是使用[Introduction][introduction]但这在我的情况下违背了交叉引用的目的。 I also tried to give my chapter sections/titles custom tags but that wouldn't work either.我还尝试给我的章节章节/标题自定义标签,但这也不起作用。 It also doesn't work with bookdown::pdf_document2 .它也不适用于bookdown::pdf_document2

According to this section from "Authoring Books with R Markdown" the syntax I'm using should work so what am I missing here?根据“Authoring Books with R Markdown”中的这一部分,我使用的语法应该有效,所以我在这里遗漏了什么?

I am using Pandoc version 2.7.3 .我正在使用 Pandoc 版本2.7.3

EDIT :编辑

Removed session info since it was not relevant to the problem.删除了会话信息,因为它与问题无关。

EDIT :编辑

@RalfStubner requested a mre (which I should have provided in the first place - thanks Ralf for the advice!): @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]

Rendered output渲染输出

This is the error message I receive:这是我收到的错误消息:

Output created: mre.html
Warning message:
The label(s) introduction not found

But the tag seems to be correctly assigned according to the html:但是标签似乎是根据 html 正确分配的:

<div id="introduction" class="section level1">
<h1>Introduction</h1>
<p>This is the introduction</p>
</div>

Here is the href:这是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>

In the process of creating the mre I also noticed that \\@ref(introduction) behaves differently if numbering of the sections isn't disabled.在创建\\@ref(introduction)的过程中,我还注意到\\@ref(introduction)如果未禁用部分编号,则行为会有所不同。 In that case \\@ref(introduction) returns the number of the section ie '1'.在这种情况下, \\@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]

Rendered output渲染输出

A section heading in R-Markdown/bookdown has the actual text (eg Hello World ) and an ID. R-Markdown/bookdown 中的部分标题具有实际文本(例如Hello World )和 ID。 That ID might be automatic ( hello-world in the example) or manually defined (eg Hello World {#hello-world-section} ).该 ID 可能是自动的(示例中的hello-world )或手动定义的(例如Hello World {#hello-world-section} )。 There are several ways to cross reference a section:有几种方法可以交叉引用一个部分:

  • \\@ref(ID) , ie \\@ref(hello-world) or \\@ref(hello-world-section) . \\@ref(ID) ,即\\@ref(hello-world)\\@ref(hello-world-section) This will produce a numeric reference to the section if the section is numbered.如果该部分已编号,这将生成对该部分的数字引用。 Otherwise an error/warning is produced.否则会产生错误/警告。

  • [section text] , ie [Hello World] . [section text] ,即[Hello World] You can refer to a section by the actual text.您可以通过实际文本引用一节。 The actual text of the section heading will also be the link text.部分标题的实际文本也将是链接文本。

  • [link text][section text] , ie [see above][Hello World] . [link text][section text] ,即[see above][Hello World] You still use the section text, but your alternate link text is used.您仍然使用部分文本,但使用了备用链接文本。

  • [link text](#ID) , ie [see above](#hello-world) or [see above](#hello-world-section) . [link text](#ID) ,即[see above](#hello-world)[see above](#hello-world-section) Here you are using the section ID and specify the actual link text.在这里,您将使用部分 ID 并指定实际的链接文本。

Note that there is no mode where you specify the ID and automatically get the actual text!请注意,没有指定 ID 并自动获取实际文本的模式! Also, you never specify the ID brackets but always in parenthesis.此外,您从不指定 ID 括号,而是始终在括号中。

BTW, I found the cross-reference a section in the bookdown book quite clear.顺便说一句,我发现 bookdown 书中的交叉引用非常清楚。 But since you seem to have misunderstood it, you might be able to suggest an improved wording .但是由于您似乎误解了它,您也许可以建议改进措辞

引用 id 不能在 id 中使用_ :例如, {#hello_world} \\@ref(hello_world) {#hello_world}\\@ref(hello_world)不起作用,但{#helloworld}\\@ref(helloworld)应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM