【问题标题】:"runin" & bfseries style for rmarkdown html section and subsection titlermarkdown html 部分和小节标题的 \"runin\" & bfseries 样式
【发布时间】:2023-01-03 20:23:48
【问题描述】:
我想使用 rmarkdown 为section和subsection标题使用“runin”和bfseries样式
在 pdf 和 html 中,“runin”表示章节或小节标题和文本在同一行,bfseries 表示标题使用粗体黑色字体样式。
在 pdf 中,我们可以通过求助于 latex 和 \usepackage{titlesec} 来实现这两个目标,而在 html 中,我不知道这些问题,尤其是对于“runin”的问题(bfseries 似乎可以满足通过手动添加类似 **sec-title** 或 **subsec-title** 的内容)。
我的演示rmd文件给出如下:
---
output:
html_document:
toc: no
keep_md: no
pdf_document:
latex_engine: pdflatex
keep_tex: no
header-includes:
- \usepackage{lipsum}
- \usepackage{titlesec}
- \titleformat{\section}[runin]{\bfseries}{}{0em}{}
- \titlespacing{\section}{0pt}{*3}{2ex}
- \titleformat{\subsection}[runin]{\bfseries}{}{0em}{}
- \titlespacing{\subsection}{0pt}{*1}{2ex}
---
```{css style, echo = FALSE}
h1{font-size: 20px; color: black;}
h2{font-size: 20px; color: black;}
body{font-size: 20px;}
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Ex. 14.3
In Section 14.2.6 we discuss the use of CART or PRIM for constructing generalized association rules. Show that a problem occurs with either of these methods when we generate the random data from the productmarginal distribution; i.e., by randomly permuting the values for each of the variables. Propose ways to overcome this problem.
## Solution
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
\lipsum[1]
# Ex. 14.4
Cluster the demographic data of Table $14.1$ using a classification tree. Specifically, generate a reference sample of the same size of the training set, by randomly permuting the values within each feature. Build a classification tree to the training sample (class 1 ) and the reference sample (class 0 ) and describe the terminal nodes having highest estimated class 1 probability. Compare the results to the PRIM results near Table $14.1$ and also to the results of $K$-means clustering applied to the same data.
## Solution
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
\lipsum[2]
【问题讨论】:
标签:
html
latex
r-markdown
【解决方案1】:
这可以在 CSS 的帮助下完成。添加班级.inline,标题和副标题要使用“runin”和bfseries 样式。
---
output:
html_document:
toc: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{css style, echo = FALSE}
h1{font-size: 20px; color: black;}
h2{font-size: 20px; color: black;}
body{font-size: 20px;}
div.inline > h1,
div.inline > h2,
div.inline > p {
display: inline;
}
div.inline > h1,
div.inline > h2 {
font-weight: bold;
}
div.inline {
margin-top: 1em;
margin-bottom: 1em;
}
```
# Ex. 14.3 {.inline}
In Section 14.2.6 we discuss the use of CART or PRIM for constructing generalized association rules. Show that a problem occurs with either of these methods when we generate the random data from the productmarginal distribution; i.e., by randomly permuting the values for each of the variables. Propose ways to overcome this problem.
## Solution {.inline}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
# Ex. 14.4 {.inline}
Cluster the demographic data of Table $14.1$ using a classification tree. Specifically, generate a reference sample of the same size of the training set, by randomly permuting the values within each feature. Build a classification tree to the training sample (class 1 ) and the reference sample (class 0 ) and describe the terminal nodes having highest estimated class 1 probability. Compare the results to the PRIM results near Table $14.1$ and also to the results of $K$-means clustering applied to the same data.
## Solution {.inline}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
【解决方案2】:
如果也有可能切换到 Quarto (.qmd),则有可参考的 exercise 选项,例如
---
title: Exercise Example
format:
html:
css: styles.css
---
::: {#exr-14.3}
In Section 14.2.6 we discuss the use of CART or PRIM for constructing generalized association rules. Show that a problem occurs with either of these methods when we generate the random data from the productmarginal distribution; i.e., by randomly permuting the values for each of the variables. Propose ways to overcome this problem.
:::
::: {.solution}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
:::
在哪里样式.css看起来像这样:
.proof-title{
font-weight: bold;
}
结果: