R Bookdown:标签式标题

R Bookdown: Tabbed headings

如何像 RMarkdown 一样在 bookdown 中使用标签式标题?

在 RMarkdown 中:

# heading1 {.tabset}

## tab1
content1

## tab2
content2


bookdown 中的相同代码:

https://github.com/rstudio/bookdown/issues/393

yihui: bookdown 不支持此功能(因为它不是可移植的功能,例如 LaTeX/PDF 输出不可用)。对不起。

在使用gitbook模型时,可以使用HTML+CSS+JS来实现。如果您仍想处理 pdf 输出,则需要创建 LaTeX 环境,如 here 所述。

您可以从 w3schools 解决方案开始,然后在 bookdown 上下文中对其进行调整。

HTML实施

一个纯粹的 HTML 实现是:

function openCity(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}
body {font-family: Arial;}

/* Style the tab */
.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
<h2>Tabs</h2>
<p>Click on the buttons inside the tabbed menu:</p>

<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')">London</button>
  <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
  <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>

它在 bookdown 中的适应方式

这可以在 bookdown

中进行调整

1。在 CSS 文件中

让我们说 style.css,把这行:

/* Style the tab */
.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}

2。在 header.html 文件中

在 HTML 文件中,假设 header.html:

<script>
function unrolltab(evt, tabName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(tabName).style.display = "block";
  evt.currentTarget.className += " active";
}
</script>

3。在 _output.yml:

中声明这些文件

您必须在此文件中声明 CSS 和 JS 片段:

bookdown::gitbook:
  css: ./css/style.css
  includes:
    in_header: ./header.html

4。在你的书本中使用它

你可以直接写

<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>

但是,使用 R Markdown 时有一个更合适的解决方案(如果您定义了适当的 LaTeX 样式,这可能会有所帮助。它基于 custom blocks.

::: {.tab}
<button class="tablinks" onclick="unrolltab(event, 'London')">London</button>
<button class="tablinks" onclick="unrolltab(event, 'Paris')">Paris</button>
<button class="tablinks" onclick="unrolltab(event, 'Tokyo')">Tokyo</button>
::: {#London .tabcontent}
#### London {-}
London is the capital city of England
:::
::: {#Paris .tabcontent}
#### Paris  {-}
Paris is the capital city of France
:::
::: {#Tokyo .tabcontent}
#### Tokyo  {-}
Tokyo is the capital city of Japan
:::
:::

例如,将文本放在 :::{#London .tabcontent}::: 之间会将内容包裹在 <div id="London" class="tabcontent"> 块中