【问题标题】:Change heading and include bold font in xtable to .pdf from RMarkdown更改标题并将 xtable 中的粗体字体从 RMarkdown 更改为 .pdf
【发布时间】:2017-09-13 16:06:42
【问题描述】:

我是 RMarkdown 和 LaTex 的初学者,虽然喜欢在 R 中创建报告!我已经拿起了一些代码来创建我所追求的确切 .pdf 报告,尽管在某些美学方面存在困难。

我的数据是机密的,但这里是使用内置 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 标题设为粗体并居中,但将其保留为目录中的标题。
    • 将三个表格中的每个表格的标题加粗
    • 将每个表格中的所有文本居中
    • 删除表 1 中 1.7, 0.5, setosa 之后的大间隙,从而删除该行中的其他表。

我们将不胜感激任何帮助或指向特定文档的链接。

更新

下面提供的答案效果很好,除了我的数据包含一个百分比列,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}"
)) 
```

【问题讨论】:

    标签: r markdown knitr xtable


    【解决方案1】:

    对于与表格相关的问题,您可以使用以下方法来获得所需的布局。粗体标题是通过将列名包装在\code{textbf} 中获得的。如果您希望有可以适应 HTML 输出的表格标题,您将需要额外的逻辑(或者您需要使用不同的表格生成包)。有了这个,你需要在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 的上方和下方放置一行空白,您的部分标题将正确显示。

    要使章节标题居中,您可能需要在 https://tex.stackexchange.com/ 上搜索提示

    pixiedust版本

    出于某种我不明白的原因,我不得不在这些表中设置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}"
    )) 
    ```
    

    【讨论】:

    • 感谢您的回复 - 此代码在我所有不包含百分比的数据框上都能完美运行。但是,当我使用包含百分比的数据运行它时,.pdf 文档没有编译。当列中有 % 符号时,您的上述建议可能不起作用?当我在没有您编辑的情况下运行我的代码时,虽然没有粗体文​​本,但 .pdf 已编译。请查看我更新的帖子,再次感谢您的所有帮助。
    • 它不适用于百分比,因为我关闭了转义。未转义的% 是 LaTeX 中的注释字符。如果您在% 前面使用两个反斜杠,它将起作用(例如15\\%
    • 谢谢,我在 data.frame 中添加了两个反斜杠,这很有效。但是,是否有替代方法,而不是直接编辑 data.frame?
    • pixiedust 无需直接编辑数据即可很好地创建表格。如何获取乳胶代码以便按照您的方式使用它更加冗长并且不是很明显。当我开始工作时,我可以举一个例子。
    • pixiedust 添加示例。
    猜你喜欢
    • 2019-07-07
    • 2023-03-02
    • 2017-04-27
    • 1970-01-01
    • 2014-08-16
    • 2019-10-09
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    相关资源
    最近更新 更多