引用Rnw文件的r代码块中的几张图之一

时间:2018-10-29 18:40:54

标签: r knitr rnw

使用rnw文件创建参数化报告。我正在尝试从其中包含多个图形(由遍历图形列表的循环生成)的代码块中引用特定图形。我知道是否有一个单一的图形,我可以使用\ ref {fig:foo}从块标签中引用它,正如Yihui在https://bookdown.org/yihui/bookdown/figures.html中提到的那样。但我似乎无法参考其中的具体数字。我尝试引用唯一的图形标题或整个块,但两者都给了我??。有办法吗?

我搜索了这个Dynamic LaTeX references in R comment with knitr及其关联的问题,但无法使其正常工作。

还在Figures captions and labels in knitr中,这些图被组合成一个大图,从而绕过了问题。

MVWE:

\documentclass{article}

\usepackage{float}
\usepackage{hyperref}
\usepackage{caption} % Needs to be after hyperref. jumps you to top of figure not to label.

\begin{document}



<<figures, fig.cap=c('fig1','fig2')>>=
library(knitr)
library(markdown)
library(rmarkdown)
library(ggplot2)

figure1 <- ggplot(mtcars) + geom_point(aes(x=mpg,y=cyl))
figure2 <- ggplot(mtcars) + geom_point(aes(x=drat,y=wt))

plots <- list(figure1,figure2)

plots
@


as we can see in \ref{fig:figures}

\end{document}

1 个答案:

答案 0 :(得分:2)

只需在其上附加一个数字:

import os
import PIL.Image
from PIL import ImageTk
from tempfile import NamedTemporaryFile
import tkinter as tk

im = PIL.Image.open("lightbulb.gif")
small_img = im.resize((20,20), resample=PIL.Image.NEAREST).convert('1');

with NamedTemporaryFile(suffix='.xbm', delete=False) as temp_img:
    small_img.save(temp_img.name)

root = tk.Tk()
canvas = tk.Canvas(root, width=100, height=100, bg='black')
canvas.pack()
bitmap_id = canvas.create_bitmap(3, 3, background='', foreground='gray',
                                 bitmap='@'+temp_img.name, anchor=tk.NW)
root.mainloop()

try:  # Cleanup
    os.remove(temp_img.name)  # Get rid of named temporary file.
except FileNotFoundError:
    pass

要弄清楚这一点,您应该查看as we can see in \ref{fig:figures1} and \ref{fig:figures2} 文件,其中包含

.tex

第一个,其他类似。 \begin{figure} \includegraphics[width=\maxwidth]{figure/figures-1} \caption[fig1]{fig1}\label{fig:figures1} \end{figure} 部分是您的\label{fig:figures1}需要引用的部分。

相关问题