R Bookdown:标签标题

时间:2017-04-30 13:21:36

标签: r bookdown

如何在 bookdown 中使用标签式标题,如RMarkdown?

在RMarkdown中

# heading1 {.tabset}

## tab1
content1

## tab2
content2

enter image description here

书记中的相同代码:

enter image description here

2 个答案:

答案 0 :(得分:2)

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

您可以从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

中进行调整

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">块内

答案 1 :(得分:1)

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

yihui:bookdown不支持此功能(因为它不是便携式功能,例如LaTeX / PDF输出不可用)。遗憾。