knitr pdflatex输出控制台和PDF之间的差异

时间:2013-12-03 01:40:52

标签: pdf knitr rstudio pdflatex

我在OS X Mavericks上运行RStudio版本0.98.484和R版本3.0.2。

使用knitr时,我注意到summary()命令的控制台输出与PDF中生成的控制台输出(通过pdflatex)之间存在差异。这是一个例子。

\documentclass[11pt]{article}
\usepackage{MinionPro}
\usepackage{MnSymbol}
\usepackage[margin = 1 in]{geometry}
\geometry{verbose,tmargin=2.5cm,bmargin=2.5cm,lmargin=2.5cm,rmargin=2.5cm}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{url}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=true,bookmarksopen=true,bookmarksopenlevel=2,
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
 {hyperref}
\hypersetup{
 pdfstartview={XYZ null null 1}}
\usepackage{breakurl}

\usepackage{color}
\usepackage{graphicx}
\usepackage{fancyhdr}
\definecolor{darkred}{rgb}{0.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\definecolor{darkblue}{rgb}{0,0,0.5}
\hypersetup{ colorlinks,
linkcolor=darkblue,
filecolor=darkgreen,
urlcolor=darkred,
citecolor=darkblue }

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}

\usepackage{booktabs}

\usepackage{listings}
\lstset{breaklines=true,showstringspaces=false}


\makeatletter
\newcommand\gobblepars{%
    \@ifnextchar\par%
        {\expandafter\gobblepars\@gobble}%
        {}}
\makeatother

\newcommand{\R}{R}

\title{\textsc{Laboratory Session 1}}
\author{Ani}



\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
# set global chunk options
opts_chunk$set(fig.path='figure/minimal-', fig.align='center', fig.show='hold')
options(replace.assign=FALSE, width=90, tidy=TRUE)
render_listings()
@

\maketitle
<<chunk26>>==
require(rpart) 
data(car90)
summary(car90$Price) 
@

Hello!


\end{document}

控制台显示:

> summary(car90$Price)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
   5866    9995   13070   15810   19940   41990       6 

pdf显示

Min. 1st Qu. Median Mean  3rd Qu. Max.  NA 's
5870 10000   13100  15800 19900   42000  6

为什么会这样?向上舍入没有小数。任何线索将非常感激。

谢谢!

阿尼

1 个答案:

答案 0 :(得分:0)

这是因为knitr设置了options(digits=4),而digits选项的默认值是R中的7。您可以通过

重现它
library(rpart)
options(digits=4)
summary(car90$Price)