我正在编写一个大型Markdown文档,并希望在开始时放置一个目录,将提供到文档中各个位置的链接。我该怎么做呢?
我试着用:
[a link](# MyTitle)
其中MyTitle是文档中的一个标题,但这不起作用。
我正在编写一个大型Markdown文档,并希望在开始时放置一个目录,将提供到文档中各个位置的链接。我该怎么做呢?
我试着用:
[a link](# MyTitle)
其中MyTitle是文档中的一个标题,但这不起作用。
当前回答
除了以上的答案,
当在YAML头中设置选项number_sections: true时:
number_sections: TRUE
RMarkdown会自动为你的章节编号。
要引用这些自动编号的部分,只需在您的R Markdown文件中放入以下内容:
(我的部分)
Where My Section是Section的名称
这似乎不管section级别都有效:
#我的部分
##我的部分
###我的部分
其他回答
在Markdown规范中没有这样的指令,抱歉。
通用解决方案
根据markdown的实现,这个问题似乎有不同的答案。 事实上,官方Markdown文档对这个话题只字未提。 在这种情况下,如果您想要一个可移植的解决方案,您可以使用HTML。
在任何标题之前,或在同一标题行中,使用一些HTML标记定义一个ID。 例如:<a id="Chapter1"></a> . 您将在代码中看到这一点,但在呈现的文档中看不到。
完整的例子:
请在这里查看完整的示例(在线和可编辑)。
## Content
* [Chapter 1](#Chapter1)
* [Chapter 2](#Chapter2)
<div id="Chapter1"></div>
## Chapter 1
Some text here.
Some text here.
Some text here.
## Chapter 2 <span id="Chapter2"><span>
Some text here.
Some text here.
Some text here.
为了测试这个例子,您必须在内容列表和第一章之间增加一些额外的空间,或者降低窗口的高度。 另外,不要在id的名称中使用空格。
在pandoc中,如果在生成html时使用——toc选项,将生成一个包含到章节的链接的目录,并从章节标题返回到目录。它类似于pandoc编写的其他格式,如LaTeX、rtf、rst等。对于命令
pandoc --toc happiness.txt -o happiness.html
这里有一点降价:
% True Happiness
Introduction
------------
Many have posed the question of true happiness. In this blog post we propose to
solve it.
First Attempts
--------------
The earliest attempts at attaining true happiness of course aimed at pleasure.
Soon, though, the downside of pleasure was revealed.
将产生这个作为html的主体:
<h1 class="title">
True Happiness
</h1>
<div id="TOC">
<ul>
<li>
<a href="#introduction">Introduction</a>
</li>
<li>
<a href="#first-attempts">First Attempts</a>
</li>
</ul>
</div>
<div id="introduction">
<h2>
<a href="#TOC">Introduction</a>
</h2>
<p>
Many have posed the question of true happiness. In this blog post we propose to solve it.
</p>
</div>
<div id="first-attempts">
<h2>
<a href="#TOC">First Attempts</a>
</h2>
<p>
The earliest attempts at attaining true happiness of course aimed at pleasure. Soon, though, the downside of pleasure was revealed.
</p>
</div>
关于<a name="">技巧的更多旋转:
<a id="a-link"></a> Title
------
#### <a id="a-link"></a> Title (when you wanna control the h{N} with #'s)
使用kramdown,看起来效果不错:
[I want this to link to foo](#foo)
....
....
{: id="foo"}
### Foo are you?
我看到有人提到过
[foo][#foo]
....
#Foo
工作效率很高,但对于除标题外的元素或具有多个单词的其他标题,前者可能是一个很好的选择。