我正在使用R Studio創建降價文檔。 我想:在RMarkdown HTML輸出中使用內部鏈接
Jump to [Header 1](#anchor)
我想建立一個鏈接,這樣,當讀者點擊它,可以跳轉到頁面上的特定點。
讓我們說我希望他們被引導的點有一個標題「##測試」。
我正在使用R Studio創建降價文檔。 我想:在RMarkdown HTML輸出中使用內部鏈接
Jump to [Header 1](#anchor)
我想建立一個鏈接,這樣,當讀者點擊它,可以跳轉到頁面上的特定點。
讓我們說我希望他們被引導的點有一個標題「##測試」。
下面是使用jQuery的HTML文檔的解決方案:
---
title: "Internal Links"
output: html_document
---
# First Section
## Second Section
### Third Section
<script type="text/javascript">
// When the document is fully rendered...
$(document).ready(function() {
// ...select all header elements...
$('h1, h2, h3, h4, h5').each(function() {
// ...and add an id to them corresponding to their 'titles'
$(this).attr('id', $(this).html());
});
});
</script>
<a href="#First Section">Go to first section</a><br>
<a href="#Second Section">Go to second section</a><br>
<a href="#Third Section">Go to third section</a>
正如評論指出的,我們簡單地選擇所有的頭,讀出他們的內容(例如,「第一部」),並添加屬性id
對應於每個標題的特定內容的值。 現在您可以使用#HEADER
(例如#First Section
)鏈接到任何標題。
這當然可以擴展到您希望放置在其他所有元素上。所以,如果你要鏈接到你的任何塊,只要這個腳本添加到您的文檔:
<script type="text/javascript">
$(document).ready(function() {
$('pre.r').each(function(i) {
$(this).attr('id', 'Chunk_' + i);
});
});
</script>
現在,您可以鏈接到該塊使用<a href="Chunk_i">My Chunk</a>
其中i
從0,第一塊,到N
,文檔中最後一個塊。
Pandoc支持標題的顯式和隱式部分引用;請參閱the pandoc manual。
## Test {#test}
後來引用它的鏈接語法:see [the relevant section](#test)
。## Test
,仍然可以引用:See the section called [Test]
。這兩種語法都應該允許您單擊鏈接以轉到錨點,並且應該以大多數輸出格式工作。 (只用html和pdf進行測試)。
---
output: pdf_document
---
## A section
blah blah
## A second section with a custom identifier {#custom}
blah blah
## Links
You can use implicit references to refer to sections
you have not explicitly named, like this:
see [A section].
You can use links to refere to sections with explicit
references, like this: see [the second section](#custom).