我正在编写一个大型Markdown文档,并希望在开始时放置一个目录,将提供到文档中各个位置的链接。我该怎么做呢?

我试着用:

[a link](# MyTitle)

其中MyTitle是文档中的一个标题,但这不起作用。


当前回答

除了以上的答案,

当在YAML头中设置选项number_sections: true时:

number_sections: TRUE

RMarkdown会自动为你的章节编号。

要引用这些自动编号的部分,只需在您的R Markdown文件中放入以下内容:

(我的部分)

Where My Section是Section的名称

这似乎不管section级别都有效:

#我的部分

##我的部分

###我的部分

其他回答

因为在评论中提到了MultiMarkdown作为一个选项。

在MultiMarkdown中,内部链接的语法很简单。

对于文档中的任何标题,只需以这种格式给出标题名称[heading][]以创建内部链接。

阅读更多:MultiMarkdown-5交叉引用。

Cross-References An oft-requested feature was the ability to have Markdown automatically handle within-document links as easily as it handled external links. To this aim, I added the ability to interpret [Some Text][] as a cross-link, if a header named “Some Text” exists. As an example, [Metadata][] will take you to # Metadata (or any of ## Metadata, ### Metadata, #### Metadata, ##### Metadata, ###### Metadata). Alternatively, you can include an optional label of your choosing to help disambiguate cases where multiple headers have the same title: ### Overview [MultiMarkdownOverview] ## This allows you to use [MultiMarkdownOverview] to refer to this section specifically, and not another section named Overview. This works with atx- or settext-style headers. If you have already defined an anchor using the same id that is used by a header, then the defined anchor takes precedence. In addition to headers within the document, you can provide labels for images and tables which can then be used for cross-references as well.

只需遵循[text](#link)语法并遵循以下指导原则:

把字母和数字原封不动地写下来 用破折号-替换空格 删除其余的字符

举个例子,如果你有这些部分:

# 1. Python

# 2. c++

# 3. c++11

# 4. asp.net-core

您可以使用以下命令添加引用:

[1. Python](#1-python)
[2. c++](#2-c)
[3. c++11](#3-c11)
[4. asp.net-core](#4-aspnet-core)

注意asp.net-core如何变成aspnet-core, 1。Python变成了1-python,等等。

试验,我发现了一个解决方案使用<div…/>,但一个明显的解决方案是在页面中放置自己的锚点,无论你喜欢,因此:

<a name="abcde">

之前和

</a>

在你想要“链接”到的行之后。然后是一个降价链接:

[link text](#abcde)

文档中的任何地方都可以。

<div…/>解决方案插入一个“假”除法只是为了添加id属性,这可能会破坏页面结构,但<a name="abcde"/>解决方案应该是非常无害的。

(PS:把锚点放在你想链接的行中也可以,如下所示:

## <a name="head1">Heading One</a>

但这取决于Markdown如何处理。我注意到,例如,Stack Overflow答案格式化器很高兴这样!)

通用解决方案

根据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的名称中使用空格。

Github会自动从你的标题中解析锚标记。所以你可以这样做:

[Custom foo description](#foo)

# Foo

在上面的例子中,Foo头生成了一个名为Foo的锚标记

注意:所有标题大小只有一个#,#和锚点名称之间没有空格,锚点标签名称必须小写,如果有多个单词,则用破折号分隔。

[click on this link](#my-multi-word-header)

### My Multi Word Header

更新

潘多克也不例外。