1
## Copyright (C) 2012 Markus Bergholz <markuman@gmail.com>
2
## Copyright (C) 2012 carnë Draug <carandraug+dev@gmail.com>
4
## This program is free software; you can redistribute it and/or modify it under
5
## the terms of the GNU General Public License as published by the Free Software
6
## Foundation; either version 3 of the License, or (at your option) any later
9
## This program is distributed in the hope that it will be useful, but WITHOUT
10
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
## You should have received a copy of the GNU General Public License along with
15
## this program; if not, see <http://www.gnu.org/licenses/>.
18
## @deftypefn {Function File} {} textable (@var{matrix})
19
## @deftypefnx {Function File} {} textable (@var{matrix}, @var{params}, @dots{})
20
## Save @var{matrix} in LaTeX format (tabular or array).
22
## The input matrix must be numeric and two dimensional.
24
## The generated LaTeX source can be saved directly to a file with the option
25
## @command{file}. The file can then be inserted in any latex document by using
26
## the @code{\input@{latex file name without .tex@}} statement.
28
## Available parameters are:
30
## @item @code{file}: filename to save the generated LaTeX source. Requires a string
32
## @item @code{rlines}: display row lines.
33
## @item @code{clines}: display column lines.
34
## @item @code{align}: column alignment. Valid values are `l', `c' and `r' for
35
## center, left and right (default).
36
## @item @code{math}: create table in array environment inside displaymath
37
## environment. It requires a string as value which will be the name of the matrix.
40
## The basic usage is to generate the source for a table without lines and right
41
## alignment (default values):
46
## \begin@{tabular@}@{rrr@}
47
## 0.889283 & 0.949328 & 0.205663 \\
48
## 0.225978 & 0.426528 & 0.189561 \\
49
## 0.245896 & 0.466162 & 0.225864 \\
54
## Alternatively, the source can be saved directly into a file:
57
## textable (data, "file", "data.tex");
61
## The appearance of the table can be controled with switches and key values. The
62
## following generates a table with both row and column lines (rlines and clines),
63
## and center alignment:
66
## textable (data, "rlines", "clines", "align", "c")
68
## \begin@{tabular@}@{|c|c|c|@}
70
## 0.889283 & 0.949328 & 0.205663 \\
72
## 0.225978 & 0.426528 & 0.189561 \\
74
## 0.245896 & 0.466162 & 0.225864 \\
80
## Finnally, for math mode, it is also possible to place the matrix in an array
81
## environment and name the matrix:
84
## textable (data, "math", "matrix-name")
86
## \begin@{displaymath@}
87
## \mathbf@{matrix-name@} =
89
## \begin@{array@}@{*@{ 3 @}@{rrr@}@}
90
## 0.889283 & 0.949328 & 0.205663 \\
91
## 0.225978 & 0.426528 & 0.189561 \\
92
## 0.245896 & 0.466162 & 0.225864 \\
95
## \end@{displaymath@}
99
## @seealso{csv2latex, publish}
102
function [str] = textable (data, varargin)
106
elseif (!isnumeric (data) || !ismatrix (data)|| ndims (data) != 2)
107
error ("first argument must be a 2D numeric matrix");
111
p = p.addSwitch ("clines");
112
p = p.addSwitch ("rlines");
113
p = p.addParamValue ("math", "X", @ischar);
114
p = p.addParamValue ("file", "matrix.tex", @ischar);
115
p = p.addParamValue ("align", "r", @(x) any(strcmpi(x, {"l", "c", "r"})));
116
p = p.parse (varargin{:});
118
## if there is no filename given we won't print to file
119
print_to_file = all (!strcmp ("file", p.UsingDefaults));
121
## will we use an array environment (in displaymath)
122
math = all (!strcmp ("math", p.UsingDefaults));
124
if (p.Results.clines)
125
align = sprintf ("|%s", repmat (cstrcat (p.Results.align, "|"), [1, columns(data)]));
126
## if we are in a array environment, we need to remove the last | or we end
127
## with two lines at the end
128
if (math), align = align(1:end-1); endif
130
align = sprintf ("%s", repmat (p.Results.align, [1, columns(data)]));
133
## start making table
135
str = "\\begin{displaymath}\n";
136
str = strcat (str, sprintf (" \\mathbf{%s} =\n", p.Results.math));
137
str = strcat (str, " \\left(\n");
138
str = strcat (str, sprintf (" \\begin{array}{*{ %d }{%s}}\n", columns (data), align));
140
str = sprintf ("\\begin{tabular}{%s}\n", align);
143
if (p.Results.rlines)
144
str = strcat (str, " \\hline \n");
147
for ii = 1:rows(data)
148
str = strcat (str, sprintf (" %g", data (ii, 1)));
149
str = strcat (str, sprintf (" & %g", data (ii, 2:end)));
150
str = strcat (str, " \\\\\n");
151
if (p.Results.rlines)
152
str = strcat (str, " \\hline \n");
157
str = strcat (str, " \\end{array}\n \\right)\n\\end{displaymath}");
159
str = strcat (str, "\\end{tabular}\n");
163
[fid, msg] = fopen (p.Results.file, "w");
165
error ("Could not fopen file '%s' for writing: %s", p.Results.file, msg);