wolffd@0: function s = latex(M,varargin) wolffd@0: %LATEX Print a matrix in LaTeX tabular format. wolffd@0: % LATEX(M) prints out the numeric matrix M in a LaTeX tabular wolffd@0: % format. The '&' character appears between entries in a row, '\\' wolffd@0: % is appended to the ends of rows, and each entry is set in math wolffd@0: % mode. Complex numbers are understood, and exponentials will be wolffd@0: % converted to a suitable format. wolffd@0: % wolffd@0: % LATEX(M,'nomath') does not include the $$ needed to put each wolffd@0: % entry in math mode (e.g., for use with the amsmath matrix modes). wolffd@0: % wolffd@0: % LATEX(M,FMT) uses a format specifier FMT of the SPRINTF type for wolffd@0: % each entry. wolffd@0: % wolffd@0: % LATEX(M,FMT1,FMT2,...) works through the given format specifiers wolffd@0: % on each row of M. If fewer are given than the column size of M, wolffd@0: % the last is used repeatedly for the rest of the row. wolffd@0: % wolffd@0: % S = LATEX(M,...) does not display output but returns a character wolffd@0: % array S. wolffd@0: % wolffd@0: % Examples: wolffd@0: % latex( magic(4) ) wolffd@0: % latex( magic(4), '%i', 'nomath' ) wolffd@0: % latex( magic(4), '%i', '%.2f' ) wolffd@0: % wolffd@0: % See also SPRINTF, SYM/LATEX. wolffd@0: wolffd@0: % Copyright 2002 by Toby Driscoll. Last updated 12/06/02. wolffd@0: wolffd@0: if ~isa(M,'double') wolffd@0: error('Works only for arrays of numbers.') wolffd@0: elseif ndims(M) > 2 wolffd@0: error('Works only for 2D arrays.') wolffd@0: end wolffd@0: wolffd@0: if nargin < 2 wolffd@0: fmt = {'%#.5g'}; wolffd@0: mathstr = '$'; wolffd@0: else wolffd@0: fmt = varargin; wolffd@0: idx = strmatch('nomath',fmt); wolffd@0: if isempty(idx) wolffd@0: mathstr = '$'; wolffd@0: else wolffd@0: mathstr = ''; wolffd@0: fmt = fmt([1:idx-1 idx+1:end]); wolffd@0: if isempty(fmt), fmt = {'%#.5g'}; end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % Extend the format specifiers. wolffd@0: [m,n] = size(M); wolffd@0: if n > length(fmt) wolffd@0: [fmt{end:n}] = deal(fmt{end}); wolffd@0: end wolffd@0: wolffd@0: % Create one format for a row. wolffd@0: rowfmt = ''; wolffd@0: for p = 1:n wolffd@0: % Remove blanks. wolffd@0: thisfmt = deblank(fmt{p}); wolffd@0: wolffd@0: % Add on imaginary part if needed. wolffd@0: if ~isreal(M(:,p)) wolffd@0: % Use the same format as for the real part, but force a + sign for wolffd@0: % positive numbers. wolffd@0: ifmt = thisfmt; wolffd@0: j = findstr(ifmt,'%'); wolffd@0: if ~any(strcmp(ifmt(j+1),['-';'+';' ';'#'])) wolffd@0: ifmt = [ifmt(1:j) '+' ifmt(j+1:end)]; wolffd@0: else wolffd@0: ifmt(j+1) = '+'; wolffd@0: end wolffd@0: ifmt = [ifmt 'i']; wolffd@0: thisfmt = [thisfmt ifmt]; wolffd@0: end wolffd@0: wolffd@0: % Add to row. wolffd@0: rowfmt = [rowfmt mathstr thisfmt mathstr ' & ']; wolffd@0: end wolffd@0: wolffd@0: % After last column, remove column separator and put in newline. wolffd@0: rowfmt(end-1:end) = []; wolffd@0: rowfmt = [rowfmt '\\\\\n']; wolffd@0: wolffd@0: % Use it. wolffd@0: A = M.'; wolffd@0: if isreal(M) wolffd@0: S = sprintf(rowfmt,A); wolffd@0: else wolffd@0: S = sprintf(rowfmt,[real(A(:)) imag(A(:))].'); wolffd@0: end wolffd@0: wolffd@0: % Remove extraneous imaginary part for real entries. wolffd@0: if ~isreal(M) wolffd@0: zi = sprintf(ifmt,0); wolffd@0: S = strrep(S,zi,blanks(length(zi))); wolffd@0: end wolffd@0: wolffd@0: % Remove NaNs. wolffd@0: S = strrep(S,'$NaN$','--'); wolffd@0: S = strrep(S,'NaN','--'); wolffd@0: wolffd@0: % Convert 'e' exponents to LaTeX form. This is probably really slow, but wolffd@0: % what can you do without regular expressions? wolffd@0: S = strrep(S,'e','E'); wolffd@0: ex = min(findstr(S,'E')); wolffd@0: while ~isempty(ex) wolffd@0: % Find first non-digit character. Where is ISDIGIT? wolffd@0: j = ex+2; wolffd@0: while ~isempty(str2num(S(j))) & ~strcmp(S(j),'i') wolffd@0: j = j+1; wolffd@0: end wolffd@0: wolffd@0: % This strips off leading '+' and zeros. wolffd@0: num = sprintf('%i',str2num(S(ex+1:j-1))); wolffd@0: wolffd@0: ee = ['\times 10^{' num '}']; wolffd@0: S = [S(1:ex-1) ee S(j:end)]; wolffd@0: wolffd@0: ex = ex + min(findstr(S(ex+1:end),'E')); wolffd@0: end wolffd@0: wolffd@0: % For good form, remove that last '\\'. wolffd@0: S(end-2:end-1) = ' '; wolffd@0: wolffd@0: % Display or output? wolffd@0: if nargout==0 wolffd@0: disp(S) wolffd@0: else wolffd@0: s = S; wolffd@0: end