You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在RMarkdown的PDF与HTML中实现runin及bfseries章节标题样式

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}

/* HTML端样式调整:实现runin和粗体 */
h1, h2 {
  display: inline-block; /* 将块级标题改为行内块,实现与正文同行 */
  font-weight: bold; /* 粗体效果,替代手动添加** */
  font-size: 20px; 
  color: black;
  margin-right: 1em; /* 标题与正文之间添加间距,避免拥挤 */
  margin-top: 1em; /* 保持标题上方的空白,和PDF端排版一致 */
}
body{font-size: 20px;}
/* 确保标题后的正文不会被异常换行 */
h1 + p, h2 + p {
  display: inline;
}
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]

### 关键说明
1. **HTML端核心CSS调整**
   - `display: inline-block`:将默认是块级元素的h1、h2改为行内块,让标题能和后续正文处于同一行
   - `font-weight: bold`:直接实现粗体效果,无需手动给标题文本加`**`标记
   - `margin-right: 1em`:给标题和正文之间设置合适间距,避免排版拥挤
   - `h1 + p, h2 + p {display: inline}`:确保标题紧跟的段落也保持行内显示,不会被强制换行

2. **PDF端保持原有设置**
   你之前用`titlesec`包配置的`\titleformat{\section}[runin]{\bfseries}{}{0em}{}`等代码已经完美实现PDF端的runin和粗体需求,无需修改。

内容的提问来源于stack exchange,提问作者John Stone
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 09:25:44