Chunk engine for easy tex output in Rmarkdown and Quarto

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(caracas)
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))
  }
}
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:

```r
library(caracas)
def_sym(x, y)
p <- x^3 + y^2
der(p, c(x, y))
```

You will get this result:

library(caracas)
def_sym(x, y)
p <- x^3 + y^2
der(p, c(x, y))

Multiple lines

```r
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

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

```r
der2(p, c(z, w))
```
der2(p, c(z, w))

Custom tex()

You can also create your own custom tex() function:

tex <- function(x) {
  caracas::tex(x, zero_as_dot = TRUE)
}
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.

rm(tex)

Notes

Note that all lines generating output will get tex()'ed.



Try the caracas package in your browser

Any scripts or data that you put into this service are public.

caracas documentation built on Oct. 17, 2023, 5:08 p.m.