【问题标题】:Cross-referencing bookdown::html_document2 not working交叉引用 bookdown::html_document2 不起作用
【发布时间】: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]

Rendered output

这是我收到的错误消息:

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]

Rendered output

【问题讨论】:

  • 我很确定我以前有过这个工作。也许应该是\@ref(#introduction)?
  • 感谢@Marius 的评论!我忘了提到我尝试过\@ref(#introduction),在此之前我在 html 和 pdf 文档中将输出按字面意思呈现为“@ref(#introduction)”。
  • 如何渲染文档?您实际上是在使用 bookdown 和 render_book 还是别的什么?
  • 好电话。我正在使用以下命令从 Vim 中渲染文档:autocmd Filetype rmd map &lt;F5&gt; :w&lt;CR&gt; :!echo&lt;space&gt;"require(rmarkdown);&lt;space&gt;render('&lt;c-r&gt;%')"&lt;space&gt;\|&lt;space&gt;R&lt;space&gt;--vanilla&lt;enter&gt;。但是,根据您的评论,我尝试使用render_book("filename.Rmd", "bookdown::html_document2") 直接呈现文档,但我仍然得到相同的结果,即??
  • 请提供minimal reproducible example。您的 YAML 标头包含许多(可能)不相关的内容。

标签: r r-markdown pandoc bookdown cross-reference


【解决方案1】:

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

【讨论】:

  • 天哪,经过数小时的搜索,这解决了我的问题!考虑到 LaTeX 处理下划线的方式,事后看来这是有道理的。谢谢!
【解决方案2】:

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 括号,但始终在括号中。

顺便说一句,我发现 bookdown 书中的cross-reference a section 很清楚。不过既然你好像误会了,或许可以suggest an improved wording。

【讨论】:

  • 感谢@RalfStubner 的详尽回答 - 它帮助我更好地了解交叉引用的工作原理!特别是您的第二个要点是我正在寻找的。我想我最初被这句话弄糊涂了:“当找不到引用的标签时,你会看到两个问号,如??,以及渲染书籍时在R控制台中的警告消息。”我>。我没有意识到它只会交叉引用节号(并且仅在未关闭编号的情况下),因为 html 中的 href 仍然引用了正确的节 ID,但返回了 ??。
  • @jono3030 不客气。您现在处于提出改进文档建议的理想位置!
  • 你是对的——我会这样做的!最初我犹豫不决,因为我不确定这是否只是我没有充分阅读文档的问题,或者是否可以更清楚地表述文本,但我现在认为这里有必要提出改进建议.再次感谢您的帮助!
猜你喜欢
  • 2020-04-25
  • 1970-01-01
  • 2020-04-27
  • 2020-10-06
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 2018-10-14
  • 2019-01-06
相关资源
最近更新 更多