Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

latex - R markdown: How to create a table with images and text which should be knitted as PDF?

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 then
    • fig1 <- 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:

enter image description here

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)

enter image description here

Latex approach

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}
  • 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. "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)

Markdown approach (NO SOLUTION YET)

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, ...           |
|                  |             |
  • 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 caption and reference it with ef{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("![](", 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')
```
  • 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For your latex approach:

  • vertical alignment: of column 2 not working correctly

    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

    Using image paths is easy with graphicspath{{./older/Subfolder/}} in your header includes

other comments:

  • 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.

  • don't use oprule within the table, that's what midrule is made for

  • don't use hline when you load the booktabs package which provides alternatives with superior spacing



documentclass{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}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...