【问题标题】:Using texlive, why can I compile knitr Rnw file to pdf from R but not the tex ouput from knit R to pdf使用 texlive,为什么我可以将 knitr Rnw 文件从 R 编译为 pdf,但不能将 knit R 的 tex 输出编译为 pdf
【发布时间】:2013-12-12 19:01:45
【问题描述】:

knitr 编译并生成 pdf 文件,但由 knitr 生成的 tex 文件无法编译(使用 livetex)

这里是 test.Rnw

\documentclass[letter]{article}
\title{y}
\author{z}
\begin{document}
\maketitle
<<assignment, echo = TRUE>>=
(X <- 10)
@
\end{document}

以下生成pdf文件:texi2pdf(file = "test.tex", clean = TRUE)

knitr产生的reamble有问题:

  \usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

以下是错误:

    \begin{minipage} does not have matching end; at least one unbalanced
 begin-end  test.tex    /tests-R    line 39 Problem

\end{minipage} found with no preceding \begin   test.tex    /tests-R    line 38

出于显而易见的原因,至少有一个出版商想要 tex 文档,而不是 Rnw 文档

【问题讨论】:

  • 无法在我的机器上重现这个。什么是 livetex?
  • 也无法在我的机器上重现。 Ubuntu 13.10 + TeXLive 2013。
  • 同上 Ubuntu 12.04 TeXLive 2009
  • 对我来说也很好用:OSX 10.8.5 + TeXLive 2013

标签: r pdf knitr


【解决方案1】:

我也遇到过这个问题,不过好像是路径问题。如果yourfile.Rnw 位于带有空格的路径上,knit2pdf 将抛出错误,您可以在texput.log 中清楚地看到它,而不是在错误消息中的yourfile.log 中。但是,如果您手动编译yourfile.tex,一切似乎都很好。

也许@Yihui 可以解释为什么会这样。

编辑这是输出日志:

knit2pdf("test.Rnw")


processing file: test.Rnw
  |......................                                           |  33%
  ordinary text without R code

  |...........................................                      |  67%
label: assignment (with options) 
List of 1
 $ echo: logi TRUE

  |.................................................................| 100%
  ordinary text without R code


output file: test.tex

Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet,  : 
  Running 'texi2dvi' on 'test.tex' failed.
Messages:
/usr/bin/texi2dvi: pdflatex exited with bad status, quitting.
/usr/bin/texi2dvi: see test.log for errors.

test.log 在工作目录中不存在; texput.log 包含并包含此消息:

This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014) (preloaded format=pdflatex 2014.7.13)  24 JUL 2014 10:04
entering extended mode
 restricted \write18 enabled.
 file:line:error style messages enabled.
 %&-line parsing enabled.
**\nonstopmode \input /Users/james/Documents/stack exchange/test.tex

! Emergency stop.
<*> ...opmode \input /Users/james/Documents/stack 
                                                  exchange/test.tex
*** (job aborted, file error in nonstop mode)

【讨论】:

  • 抱歉,我无法在包含空格的目录下重现 test.Rnw 的问题。你在使用 Windows 吗?你的 LaTeX 发行版也是 TeXLive 吗?
  • 这是在 Mac 上,R 3.1.0,knitr 1.6,使用 TeXLive (MacTeX)。我已经用输出日志编辑了上面的响应。
  • 嗯...我认为您是对的:文件路径中的空间似乎确实是个问题。谢谢!
  • @Yihui 那么文件路径有空格时有没有办法避免这个错误呢?
  • @BerkU。抱歉我不知道。我不经常使用 Mac OS X。
【解决方案2】:

我也没有收到错误。使用 RStudio 中的“编译 PDF”按钮,该按钮创建了以下 *.tex 文件,我可以使用 TexMaker 进行编译:

\documentclass[letter]{article}
\usepackage[]{graphicx}
\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
\def\at@end@of@kframe{}%
\ifinner\ifhmode%
\def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
\fi\fi%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
 % There is no \\@totalrightmargin, so:
 \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed%
\at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\title{y}
\author{z}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
\maketitle
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{(X} \hlkwb{<-} \hlnum{10}\hlstd{)}
\end{alltt}
\begin{verbatim}
## [1] 10
\end{verbatim}
\end{kframe}
\end{knitrout}

\end{document}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2013-02-14
    • 2015-07-11
    • 2017-02-15
    • 2018-05-21
    • 2016-12-13
    相关资源
    最近更新 更多