title: R markdown: How to create a table with images and text which should be knitted as PDF?
body: <p>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:</p>
  
  <ul>
  <li><p><em>width:</em> fixed column or table width</p></li>
  <li><p><em>alignment:</em> content alignment in columns</p>
  
  <ul>
  <li>top center alignment of an image in column 1 </li>
  <li>top left alignment of text in column 2 </li>
  </ul></li>
  <li><p><em>text content:</em> is at best easy and good to read also in code</p></li>
  <li><p><em>text formatting:</em> </p>
  
  <ul>
  <li>text formatting required, at best using markdown syntax, i.e, <strong>bold</strong></li>
  <li>linebreaks required</li>
  </ul></li>
  <li><p><em>image path:</em> as images are stored in a subdirectory, at best use abbreviated image paths, like </p>
  
  <ul>
  <li><code>figpath &lt;- "Folder/Subfolder/"</code> and then </li>
  <li><code>fig1 &lt;- paste0(figpath, "image1.png")</code></li>
  </ul></li>
  <li><p><em>caption:</em> table caption is required </p></li>
  <li><p><em>citations:</em> adding references to the table is required, i.e., like [@R-base]</p></li>
  <li><p><em>referencing:</em> the table elsewhere is required </p></li>
  </ul>
  
  <p>In ideal, the table would look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/szZB4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/szZB4.png" alt="enter image description here"></a></p>
  
  <p>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.</p>
  
  <p>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!</p>
  
  <p>Table below summarizes my approaches, MWEs provided below (for improved LaTex MWE see reply by @samcarter)</p>
  
  <p><a href="https://i.stack.imgur.com/TxzQW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TxzQW.png" alt="enter image description here"></a></p>
  
  <h2>Latex approach</h2>
  
  <pre><code>YAML header:
  header-includes:
    \usepackage{array}
    \newcolumntype{L}[1]{&gt;{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
    \newcolumntype{C}[1]{&gt;{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
    \newcolumntype{R}[1]{&gt;{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
  
  
  \begin{table}[H]
  \centering
  \caption{My caption}
  \begin{tabular}{@{} C{6cm} L{9cm} @{}}
  \\
       \toprule
         Image &amp; Description \\
       \toprule 
        \includegraphics[width=60mm]{Folder/Subfolder/image1.png} &amp;
         \textbf{Lorem ipsum dolor sit amet} [@R-base] \linebreak mauris mauris sollicitudin malesuada amet.\\
        &amp; \\
        \hline
        &amp; \\
        \includegraphics[width=60mm]{Folder/Subfolder/image2.png} &amp;
        \textbf{Lorem ipsum dolor} [@R-bookdown]\linebreak sit amet, mauris mauris sollicitudin malesuada amet. \
  \end{tabular}
  \end{table}
  </code></pre>
  
  <ul>
  <li><p><strong>Pro:</strong></p>
  
  <ul>
  <li><em>vertical alignment:</em> of column 1 working somehow correctly</li>
  <li><em>caption:</em> easy to add </li>
  <li><em>text formatting:</em> linebreaks feasible with "\linebreak" (but does not work so nicely due to block text)</li>
  <li>generally versatile coding of tables in LaTex</li>
  </ul></li>
  <li><p><strong>Con:</strong> </p>
  
  <ul>
  <li><em>vertical alignment:</em> of column 2 not working correctly - <strong><em>SOLVED</strong> for LaTex and simple markdown file ONLY</em>, <strong><em>NOT solved for bookdown/thesisdown</em></strong></li>
  <li><em>text content:</em> adding text content to LaTex table is rather ugly</li>
  <li><em>text formatting:</em> only latex formatting works, i.e. "\textbf{}"</li>
  <li>Simple markdown text formatting like <code>**bold**</code> (obviously) does not work in LaTex table</li>
  <li><em>image path:</em> can not include abbreviated image paths (<strong>SOLVED</strong>)</li>
  <li><em>citations:</em> do NOT work in LaTex table - <strong><em>NOT solved</em></strong></li>
  <li><em>referencing:</em> how to reference the LaTex table? (<strong>SOLVED</strong>)</li>
  </ul></li>
  </ul>
  
  <h2>Markdown approach (<em>NO SOLUTION YET</em>)</h2>
  
  <pre><code>Table: Caption of my table
  &lt;!-- Table: (\#tab:myTable-reference) Caption of my table --&gt;
  
  | Image | Description |
  | :-------: | :----------- |
  | ![](Folder/Subfolder/image1.png){#id .class height=50%} | **Image description** [@R-base]  &lt;br/&gt;Lorem ipsum dolor sit amet, ...          |
  | ![](Folder/Subfolder/image2.png){#id .class height=50%} | **Image description** [@R-bookdown] &lt;br/&gt;Lorem ipsum dolor sit amet, ...           |
  |                  |             |
  </code></pre>
  
  <ul>
  <li><p><strong>Pro:</strong></p>
  
  <ul>
  <li><em>caption:</em> easy to add </li>
  <li><em>vertical alignment:</em> of column 1 working correctly</li>
  <li><em>text formatting:</em> simple markdown text formatting like <code>**bold**</code> works well</li>
  <li><em>citations:</em> like [@R-bookdown] work well in markdown table</li>
  </ul></li>
  <li><p><strong>Con:</strong> </p>
  
  <ul>
  <li><em>vertical alignment:</em> of column 2 not working correctly</li>
  <li><em>text content</em>: adding text content to markdown table is rather ugly</li>
  <li><em>text formatting:</em> linebreaks NOT feasible with <code>&lt;br/&gt;</code></li>
  <li><em>image path:</em> can not include abbreviated image paths</li>
  <li><em>referencing:</em> how to reference table in simple markdown file? In bookdown one can label the table with <code>Table: (\#tab:md-table) My caption</code> and reference it with <code>\ref{tab:md-table}</code>. But how about in a simple md file?</li>
  </ul></li>
  </ul>
  
  <h2>kable approach (<em>NO SOLUTION YET</em>)</h2>
  
  <pre><code>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 &lt;- stri_rand_lipsum(1)
  some_text &lt;- paste("**Image description**", "[@R-bookdown]", "&lt;br/&gt;", some_text)
  figpath &lt;- "Folder/Subfolder/"
  dat &lt;- 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')
  ```
  </code></pre>
  
  <ul>
  <li><p><strong>Pro:</strong></p>
  
  <ul>
  <li><em>vertical alignment:</em> of column 1 is working correctly</li>
  <li><em>text content:</em> adding text content to kable table is rather nice</li>
  <li><em>image path:</em> can include abbreviated image paths</li>
  <li><em>referencing:</em> easy to reference with label of code chunk</li>
  <li>easy coding of tables in R markdown; md code of table is nicely structured/readable</li>
  <li><em>text formatting:</em> simple markdown text formatting like <code>**bold**</code> works well</li>
  <li><em>citations:</em> work well in kable table</li>
  </ul></li>
  <li><p><strong>Con:</strong> </p>
  
  <ul>
  <li><em>width:</em> of column 2 far too wide</li>
  <li><em>vertical alignment:</em> of column 2 not working correctly</li>
  <li><em>text formatting:</em> linebreaks NOT feasible with <code>&lt;br/&gt;</code></li>
  <li><em>caption:</em> not working as usual </li>
  </ul></li>
  </ul>
  
  <h2>kableExtra approach (<em>NO SOLUTION YET</em>)</h2>
  
  <pre><code>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) %&gt;%
    kable_styling(full_width = F) %&gt;%
    column_spec(1, width = "30em")
  ```
  </code></pre>
  
  <ul>
  <li><strong>Con:</strong> 
  
  <ul>
  <li><em>width:</em> of column 2 far too wide</li>
  <li><em>images:</em> do not show</li>
  </ul></li>
  </ul>
  
  <p>I am happy to provide an Rmd file with my approaches as well as the generated PDF if of any help.</p>