Convert an S object to LaTeX, and Related Utilities
latex
converts its argument to a ‘.tex’ file appropriate
for inclusion in a LaTeX2e document. latex
is a generic
function that calls one of latex.default
,
latex.function
, latex.list
.
latex.default
does appropriate rounding and decimal alignment and produces a
file containing a LaTeX tabular environment to print the matrix or data.frame
x
as a table.
latex.function
prepares an S function for printing by issuing sed
commands that are similar to those in the
S.to.latex
procedure in the s.to.latex
package (Chambers
and Hastie, 1993). latex.function
can also produce
verbatim
output or output that works with the Sweavel
LaTeX style at https://biostat.app.vumc.org/wiki/Main/SweaveTemplate.
latex.list
calls latex
recursively for each element in the argument.
latexTranslate
translates particular items in character
strings to LaTeX format, e.g., makes a^2 = a\$^2\$ for superscript within
variable labels. LaTeX names of greek letters (e.g., "alpha"
)
will have backslashes added if greek==TRUE
. Math mode is
inserted as needed.
latexTranslate
assumes that input text always has matches,
e.g. [) [] (] ()
, and that surrounding by \$\$ is OK.
htmlTranslate
is similar to latexTranslate
but for html
translation. It doesn't need math mode and assumes dollar signs are
just that.
latexSN
converts a vector floating point numbers to character
strings using LaTeX exponents. Dollar signs to enter math mode are not
added. Similarly, htmlSN
converts to scientific notation in html.
latexVerbatim
on an object executes the object's print
method,
capturing the output for a file inside a LaTeX verbatim environment.
dvi
uses the system latex
command to compile LaTeX code produced
by latex
, including any needed styles. dvi
will put a \documentclass{report} and \end{document} wrapper
around a file produced by latex
. By default, the geometry LaTeX package is
used to omit all margins and to set the paper size to a default of
5.5in wide by 7in tall. The result of dvi
is a .dvi file. To both
format and screen display a non-default size, use for example
print(dvi(latex(x), width=3, height=4),width=3,height=4)
. Note that
you can use something like xdvi -geometry 460x650 -margins 2.25in
file without changing LaTeX defaults to emulate this.
dvips
will use the system dvips
command to print the .dvi file to
the default system printer, or create a postscript file if file
is specified.
dvigv
uses the system dvips
command to convert the input object
to a .dvi file, and uses the system dvips
command to convert it to
postscript. Then the postscript file is displayed using Ghostview
(assumed to be the system command gv
).
There are show
methods for displaying typeset LaTeX
on the screen using the system xdvi
command. If you show
a LaTeX file created by
latex
without running it through dvi
using
show.dvi(object)
, the
show
method will run it through dvi
automatically.
These show
methods are not S Version 4 methods so you have to use full names such
as show.dvi
and show.latex
. Use the print
methods for
more automatic display of typesetting, e.g. typing latex(x)
will
invoke xdvi to view the typeset document.
latex(object, ...) ## Default S3 method: latex(object, title=first.word(deparse(substitute(object))), file=paste(title, ".tex", sep=""), append=FALSE, label=title, rowlabel=title, rowlabel.just="l", cgroup=NULL, n.cgroup=NULL, rgroup=NULL, n.rgroup=NULL, cgroupTexCmd="bfseries", rgroupTexCmd="bfseries", rownamesTexCmd=NULL, colnamesTexCmd=NULL, cellTexCmds=NULL, rowname, cgroup.just=rep("c",length(n.cgroup)), colheads=NULL, extracolheads=NULL, extracolsize='scriptsize', dcolumn=FALSE, numeric.dollar=!dcolumn, cdot=FALSE, longtable=FALSE, draft.longtable=TRUE, ctable=FALSE, booktabs=FALSE, table.env=TRUE, here=FALSE, lines.page=40, caption=NULL, caption.lot=NULL, caption.loc=c('top','bottom'), star=FALSE, double.slash=FALSE, vbar=FALSE, collabel.just=rep("c",nc), na.blank=TRUE, insert.bottom=NULL, insert.bottom.width=NULL, insert.top=NULL, first.hline.double=!(booktabs | ctable), where='!tbp', size=NULL, center=c('center','centering','centerline','none'), landscape=FALSE, multicol=TRUE, math.row.names=FALSE, already.math.row.names=FALSE, math.col.names=FALSE, already.math.col.names=FALSE, hyperref=NULL, continued='continued', ...) # x is a matrix or data.frame ## S3 method for class 'function' latex( object, title=first.word(deparse(substitute(object))), file=paste(title, ".tex", sep=""), append=FALSE, assignment=TRUE, type=c('example','verbatim','Sinput'), width.cutoff=70, size='', ...) ## S3 method for class 'list' latex( object, title=first.word(deparse(substitute(object))), file=paste(title, ".tex", sep=""), append=FALSE, label, caption, caption.lot, caption.loc=c('top','bottom'), ...) ## S3 method for class 'latex' print(x, ...) latexTranslate(object, inn=NULL, out=NULL, pb=FALSE, greek=FALSE, na='', ...) htmlTranslate(object, inn=NULL, out=NULL, greek=FALSE, na='', code=htmlSpecialType(), ...) latexSN(x) htmlSN(x, pretty=TRUE, ...) latexVerbatim(x, title=first.word(deparse(substitute(x))), file=paste(title, ".tex", sep=""), append=FALSE, size=NULL, hspace=NULL, width=.Options$width, length=.Options$length, ...) dvi(object, ...) ## S3 method for class 'latex' dvi(object, prlog=FALSE, nomargins=TRUE, width=5.5, height=7, ...) ## S3 method for class 'dvi' print(x, ...) dvips(object, ...) ## S3 method for class 'latex' dvips(object, ...) ## S3 method for class 'dvi' dvips(object, file, ...) ## S3 method for class 'latex' show(object) # or show.dvi(object) or just object dvigv(object, ...) ## S3 method for class 'latex' dvigv(object, ...) # or gvdvi(dvi(object)) ## S3 method for class 'dvi' dvigv(object, ...)
object |
For |
x |
any object to be |
title |
name of file to create without the .tex extension. If this
option is not set, value/string of |
file |
name of the file to create. The default file name is ‘x.tex’ where
|
append |
defaults to |
label |
a text string representing a symbolic label for the table for referencing
in the LaTeX \label and \ref commands.
|
rowlabel |
If |
rowlabel.just |
If |
cgroup |
a vector of character strings defining major column headings. The default is to have none. |
n.cgroup |
a vector containing the number of columns for which each element in
cgroup is a heading. For example, specify |
rgroup |
a vector of character strings containing headings for row groups.
|
n.rgroup |
integer vector giving the number of rows in each grouping. If |
cgroupTexCmd |
A character string specifying a LaTeX command to be
used to format column group labels. The default, |
rgroupTexCmd |
A character string specifying a LaTeX command to be
used to format row group labels. The default, |
rownamesTexCmd |
A character string specifying a LaTeX
command to be used to format rownames. The default, |
colnamesTexCmd |
A character string specifying a LaTeX command to be
used to format column labels. The default, |
cellTexCmds |
A matrix of character strings which are LaTeX
commands to be
used to format each element, or cell, of the object. The matrix
must have the same |
na.blank |
Set to |
insert.bottom |
an optional character string to typeset at the bottom of the table.
For |
insert.bottom.width |
character string; a tex width controlling the width of the
insert.bottom text. Currently only does something with using
|
insert.top |
a character string to insert as a heading right
before beginning |
first.hline.double |
set to |
rowname |
rownames for |
cgroup.just |
justification for labels for column groups. Defaults to |
colheads |
a character vector of column headings if you don't want
to use |
extracolheads |
an optional vector of extra column headings that will appear under the
main headings (e.g., sample sizes). This character vector does not
need to include an empty space for any |
extracolsize |
size for |
dcolumn |
see |
numeric.dollar |
logical, default |
math.row.names |
logical, set true to place dollar signs around the row names. |
already.math.row.names |
set to |
math.col.names |
logical, set true to place dollar signs around the column names. |
already.math.col.names |
set to |
hyperref |
if |
continued |
a character string used to indicate pages after the first when making a long table |
cdot |
see |
longtable |
Set to |
draft.longtable |
I forgot what this does. |
ctable |
set to |
booktabs |
set |
table.env |
Set |
here |
Set to |
lines.page |
Applies if |
caption |
a text string to use as a caption to print at the top of the first page of the table. Default is no caption. |
caption.lot |
a text string representing a short caption to be used in the “List of Tables”.
By default, LaTeX will use |
caption.loc |
set to |
star |
apply the star option for ctables to allow a table to spread over two columns when in twocolumn mode. |
double.slash |
set to |
vbar |
logical. When |
collabel.just |
justification for column labels. |
assignment |
logical. When |
where |
specifies placement of floats if a table environment is used. Default
is |
size |
size of table text if a size change is needed (default is no change).
For example you might specify |
center |
default is |
landscape |
set to |
type |
The default uses the S |
width.cutoff |
width of function text output in columns; see
|
... |
other arguments are accepted and ignored except that |
inn, out |
specify additional input and translated strings over the usual defaults |
pb |
If |
greek |
set to |
na |
single character string to translate |
code |
set to |
pretty |
set to |
hspace |
horizontal space, e.g., extra left margin for verbatim text. Default
is none. Use e.g. |
length |
for S-Plus only; is the length of the output page for printing and capturing verbatim text |
width,height |
are the |
prlog |
set to |
multicol |
set to |
nomargins |
set to |
latex.default
outputs a LaTeX comment containing the calling
statement. To suppress outputting this comment, run
options(omitlatexcom=TRUE)
before running. This is helpful
when running RMarkdown to produce pdf output using LaTeX, as this uses
pandoc
which is fooled into try to escape the percent
comment symbol.
If running under Windows and using MikTeX, latex
and yap
must be in your system path, and yap
is used to browse
‘.dvi’ files created by latex
. You should install the
‘geometry.sty’ and ‘ctable.sty’ styles in MikTeX to make optimum use
of latex()
.
On Mac OS X, you may have to append the ‘/usr/texbin’ directory to the
system path. Thanks to Kevin Thorpe
(kevin.thorpe@utoronto.ca) one way to set up Mac OS X is
to install X11 and X11SDK if not already installed,
start X11 within the R GUI, and issue the command
Sys.setenv( PATH=paste(Sys.getenv("PATH"),"/usr/texbin",sep=":")
)
. To avoid any complications of using X11 under MacOS, users
can install the TeXShop package, which will associate
‘.dvi’ files with a viewer that displays a ‘pdf’ version of
the file after a hidden conversion from ‘dvi’ to ‘pdf’.
System options can be used to specify external commands to be used.
Defaults are given by options(xdvicmd='xdvi')
or
options(xdvicmd='yap')
, options(dvipscmd='dvips')
,
options(latexcmd='latex')
. For MacOS specify
options(xdvicmd='MacdviX')
or if TeXShop is installed,
options(xdvicmd='open')
.
To use pdflatex rather than latex, set
options(latexcmd='pdflatex')
,
options(dviExtension='pdf')
, and set
options('xdvicmd')
to your chosen PDF previewer.
If running S-Plus and your directory for temporary files is not
‘/tmp’ (Unix/Linux) or ‘\windows\temp’ (Windows), add your
own tempdir
function such as
tempdir <- function() "/yourmaindirectory/yoursubdirectory"
To prevent the latex file from being displayed store the result of
latex
in an object, e.g. w <- latex(object, file='foo.tex')
.
latex
and dvi
return a
list of class latex
or dvi
containing character string
elements file
and style
. file
contains the name of the
generated file, and style
is a vector (possibly empty) of styles to
be included using the LaTeX2e \usepackage command.
latexTranslate
returns a vector of character strings
creates various system files and runs various Linux/UNIX system commands which are assumed to be in the system path.
Frank E. Harrell, Jr.,
Department of Biostatistics,
Vanderbilt University,
fh@fharrell.com
Richard M. Heiberger,
Department of Statistics,
Temple University, Philadelphia, PA.
rmh@temple.edu
David R. Whiting,
School of Clinical Medical Sciences (Diabetes),
University of Newcastle upon Tyne, UK.
david.whiting@ncl.ac.uk
x <- matrix(1:6, nrow=2, dimnames=list(c('a','b'),c('c','d','this that'))) ## Not run: latex(x) # creates x.tex in working directory # The result of the above command is an object of class "latex" # which here is automatically printed by the latex print method. # The latex print method prepends and appends latex headers and # calls the latex program in the PATH. If the latex program is # not in the PATH, you will get error messages from the operating # system. w <- latex(x, file='/tmp/my.tex') # Does not call the latex program as the print method was not invoked print.default(w) # Shows the contents of the w variable without attempting to latex it. d <- dvi(w) # compile LaTeX document, make .dvi # latex assumed to be in path d # or show(d) : run xdvi (assumed in path) to display w # or show(w) : run dvi then xdvi dvips(d) # run dvips to print document dvips(w) # run dvi then dvips library(tools) texi2dvi('/tmp/my.tex') # compile and produce pdf file in working dir. ## End(Not run) latex(x, file="") # just write out LaTeX code to screen ## Not run: # Use paragraph formatting to wrap text to 3 in. wide in a column d <- data.frame(x=1:2, y=c(paste("a", paste(rep("very",30),collapse=" "),"long string"), "a short string")) latex(d, file="", col.just=c("l", "p{3in}"), table.env=FALSE) ## End(Not run) ## Not run: # After running latex( ) multiple times with different special styles in # effect, make a file that will call for the needed LaTeX packages when # latex is run (especially when using Sweave with R) if(exists(latexStyles)) cat(paste('\usepackage{',latexStyles,'}',sep=''), file='stylesused.tex', sep='\n') # Then in the latex job have something like: # \documentclass{article} # \input{stylesused} # \begin{document} # ... ## End(Not run)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.