2
我想找到一種方法來使用LaTeX \ref{}
標記在Sweave .Rnw文件中註釋R代碼。這裏有兩個例子,一個在打印對Sweave文檔中的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)
任何有關這將是如何偉大的見識。
您可以使用'brew'在'Sweave'之前處理'Rnw'文件。 – baptiste
Sweave輸入/代碼列表是逐字環境中的變體,專門用於不嘗試將「R」代碼解釋爲LaTeX。您希望將'R'代碼註釋解釋爲LaTeX。我不知道如何,但也許這個重新措詞將有助於解決方案。 –
@BrianDiggs尼斯點。您的評論將我引導至我發佈的解決方案。只需要一個不同的'verbatim'環境,讓'LaTeX'解析一些標記。 – Ramnath