轻松记录您
灵感和创意

r-knitr / rmarkdown / Latex:如何交叉引用图形和表格?

我正在尝试交叉引用knitr / rmarkdown生成的PDF中的图形和表格。 关于SO和tex.stackexchange(例如,在这里和在这里),存在一些问题,这些建议建议以内联方式添加header.tex,其中kable是块标签。 但是,当我在latex文档中尝试该操作时,会得到pandoc,该数字应为数字。 我想了解如何获得交叉引用才能正常工作。

下面是一个可重现的示例。 有两个文件:header.tex文件和我包括的kable文件,以防万一它影响答案(尽管是否包含latex文件也存在相同的问题)。

在header.tex文件中,存在三个交叉引用示例。 示例1是对其进行交叉引用失败的图形(显示kable而不是图形编号)。 还有第二种注释掉的尝试(基于此SO答案),在该尝试中,我尝试在该块之前和之后使用latex标记设置图形环境,标签和标题,但是当我尝试编织时会导致pandoc错误 文件。 错误是:

header.tex
示例2使用header.tex和交叉引用作品。 示例3使用kable,并且交叉引用失败。

PDF输出的屏幕截图包含在本文的底部。

header.tex文件

title: |
| My Title
author: |
| eipi10
| Department of Redundancy Department
date: “r format(Sys.time(), '%B %e, %Y')
output:
pdf_document:
fig_caption: yes
includes:
in_header: header.tex
keep_tex: yes
fontsize: 11pt
geometry: margin=1in
graphics: yes

{r setup, include=FALSE} knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning=FALSE, fig.height=2, fig.width=4) {{EJS0}} <!-- Now, let's take a look at this other plot in Figure \ref{fig:fig2}. --> <!-- \begin{figure} --> <!-- {r fig2, echo=FALSE} –>
<!– plot(mtcars$cyl, mtcars$mpg) –>
<!– --> <!-- \caption{This is another caption} --> <!-- \label{fig:fig2} --> <!-- \end{figure} --> # Example 2: Some more text. See Table \ref{tab:tab1} below. {r echo=FALSE, results=”asis”}
library(xtable)
print.xtable(
xtable(mtcars[1:3,1:4], label=”tab:tab1″, caption=”An xtable table”),
comment=FALSE)


# Example 3: `kable`

Some more text. See Table \ref{tab:tab2} below.

```{r tab2, echo=FALSE}
library(knitr)
kable(mtcars[1:3,1:4], caption="A `kable` table")

header.tex file
% Caption on top
% https://tex.stackexchange.com/a/14862/4762
\usepackage{floatrow}
\floatsetup[figure]{capposition=top}
\floatsetup[table]{capposition=top}

未经允许不得转载:坚果云Markdown » r-knitr / rmarkdown / Latex:如何交叉引用图形和表格?
分享到: 更多 (0)

坚果云Markdown轻松记录您 灵感和创意

坚果云Markdown下载坚果云Markdown介绍