Debian Codegraph @

man/mat2tex.Rd

raw
1 %%-- Original from Vincent Carey, see (commented) E-mail at end !
2 \name{mat2tex}
3 \alias{mat2tex}
4 \title{Produce LaTeX commands to print a matrix}
5 \usage{
6 mat2tex(x, file= "mat.tex", envir = "tabular",
7 nam.center = "l", col.center = "c",
8 append = TRUE, digits = 3, title)
9 }
10 \arguments{
11 \item{x}{a matrix}
12 \item{file}{names the file to which LaTeX commands should be written}
13 \item{envir}{a string, the LaTeX environment name; default is
14 \code{"tabular"}; useful maybe \code{"array"}, or other versions of
15 tabular environments.}
16 \item{nam.center}{character specifying row names should be center;
17 default \code{"l"}.}
18 \item{col.center}{character (vector) specifying how the columns should
19 be centered; must have values from \code{c("l","c","r")}; defaults
20 to \code{"c"}.}
21 \item{append}{logical; if \code{FALSE}, will destroy the file
22 \code{file} before writing commands to it; otherwise (by default),
23 simply adds commands at the end of file \code{file}.}
24 \item{digits}{integer; setting of \code{\link{options}(digits=..)} for
25 purpose of number representation.}
26 \item{title}{a string, possibly using LaTeX commands, which will span
27 the columns of the LaTeX matrix}
28 }
29 \description{
30 \dQuote{Translate} an \R matrix (like object) into a LaTeX table,
31 using \code{\\begin{tabular} ...}.
32 }
33 \value{
34 No value is returned. This function, when used correctly,
35 only writes LaTeX commands to a file.
36 }
37 \author{For S: Vincent Carey \email{[email protected]}, from a
38 post on Feb.19, 1991 to S-news. Port to \R (and a bit more) by
39 Martin Maechler \email{[email protected]}.
40 }
41 \seealso{
42 \code{\link[Hmisc]{latex}} in package \CRANpkg{Hmisc} is more flexible
43 (but may surprise by its auto-printing ..).
44 }
45 \examples{
46 mex <- matrix(c(pi,pi/2,pi/4,exp(1),exp(2),exp(3)),nrow=2, byrow=TRUE,
47 dimnames = list(c("$\\\\pi$","$e$"), c("a","b","c")))
48 mat2tex(mex, file = print(tf <- tempfile("mat", , ".tex")),
49 title="$\\\\pi, e$, etc." )% double-esc -> 4 x backslash
50
51 ## The last command produces the file "mat<xyz>.tex" containing
52
53 ##> \\begin{tabular} {| l|| c| c| c|}
54 ##> \\multicolumn{ 4 }{c}{ $\\pi, e$, etc. } \\\\ \\hline
55 ##> \\ & a & b & c \\\\ \\hline \\hline
56 ##> $\pi$ & 3.14 & 1.57 & 0.785 \\\\ \\hline
57 ##> $e$ & 2.72 & 7.39 & 20.1 \\\\ \\hline
58 ##> \\end{tabular}
59
60 ## Now you have to properly embed the contents of this file
61 ## in a LaTeX document -- for example, you will need a
62 ## preamble, the \\begin{document} statement, etc.
63
64 ## Note that the backslash needs protection in dimnames
65 ## or title actions.
66
67 mat2tex(mex, stdout(), col.center = c("r","r","c"))
68 }
69 \keyword{interface} % to latex
70 \keyword{utilities}
71 %%
72 %% From: [email protected] (Vincent J. Carey)
73 %% Subject: mat2tex, a report generation aid (LONG)
74 %% To: [email protected]
75 %% Date: Tue, 19 Feb 91 19:25:05 EST
76 %%
77 %% Has there been any work on "report generation" from S
78 %% beyond the tbl() function? I have been manually
79 %% transcribing S matrix elements to LaTeX tables
80 %% and find this to be a tedious and error-prone process.
81 %% Certainly there are ways of cutting and pasting window
82 %% contents to reduce the troubles associated with digit
83 %% keying, but this is far from foolproof, and is also
84 %% pretty dull.
85 %%
86 %% Therefore I offer this function, mat2tex(), which
87 %% produces LaTeX commands to format the entries of
88 %% an S matrix. The function will, by default, place
89 %% the commands in the file "mat.tex".
90 %%
91 %% The documentation file included is fairly explicit. The
92 %% function does not produce a complete LaTeX program, but
93 %% a fragment "containing" a matrix. Column and row labels
94 %% (which may include references to special LaTeX symbols,
95 %% provided backslash protection is maintained) are propagated
96 %% to the LaTeX fragment. There is a provision for a matrix "title".
97 %%
98 %% Unadulterated output of a simple example (see the doc page
99 %% for the input):
100 %%
101 %% \begin{tabular} {| l|| c| c| c|}
102 %% \multicolumn{ 4 }{c}{ $\pi, e$, etc. } \\ \hline
103 %% \ & a & b & c \\ \hline \hline
104 %% $\pi$ & 3.14 & 1.57 & 0.785 \\ \hline
105 %% $e$ & 2.72 & 7.39 & 20.1 \\ \hline
106 %% \end{tabular}
107 %%
108 %% Further work needed: digit/decimal-point alignment,
109 %% consistent boxing of the title, more arguments to allow
110 %% sensible customization -- e.g., the user may not desire
111 %% \hline between rows. Comments, criticism and enhancements
112 %% are welcome.
113 %%
114 %% -------------------------------------------------------------------------
115 %%
116 %% Vincent J. Carey
117 %% Department of Biostatistics
118 %% Johns Hopkins School of Public Health
119 %%
120 %% [email protected]