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