asltx: Convert REDUCE output to LaTeX

View source: R/redcasltx.R

asltxR Documentation

Convert REDUCE output to LaTeX

Description

sends the name of an array or expression to the REDUCE process for display either as is or as LaTeX using the FANCY switch. If the object is an array, each element is printed. The LaTeX produced by REDUCE is then post-processed to translate object names according to a map specified by the user, to convert array arguments to indices and, optionally, to enclose the result in a math environment specified by the user.

Usage

asltx(id, x, usermap, mathenv="", mode="fancy", notify=0, timeout=0, debug=FALSE)

Arguments

id

session identification returned by redStart. Required.

x

A character vector with one element naming the object to display.

usermap

A list containing one or both of the following items:

ident

a named character vector specifying translation of REDUCE identifiers to LaTeX names. The element name is the REDUCE identifier and the value the LaTeX name.

index

a named character vector specifying the conversion of arguments to indices. The name specifies which identifier to process. Since this mapping is done after the identifier mapping, you need to use the LaTeX name for any name you have mapped. The value is a sequence of ^ and _ characters specifiying whether the index is contravariant (superscript) or covariant (subscript), respectively.

This argument is optional. Default is no user-specified translation.

mathenv

Character. The name of a LaTeX math environment in which to enclose an expression or each element of an array. Defaults to no environment. This is useful for displaying the array elements in a subordinate math environment, for example \dmath* within a dgroup environment.

mode

Character. Whether to display x as LaTeX ("fancy") or not ("nat"). Default is "fancy". "" is treated as "nat".

notify

while waiting for the REDUCE commands to complete, write a note to the console every ⁠notify⁠ seconds. Default is zero which suppresses the message.

timeout

numer of seconds after which to terminate the function if it is still waiting for output from the REDUCE process. Default is 0 which will never initiate termination.

debug

Boolean. When TRUE the mappings are printed to enable debugging since regular expressions can be tricky. Default is FALSE.

Details

asltx uses redExec to display the object x as desired by:

  1. constructing a call to the REDUCE function asltx with appropriate quoting of the arguments;

  2. executing this call using redExec and specifying split=TRUE;

  3. applying a standard set of transformations to remove some non-LaTeX markup and perform some conversions on the result. See below;

  4. applying a set of transformations specified by the usermap argument.

Features of REDUCE conversion to LaTeX

REDUCE provides three different methods for converting output to LaTeX, the packages TMPRINT, RLFI and TRI. redcas uses the TMPRINT package which was designed for use with the TeXmacs editor (hence TM) and triggers conversion using the fancy and fancy_tex switches. TMPRINT was chosen because it can be easily applied to fragments, supports using the \frac command, produces LaTeX output and is well supported. In contrast RLFI is designed to produce complete documents using LaTeX 2.09 syntax and does not support the \frac command. TRI produces plain TeX output.

TMPRINT supports

  1. inserting \left and \right in nested parentheses, braces and brackets;

  2. converting variables whose names are those of Greek letters to the corresponding LaTeX command. Capitalized names are mapped to the command for the capital. For example, psi is mapped to \psi while Psi is mapped to \Psi. There are two exceptions; epsilon and kappa are mapped to \varepsilon and \varkappa, respectively. If you need \epsilon or \kappa you can use the usermap argument;

  3. the following names are mapped to special symbols:

    infinity \infty union \cup
    partial!-df \partial member \in
    empty!-set \emptyset and \wedge
    not \neg or \vee
    not \neg when |
    leq \leq !*wcomma!* ,\,
    geq \geq replaceby \Rightarrow
    neq \neq !~ \forall
    intersection \cap

Standard transformations

Standard transformations done by asltx are as follows:

  1. removal of markup used for interactive display in TeXmacs and which has no correspondence with LaTeX markup;

  2. replacement of REDUCE assignments := and ~:=~ with a plain equals sign;

  3. reversing the unwanted conversion of numeric suffix to index. The REDUCE FANCY option assumes that a numeric suffix to a variable is a subscript index even when the variable is an array, e.g. cs1(a,b) is converted to cs_{1}(a,b). asltx undoes this conversion.

  4. removal of redundant mathrm commands;

  5. removal of unnecessary trailing spaces from Greek capitals;

  6. replacement of mathit{Q} with mathrm{Q} where Q is one of the Greek capitals A, B, E, Z, H, I, M, N, O, P. i.e. those which have the same form as a Latin capital. This transformation is required to have the same type for as the Greek-only capitals.

User transformations

The usermap argument allows renaming variables, converting arguments in REDUCE objects to indices and specifying whether indices are covariant or contravariant. If a LaTeX command is used in the map, four backslashes are required to get the single backslash for LaTeX because the mapping is done using regular expressions. For example suppose we have arrays s, g, kminus and kplus in REDUCE and want the following mapping to LaTeX

REDUCE LaTeX
s \Sigma
g g
kminus K^-
kplus K^+

then the ident element of the list passed to usermap would be

ident=c("s"="\\\\Sigma", kminus="K^-", kplus="K^\\+")

Since we are not mapping g it does not need to appear here.

To illustrate mapping arguments to indices, assume that all four arrays in the example above are two dimensional and s has one subscript and one superscript, g and kminus have two subscripts and kplus has two superscripts then the index element will be

index=c("\\\\Sigma"="_^", "g"="__", "K^-"="__", "K^\\+"="^^")

Note that the name of the index elements must be the result of applying the ident mapping. The names are regular expressions so the character + must be escaped in the names.

Value

A list containing the following elements is returned:

tex

the transformed LaTeX output;

out

the output of the executed commands;

cmd

the executed commands

raw

the interspersed commands and output.

Author(s)

Martin Gregory

See Also

redExec for details of executing REDUCE code.

Examples

## start the session
s1 <- redStart()

## can only run code if session was successfully started
if (is.numeric(s1)) {
   ## create the arrays
   redcode <- c("array g(2,2), s(3,3), kplus(2,2), kminus(2,2) ;",
		"operator x;",
		"g(0,0) := -u^(-2);",
		"g(1,1) := (u*x(3))^2;",
		"g(2,2) := g(1,1) * (sin(x(1)))^2;",
		"s(0,0) := 0;",
		"s(1,1) := df((u*x(3))^2, x(3));",
		"s(2,2) := df(g(2,2), x(1));",
		"s(3,3) := u^2;",
		"kplus(0,0) := df(g(0,0), u);",
		"kplus(1,1) := df(g(1,1), u);",
		"kplus(2,2) := df(g(2,2), u) ;",
		"kminus(0,0) := df(g(2,2), u);",
		"kminus(1,1) := df(g(1,1), u);",
		"kminus(2,2) := df(g(0,0), u) ;",
		"on nero ;"
		)

   o2 <- redExec(s1, redcode)

   ## create LaTeX output 
   writeLines(c("", asltx(s1, "g", mathenv="")[["tex"]], ""))
   writeLines(c("", asltx(s1, "s", mathenv="")[["tex"]], ""))
   writeLines(c("", asltx(s1, "kplus", mathenv="")[["tex"]], ""))
   writeLines(c("", asltx(s1, "kminus", mathenv="")[["tex"]], ""))

   ## close the session
   redClose(s1)
}

redcas documentation built on April 12, 2025, 1:40 a.m.