从 RMarkdown 更改标题并将 xtable 中的粗体字体包含到 .pdf
Change heading and include bold font in xtable to .pdf from RMarkdown
我是 RMarkdown 和 LaTex 的初学者,尽管我很喜欢在 R 中创建报告!我已经选择了一些代码来创建我想要的精确 .pdf 报告,尽管在一些美学方面有困难。
我的数据是机密的,但这里是布局的副本/确切数量的地块和 tables,使用内置的 R 数据集。
---
title: <center> <h1>Call Centre Report</h1> </center>
mainfont: Arial
output:
pdf_document:
latex_engine: xelatex
sansfont: Arial
fig_crop: false
toc: true
classoption: landscape
fontsize: 14pt
geometry: margin=0.5in
header-includes:
- \usepackage{booktabs}
---
<style>
.main-container {
max-width: 1200px !important;
}
</style>
```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```
\newpage
# Iris
```{r fig.width=18, fig.height=7, echo=FALSE, comment=" "}
library(ggplot2)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point() +
theme_classic()
```
<br>
<br>
```{r, echo=FALSE, results='asis'}
library(knitr)
library(xtable)
t1 <- kable(subset(iris, Sepal.Length=="5.1", select = Petal.Length:Species),
format = "latex", booktabs = TRUE, row.names = FALSE)
t2 <- kable(subset(chickwts, feed=="horsebean", select = weight:feed),
format = "latex", booktabs = TRUE, row.names = FALSE)
t3 <- kable(subset(mtcars, gear=="4", select = disp:wt),
format = "latex", booktabs = TRUE, row.names = FALSE, digits=2)
cat(c("\begin{table}[!htb]
\begin{minipage}{.35\linewidth}
\centering",
t1,
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
t2,
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
t3,
"\end{minipage}
\end{table}"
))
```
我想整理以下内容,但不确定如何操作:
- 将第 2 页上的
Iris
标题设为粗体并居中,但将其保留为 table 目录中的标题。
- 将三个 table 中每一个的标题加粗
- 将每个文本中的所有文本居中 table
- 删除 Table 1 中
1.7, 0.5, setosa
之后的大间隙,因此,该行中的其他 tables。
任何帮助或特定文档的链接将不胜感激。
更新
除了我的数据包含百分比列外,下面提供的答案效果很好,RMarkdown 和 LaTeX 似乎不喜欢它。我已经用一些虚拟数据更新了我的代码,这与我的机密数据非常相似,希望这个错误能够得到解决。
谢谢!
---
title: <center> <h1>Call Centre Report</h1> </center>
mainfont: Arial
output:
pdf_document:
latex_engine: xelatex
sansfont: Arial
fig_crop: false
toc: true
classoption: landscape
fontsize: 14pt
geometry: margin=0.5in
header-includes:
- \usepackage{booktabs}
---
<style>
.main-container {
max-width: 1200px !important;
}
</style>
```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```
\newpage
# Iris
```{r fig.width=18, fig.height=7, echo=FALSE, comment=" "}
library(ggplot2)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point() +
theme_classic()
```
<br>
<br>
```{r, echo=FALSE, results='asis'}
library(knitr)
library(xtable)
# Create some dummy data
Data <- data.frame(Fruit = sample(c("Apple", "Orange"), 10, replace = TRUE),
Score = sample(1:10))
# Include percentage column
Data$Percentage <- (paste0(round(100 * Data$Score/100), "%"))
t1 <- kable({x <- subset(iris, Sepal.Length=="5.1", select = Petal.Length:Species);
names(x) <- sprintf("\textbf{%s}", names(x))
x},
format = "latex", booktabs = TRUE, row.names = FALSE,
align = 'c', escape = FALSE)
t2 <- kable({x <- subset(chickwts, feed=="horsebean", select = weight:feed);
names(x) <- sprintf("\textbf{%s}", names(x))
x},
format = "latex", booktabs = TRUE, row.names = FALSE,
align = 'c', escape = FALSE)
t3 <- kable({x <- subset(Data, Fruit=="Apple", select = Score:Percentage);
names(x) <- sprintf("\textbf{%s}", names(x))
x},
format = "latex", booktabs = TRUE, row.names = FALSE,
align = 'c', escape = FALSE)
cat(c("\begin{table}[!htb]
\begin{minipage}{.35\linewidth}
\centering",
t1,
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
t2,
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
t3,
"\end{minipage}
\end{table}"
))
```
对于您的 table 相关问题,您可以使用以下方法获得您想要的布局。粗体标题是通过将列名称包裹在 \code{textbf}
中获得的。如果您希望 table 标题可以适应 HTML 输出,您将需要额外的逻辑(或者您需要使用不同的 table 生成包)。这样,您需要在 kable
调用中设置 escape = TRUE
。
将内容居中就像在 kable
函数中使用 align
参数一样简单。
t1 <- kable({x <- subset(iris, Sepal.Length=="5.1", select = Petal.Length:Species);
names(x) <- sprintf("\textbf{%s}", names(x))
x},
format = "latex", booktabs = TRUE, row.names = FALSE,
align = 'c', escape = FALSE)
如果您在 # Iris
的上方和下方放置一行白色 space,您的部分标题将正确显示。
要使部分标题居中,您可能需要在 https://tex.stackexchange.com/
上搜索提示
pixiedust
版本
出于某种我不明白的原因,我必须在这些 table 中设置 tabcolsep
参数。在 minipage
环境中,您需要使用 float = FALSE
。但这似乎可以在不编辑数据源的情况下很好地近似您想要的内容。
在使用百分比的地方,您需要使用 sprinkle(halign = "center", sanitize = TRUE)
---
title: <center> <h1>Call Centre Report</h1> </center>
mainfont: Arial
output:
pdf_document:
keep_tex: yes
sansfont: Arial
fig_crop: false
toc: true
classoption: landscape
fontsize: 14pt
geometry: margin=0.5in
header-includes:
- \usepackage{amssymb}
- \usepackage{arydshln}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{hhline}
- \usepackage{longtable}
- \usepackage{multirow}
- \usepackage[dvipsnames,table]{xcolor}
- \usepackage{booktabs}
---
<style>
.main-container {
max-width: 1200px !important;
}
</style>
```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```
\newpage
# Iris
```{r fig.width=18, fig.height=7, echo=FALSE, comment=" "}
library(ggplot2)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point() +
theme_classic()
```
<br>
<br>
```{r, echo=FALSE, results='asis'}
library(knitr)
library(xtable)
library(pixiedust)
t1 <- subset(iris,
Sepal.Length == "5.1",
select = Petal.Length:Species) %>%
dust(float = FALSE) %>%
sprinkle(tabcolsep = 10) %>%
sprinkle(bold = TRUE,
part = "head") %>%
sprinkle(halign = "center") %>%
medley_bw()
t2 <- subset(chickwts, feed=="horsebean", select = weight:feed) %>%
dust(float = FALSE) %>%
sprinkle(tabcolsep = 10) %>%
sprinkle(bold = TRUE,
part = "head") %>%
sprinkle(halign = "center") %>%
medley_bw()
t3 <- subset(mtcars, gear=="4", select = disp:wt) %>%
dust(float = FALSE) %>%
sprinkle(tabcolsep = 10) %>%
sprinkle(bold = TRUE,
part = "head") %>%
sprinkle(halign = "center") %>%
medley_bw()
cat(c("\begin{table}[!htb]
\begin{minipage}{.35\linewidth}
\centering",
print(t1, asis = FALSE),
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
print(t2, asis = FALSE),
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
print(t3, asis = FALSE),
"\end{minipage}
\end{table}"
))
```
我是 RMarkdown 和 LaTex 的初学者,尽管我很喜欢在 R 中创建报告!我已经选择了一些代码来创建我想要的精确 .pdf 报告,尽管在一些美学方面有困难。
我的数据是机密的,但这里是布局的副本/确切数量的地块和 tables,使用内置的 R 数据集。
---
title: <center> <h1>Call Centre Report</h1> </center>
mainfont: Arial
output:
pdf_document:
latex_engine: xelatex
sansfont: Arial
fig_crop: false
toc: true
classoption: landscape
fontsize: 14pt
geometry: margin=0.5in
header-includes:
- \usepackage{booktabs}
---
<style>
.main-container {
max-width: 1200px !important;
}
</style>
```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```
\newpage
# Iris
```{r fig.width=18, fig.height=7, echo=FALSE, comment=" "}
library(ggplot2)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point() +
theme_classic()
```
<br>
<br>
```{r, echo=FALSE, results='asis'}
library(knitr)
library(xtable)
t1 <- kable(subset(iris, Sepal.Length=="5.1", select = Petal.Length:Species),
format = "latex", booktabs = TRUE, row.names = FALSE)
t2 <- kable(subset(chickwts, feed=="horsebean", select = weight:feed),
format = "latex", booktabs = TRUE, row.names = FALSE)
t3 <- kable(subset(mtcars, gear=="4", select = disp:wt),
format = "latex", booktabs = TRUE, row.names = FALSE, digits=2)
cat(c("\begin{table}[!htb]
\begin{minipage}{.35\linewidth}
\centering",
t1,
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
t2,
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
t3,
"\end{minipage}
\end{table}"
))
```
我想整理以下内容,但不确定如何操作:
- 将第 2 页上的
Iris
标题设为粗体并居中,但将其保留为 table 目录中的标题。- 将三个 table 中每一个的标题加粗
- 将每个文本中的所有文本居中 table
- 删除 Table 1 中
1.7, 0.5, setosa
之后的大间隙,因此,该行中的其他 tables。
任何帮助或特定文档的链接将不胜感激。
更新
除了我的数据包含百分比列外,下面提供的答案效果很好,RMarkdown 和 LaTeX 似乎不喜欢它。我已经用一些虚拟数据更新了我的代码,这与我的机密数据非常相似,希望这个错误能够得到解决。
谢谢!
---
title: <center> <h1>Call Centre Report</h1> </center>
mainfont: Arial
output:
pdf_document:
latex_engine: xelatex
sansfont: Arial
fig_crop: false
toc: true
classoption: landscape
fontsize: 14pt
geometry: margin=0.5in
header-includes:
- \usepackage{booktabs}
---
<style>
.main-container {
max-width: 1200px !important;
}
</style>
```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```
\newpage
# Iris
```{r fig.width=18, fig.height=7, echo=FALSE, comment=" "}
library(ggplot2)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point() +
theme_classic()
```
<br>
<br>
```{r, echo=FALSE, results='asis'}
library(knitr)
library(xtable)
# Create some dummy data
Data <- data.frame(Fruit = sample(c("Apple", "Orange"), 10, replace = TRUE),
Score = sample(1:10))
# Include percentage column
Data$Percentage <- (paste0(round(100 * Data$Score/100), "%"))
t1 <- kable({x <- subset(iris, Sepal.Length=="5.1", select = Petal.Length:Species);
names(x) <- sprintf("\textbf{%s}", names(x))
x},
format = "latex", booktabs = TRUE, row.names = FALSE,
align = 'c', escape = FALSE)
t2 <- kable({x <- subset(chickwts, feed=="horsebean", select = weight:feed);
names(x) <- sprintf("\textbf{%s}", names(x))
x},
format = "latex", booktabs = TRUE, row.names = FALSE,
align = 'c', escape = FALSE)
t3 <- kable({x <- subset(Data, Fruit=="Apple", select = Score:Percentage);
names(x) <- sprintf("\textbf{%s}", names(x))
x},
format = "latex", booktabs = TRUE, row.names = FALSE,
align = 'c', escape = FALSE)
cat(c("\begin{table}[!htb]
\begin{minipage}{.35\linewidth}
\centering",
t1,
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
t2,
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
t3,
"\end{minipage}
\end{table}"
))
```
对于您的 table 相关问题,您可以使用以下方法获得您想要的布局。粗体标题是通过将列名称包裹在 \code{textbf}
中获得的。如果您希望 table 标题可以适应 HTML 输出,您将需要额外的逻辑(或者您需要使用不同的 table 生成包)。这样,您需要在 kable
调用中设置 escape = TRUE
。
将内容居中就像在 kable
函数中使用 align
参数一样简单。
t1 <- kable({x <- subset(iris, Sepal.Length=="5.1", select = Petal.Length:Species);
names(x) <- sprintf("\textbf{%s}", names(x))
x},
format = "latex", booktabs = TRUE, row.names = FALSE,
align = 'c', escape = FALSE)
如果您在 # Iris
的上方和下方放置一行白色 space,您的部分标题将正确显示。
要使部分标题居中,您可能需要在 https://tex.stackexchange.com/
上搜索提示pixiedust
版本
出于某种我不明白的原因,我必须在这些 table 中设置 tabcolsep
参数。在 minipage
环境中,您需要使用 float = FALSE
。但这似乎可以在不编辑数据源的情况下很好地近似您想要的内容。
在使用百分比的地方,您需要使用 sprinkle(halign = "center", sanitize = TRUE)
---
title: <center> <h1>Call Centre Report</h1> </center>
mainfont: Arial
output:
pdf_document:
keep_tex: yes
sansfont: Arial
fig_crop: false
toc: true
classoption: landscape
fontsize: 14pt
geometry: margin=0.5in
header-includes:
- \usepackage{amssymb}
- \usepackage{arydshln}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{hhline}
- \usepackage{longtable}
- \usepackage{multirow}
- \usepackage[dvipsnames,table]{xcolor}
- \usepackage{booktabs}
---
<style>
.main-container {
max-width: 1200px !important;
}
</style>
```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```
\newpage
# Iris
```{r fig.width=18, fig.height=7, echo=FALSE, comment=" "}
library(ggplot2)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point() +
theme_classic()
```
<br>
<br>
```{r, echo=FALSE, results='asis'}
library(knitr)
library(xtable)
library(pixiedust)
t1 <- subset(iris,
Sepal.Length == "5.1",
select = Petal.Length:Species) %>%
dust(float = FALSE) %>%
sprinkle(tabcolsep = 10) %>%
sprinkle(bold = TRUE,
part = "head") %>%
sprinkle(halign = "center") %>%
medley_bw()
t2 <- subset(chickwts, feed=="horsebean", select = weight:feed) %>%
dust(float = FALSE) %>%
sprinkle(tabcolsep = 10) %>%
sprinkle(bold = TRUE,
part = "head") %>%
sprinkle(halign = "center") %>%
medley_bw()
t3 <- subset(mtcars, gear=="4", select = disp:wt) %>%
dust(float = FALSE) %>%
sprinkle(tabcolsep = 10) %>%
sprinkle(bold = TRUE,
part = "head") %>%
sprinkle(halign = "center") %>%
medley_bw()
cat(c("\begin{table}[!htb]
\begin{minipage}{.35\linewidth}
\centering",
print(t1, asis = FALSE),
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
print(t2, asis = FALSE),
"\end{minipage}%
\begin{minipage}{.35\linewidth}
\centering",
print(t3, asis = FALSE),
"\end{minipage}
\end{table}"
))
```