10

I am not sure about how to include my r output in the best way in my latex document.

Currently I saved my R output (NOT R CODE!) into a txt file:

*---------------------------------*
*          GARCH Model Fit        *
*---------------------------------*

Conditional Variance Dynamics   
-----------------------------------
GARCH Model : sGARCH(1,1)
Mean Model  : ARFIMA(0,0,5)
Distribution    : norm 

Optimal Parameters
------------------------------------
        Estimate  Std. Error  t value Pr(>|t|)
ma1     0.000000          NA       NA       NA
ma2     0.000000          NA       NA       NA
ma3     0.000000          NA       NA       NA
ma4     0.000000          NA       NA       NA
ma5    -0.041598    0.020328  -2.0463 0.040727
omega   0.000006    0.000001   4.2148 0.000025
alpha1  0.092063    0.011337   8.1205 0.000000
beta1   0.892993    0.012696  70.3366 0.000000

Robust Standard Errors:
        Estimate  Std. Error  t value Pr(>|t|)
ma1     0.000000          NA       NA       NA
ma2     0.000000          NA       NA       NA
ma3     0.000000          NA       NA       NA
ma4     0.000000          NA       NA       NA
ma5    -0.041598    0.020374  -2.0417 0.041178
omega   0.000006    0.000002   3.0914 0.001992
alpha1  0.092063    0.018209   5.0560 0.000000
beta1   0.892993    0.019863  44.9586 0.000000

LogLikelihood : 6757.949 

Information Criteria
------------------------------------

Akaike       -5.2356
Bayes        -5.2265
Shibata      -5.2356
Hannan-Quinn -5.2323

Q-Statistics on Standardized Residuals
------------------------------------
               statistic   p-value
Lag[1]             8.037 0.0045839
Lag[p+q+1][6]     12.096 0.0005053
Lag[p+q+5][10]    15.367 0.0089041
d.o.f=5
H0 : No serial correlation

Q-Statistics on Standardized Squared Residuals
------------------------------------
              statistic  p-value
Lag[1]            1.581 0.208685
Lag[p+q+1][3]    10.346 0.001298
Lag[p+q+5][7]    13.172 0.021820
d.o.f=2

I now want to include this txt output into my latex file. Now my question is: Is this a good way? Are there better ways?

And my main question: how can I include this txt file into my latex document without latex changing the indentation size, formatting etc.? I want to put this in the appendix and I want to have a caption below it. I think this is not a figure nor a table, how do I include such things in the appendix to have also a caption and label?

jub0bs
  • 58,916
Jen Bohold
  • 1,479

3 Answers3

11

Briefly, there are two options in R, these are "Sweave library" (not my favorite) and "knitr library". After using both, I recommend using knitr.

What do you need?

First, you need install knitr in R,

>install.packages("knitr")

And load it:

>library(knitr)

Then you create a regular \LaTeX file and save it with the extension .Rnw (foo.Rnw)

I usualy insert this "chunk"

<<ChunkName, options="OptionOne">>=
library("knitr")
library("xtable")
@

right after \begin{document}. This way you make sure R loads knitr library when compiling your document. You can also include within this chunk all libraries that you need in R, in this case "xtable".

A chunk is a piece of code that begins with <<>>= and ends with @. In the first part <<>>= the first thing you write is the name of the chunk, and then all options as required, i.e. echo=T, include=T, fig.cap="Whatever figure caption", etc.

There is not enough space here to explain all you can do and how to do it.

For further reading, here's knitr homepage, there you can find tons of information and examples.

QAChip
  • 121
  • thanks for your answer, but what is the advantage of this over the solution with verbatim of John Wickerson? I mean, I do NOT want to include code, but only the output, therefore e.g. the topic colored textelements should not be relevant for me. – Jen Bohold May 18 '13 at 09:31
  • With verbatim, if you change just one line of your code that alters your results, you will have to change also your output, images, etc. With knitr and Sweave, you only care for your code, since LaTeX and R interact and automatically include your output, and images.You DO NOT need to copy and paste your output nor your images. You can even include an R variable within your text. Supose you want to include a p-value from a t-test. With verbatim you will copy and paste this number, but with R, you can use something like \Sexpr{round(variable$p.value,4)} to call whatever its value is. – QAChip May 18 '13 at 09:35
  • Suppose that you perform the same analysis over and over. Using knitr+\LaTeX, you can do a template, and only change your data base. All analysis are made while compiling your Rnw file and all output included in your final pdf (or ps or dvi). Using logical sentences in your R code, you can even make the content of your template a conditional to your results. Please read and run some examples from the pages I gave you and you will catch exactly what I'm trying to explain. – QAChip May 18 '13 at 09:45
  • ok, but this is too much for me, it is too complicated. But can you tell me, how I can get pagebreaks with verbatim? This does not work currently, since my .txt file is too large and latex does not page break it automatically? – Jen Bohold May 18 '13 at 09:49
  • There's a third option in addition to Sweave and knitr: org-mode, which is perfect for emacs users.s – GTK May 18 '13 at 14:41
10

The simplest thing to do is to load the verbatim package in your document's preamble; then you can put

\verbatiminput{output.txt}

in your document. This will include the contents of your text file, set it in a typewriter font, and not change the formatting at all.

A more full-featured solution involves loading the listings package, as suggested by Marco in his comment above. This gives you control over the appearance of your text, and allows the text to appear in a floating (figure-like) environment.

Example code

\documentclass[a4paper]{article}

\usepackage[margin=1in]{geometry}
\usepackage{listings}
\renewcommand{\topfraction}{0.9}

\lstset{
basicstyle=\scriptsize\tt,
}

\begin{document}

\section{Main}
See Listing~\ref{zebra} for info.

\appendix

\section{R output}

\lstinputlisting[float=h,frame=tb,caption=R output,label=zebra]{output.txt}

\end{document}

Output

enter image description here

  • Thanks, but how can I use this for referencing? So how can I give a label to it and use it in the text with: "In \ref{} you can see, that...." – Jen Bohold May 18 '13 at 09:20
  • 1
    Give the label=... option to the \lstinputlisting command. See my updated answer. – John Wickerson May 18 '13 at 09:24
  • ok, but I have two problems: First of all the txt file is too big, so I cannot put it on one page? Second, Latex colors some elements with blue color (some Q and t). How can I change this? Or is this not colored if I print it? – Jen Bohold May 18 '13 at 09:39
  • or is there any chance to pagebreak with verbatim? – Jen Bohold May 18 '13 at 09:48
  • I don't know about the colouring. It doesn't do that for me. Perhaps the listings package is making a crude attempt at syntax highlighting? – John Wickerson May 18 '13 at 11:48
  • 1
    It doesn't currently insert a pagebreak because the entire code is put into a single floating box (like a figure). If you want pagebreaks, just remove the float=h option. – John Wickerson May 18 '13 at 11:53
4

Using sasnrdisplay a front-end to the list­ings.

code:

\documentclass[12pt]{article}
\usepackage[english,noautotitles-r]{SASnRdisplay} 
% front-end to the list­ings package
% http://www.ctan.org/tex-archive/macros/latex/contrib/sasnrdisplay
\lstdefinestyle{r-output}{
style = r-style,
style = r-output-user,
}
% Incase code output numbering is not required, replace 
% "caption={Output}" with "title={Output}"
\begin{document}
\appendix
\section{R output with SASnRdisplay package}
Here is how  R output~\ref{code:r-out} and shown on Page~\pageref{code:r-out}
\inputRcode[caption={Output},numbers=left,numberstyle=\tiny,label=code:r-out]{myfile.txt}
\end{document}

output, Only first page shown:

enter image description here