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: fixed column or table width
alignment: content alignment in columns
- top center alignment of an image in column 1
- top left alignment of text in column 2
text content: is at best easy and good to read also in code
text formatting:
- 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/"and thenfig1 <- paste0(figpath, "image1.png")
caption: table caption is required
citations: adding references to the table is required, i.e., like [@R-base]
referencing: the table elsewhere is required
In ideal, the table would look like this:
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.
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!
Table below summarizes my approaches, MWEs provided below (for improved LaTex MWE see reply by @samcarter)
Latex approach
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} Pro:
- 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
Con:
- 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)
Markdown approach (NO SOLUTION YET)
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, ... | | | | Pro:
- 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
Con:
- 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 captionand reference it with\ref{tab:md-table}. But how about in a simple md file?
kable approach (NO SOLUTION YET)
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') ``` Pro:
- 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
Con:
- 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
kableExtra approach (NO SOLUTION YET)
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") ``` - Con:
- width: of column 2 far too wide
- images: do not show
I am happy to provide an Rmd file with my approaches as well as the generated PDF if of any help.


