如何在 bookdown 中使用标签式标题,如RMarkdown?
在RMarkdown中:
# heading1 {.tabset}
## tab1
content1
## tab2
content2
答案 0 :(得分:2)
使用gitbook
模型时,可以使用HTML + CSS + JS来实现。如果仍要处理pdf输出,则需要按照here所述创建LaTeX
环境。
您可以从w3schools解决方案入手,并在bookdown
上下文中进行调整。
纯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
假设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;
}
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>
_output.yml
中声明这些文件:您必须在此文件中声明CSS和JS代码段:
bookdown::gitbook:
css: ./css/style.css
includes:
in_header: ./header.html
您可以直接写
<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输出不可用)。遗憾。