我可以在乳胶文档的标题中使用knitr内联表达式吗? [英] Can I use a knitr inline expression in the title of a latex document?

查看:107
本文介绍了我可以在乳胶文档的标题中使用knitr内联表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在\ Begin {document}命令之后但在\ maketitle命令之前,在LaTeX文档的标题中使用Knitr/Sweave内联调用(\Sexpr{}).嵌入式R代码将从我嵌入LaTeX的R脚本的早期创建的R数据帧中提取一两个信息.

我有几个Knitr块创建了一个data.frame,从中我可以导出要放入标题中的信息.我试过将这些块放在LaTeX的\ begin {document}调用和\ title代码之间,如下所示:

\documentclass
[LaTex Preamble]
\begin{document}
[%% Knitr chunks that initialize an R data-frame]
\title \Sexpr{--a snippet of R code that extracts an element from the data-frame --}
\maketitle
... (rest of the LaTeX document)

并且我还尝试过在\ begin {document}语句之前将Knitr块放入LaTeX代码的序言中.

但是在Knitr中,似乎忽略了放置在LaTeX中\ maketitle调用之前的代码(初始化以外的代码),因此内联代码片段中的标题看起来像是对Latex的错误,并且会暂停输出.

我在Knitr文档中找不到有关在LaTeX文档的标题中包含行内代码的任何信息.

有什么想法吗?


确定:由于下面@ ben-bolker的提示,找到了解决方案. Ben在输出到RNW文件之前使用R块的格式(在两步Knitr过程中:Latex-> rnw-> pdf).但是我要一步一步将LaTeX文件编译为PDF,而无需从TeXShop内部(在Mac OSX上)转换为RNW文件.我发现可以使用RNW定界符(<< >> =)和一步编译来获得Ben的示例.但是我不能混合使用普通的LaTeX块分隔符(%% begin.rcode和%% end.rcode)和RNW内联语句钩子(\ Sexpr {}).不管我怎么摆弄,后者都不起作用.最终,我发现正确的LaTeX内联钩子是 \\ rinline {} .

在Knitr文档中不是很清楚这是LaTeX的必需格式,我发现它最终主要归功于Ben的示例.最好,彼得


更新2 ... ...然后有RTFM(在这种情况下为备忘单"):解决方案

嗯.以下文件对我有用:

\documentclass{article}
<<echo=FALSE>>=
x <- 5
@ 
\title{The number is \Sexpr{x^2}}
\begin{document}
\maketitle

Some stuff
\end{document}

在Ubuntu 10.04上使用knitr版本0.8,通过knit2pdf("knitr_title.Rnw") ...

I'd like to use a Knitr/Sweave in-line call (\Sexpr{}) in the title of a LaTeX document, after the \begin{document} command but before the \maketitle command. The in-line R code would extract one or two pieces of information from an R data-frame created early in the R script I'm embedding in LaTeX.

I have a couple of Knitr chunks that create a data.frame from which I derive the information I want to put in the Title. I've tried placing these chunks between LaTeX's \begin{document} call and the \title code, like this:

\documentclass
[LaTex Preamble]
\begin{document}
[%% Knitr chunks that initialize an R data-frame]
\title \Sexpr{--a snippet of R code that extracts an element from the data-frame --}
\maketitle
... (rest of the LaTeX document)

and I've also tried putting the Knitr chunks in the preamble to the LaTeX code before \begin{document} statement.

But in Knitr seems to ignore code (other than initialization) that is placed ahead of the \maketitle call in LaTeX, so the in-line snippets included the title look like errors to Latex and it halts output.

I can't find any information in the Knitr documentation on including in-line code in the Title of a LaTeX document.

Any ideas?


OK: Found the solution thanks to the hint from @ben-bolker below. Ben uses the formatting of R chunks before output to an RNW file (in a 2-step Knitr process: latex -> rnw -> pdf) . But I'm compiling the LaTeX file to PDF in one-step without going to an RNW file from inside TeXShop (on Mac OSX). I found that I could get Ben's example to work using the RNW delimiters (<<>>=) and one-step compiling. But I couldn't mix the usual LaTeX chunk-delimiters (%%begin.rcode and %% end.rcode) and the RNW in-line statement hook (\Sexpr{}). The latter didn't work no matter how I fiddled with it. Eventually I found that the correct in-line hook for LaTeX is \\rinline{}.

It's not very clear in the Knitr documentation that this is the required format for LaTeX and I found it eventually mainly thanks to Ben's example. Best, Peter


Update 2 ... and then there's RTFM (or the 'cheat sheet' in this case): http://cran.r-project.org/web/packages/knitr/vignettes/knitr-refcard.pdf

解决方案

Hmm. The following file works for me:

\documentclass{article}
<<echo=FALSE>>=
x <- 5
@ 
\title{The number is \Sexpr{x^2}}
\begin{document}
\maketitle

Some stuff
\end{document}

with knitr version 0.8 on Ubuntu 10.04, via knit2pdf("knitr_title.Rnw") ...

这篇关于我可以在乳胶文档的标题中使用knitr内联表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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