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

后端 未结 1 1561
后悔当初
后悔当初 2020-12-11 07:49

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 requi

相关标签:
1条回答
  • 2020-12-11 08:21

    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 \toprule 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}
    \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}
    

    0 讨论(0)
提交回复
热议问题