【问题标题】:Using markdown in flextable caption在 flextable 标题中使用 markdown
【发布时间】:2021-12-18 12:10:46
【问题描述】:

我想在灵活的标题中使用粗体和斜体。我知道可以使用ftExtra 包将字体样式应用于灵活表的标题或正文中的文本,并且可以为officeR 的文字输出中的标题选择不同的输出样式。但要么这些不适用于标题 (ftExtra),要么不提供允许选择性应用粗体和斜体的样式。

这是一些在标题中带有预期降价的代码:

library(flextable)
library(dplyr)

iris[1:10,] %>% 
  flextable() %>% 
  set_caption(caption = "**Tab. 1:** First ten (*n* = 10) samples of the Iris species dataset")

标题应为“Tab. 1: Iris 物种数据集的前十个 (n = 10) 个样本”

更新

根据 dario 的评论,一个可能的解决方案是:

  library(flextable)
  library(dplyr)

  iris[1:10,] %>% 
  flextable() %>% 
  
  add_header_lines("") %>% 
  
  compose(
    i = 1, part = "header",
    value = as_paragraph(
      as_chunk("Table 1: ", props = fp_text(bold = TRUE)),
      as_chunk("First ten ("),
      as_chunk("n", props = fp_text(italic = TRUE)),
      as_chunk(" = 10) samples of the Iris species dataset")
    )
  )

非常感谢。最后,我需要在不同的章节中制作大约 20 个表格,因此我将尝试 David 的建议,在编写降价时构建标题。但很高兴知道有办法,虽然使用compose 很笨拙且不灵活。

【问题讨论】:

  • 也许this 可以提供帮助。不确定它是否适用于您的用例...
  • set_caption 不支持降价。为什么不在你的表格之前或之后使用简单的降价(如果我没记错的话,你需要在它前面加上Table:
  • @Jens,看看我的解决方案,它更简单;)
  • @jens 我不认为标题应该在表格中,因为它必须在表格列表中可用,而这个解决方案无法实现。 manro 的解决方案让标题在表格列表中,这很好
  • @DavidGohel 是的,我同意。最后我需要多个表。但是,它可能是单个表的快速解决方案。

标签: r caption flextable


【解决方案1】:

我为你准备了一个markdown模板:

---
title: "Hello World"
header-includes:
- \usepackage{caption}

output:
  pdf_document: 
       latex_engine: xelatex
---

```{r, include = FALSE}
library(flextable)
library(dplyr)    
table2 <- flextable(mtcars[1:5, 1:6])        
```     
   
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

\begin{table}[hbtp]
\captionof{table}{Text of the caption.}
`r table2`
\end{table}

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

输出

【讨论】:

  • 谢谢 - 我想现在可以轻松处理标题了。
  • @Jens gute Arbeit ;)
猜你喜欢
  • 2019-03-11
  • 1970-01-01
  • 2013-10-20
  • 2021-09-04
  • 2020-02-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多