R markdown:如何创建一个包含应该编织为 PDF 的图像和文本的表格? [英] R markdown: How to create a table with images and text which should be knitted as PDF?
问题描述
我想在使用 R markdown 编译的 PDF 报告中包含一个包含 2 列的表格,包括图像和文本(图像描述).在这样做时,我对我的表有以下要求:
I would like to include a table with 2 columns including images and text (image descriptions) in PDF report compiled with R markdown. In doing so, I have the following requirements for my table:
宽度:固定列或表格宽度
对齐方式: 列中的内容对齐方式
- 第 1 列中图像的顶部居中对齐
- 第 2 列中的文本左上对齐
文本内容:在代码中也很容易阅读
文本格式:
- 需要文本格式,最好使用 markdown 语法,即 bold
- 需要换行符
图片路径: 因为图片存放在子目录中,最好使用缩写的图片路径,比如
image path: as images are stored in a subdirectory, at best use abbreviated image paths, like
figpath <- "文件夹/子文件夹/"
然后fig1 <- paste0(figpath, "image1.png")
caption:需要表格标题
引文:需要添加对表的引用,即像 [@R-base]
citations: adding references to the table is required, i.e., like [@R-base]
引用:其他地方的表是必需的
理想情况下,表格应如下所示:
In ideal, the table would look like this:
我基于 LaTex 语法、markdown 语法和 R markdown 语法(使用 kable 和 kableExtra)做了几次尝试,见下文 MWE.然而,这些方法都没有产生令人满意的结果.LaTex 方法最接近,但不允许包含引用.
I made several attempts based on LaTex syntax, markdown syntax and R markdown syntax (with kable and kableExtra), see MWEs below. However, none of the approaches yield a satisfying result. The LaTex approach comes closest but does not allow to include citations.
带有图片的表格稍后应该包含在使用 huskydown 编译的报告(论文)中,该报告与 thesisdown/bookdown 相关.非常感谢任何帮助!
The table with images should later be included in a report (thesis) compiled with huskydown, which is related to thesisdown/bookdown. Any help is greatly appreciated!
下表总结了我的方法,下面提供了 MWE(有关改进的 LaTex MWE,请参阅@samcarter 的回复)
Table below summarizes my approaches, MWEs provided below (for improved LaTex MWE see reply by @samcarter)
YAML header:
header-includes:
usepackage{array}
ewcolumntype{L}[1]{>{
aggedrightlet
ewline\arraybackslashhspace{0pt}}m{#1}}
ewcolumntype{C}[1]{>{centeringlet
ewline\arraybackslashhspace{0pt}}m{#1}}
ewcolumntype{R}[1]{>{
aggedleftlet
ewline\arraybackslashhspace{0pt}}m{#1}}
egin{table}[H]
centering
caption{My caption}
egin{tabular}{@{} C{6cm} L{9cm} @{}}
\
oprule
Image & Description \
oprule
includegraphics[width=60mm]{Folder/Subfolder/image1.png} &
extbf{Lorem ipsum dolor sit amet} [@R-base] linebreak mauris mauris sollicitudin malesuada amet.\
& \
hline
& \
includegraphics[width=60mm]{Folder/Subfolder/image2.png} &
extbf{Lorem ipsum dolor} [@R-bookdown]linebreak sit amet, mauris mauris sollicitudin malesuada amet.
end{tabular}
end{table}
专业版:
- 垂直对齐:第 1 列的工作正常
- 标题:易于添加
- 文本格式: 可以使用 "linebreak" 进行换行(但由于文本块而效果不佳)
- 在 LaTex 中对表格进行通用编码
- vertical alignment: of column 1 working somehow correctly
- caption: easy to add
- text formatting: linebreaks feasible with "linebreak" (but does not work so nicely due to block text)
- generally versatile coding of tables in LaTex
缺点:
- 垂直对齐: 第 2 列无法正常工作 - 已解决仅适用于 LaTex 和简单的 markdown 文件,不能解决了 bookdown/thesisdown
- 文本内容:向 LaTex 表格添加文本内容相当难看
- 文本格式:只有乳胶格式有效,即 extbf{}"
**bold**
等简单的 markdown 文本格式(显然)在 LaTex 表格中不起作用- 图片路径:不能包含缩写的图片路径(已解决)
- 引文: 不适用于 LaTex 表 - 未解决
- 引用:如何引用 LaTex 表?(已解决)
- vertical alignment: of column 2 not working correctly - SOLVED for LaTex and simple markdown file ONLY, NOT solved for bookdown/thesisdown
- text content: adding text content to LaTex table is rather ugly
- text formatting: only latex formatting works, i.e. " extbf{}"
- Simple markdown text formatting like
**bold**
(obviously) does not work in LaTex table - image path: can not include abbreviated image paths (SOLVED)
- citations: do NOT work in LaTex table - NOT solved
- referencing: how to reference the LaTex table? (SOLVED)
Table: Caption of my table <!-- Table: (#tab:myTable-reference) Caption of my table --> | Image | Description | | :-------: | :----------- | | ![](Folder/Subfolder/image1.png){#id .class height=50%} | **Image description** [@R-base] <br/>Lorem ipsum dolor sit amet, ... | | ![](Folder/Subfolder/image2.png){#id .class height=50%} | **Image description** [@R-bookdown] <br/>Lorem ipsum dolor sit amet, ... | | | |
专业版:
- 标题:易于添加
- 垂直对齐:第 1 列工作正常
- 文本格式:
**bold**
等简单的 Markdown 文本格式效果很好 - 引文: 像 [@R-bookdown] 在降价表中效果很好
- caption: easy to add
- vertical alignment: of column 1 working correctly
- text formatting: simple markdown text formatting like
**bold**
works well - citations: like [@R-bookdown] work well in markdown table
缺点:
- 垂直对齐:第 2 列无法正常工作
- 文本内容:在markdown表中添加文本内容比较难看
- 文本格式:
<br/>
不可行的换行符 - 图片路径:不能包含缩写的图片路径
- 引用: 如何在简单的markdown文件中引用表格?在 bookdown 中,可以用
Table: (#tab:md-table) My caption
标记表格,并用ef{tab:md-table}
引用它.但是在一个简单的 md 文件中呢?
- vertical alignment: of column 2 not working correctly
- text content: adding text content to markdown table is rather ugly
- text formatting: linebreaks NOT feasible with
<br/>
- image path: can not include abbreviated image paths
- referencing: how to reference table in simple markdown file? In bookdown one can label the table with
Table: (#tab:md-table) My caption
and reference it withef{tab:md-table}
. But how about in a simple md file?
Refer to this table with [foo] or @ref(tab:foo) or @ref(fig:foo). (ref:foo-caption) caption (ref:foo-scaption) short caption ```{r foo, echo=FALSE, out.width='90%', fig.align = "center", fig.cap='(ref:foo-caption)', fig.scap='(ref:foo-scaption)', results='asis'} library(stringi) some_text <- stri_rand_lipsum(1) some_text <- paste("**Image description**", "[@R-bookdown]", "<br/>", some_text) figpath <- "Folder/Subfolder/" dat <- data.frame( Image = c( paste0("![](", figpath, "image1.png){#id .class height=120px}"), paste0("![](", figpath, "image2.png){#id .class height=120px}") ), Description = c( some_text, # TEXT IMAGE 1 some_text # TEXT IMAGE 2 ) ) library(knitr) kable(dat, format = 'pandoc') ```
专业版:
- 垂直对齐:第 1 列工作正常
- 文本内容:在 kable 表中添加文本内容相当不错
- 图片路径:可以包含缩写的图片路径
- 引用:代码块标签便于引用
- 在 R markdown 中轻松编码表格;表的 md 代码结构良好/可读性好
- 文本格式:
**bold**
等简单的 Markdown 文本格式效果很好 - 引文:在 kable 表中表现良好
- vertical alignment: of column 1 is working correctly
- text content: adding text content to kable table is rather nice
- image path: can include abbreviated image paths
- referencing: easy to reference with label of code chunk
- easy coding of tables in R markdown; md code of table is nicely structured/readable
- text formatting: simple markdown text formatting like
**bold**
works well - citations: work well in kable table
缺点:
- 宽度:第 2 列太宽了
- 垂直对齐:第 2 列无法正常工作
- 文本格式:
<br/>
不可行的换行符 - 说明: 无法正常工作
- width: of column 2 far too wide
- vertical alignment: of column 2 not working correctly
- text formatting: linebreaks NOT feasible with
<br/>
- caption: not working as usual
Refer to this table with [foo2] or @ref(tab:foo2) or @ref(fig:foo2). (ref:foo2-caption) caption (ref:foo2-scaption) short caption ```{r foo2, echo=FALSE, out.width='90%', fig.align = "center", fig.cap='(ref:foo2-caption)', fig.scap='(ref:foo2-scaption)', results='asis'} library(kableExtra) kable(dat) %>% kable_styling(full_width = F) %>% column_spec(1, width = "30em") ```
- 缺点:
- 宽度:第 2 列太宽了
- 图片:不显示
- Con:
- width: of column 2 far too wide
- images: do not show
垂直对齐:第 2 列无法正常工作
vertical alignment: of column 2 not working correctly
使用
[H]
作为浮动说明符通常不是一个好主意.这基本上保证了次优的图像放置.而是使用[htbp]
来确保 Latex 为您的图像找到最佳位置.using
[H]
as floating specifier is usually not a good idea. This basically guarantees a suboptimal image placement. Instead use[htbp]
to ensure that latex find the best possible locatiosn for your image.
如果有任何帮助,我很乐意提供包含我的方法以及生成的 PDF 的 Rmd 文件.
I am happy to provide an Rmd file with my approaches as well as the generated PDF if of any help.
推荐答案
对于您的乳胶方法:
您可以通过组合 p 列(而不是您使用的 m 列)和顶部对齐的图像来获得所需的对齐方式.对于顶部对齐的图像,将
usepackage[export]{adjustbox}
添加到您的标头包含中,并将,valign=t
添加到图像选项you get your desired alignment by combining a p column (instead of the m column you used) and a top aligned image. For the top aligned image add
usepackage[export]{adjustbox}
to your header includes and,valign=t
to the image options图片路径:不能包含缩写的图片路径
image path: can not include abbreviated image paths
在标题中包含
graphicspath{{./older/Subfolder/}}
可以轻松使用图像路径Using image paths is easy with
graphicspath{{./older/Subfolder/}}
in your header includes其他评论:
不要在表格中使用
oprule
,这就是midrule
的用途don't use
oprule
within the table, that's whatmidrule
is made for在加载
booktabs
包时不要使用hline
,因为它提供了具有优越间距的替代方案don't use
hline
when you load thebooktabs
package which provides alternatives with superior spacingdocumentclass{article} usepackage{booktabs} usepackage{graphicx} usepackage{array} usepackage[export]{adjustbox} ewcolumntype{L}[1]{>{ aggedrightarraybackslash}p{#1}} graphicspath{{./older/Subfolder/}} egin{document} egin{table}[htbp] centering caption{My caption} label{foo} egin{tabular}{@{} L{6cm} L{8.5cm} @{}} oprule Image & Description \ midrule includegraphics[width=60mm,valign=t]{example-image-duck} & extbf{Lorem ipsum dolor sit amet} [@R-base] linebreak mauris mauris sollicitudin malesuada amet.\ midrule includegraphics[width=60mm,valign=t]{example-image-duck} & extbf{Lorem ipsum dolor} [@R-bookdown]linebreak sit amet, mauris mauris sollicitudin malesuada amet. \ ottomrule end{tabular} end{table} ef{foo} end{document}
这篇关于R markdown:如何创建一个包含应该编织为 PDF 的图像和文本的表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!