1

我正在 Rmarkdown 中写一篇论文,我正在使用bookdown::html_document2它来编织输出。交叉引用图形和表格适用于常见的 Bookdown 语法,例如\@ref(tab:example_table). 但是,交叉引用章节/标题不起作用:\@ref(introduction)不起作用。我最终会发现??这很奇怪,因为链接指向正确的标签:/example_path/example_filename.html#introduction。我正在处理一个文档。

我检查了源代码,章节标题的标签 ID 也是正确的。当我使用 Pandoc 的本机语法引用章节部分/标题时,它确实有效:[introduction]works。但是,这将打印介绍部分/标题的标签,而不是标题本身,因此“介绍”而不是带有大写“I”的“介绍”。一个明显的解决方法是使用[Introduction][introduction],但这违背了我的情况下交叉引用的目的。我也尝试给我的章节章节/标题自定义标签,但这也行不通。它也不适用于bookdown::pdf_document2.

根据“使用 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>

在创建 mre 的过程中,我还注意到\@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]

渲染输出

4

2 回答 2

5

参考 id_在 id 中不起作用:例如,{#hello_world}并且\@ref(hello_world)不起作用,但 {#helloworld}应该\@ref(helloworld)没问题。

于 2019-12-20T14:08:55.597 回答
1

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 book 中的交叉引用部分非常清楚。但由于您似乎误解了它,您也许可以建议改进措辞

于 2019-08-14T09:00:47.130 回答