[Tex/LaTex] How to include R output in LaTex?

formattingr

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?

Best Answer

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

Related Question