简体   繁体   English

使用texlive,为什么我可以将R的knitr Rnw文件编译为pdf,而不能将knit R的tex输出编译为pdf

[英]Using texlive, why can I compile knitr Rnw file to pdf from R but not the tex ouput from knit R to pdf

knitr compiles and produces pdf file but the tex file that is produced by knitr does not compile (with livetex) Knitr编译并生成pdf文件,但knitr生成的tex文件无法编译(使用livetex)

Here is test.Rnw 这是test.Rnw

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

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

The reamble that is produced by knitr has a problem: knitr产生的重复码有一个问题:

  \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

Here are the errors: 错误如下:

    \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

For obvious reasons, at least one publisher wants the tex document, not the Rnw document 出于明显的原因,至少有一个发布者想要tex文档,而不是Rnw文档

I had this problem too, but it seems to be a path issue. 我也有这个问题,但这似乎是一个路径问题。 If yourfile.Rnw is on a path with spaces, knit2pdf will throw an error and you can see it clearly in texput.log not yourfile.log as the error message states. 如果yourfile.Rnw在带有空格的路径上,则knit2pdf将引发错误,并且在错误消息中指出,您可以在texput.log清楚地看到它, texput.log不是yourfile.log看到它。 However if you compile the yourfile.tex by hand, everything seems to be okay. 但是,如果您手动编译yourfile.tex ,一切似乎都还可以。

Perhaps @Yihui can shed some light on why this might be the case. 也许@Yihui可以阐明为什么会这样。

EDIT Here's a log of the output: 编辑这是输出的日志:

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 doesn't exist in the working directory; test.log在工作目录中不存在; texput.log does and contains this message: 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)

I also didn't receive an error. 我也没有收到错误。 Used the "Compile PDF" button from RStudio, which created following *.tex-file, which I can compile using TexMaker: 使用了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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM