Sharedlatex-tests / rtex.texOpen in CoCalc
Authors: harald @schil.ly, Testing CoCalc, John Jeng, Harald Schilly, Harald Schilly, ℏal Snyder, William A. Stein
1
\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
2
%% maxwidth is the original width if it is less than linewidth
3
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
4
\makeatletter
5
\def\maxwidth{ %
6
\ifdim\[email protected]@width>\linewidth
7
\linewidth
8
\else
9
\[email protected]@width
10
\fi
11
}
12
\makeatother
13
14
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
15
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
16
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
17
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
18
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
19
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
20
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
21
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
22
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
23
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
24
\let\hlipl\hlkwb
25
26
\usepackage{framed}
27
\makeatletter
28
\newenvironment{kframe}{%
29
\def\[email protected]@[email protected]{}%
30
\ifinner\ifhmode%
31
\def\[email protected]@[email protected]{\end{minipage}}%
32
\begin{minipage}{\columnwidth}%
33
\fi\fi%
34
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
35
\colorbox{shadecolor}{##1}\hskip-\fboxsep
36
% There is no \\@totalrightmargin, so:
37
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
38
\MakeFramed {\advance\hsize-\width
39
\@totalleftmargin\[email protected] \linewidth\hsize
40
\@setminipage}}%
41
{\par\unskip\endMakeFramed%
42
\[email protected]@[email protected]}
43
\makeatother
44
45
\definecolor{shadecolor}{rgb}{.97, .97, .97}
46
\definecolor{messagecolor}{rgb}{0, 0, 0}
47
\definecolor{warningcolor}{rgb}{1, 0, 1}
48
\definecolor{errorcolor}{rgb}{1, 0, 0}
49
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
50
51
\usepackage{alltt}
52
\usepackage[utf8]{inputenc}
53
\usepackage[T1]{fontenc}
54
\usepackage{url}
55
\usepackage{graphicx}
56
57
% this is based on https://github.com/yihui/knitr-examples/blob/master/005-latex.Rtex
58
59
%% for inline R code: if the inline code is not correctly parsed, you will see a message
60
\newcommand{\rinline}[1]{SOMETHING WRONG WITH knitr}
61
62
63
64
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
65
\begin{document}
66
67
\title{Rtex Knitr in CoCalc}
68
69
\author{Author Name}
70
71
\maketitle
72
73
Boring stuff as usual:
74
75
%% a chunk with default options
76
\begin{knitrout}
77
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
78
\begin{alltt}
79
\hlnum{1}\hlopt{+}\hlnum{1}
80
\end{alltt}
81
\begin{verbatim}
82
## [1] 2
83
\end{verbatim}
84
\begin{alltt}
85
\hlstd{x}\hlkwb{=}\hlkwd{rnorm}\hlstd{(}\hlnum{5}\hlstd{);} \hlkwd{t}\hlstd{(x)}
86
\end{alltt}
87
\begin{verbatim}
88
## [,1] [,2] [,3] [,4] [,5]
89
## [1,] 0.2950348 0.6188449 -1.766488 0.7433172 -1.358912
90
\end{verbatim}
91
\end{kframe}
92
\end{knitrout}
93
94
For the cached chunk below, you will need to wait for 3 seconds for
95
the first time you compile this document, but it takes no time the
96
next time you run it again.
97
98
%% chunk options: cache this chunk
99
\begin{knitrout}
100
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
101
\begin{alltt}
102
\hlkwd{set.seed}\hlstd{(}\hlnum{123}\hlstd{)}
103
\hlstd{x} \hlkwb{=} \hlkwd{runif}\hlstd{(}\hlnum{10}\hlstd{)}
104
\hlkwd{sd}\hlstd{(x)} \hlcom{# standard deviation}
105
\end{alltt}
106
\begin{verbatim}
107
## [1] 0.29474
108
\end{verbatim}
109
\begin{alltt}
110
\hlkwd{Sys.sleep}\hlstd{(}\hlnum{3}\hlstd{)} \hlcom{# test cache}
111
\end{alltt}
112
\end{kframe}
113
\end{knitrout}
114
115
Now we know the first element of x is 0.2875775.
116
And we also know the 26 letters are A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z.
117
An expression that returns a value of length 0 will be removed from the output, but it was indeed evaluated,
118
i.~e. now the first element of x becomes 2011.
119
120
How about figures? Let's use the Cairo PDF device (assumes R $\geq$ 2.14.0).
121
122
\begin{knitrout}
123
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
124
\begin{alltt}
125
\hlkwd{plot}\hlstd{(cars)} \hlcom{# a scatter plot}
126
\end{alltt}
127
\end{kframe}
128
\includegraphics[width=.8\textwidth]{figure/latex-cairo-scatter-1}
129
130
\end{knitrout}
131
132
Warnings, messages and errors are preserved by default.
133
134
\begin{knitrout}
135
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
136
\begin{alltt}
137
\hlkwd{sqrt}\hlstd{(}\hlopt{-}\hlnum{1}\hlstd{)} \hlcom{# here is a warning!}
138
\end{alltt}
139
140
141
{\ttfamily\noindent\color{warningcolor}{\#\# Warning in sqrt(-1): NaNs produced}}\begin{verbatim}
142
## [1] NaN
143
\end{verbatim}
144
\begin{alltt}
145
\hlkwd{message}\hlstd{(}\hlstr{'this is a message you should know'}\hlstd{)}
146
\end{alltt}
147
148
149
{\ttfamily\noindent\itshape\color{messagecolor}{\#\# this is a message you should know}}\begin{alltt}
150
\hlnum{1}\hlopt{+}\hlstr{'a'} \hlcom{# impossible}
151
\end{alltt}
152
153
154
{\ttfamily\noindent\bfseries\color{errorcolor}{\#\# Error in 1 + "{}a"{}: non-numeric argument to binary operator}}\end{kframe}
155
\end{knitrout}
156
157
\end{document}
158