Sweave 结果:

Sweave 结果:

有时我会编写一个 R 代码块(在 Sweave 中),它比页面边距还长。一旦发生这种情况,有没有办法强制它“转到下一行”?

以下是一个简单示例:

\documentclass[a4paper]{article}

\usepackage{Sweave}

\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em,
                                              frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
                                              frame=single}
\title{Sweave with boxes}

\begin{document}
\maketitle

<<echo=FALSE>>=
options(width=60)
@

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<>>=
print(rnorm(99))
@


<<>>=
print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
@


\end{document}

最终的 tex 文件如下:

\documentclass[a4paper]{article}

\usepackage{Sweave}
\usepackage{listings}

\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em,
                                              frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
                                              frame=single}

\lstset{breaklines=true} 

\title{Sweave with boxes}

\begin{document}
\maketitle


Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

\begin{Schunk}
\begin{Sinput}
> print(rnorm(99))
\end{Sinput}
\begin{Soutput}
 [1]  0.36727922  0.25285078 -0.70328574  1.71655755
 [5]  0.30473595 -0.11520852 -1.36801956  0.49911603
 [9]  0.53733672 -1.26568069  0.33561173  0.93723468
[13]  2.41014561  0.09806442 -1.34404921 -0.98648477

[85] -0.40756482 -1.39450719  0.59070374 -1.09769309
[89] -1.43169931  0.87022380 -0.27047513  0.67547425
[93]  0.87007650 -0.08518324 -0.11001269 -0.91401310
[97]  0.25477667 -1.52641463  0.22896815
\end{Soutput}
\end{Schunk}


\begin{Schunk}
\begin{Sinput}
> print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
\end{Sinput}
\begin{Soutput}
[1] "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
\end{Soutput}
\end{Schunk}






\end{document}

更新,这是一个更简单的 tex 情况,我也想解决它:

\begin{Schunk}
\begin{Soutput}
Some Table

Model 1: SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
Model 2: SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

\end{Soutput}
\end{Schunk}

答案1

为了说明该问题,下面是以下 Sweave 文档的输出:

\documentclass[a4paper]{article}

\usepackage{Sweave}
\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em,
                                              frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
                                             frame=single}
<<echo=FALSE>>=
options(width=60)
@

\title{Sweave with boxes}

\begin{document}
\maketitle

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<>>=
model1 <- SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
model2 <- SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

model1
model2
@

\end{document}

Sweave 结果:

Sweave 的默认输出


通过从 Sweave 切换到 Yihui Xie 的输入格式,可以大大改进输入格式针织包. 它提供了tidy重新格式化和漂亮地打印输入的代码块选项:

\documentclass[a4paper]{article}

\title{Sweave with boxes}

<<echo=FALSE>>=
options(width=60)
@

\begin{document}
\maketitle

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<tidy=TRUE>>=
model1 <- SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
model2 <- SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

model1
model2
@

\end{document}

针织结果:

Knitr 结果

输入已经重新很好地换行和缩进,但输出仍然悬在边缘。


可以尝试通过告诉 Knitr 使用listings包包装 R 块来解决输出问题,方法是在之后的设置块中定义钩子函数options(width=60)。在列表环境中调用该breaklines选项将导致列表尝试确保没有代码行超出页面的宽度:

\documentclass[a4paper]{article}

\usepackage{listings}
\usepackage{inconsolata}

<<echo=FALSE>>=
  options(width=60)

  listing <- function(x, options) {
    paste("\\begin{lstlisting}[basicstyle=\\ttfamily,breaklines=true]\n",
      x, "\\end{lstlisting}\n", sep = "")
  }
  knit_hooks$set(source=listing, output=listing)
@

\title{Sweave with boxes}

\begin{document}
\maketitle

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<tidy=TRUE,highlight=FALSE>>=
model1 <- SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
model2 <- SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

model1
model2
@

\end{document}

Knitr 结果及列表

Knitr 使用列表

通过设置其他选项,样式肯定可以得到改善listings,但真正的问题是,它listings真的没有办法智能地分行输出。每行都小于文本宽度,但缩进关闭,并且缺少一些“输出开头”分隔符。

只有通过调整负责格式化和打印的 R 函数才有可能正确解决长输出问题。

答案2

\documentclass{article}
\usepackage{listings}
\usepackage{inconsolata}
<<echo=FALSE>>=
  options(width=60)

  listing <- function(x, options) {
    paste("\\begin{lstlisting}[basicstyle=\\ttfamily,breaklines=true]\n",
      x, "\\end{lstlisting}\n", sep = "")
  }
  knit_hooks$set(source=listing, output=listing)
@
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
# Run the following if you don't have these installed
# install.packages(c("knitr", "ggplot2", "GGally", "xtable", "ggthemes", "xkcd"))

# set global chunk options
opts_chunk$set(fig.path='figure/minimal-', fig.align='center',
               fig.show='markup', warning=FALSE)
options(replace.assign=TRUE,width=90)
@   

Look at \ref{tab:mytable}.
<<echo=FALSE,results='asis'>>=
  library(xtable, car)
  print(xtable(x=mtcars[1:5,1:5], caption = "example", label = "tab:mytable"))
@

<<background='lavenderblush'>>=
a<-matrix(c("IntervalosDeEdad","Fecha.de.Inicio.de.Vigencia.de.la.Poliza","Fecha.de.Ocurrencia.del.Siniestro","intervalos"),ncol=2)
@

<<echo=FALSE,results='asis'>>=
  print(xtable(a, caption = "example", label = "tab:mytable"))
@

\end{document}

相关内容