~ubuntu-branches/ubuntu/wily/octave-miscellaneous/wily

« back to all changes in this revision

Viewing changes to inst/textable.m

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2012-10-17 13:40:55 UTC
  • mfrom: (1.1.6)
  • mto: (5.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: package-import@ubuntu.com-20121017134055-e8lrxjd3qgcd3kmt
Tags: upstream-1.2.0
Import upstream version 1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 2012 Markus Bergholz <markuman@gmail.com>
 
2
## Copyright (C) 2012 carnë Draug <carandraug+dev@gmail.com>
 
3
##
 
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
 
7
## version.
 
8
##
 
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
 
12
## details.
 
13
##
 
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/>.
 
16
 
 
17
## -*- texinfo -*-
 
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).
 
21
##
 
22
## The input matrix must be numeric and two dimensional.
 
23
##
 
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.
 
27
##
 
28
## Available parameters are:
 
29
## @itemize @bullet
 
30
## @item @code{file}: filename to save the generated LaTeX source. Requires a string
 
31
## as value.
 
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.
 
38
## @end itemize
 
39
##
 
40
## The basic usage is to generate the source for a table without lines and right
 
41
## alignment (default values):
 
42
## @example
 
43
## @group
 
44
## textable (data)
 
45
##     @result{}
 
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 \\
 
50
##        \end@{tabular@}
 
51
## @end group
 
52
## @end example
 
53
##
 
54
## Alternatively, the source can be saved directly into a file:
 
55
## @example
 
56
## @group
 
57
## textable (data, "file", "data.tex");
 
58
## @end group
 
59
## @end example
 
60
##
 
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:
 
64
## @example
 
65
## @group
 
66
## textable (data, "rlines", "clines", "align", "c")
 
67
##     @result{}
 
68
##        \begin@{tabular@}@{|c|c|c|@}
 
69
##            \hline 
 
70
##            0.889283 & 0.949328 & 0.205663 \\
 
71
##            \hline 
 
72
##            0.225978 & 0.426528 & 0.189561 \\
 
73
##            \hline 
 
74
##            0.245896 & 0.466162 & 0.225864 \\
 
75
##            \hline 
 
76
##        \end@{tabular@}
 
77
## @end group
 
78
## @end example
 
79
##
 
80
## Finnally, for math mode, it is also possible to place the matrix in an array
 
81
## environment and name the matrix:
 
82
## @example
 
83
## @group
 
84
## textable (data, "math", "matrix-name")
 
85
##     @result{}
 
86
##        \begin@{displaymath@}
 
87
##          \mathbf@{matrix-name@} =
 
88
##          \left(
 
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 \\
 
93
##          \end@{array@}
 
94
##          \right)
 
95
##        \end@{displaymath@}
 
96
## @end group
 
97
## @end example
 
98
##
 
99
## @seealso{csv2latex, publish}
 
100
## @end deftypefn
 
101
 
 
102
function [str] = textable (data, varargin)
 
103
 
 
104
  if (nargin < 1)
 
105
    print_usage;
 
106
  elseif (!isnumeric (data) || !ismatrix (data)|| ndims (data) != 2)
 
107
    error ("first argument must be a 2D numeric matrix");
 
108
  endif
 
109
 
 
110
  p = inputParser;
 
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{:});
 
117
 
 
118
  ## if there is no filename given we won't print to file
 
119
  print_to_file = all (!strcmp ("file", p.UsingDefaults));
 
120
 
 
121
  ## will we use an array environment (in displaymath)
 
122
  math = all (!strcmp ("math", p.UsingDefaults));
 
123
 
 
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
 
129
  else
 
130
    align = sprintf ("%s", repmat (p.Results.align, [1, columns(data)]));
 
131
  endif
 
132
 
 
133
  ## start making table
 
134
  if (math)
 
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));
 
139
  else
 
140
    str = sprintf ("\\begin{tabular}{%s}\n", align);
 
141
  endif
 
142
 
 
143
  if (p.Results.rlines)
 
144
    str = strcat (str, "    \\hline \n");
 
145
  endif
 
146
 
 
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");
 
153
    endif
 
154
  endfor
 
155
 
 
156
  if (math)
 
157
    str = strcat (str, "  \\end{array}\n  \\right)\n\\end{displaymath}");
 
158
  else
 
159
    str = strcat (str, "\\end{tabular}\n");
 
160
  endif
 
161
 
 
162
  if (print_to_file)
 
163
    [fid, msg] = fopen (p.Results.file, "w");
 
164
    if (fid == -1)
 
165
      error ("Could not fopen file '%s' for writing: %s", p.Results.file, msg);
 
166
    endif
 
167
    fputs (fid, str);
 
168
    fclose (fid);
 
169
  endif
 
170
endfunction