Sweave 文档中 R 注释中对图形的动态引用

发布于 2024-12-29 07:40:04 字数 2277 浏览 2 评论 0原文

我想找到一种使用 LaTeX \ref{} 标记在 Sweave .Rnw 文件中的 R 代码中进行注释的方法。这里有两个例子,一个是印刷版的

http:// /cm.bell-labs.com/cm/ms/departments/sia/project/nlme/UGuide.pdf

和一个用于使用的文件:

.Rnw 文件

% File: example.Rnw

\documentclass{article}
\usepackage{fullpage}
\usepackage{graphics}
\usepackage{Sweave} 
\usepackage[margin = 10pt, font=small, labelfont={bf}]{caption}

\begin{document}

Here is an example file to show what I want to do.  I would like to figure out how to use the \LaTeX\ reference command to reference a figure being generated by R code.  Note in the R code, in a comment there is a reference to the figure, but of course the output file shows a verbatim copy of the \LaTeX\ markup.  Does anyone know how to get something for Figure \ref{fig2}?

<< example plot >>=
library(reshape)
library(ggplot2)

n <- 100
lambda <- 1 / 3 
x <- seq(0, qexp(0.999, rate = lambda), length = n)
q1.a <- data.frame(x =   x,
                   f =   dexp(x, rate = lambda),
                   F =   pexp(x, rate = lambda))

q1.a <- melt(q1.a, id.vars = 'x')
g <- ggplot(q1.a) +                                     # Produces \ref{fig1} 
       aes(x = x, y = value) + 
       geom_line() + 
       facet_wrap( ~ variable, scale = "free_y")
ggsave(g, filename = "example1.jpeg")                    
@

\begin{figure}[h]
\centering
\includegraphics[width = 0.48\textwidth]{./example1}
\caption{Exponential Distribution based plots.}
\label{fig1}
\end{figure}

Here is more of what I would like to see:

<< example plot 2 >>=
ggsave(g + geom_point(), filename = "example2.jpeg")    # Produces Figure 2
@

\begin{figure}
\centering
\includegraphics[width = 0.48\textwidth]{./example2}
\caption{Exponential Distribution based plots with points and lines.}
\label{fig2}
\end{figure}

\end{document}

和 pdf 是使用 R 命令构建

Sweave(file = 'example.Rnw',
       engine = "R",
       keep.source = 'TRUE',
       echo = 'TRUE',
       results = 'verbatim')

tools::texi2dvi(file  = "example.tex",
                pdf   = TRUE,
                clean = TRUE)

的洞察如何这样做就太好了。

I would like to find a way to use the LaTeX \ref{} markup to comment in the R code within a Sweave .Rnw file. Here are two examples, one in print

http://cm.bell-labs.com/cm/ms/departments/sia/project/nlme/UGuide.pdf

and one to use to work with:

The .Rnw file

% File: example.Rnw

\documentclass{article}
\usepackage{fullpage}
\usepackage{graphics}
\usepackage{Sweave} 
\usepackage[margin = 10pt, font=small, labelfont={bf}]{caption}

\begin{document}

Here is an example file to show what I want to do.  I would like to figure out how to use the \LaTeX\ reference command to reference a figure being generated by R code.  Note in the R code, in a comment there is a reference to the figure, but of course the output file shows a verbatim copy of the \LaTeX\ markup.  Does anyone know how to get something for Figure \ref{fig2}?

<< example plot >>=
library(reshape)
library(ggplot2)

n <- 100
lambda <- 1 / 3 
x <- seq(0, qexp(0.999, rate = lambda), length = n)
q1.a <- data.frame(x =   x,
                   f =   dexp(x, rate = lambda),
                   F =   pexp(x, rate = lambda))

q1.a <- melt(q1.a, id.vars = 'x')
g <- ggplot(q1.a) +                                     # Produces \ref{fig1} 
       aes(x = x, y = value) + 
       geom_line() + 
       facet_wrap( ~ variable, scale = "free_y")
ggsave(g, filename = "example1.jpeg")                    
@

\begin{figure}[h]
\centering
\includegraphics[width = 0.48\textwidth]{./example1}
\caption{Exponential Distribution based plots.}
\label{fig1}
\end{figure}

Here is more of what I would like to see:

<< example plot 2 >>=
ggsave(g + geom_point(), filename = "example2.jpeg")    # Produces Figure 2
@

\begin{figure}
\centering
\includegraphics[width = 0.48\textwidth]{./example2}
\caption{Exponential Distribution based plots with points and lines.}
\label{fig2}
\end{figure}

\end{document}

and the pdf is build with the R commands

Sweave(file = 'example.Rnw',
       engine = "R",
       keep.source = 'TRUE',
       echo = 'TRUE',
       results = 'verbatim')

tools::texi2dvi(file  = "example.tex",
                pdf   = TRUE,
                clean = TRUE)

Any insight on how do this would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2025-01-05 07:40:04

解决此问题的一种方法是重新定义 Sinput 环境,其中源代码由 Sweave 包装。默认情况下,它是一个简单的逐字环境,不由 latex 处理令牌。诀窍是重新定义它以使用 alltt 环境,该环境允许在 alltt 环境内解析某些标记。请注意,这可能会导致我不知道的不良副作用,因此请谨慎使用!

这是一个有效的可重现示例。如果编译它,您将生成一个文件,其中 ref{fig1} 被图形编号替换。

\documentclass{article}
\usepackage{Sweave}
\usepackage{alltt}
\renewenvironment{Sinput}{\begin{alltt}}{\end{alltt}}

\begin{document}

In this document, we will create a plot using `R`, and reference its position in 
the source code.

<<produce-plot, results = hide>>=
pdf('example1.pdf')
plot(1:10, 1:10)     # Produces Figure \ref{fig1}
dev.off()
@

\begin{figure}
\includegraphics{example1.pdf}
\caption{Figure 1}
\label{fig1}
\end{figure}

\end{document}

Here is one way to solve this issue by redefining the Sinput environment in which source code is wrapped by Sweave. By default, it is a simple verbatim environment which is not processed by latex for tokens. The trick is to redefine it to use the alltt environment which allows some tokens to be parsed inside the alltt environment. Note that this might lead to unwanted side effects that I am not aware of, so use with caution!

Here is a reproducible example that works. If you compile it, you will generate a file where ref{fig1} is replaced by the figure number.

\documentclass{article}
\usepackage{Sweave}
\usepackage{alltt}
\renewenvironment{Sinput}{\begin{alltt}}{\end{alltt}}

\begin{document}

In this document, we will create a plot using `R`, and reference its position in 
the source code.

<<produce-plot, results = hide>>=
pdf('example1.pdf')
plot(1:10, 1:10)     # Produces Figure \ref{fig1}
dev.off()
@

\begin{figure}
\includegraphics{example1.pdf}
\caption{Figure 1}
\label{fig1}
\end{figure}

\end{document}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文