我无法使用knitr生成\ label {fig:mwe-plot} [英] I can't generate \label{fig:mwe-plot} with knitr

查看:66
本文介绍了我无法使用knitr生成\ label {fig:mwe-plot}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用knitr通过knitr*.Rmd文件转换为*.md文件时,我无法为图生成\label{};然后通过pandoc转换为*.pdf.

I am having trouble generating \label{} for plots when using knitr to go from a *.Rmd file to a *.md file via knitr; and then converting to *.pdf via pandoc.

我的*.Rmd的MWE如下:

```{r Setup, include=FALSE, results="hide", warning=FALSE}
opts_chunk$set(dev="cairo_pdf", fig.lp="fig:", echo=FALSE, results="hide", 
               message=FALSE, warning=FALSE)
```

```{r mwe-plot, fig.cap = "MWE plot."}
library(ggplot2)
ggplot(mtcars, aes(factor(cyl))) +
  geom_bar() 
```

我编织:

knit("mwe.Rmd") 

然后我使用pandoc

Then I use pandoc

pandoc -o mwe.pdf mwe.md 

我应该能够在我的*.Rmd源代码中使用Figure \ref{fig:mwe-plot}交叉引用该图.但是,如果我运行,看来\label{fig:mwe-plot}尚未在mwe.tex中创建:

I should be able to cross-reference the plot with Figure \ref{fig:mwe-plot} in my *.Rmd source. But it seems that the \label{fig:mwe-plot} hasn't been created in mwe.tex if I run:

pandoc -o mwe.pdf mwe.md

谢谢!

推荐答案

问题是您正在编写R markdown文件,并且与LaTeX相关的选项在此类文档中不起作用(它们无效). fig.cap可以工作,但fig.lp不能,并且您也不会添加任何\label{},因为Rmd文档的输出挂钩是markdown的,并且通常那里没有标签等.

The issue is that you are writing a R markdown file and the options related to LaTeX don't work (they have no effect) in such documents. fig.cap works, but fig.lp won't and you won't get any \label{} added at all because the output hook for Rmd documents is markdown and in general there is no label etc there.

在这种情况下,您需要在fig.cap中手动编写\label{},就像在LaTeX文档中显式添加它一样.例如:

In this case you need to write the \label{} manually in fig.cap as if you were adding this explicitly in a LaTeX document. For example:

```{r mwe-plot, fig.cap = "\\label{fig:mwe-plot}MWE plot."}
library(ggplot2)
ggplot(mtcars, aes(factor(cyl))) +
  geom_bar()
```

现在,knitr将使用markdown图像标记约定将标题逐字转储到markdown文件中(在R中输入字符串时需要转义反斜杠,因此在fig.cap参数中输入\\).然后Pandoc将能够使用此标题,并且标签及其引用都应自行解决.

Now knitr will dump that caption verbatim into the markdown file using the markdown image markup conventions (we need to escape the backslash when entering the string in R, hence the \\ in the fig.cap argument). Pandoc will then be able to work with this caption and the label and the references to it should all resolve themselves.

另一个选项更复杂;没有什么可以阻止您编写自己的自定义钩子来为您完成此操作,但是您必须研究LaTeX钩子和MD钩子,才能了解如何组合所需的两个元素.

The other option is more complicated; there is nothing stopping you from writing your own custom hooks to do this for you, but you'll have to study the LaTeX hook and the MD hook to see how to combine elements of both that you need.

请注意,此问题(与LaTeX输出有关的块选项)适用于写入Rmd文件时的所有此类块选项. KNitr网站的选项"页面中暗含了这种暗示,但是当我第一次开始使用带有markdown的Knitr并使用pandoc进行渲染时,仍然让我感到惊讶.

Note that this issue (chunk options that pertain to LaTeX outputs) applies to all such chunk options when writing an Rmd file. This is sort of implied in the Options page of the KNitr website but it still caught me by surprise when I first started using Knitr with markdown and using pandoc to render.

这篇关于我无法使用knitr生成\ label {fig:mwe-plot}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆