latex - R markdown:如何创建包含图像和文本的表格,这些表格应被编织为PDF?

标签 latex markdown r-markdown kable kableextra

我想在用R markdown编译的PDF报告中包括一个包含2列的表格,其中包括图像和文本(图像描述)。
为此,我对表有以下要求:

  • width:固定列或表的宽度
  • 对齐:列中的内容对齐
  • 第1列中图像的顶部中心对齐
  • 第2列中文本的左上对齐
  • 文本内容:在代码
  • 中充其量也很容易阅读
  • 文本格式:

    需要
  • 文本格式,最好使用markdown语法,即粗体
  • 需要换行符
  • 图像路径:由于图像存储在子目录中,因此最好使用缩略图像路径,例如
  • figpath <- "Folder/Subfolder/"然后
  • fig1 <- paste0(figpath, "image1.png")
  • 标题:需要表格标题
  • 引文:需要向表中添加引用,例如[@ R-base]
  • 引用:其他表是必需的

  • 理想情况下,该表如下所示:

    enter image description here

    我基于LaTex语法,markdown语法和R markdown语法(使用kable和kableExtra)进行了几次尝试,请参见下面的MWE。
    但是,没有一种方法能令人满意。
    LaTex方法最接近,但不允许包含引用。

    带有图像的表格应稍后包含在由huskydown编译的报告(论文)中,该报告与论文/书本有关。
    任何帮助是极大的赞赏!

    下表总结了我的方法,下面提供了MWE(有关改进的LaTex MWE,请参见@samcarter的回复)

    enter image description here

    乳胶法
    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}
    
  • 专业版:
  • 垂直对齐:第1列的正确工作
  • 标题:易于添加
  • 文本格式:“\ linebreak”可以使用换行符(但是由于块文本而不能很好地工作)
  • 通常在LaTex中对表进行通用编码
  • 骗局:
  • 垂直对齐:第2列无法正常工作-仅针对LaTex和简单的markdown文件解决了,对于bookdown / thesisdown 未解决
  • 文本内容:将文本内容添加到LaTex表相当难看
  • 文本格式:仅乳胶格式有效,即“\ textbf {}”
  • **bold**这样的简单markdown文本格式(显然)在LaTex表
  • 中不起作用
  • 图像路径:不能包含缩写的图像路径(已解决)
  • 引文:在LaTex表中不起作用-未解决
  • 引用:如何引用LaTex表? (解决了)

  • Markdown 方式(尚无解决方案)
    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, ...           |
    |                  |             |
    
  • 专业版:
  • 标题:易于添加
  • 垂直对齐:第1列的
  • 正常工作
  • 文本格式:像**bold**这样的简单markdown文本格式效果很好
  • 引文:像[@ R-bookdown]在markdown表中效果很好
  • 骗局:
  • 垂直对齐:第2列无法正常工作
  • 文本内容:将文本内容添加到markdown表中相当难看
  • 文本格式:换行符不适用于<br/>
  • 图像路径:不能包含缩略图像路径
  • 引用:如何在简单的markdown文件中引用表?在书本中,可以使用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("![](", 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')
    ```
    
  • 专业版:
  • 垂直对齐:第1列正常工作
  • 文本内容:将文本内容添加到kable表是相当不错的
  • 图像路径:可以包含缩写的图像路径
  • 引用:易于使用代码块
  • 的标签进行引用
  • 在R markdown中轻松编码表;表的md代码结构良好/可读性
  • 文本格式:像**bold**这样的简单markdown文本格式效果很好
  • 引文:在电缆表
  • 中工作良好
  • 骗局:
  • width:第2列的
  • 太宽
  • 垂直对齐:第2列无法正常工作
  • 文本格式:换行符不适用于<br/>
  • 标题:不能像往常一样工作

  • kableExtra方法(尚无解决方案)
    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")
    ```
    
  • 骗局:
  • width:第2列的
  • 太宽
  • 图片:不显示

  • 如果有帮助,我很乐意提供一个带有我的方法的Rmd文件以及生成的PDF。

    最佳答案

    对于您的乳胶方法:

  • 垂直对齐:第2列无法正常工作

    您可以通过组合p列(而不是使用的m列)和顶部对齐的图像来获得所需的对齐方式。对于顶部对齐的图像,将\usepackage[export]{adjustbox}添加到标题包括,并将,valign=t添加到图像选项
  • 图像路径:不能包含缩写的图像路径

    标题中包含
  • \graphicspath{{./older/Subfolder/}}可以轻松使用图像路径

    其他的建议:

    使用[H]作为 float 说明符的
  • 通常不是一个好主意。这基本上保证了次优的图像放置。而是使用[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}
    

    enter image description here

    关于latex - R markdown:如何创建包含图像和文本的表格,这些表格应被编织为PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58204272/

    相关文章:

    latex - "Latex Error: The font size command/normalsize is not defined"的原因是什么

    latex - 在 latex 中手动定义页码?

    github - 我可以在 Markdown 中合并表格行吗

    ruby - Middleman & Haml 与 Github 风格的围栏代码块

    r-markdown - 调整 R Markdown HTML 输出的输出宽度

    r - 在 R markdown 中循环

    matlab - 从 Matlab 发布到 Latex

    Rmarkdown错误: "! Paragraph ended before\@fileswith@ptions was complete"

    ruby - 语法错误,意外 ')' ,期望 '='

    imagemagick - 由于 magick_image_readpath 中的错误,无法将 kable 保存为 PDF