-
Notifications
You must be signed in to change notification settings - Fork 7
/
engine.Rmd
199 lines (146 loc) · 5.23 KB
/
engine.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
---
title: "Using texPreview Built-in Knitr Engine"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Using texPreview Built-in Knitr Engine}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
`texPreview` contains inside its own `knitr` engine to render TeX snippets more naturally within the RMarkdown workflow. We achieve this by leveraging texPreview's integration via S3 for various classes.
The following inputs can be run inside texpreview chunks in RStudio version [1.2.5033](https://posit.co/download/rstudio-desktop/) and above. They will render in the same fashion as would a regular `texPreview::tex_preview` call.
- Raw TeX
- TeX in a character vector
- knitr_kable (kable/kableExtra)
- xtable
- texreg
- equatiomatic
Arguments that were used in the `texPreview::tex_preview` function are now set in the chunk options. To make them unique to this engine we prefix them with `texpreview`, below is a table with the chunk options and their tex_preview analogue.
|tex_preview argument|chunk option|default value|
|:--|:--|:--|
|returnType|texpreview.return|'engine'|
|fileDir|texpreview.path|tex_opts$get('fileDir')|
|stem|texpreview.stem|'tex_temp'|
The user still has the option to use `texPreview::tex_opts` to control chunk outputs as they are passed into the call internally. The chunk options will override any settings that are in `texPreview::tex_opts`.
Rendering the chunk interactively will have different side effects depending on the `texpreview.return` setting. These settings have the same side effects as the returnType settings with one addition 'engine' which mimics returnType='viewer'. It should be the preferred settings when working with RMarkdown documents that return HTML outputs.
The following are examples of some uses of the `texpreview` engine.
```r
library(texPreview)
dir.create('images/engine',showWarnings = FALSE)
```
## Default
### Chunk Call
````markdown
```{texpreview, echo = FALSE}
knitr::kable(head(iris),format = 'latex',booktabs = TRUE)
```
````
### Output
```texpreview
knitr::kable(head(iris),format = 'latex',booktabs = TRUE)
```
<img src="images/engine/unnamed-chunk-2-1.png" width="1011" />
## No Echo
The texpreview engine respects regular 'r' engine chunk options such as 'echo':
### Chunk Call
````markdown
```{texpreview, echo = FALSE}
knitr::kable(head(iris),format = 'latex',booktabs = TRUE)
```
````
### Output
<img src="images/engine/unnamed-chunk-3-3.png" width="1011" />
## Raw TeX Table
### Chunk Call
````markdown
```{texpreview}
\begin{tabular}{llr}
\hline
\multicolumn{2}{c}{Item} \\
\cline{1-2}
Animal & Description & Price (\$) \\
\hline
Gnat & per gram & 13.65 \\
& each & 0.01 \\
Gnu & stuffed & 92.50 \\
Emu & stuffed & 33.33 \\
Armadillo & frozen & 8.99 \\
\hline
\end{tabular}
```
````
### Output
```texpreview
\begin{tabular}{llr}
\hline
\multicolumn{2}{c}{Item} \\
\cline{1-2}
Animal & Description & Price (\$) \\
\hline
Gnat & per gram & 13.65 \\
& each & 0.01 \\
Gnu & stuffed & 92.50 \\
Emu & stuffed & 33.33 \\
Armadillo & frozen & 8.99 \\
\hline
\end{tabular}
```
<img src="images/engine/unnamed-chunk-4-5.png" width="1011" />
## Output Path
### Chunk Call
````markdown
```{texpreview, echo = FALSE, texpreview.path = 'images/engine',texpreview.stem = 'example'}
knitr::kable(head(iris),format = 'latex',booktabs = TRUE)
```
````
### Output
<img src="images/engine/unnamed-chunk-5-7.png" width="1011" />
```r
list.files('images/engine')
#> [1] "example.png" "example.tex" "unnamed-chunk-2-1.png"
#> [4] "unnamed-chunk-3-3.png" "unnamed-chunk-4-5.png" "unnamed-chunk-5-7.png"
```
## PDF Documents
Setting texpreview.return = 'input' will have the chunk return an LateX 'input' call with the results 'asis' so the user can render the lines as part of a Rmarkdown pdf document.
````markdown
```{texpreview,texpreview.return = 'input', texpreview.path = 'images/engine',texpreview.stem = 'example'}
knitr::kable(head(iris),format = 'latex',booktabs = TRUE)
```
\input{images/engine/example.tex}
````
\input{images/engine/example.tex}
Setting texpreview.return = 'tex' will have the chunk return the tex lines output with the results 'asis' so the user can render the lines as part of a Rmarkdown pdf document.
````markdown
```{texpreview,texpreview.return = 'tex'}
knitr::kable(head(iris),format = 'latex',booktabs = TRUE)
```
\resizebox{\textwidth}{!}{
\begin{tabular}{rrrrl}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
\midrule
5.1 & 3.5 & 1.4 & 0.2 & setosa\\
4.9 & 3.0 & 1.4 & 0.2 & setosa\\
4.7 & 3.2 & 1.3 & 0.2 & setosa\\
4.6 & 3.1 & 1.5 & 0.2 & setosa\\
5.0 & 3.6 & 1.4 & 0.2 & setosa\\
\addlinespace
5.4 & 3.9 & 1.7 & 0.4 & setosa\\
\bottomrule
\end{tabular}
}
````
\resizebox{\textwidth}{!}{
\begin{tabular}{rrrrl}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
\midrule
5.1 & 3.5 & 1.4 & 0.2 & setosa\\
4.9 & 3.0 & 1.4 & 0.2 & setosa\\
4.7 & 3.2 & 1.3 & 0.2 & setosa\\
4.6 & 3.1 & 1.5 & 0.2 & setosa\\
5.0 & 3.6 & 1.4 & 0.2 & setosa\\
\addlinespace
5.4 & 3.9 & 1.7 & 0.4 & setosa\\
\bottomrule
\end{tabular}
}