Learn R Programming

hwriterPlus (version 1.0-3)

hwriteLatex: Insert LaTeX Code into an HTML File

Description

This makes use of the MathJax JavaScript functions. Standard LaTeX input will be turned into MathML and displayed in any modern browser.

Usage

as.latex(x, label = NULL, inline = ifelse(is.null(label), TRUE, FALSE), count = ifelse(is.null(label), FALSE, TRUE)) hwriteLatex(ltx, page = NULL, table.attributes = NULL, tr.attributes = "", td.attributes = NULL, ...)

Arguments

x
Character. A string containing a mathematical expression in LaTeX notation.
label
Character. A text string giving the label to be displayed before the equation.
inline
Logical. Set to TRUE for a mathematical expression to be included inline. Set to FALSE for a displayed mathematical expression.
count
Logical. Set to TRUE for a numbered equation, FALSE otherwise.
ltx
Either a string containing a mathematical expression in LaTeX notation or an object of class latex as produced by as.latex.
page
Character. An optional connection. A character string naming the file to write to or a page object returned by newPage or openPage.
table.attributes
Character. A string specifying attributes of the table for a displayed equation. See details.
tr.attributes
Character. A string specifying attributes of the table row for a displayed equation. Default is "" meaning no special attributes.
td.attributes
Character. A string specifying attributes of the table data entries for a displayed equation. See details.
...
Possible attributes to be attached to the mathematical expression.

Value

as.latex returns a list of class latex with components:
alt
A character string containing the A string containing a mathematical expression in LaTeX notation.
inline
A logical value.
count
A logical value.
label
If not NULL a character string.
hwriteLatex produces a character vector containing the output HTML code. If page is specified this text is appended to the specified file.

Details

Mathematical expressions in LaTeX format will be translated into MathML by the MathJax JavaScript program.

Mathematical expressions can be inserted inline (within text) or on a single line as a displayed expression, corresponding to the use of $...$ and $$...$$ in normal LaTeX.

One problem with writing LaTeX expressions is that all backslashes must be repeated. See the examples.

See the examples for an example of arguments to be supplied to newPage to ensure mathematical expressions are displayed.

If ltx is a string containing a mathematical expression in LaTeX notation, or if it is an object of class latex with inline = TRUE then any values assigned to table.attributes tr.attributes and td.attributes are ignored. Otherwise the default value of table.attributes when this argument is NULL depends on whether the mathematical expression has an equation number or not, which is determined by whether count in the latex object is TRUE or FALSE. If count = FALSE then table.attributes is assigned the value "border = '0'". If count = FALSE then table.attributes is assigned the value "border = '0' width = '90%'". Similarly the default value of td.attributes depends on whether the mathematical expression has an equation number or not. If count = FALSE then td.attributes is ignored. If count = FALSE then table.attributes is assigned the value c("width = '50'","align = 'center'", "align = 'right' width = '50'"). The explanation for these assignments is that if a displayed mathematical expression has no equation number it is constructed as a table comprised of a single row with a single entry in that row. If the expression has an equation number, it is constructed as a table comprised of a single row with three entries: the first is a non-breaking space, the second is the mathematical expression itself and the third is the equation number.

Note that these functions are modified versions of as.latex and HTML.latex from the package R2HTML. The most notable changes are that HTML.latex does not allow for inclusion of table and table data attributes in mathematical expressions and the numbering of equations uses R code rather than JavaScript. The numbering system for equations adds an entry to the global vector hwriterEquationList whenever a numbered equation is created. If a label is specified via the label argument, by for example, label = "anequationlabel", then an anchor is created as part of the table data entry containing the equation number, using an HTML a element. The actual anchor name is eq:anequationlabel, which copies the label categorizing approach used in LaTeX. If no label is specified, but the equation is numbered, the name used for the anchor is eq:equationxx where xx is the equation number. Then the number of an equation corresponding to a given label may be retrieved using the function eqRef (intended to be reminiscent of the LaTeX \ref command).

To see what attributes are valid for tables, table rows and table data, go to http://www.w3schools.com/tags/ and click on the desired tag.

References

MathJax: http://www.mathjax.org/

See Also

newPage

Examples

Run this code
as.latex("See what is added by as.latex", label = "a label",
          inline = FALSE, count = TRUE)
### Run this example to see what is written to the file
## Not run: 
# ### An inline expression will be written to standard output as is
# ### Note that the backslashes remain duplicated in this case
# ### I think this is due to a line of code in hwriter::hwriteString
# ###   if (is.null(page)) txt
# ### which writes the txt literally rather than using cat
# hwrite("Here is an inline mathematical expression:
#         `\int_{-\infty}^{1}f(x)dx`")
# ### The same expression but displayed
# hwriteLatex(as.latex("\int_{-\infty}^{1}f(x)dx",
#                      inline = FALSE, count = FALSE),
#             table.attributes = "border = '1'",
#             tr.attributes = "bgcolor = 'white'")
# 
# 
# ### The same example written to a file
# ### Create a temporary file first
# tmpDir <- tempdir()
# fileName <- file.path(tmpDir, 'hwriteLatex.html')
# ### Copy some necessary files
# cssDir <- file.path(system.file(package = 'hwriterPlus'), 'css')
# file.copy(file.path(cssDir, "BrowserExample.css"), tmpDir)
# 
# ### Open file for writing
# p <- newPage(fileName,
#              title = "Example of a Document for Display in a Browser",
#              link.css = c("BrowserExample.css"))
# hwrite("Here is an inline mathematical expression:
#         `\int_{-\infty}^{1}f(x)dx`", p, br)
# ### The same expression but displayed
# hwrite("Here is a displayed version of the same expression enclosed in a
#        box", p, br)
# hwriteLatex(as.latex("\int_{-\infty}^{1}f(x)dx",
#                      inline = FALSE, count = FALSE),
#             p,
#             table.attributes = "border = '1'",
#             tr.attributes = "bgcolor = 'white'")
# ### We can also have equation numbers and labels when writing to a file
# hwrite("Here is a different expression which has an equation number and
# assigned a label", p, br)
# hwriteLatex(as.latex("\{ 26.119 < \sum_{i=1}^n(X_i-\bar{X})^2\}
# \bigcup\ \{ 5.629 > \sum_{i=1}^n (X_i-\bar{X})^2 \}.",
#                      inline = FALSE, label = "anequationlabel"),
#             page = p,
#             tr.attributes = "bgcolor = 'gray'",
#             td.attributes = c("width = '50'",
#                               "align = 'center' bgcolor = 'yellow'",
#                               "align = 'right' width = '50'"))
# closePage(p)
# ### Open a web browser and examine the resulting file
# if (interactive()) try(browseURL(fileName))
# ## End(Not run)

Run the code above in your browser using DataLab