TexFormatter: R6 class to typeset R expressions as latex equations

TexFormatterR Documentation

R6 class to typeset R expressions as latex equations

Description

R6 class to typeset R expressions as latex equations

R6 class to typeset R expressions as latex equations

Public fields

symbols

a named list that matches R names (names in the list) to latex symbols (values in the list). Trig functions, greek letters and infinity are altomatically defined. They do not need to be defined in this list.

Methods

Public methods


Method new()

constructor

Usage
TexFormatter$new(symbols = list())
Arguments
symbols

a named list that matches R names (names in the list) to latex symbols (values in the list). Trig functions, greek letters and infinity are automatically defined. They do not need to be defined in this list. But they may be redefined here

Examples
tex = TexFormatter$new(II="[I\gets I]")
tex$typeset(quote(sin(alpha*I)/II))

Method set.symbols()

set/change the tex symbols of R names

Usage
TexFormatter$set.symbols(symbols)
Arguments
symbols

a named list that matches R names (names in the list) to latex symbols (values in the list). Trig functions, greek letters and infinity are automatically defined. They do not need to be defined in this list. But they may be redefined here

Returns

an invisible self to chain methods

Examples
tex = TexFormatter$new(II="[I\gets I]")
tex$set.symbols(list(alpha="a")$typeset(quote(sin(alpha*I)/II))
description typeset an R expression

Method typeset()

Usage
TexFormatter$typeset(R)
Arguments
R

the R expression to be typeset.

Returns

a character holding the latex commands

Examples
tex = TexFormatter$new(II="[I\gets I]")
tex$typeset(quote(sin(alpha*I)/II))

Method enclose()

enclose the tex commands in brackets

Usage
TexFormatter$enclose(..., closure = c("{", "}"))
Arguments
...

the tex commands to enclose

closure

a character vector of the brackets to be used. It defaults to 'c("","")'. If the vector is length 1, then it is used as both the opening and closing bracket.

Returns

the enclosed tex command

Examples
tex = TexFormatter$new()
tex$enclose("\\bf This is a test")
tex$enclose(tex$typeset.equation(quote(alpha==beta)), 
  closure=c("\\begin{align}", "\\end{align}"))

Method command()

generate a latex command

Usage
TexFormatter$command(name, ..., argument = NULL, option = NULL)
Arguments
name

the name of the latex command.

...

the arguments of the latex command.

argument

a character vector as an alternative way to provide arguments for the latex command

option

the optional argument to latex command (the one that is enclosed in square brackets.)

Returns

a character holding the latex command

Examples
tex=TexFormatter$new()
tex$command("textcolor", "blue", "this text is blue")
tex$command("documentclass", argument="article", option="12pt")

Method environment()

generate a latex environment

Usage
TexFormatter$environment(name, ..., argument = NULL, option = NULL)
Arguments
name

the name of the latex environment

...

the contents of the latex environment

argument

a named list to provide arguments for the latex environment

option

the optional argument to latex environment (the one that is enclosed in square brackets.)

Returns

a character holding the latex environment

Examples
tex = TexFormatter$new()
tex$environment("frame", argument="frame title", "line 1", "line 2")

Method typeset.equation()

typeset a system of equations

This method typesets a system of equations that can be used as the content of a latex align or eqnarray environments.

Usage
TexFormatter$typeset.equation(equations, align = c("left", "right", "both"))
Arguments
equations

a list of equations or inequalities. The left and right sides of the equations may be connected using "<-" or "==".

align

can be one of "left", "right", "both", which specifies if the ampersand (&) is placed on the left, right or both sides of the equal (or other comparison) sign. The default value is left.

Details

Note that the latex environment itself (e.g., \beginalign\endalign) is not returned.

Returns

a character holding the latex commands.

Examples
SIR = Model$new(
  title = "SIR",
  S ~ -beta*S*I/N, # the dS/dt equation
  I ~ beta*S*I/N - gamma*I, # the dI/dt equation
  R ~ gamma*I, # the dR/dt equation
  N = S + I + R # the total population N
)
tex = TexFormatter$new()
tex$typeset.equation(SIR$equations$equations)
tex$typeset.equation(SIR$equations$where)

Method format()

a text representation of the class

Usage
TexFormatter$format()

Method clone()

The objects of this class are cloneable with this method.

Usage
TexFormatter$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `TexFormatter$new`
## ------------------------------------------------

tex = TexFormatter$new(II="[I\gets I]")
tex$typeset(quote(sin(alpha*I)/II))

## ------------------------------------------------
## Method `TexFormatter$set.symbols`
## ------------------------------------------------

tex = TexFormatter$new(II="[I\gets I]")
tex$set.symbols(list(alpha="a")$typeset(quote(sin(alpha*I)/II))
description typeset an R expression

## ------------------------------------------------
## Method `TexFormatter$typeset`
## ------------------------------------------------

tex = TexFormatter$new(II="[I\gets I]")
tex$typeset(quote(sin(alpha*I)/II))

## ------------------------------------------------
## Method `TexFormatter$enclose`
## ------------------------------------------------

tex = TexFormatter$new()
tex$enclose("\\bf This is a test")
tex$enclose(tex$typeset.equation(quote(alpha==beta)), 
  closure=c("\\begin{align}", "\\end{align}"))

## ------------------------------------------------
## Method `TexFormatter$command`
## ------------------------------------------------

tex=TexFormatter$new()
tex$command("textcolor", "blue", "this text is blue")
tex$command("documentclass", argument="article", option="12pt")

## ------------------------------------------------
## Method `TexFormatter$environment`
## ------------------------------------------------

tex = TexFormatter$new()
tex$environment("frame", argument="frame title", "line 1", "line 2")

## ------------------------------------------------
## Method `TexFormatter$typeset.equation`
## ------------------------------------------------

SIR = Model$new(
  title = "SIR",
  S ~ -beta*S*I/N, # the dS/dt equation
  I ~ beta*S*I/N - gamma*I, # the dI/dt equation
  R ~ gamma*I, # the dR/dt equation
  N = S + I + R # the total population N
)
tex = TexFormatter$new()
tex$typeset.equation(SIR$equations$equations)
tex$typeset.equation(SIR$equations$where)

junlingm/REpiSim documentation built on Nov. 28, 2023, 2:35 a.m.