exams (version 2.1-0)

tex2image: Transforming LaTeX Code Using ImageMagick or pdf2svg

Description

Transformation of LaTeX code into an image by compiling to PDF and then transforming to PNG (by default) via ImageMagick's convert command or to SVG via pdf2svg.

Usage

tex2image(tex, format = "png", width = 6, pt = 12, density = 350, dir = NULL, tdir = NULL, idir = NULL, width.border = 0L, col.border = "white", resize = 650, shave = 2, packages = c("amsmath", "amssymb", "amsfonts"), header, header2 = NULL, Sweave = TRUE, show = TRUE, name = "tex2image")

Arguments

tex
character vector or list of character vectors. Each character vector is either the name of a LaTeX file or a vector containing LaTeX code directly.
format
character. Suffix for the type of graphic to convert to.
width
numeric. Width of the text in inch.
pt
numeric. Pointsize of the text.
density
numeric. Resolution density of the image.
dir
character specifying the output directory.
tdir
character specifying a temporary directory, by default this is chosen via tempdir.
idir
character specifying the path additional LaTeX inputs required.
width.border
numeric. Width of the framebox border.
col.border
character. Color of framebox border.
resize
numeric. Number of pixels for resizing the image.
shave
numeric. Number of pixels to shave the framebox from the edges.
packages
character. Names of LaTeX packages to be included.
header
character. LaTeX code to be included in the header of the LaTeX file before the beginning of the document. By default the parindent is set to 0 and sans serif fonts (phv) are used for both text and math.
header2
character. LaTeX code to be included in the header of the LaTeX file after the beginning of the document.
Sweave
logical. Should the LaTeX package Sweave.sty be included in the header?
show
logical. Show the resulting image(s) using browseURL.
name
character. Base name of the image file.

Value

Character vector with path(s) to image(s) generated from the LaTeX code.

Details

tex2image converts LaTeX code to image files, e.g., for inclusion in web pages. It proceeds in the following steps: (1) LaTeX code is embedded into a suitable .tex file. (2) This is compiled to PDF using texi2dvi. (3) The PDF is converted to an image file. By default, conversion is to PNG using ImageMagick's convert function or alternatively to SVG via pdfcrop followed by pdf2svg. To enable suitable clipping and cropping, each vector of LaTeX code has to fit onto a single page (A4). It is automatically embedded into a framebox that is removed in the conversion to image. If tex is a list of LaTeX chunks, then these are compiled to separate pages of a single PDF in a single LaTeX run. Each page is subsequently converted to a separate image. The respective image manipulation tools, i.e., either ImageMagick's convert or pdfcrop/pdf2svg, are assumed to be installed and available in the search path.

See Also

texi2dvi

Examples

Run this code
## Not run: 
# ## some simple LaTeX
# tex <- c("This is \\textbf{bold} and this \\textit{italic}.",
#   "Points on the unit circle: $x^2 + y^2 = 1$.")
# 
# ## default settings: PNG with sans serif fonts and width=6
# tex2image(tex)
# 
# ## SVG output
# tex2image(tex, format = "svg")
# 
# ## reduce width
# tex2image(tex, width = 2)
# 
# ## switch off header (-> LaTeX uses its standard serif fonts)
# tex2image(tex, header = NULL)
# ## End(Not run)

Run the code above in your browser using DataLab