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:
-
width:固定列或表的宽度
alignment:列中的内容对齐
- 第1列中图像的顶部居中对齐
- 第2列中文本的左上对齐
文本内容:最好也很容易在代码中阅读
text content: is at best easy and good to read also in code
文本格式:
-
需要
- 文本格式设置,最多只能使用markdown语法,即粗体
- 需要换行符
- text formatting required, at best using markdown syntax, i.e, bold
- linebreaks required
图像路径:,因为图像存储在子目录中,因此最好使用缩写的图像路径,例如
image path: as images are stored in a subdirectory, at best use abbreviated image paths, like
-
figpath <- "Folder/Subfolder/"
然后 -
fig1 <- paste0(figpath, "image1.png")
figpath <- "Folder/Subfolder/"
and thenfig1 <- paste0(figpath, "image1.png")
标题:需要表标题
引用:需要向表中添加引用,例如[@ 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}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{table}[H]
\centering
\caption{My caption}
\begin{tabular}{@{} C{6cm} L{9cm} @{}}
\\
\toprule
Image & Description \\
\toprule
\includegraphics[width=60mm]{Folder/Subfolder/image1.png} &
\textbf{Lorem ipsum dolor sit amet} [@R-base] \linebreak mauris mauris sollicitudin malesuada amet.\\
& \\
\hline
& \\
\includegraphics[width=60mm]{Folder/Subfolder/image2.png} &
\textbf{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和简单降价文件的 已解决 , 不解决了bookdown/thesisdown
- 文本内容:向LaTex表中添加文本内容相当难看
- 文本格式:仅乳胶格式有效,即"\ textbf {}"
- 像
**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. "\textbf{}"
- 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]在markdown表中效果很好
- 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文件中引用表?在书本中,可以用
Table: (\#tab:md-table) My caption
标记表格,并用\ref{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 with\ref{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标记轻松地对表进行编码;表的md代码结构良好/可读性很好
- 文本格式:像
**bold**
这样的简单markdown文本格式效果很好 - 引用:在山墙桌中表现良好
- 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列的
- width:太宽了
- 垂直对齐:第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列的
- width:太宽了
- 图像:不显示
- Con:
- width: of column 2 far too wide
- images: do not show
-
垂直对齐:第2列无法正常工作
vertical alignment: of column 2 not working correctly
-
使用
[H]
作为浮动说明符通常不是一个好主意.这基本上保证了次优的图像放置.而是使用[htbp]
确保乳胶找到适合您图像的最佳位置. 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.
如果有帮助,我很乐意为Rmd文件提供方法以及生成的PDF.
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其他评论:
不要在表中使用
\toprule
,这就是为\midrule
做的don't use
\toprule
within the table, that's what\midrule
is made for在加载
booktabs
程序包时不要使用\hline
,该程序包提供了具有更好间距的替代方法don't use
\hline
when you load thebooktabs
package which provides alternatives with superior spacing\documentclass{article} \usepackage{booktabs} \usepackage{graphicx} \usepackage{array} \usepackage[export]{adjustbox} \newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}} \graphicspath{{./older/Subfolder/}} \begin{document} \begin{table}[htbp] \centering \caption{My caption} \label{foo} \begin{tabular}{@{} L{6cm} L{8.5cm} @{}} \toprule Image & Description \\ \midrule \includegraphics[width=60mm,valign=t]{example-image-duck} & \textbf{Lorem ipsum dolor sit amet} [@R-base] \linebreak mauris mauris sollicitudin malesuada amet.\\ \midrule \includegraphics[width=60mm,valign=t]{example-image-duck} & \textbf{Lorem ipsum dolor} [@R-bookdown]\linebreak sit amet, mauris mauris sollicitudin malesuada amet. \\ \bottomrule \end{tabular} \end{table} \ref{foo} \end{document}
这篇关于R markdown:如何创建包含图像和文本的表格,这些表格应被编织为PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!