comparison core/tools/matrix2latex.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function s = latex(M,varargin)
2 %LATEX Print a matrix in LaTeX tabular format.
3 % LATEX(M) prints out the numeric matrix M in a LaTeX tabular
4 % format. The '&' character appears between entries in a row, '\\'
5 % is appended to the ends of rows, and each entry is set in math
6 % mode. Complex numbers are understood, and exponentials will be
7 % converted to a suitable format.
8 %
9 % LATEX(M,'nomath') does not include the $$ needed to put each
10 % entry in math mode (e.g., for use with the amsmath matrix modes).
11 %
12 % LATEX(M,FMT) uses a format specifier FMT of the SPRINTF type for
13 % each entry.
14 %
15 % LATEX(M,FMT1,FMT2,...) works through the given format specifiers
16 % on each row of M. If fewer are given than the column size of M,
17 % the last is used repeatedly for the rest of the row.
18 %
19 % S = LATEX(M,...) does not display output but returns a character
20 % array S.
21 %
22 % Examples:
23 % latex( magic(4) )
24 % latex( magic(4), '%i', 'nomath' )
25 % latex( magic(4), '%i', '%.2f' )
26 %
27 % See also SPRINTF, SYM/LATEX.
28
29 % Copyright 2002 by Toby Driscoll. Last updated 12/06/02.
30
31 if ~isa(M,'double')
32 error('Works only for arrays of numbers.')
33 elseif ndims(M) > 2
34 error('Works only for 2D arrays.')
35 end
36
37 if nargin < 2
38 fmt = {'%#.5g'};
39 mathstr = '$';
40 else
41 fmt = varargin;
42 idx = strmatch('nomath',fmt);
43 if isempty(idx)
44 mathstr = '$';
45 else
46 mathstr = '';
47 fmt = fmt([1:idx-1 idx+1:end]);
48 if isempty(fmt), fmt = {'%#.5g'}; end
49 end
50 end
51
52 % Extend the format specifiers.
53 [m,n] = size(M);
54 if n > length(fmt)
55 [fmt{end:n}] = deal(fmt{end});
56 end
57
58 % Create one format for a row.
59 rowfmt = '';
60 for p = 1:n
61 % Remove blanks.
62 thisfmt = deblank(fmt{p});
63
64 % Add on imaginary part if needed.
65 if ~isreal(M(:,p))
66 % Use the same format as for the real part, but force a + sign for
67 % positive numbers.
68 ifmt = thisfmt;
69 j = findstr(ifmt,'%');
70 if ~any(strcmp(ifmt(j+1),['-';'+';' ';'#']))
71 ifmt = [ifmt(1:j) '+' ifmt(j+1:end)];
72 else
73 ifmt(j+1) = '+';
74 end
75 ifmt = [ifmt 'i'];
76 thisfmt = [thisfmt ifmt];
77 end
78
79 % Add to row.
80 rowfmt = [rowfmt mathstr thisfmt mathstr ' & '];
81 end
82
83 % After last column, remove column separator and put in newline.
84 rowfmt(end-1:end) = [];
85 rowfmt = [rowfmt '\\\\\n'];
86
87 % Use it.
88 A = M.';
89 if isreal(M)
90 S = sprintf(rowfmt,A);
91 else
92 S = sprintf(rowfmt,[real(A(:)) imag(A(:))].');
93 end
94
95 % Remove extraneous imaginary part for real entries.
96 if ~isreal(M)
97 zi = sprintf(ifmt,0);
98 S = strrep(S,zi,blanks(length(zi)));
99 end
100
101 % Remove NaNs.
102 S = strrep(S,'$NaN$','--');
103 S = strrep(S,'NaN','--');
104
105 % Convert 'e' exponents to LaTeX form. This is probably really slow, but
106 % what can you do without regular expressions?
107 S = strrep(S,'e','E');
108 ex = min(findstr(S,'E'));
109 while ~isempty(ex)
110 % Find first non-digit character. Where is ISDIGIT?
111 j = ex+2;
112 while ~isempty(str2num(S(j))) & ~strcmp(S(j),'i')
113 j = j+1;
114 end
115
116 % This strips off leading '+' and zeros.
117 num = sprintf('%i',str2num(S(ex+1:j-1)));
118
119 ee = ['\times 10^{' num '}'];
120 S = [S(1:ex-1) ee S(j:end)];
121
122 ex = ex + min(findstr(S(ex+1:end),'E'));
123 end
124
125 % For good form, remove that last '\\'.
126 S(end-2:end-1) = ' ';
127
128 % Display or output?
129 if nargout==0
130 disp(S)
131 else
132 s = S;
133 end