mav*_*cks 5 markdown latex r-markdown kableextra kable
我想在用 R markdown 编译的 PDF 报告中包含一个包含 2 列的表格,其中包括图像和文本(图像描述)。在这样做时,我对我的表有以下要求:
宽度:固定列或表格宽度
对齐:列中的内容对齐
文本内容:在代码中也很容易阅读
文本格式:
图像路径:由于图像存储在子目录中,最好使用缩写的图像路径,例如
figpath <- "Folder/Subfolder/" 进而 fig1 <- paste0(figpath, "image1.png")标题:表格标题是必需的
引文:需要添加对表的引用,例如 [@R-base]
参考:其他地方的表格是必需的
理想情况下,该表如下所示:
我根据 LaTex 语法、markdown 语法和 R markdown 语法(使用 kable 和 kableExtra)进行了几次尝试,请参阅下面的 MWE。然而,没有一种方法产生令人满意的结果。LaTex 方法最接近,但不允许包含引文。
带有图像的表格稍后应包含在使用 huskydown 编译的报告(论文)中,该报告与 thesisdown/bookdown 相关。任何帮助是极大的赞赏!
下表总结了我的方法,下面提供的 MWE(有关改进的 LaTex MWE,请参阅 @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}
Run Code Online (Sandbox Code Playgroud)
亲:
骗局:
**bold**(显然)在 LaTex 表中不起作用Table: Caption of my table
<!-- Table: (\#tab:myTable-reference) Caption of my table -->
| Image | Description |
| :-------: | :----------- |
| {#id .class height=50%} | **Image description** [@R-base] <br/>Lorem ipsum dolor sit amet, ... |
| {#id .class height=50%} | **Image description** [@R-bookdown] <br/>Lorem ipsum dolor sit amet, ... |
| | |
Run Code Online (Sandbox Code Playgroud)
亲:
**bold**效果很好骗局:
<br/>Table: (\#tab:md-table) My caption并用 引用它\ref{tab:md-table}。但是在一个简单的 md 文件中呢?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("{#id .class height=120px}"),
paste0("{#id .class height=120px}")
),
Description = c(
some_text, # TEXT IMAGE 1
some_text # TEXT IMAGE 2
)
)
library(knitr)
kable(dat, format = 'pandoc')
```
Run Code Online (Sandbox Code Playgroud)
亲:
**bold**效果很好骗局:
<br/>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")
```
Run Code Online (Sandbox Code Playgroud)
如果有任何帮助,我很高兴提供包含我的方法以及生成的 PDF 的 Rmd 文件。
对于您的乳胶方法:
垂直对齐:第 2 列无法正常工作
您可以通过组合 ap 列(而不是您使用的 m 列)和顶部对齐图像来获得所需的对齐方式。对于顶部对齐的图像,添加\usepackage[export]{adjustbox}到标题包含和,valign=t图像选项中
图像路径:不能包含缩写图像路径
\graphicspath{{./older/Subfolder/}}在标题中使用图像路径很容易
其他的建议:
用作[H]浮动说明符通常不是一个好主意。这基本上保证了次优的图像放置。相反,请[htbp]确保乳胶找到最适合您的图像的位置。
不要\toprule在表中使用,这就是\midrule目的
\hline加载booktabs提供具有优越间距的替代方案的包 时请勿使用
\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}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1867 次 |
| 最近记录: |