/
a30-chunk-engine.Rmd
129 lines (100 loc) · 2.01 KB
/
a30-chunk-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
---
title: "Chunk engine for easy tex output in Rmarkdown and Quarto"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Chunk engine for easy tex output in Rmarkdown and Quarto}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r, message=FALSE, echo=FALSE}
library(caracas)
```
```{r, include = FALSE}
inline_code <- function(x) {
x
}
if (!has_sympy()) {
# SymPy not available, so the chunks shall not be evaluated
knitr::opts_chunk$set(eval = FALSE)
inline_code <- function(x) {
deparse(substitute(x))
}
}
```
```{rtex}
def_sym(z, w)
q <- z + z^2 + w^3
```
We have added a chunk engine that outputs (an object) to tex format.
That is, `tex()` is applied to the object and the output is
put inside a display math environment.
For example, if you write the following:
````{verbatim}
```{rtex}
library(caracas)
def_sym(x, y)
p <- x^3 + y^2
der(p, c(x, y))
```
````
You will get this result:
```{rtex}
library(caracas)
def_sym(x, y)
p <- x^3 + y^2
der(p, c(x, y))
```
# Multiple lines
````{verbatim}
```{rtex}
def_sym(z, w)
q <- z + z^2 + w^3
# Comment
der(q, c(z, w))
p <- z^2 + w^8
der2(p, c(z, w))
```
````
Gives
```{rtex}
def_sym(z, w)
q <- z + z^2 + w^3
# Comment
der(q, c(z, w))
p <- z^2 + w^8
der2(p, c(z, w))
```
Note that to achieve this, tidying code has been disabled for `rtex` chunks.
# Check with no echo
````{verbatim}
```{rtex, echo = FALSE}
der2(p, c(z, w))
```
````
```{rtex, echo = FALSE}
der2(p, c(z, w))
```
# Custom `tex()`
You can also create your own custom `tex()` function:
```{r}
tex <- function(x) {
caracas::tex(x, zero_as_dot = TRUE)
}
```
```{rtex}
der2(p, c(z, w))
```
The reason this works is because `rtex` just calls `tex()` and
if you define one in global namespace, then this is previous in the
search path than the one provided by `caracas`.
```{r, include = FALSE}
rm(tex)
```
# Notes
Note that all lines generating output will get `tex()`'ed.