"Nezbytným krokem k tomu, abyste od života získali věci, po kterých toužíte, je rozhodnout se, co vlastně chcete."
Praktický matlabí skriptík latex exportuje zadanou matici přímo do konzole a umožňuje tak rychle zkopírovat matici z Matlabu do LaTeXu. Je to mnohem rychlejší, než používat public či skripty jako latex2matlab, kde se musí vyplňovat název souboru, pak jej otevřít... Tady prostě zavoláte funkci, Ctrl+C a Ctrl+V a máte to v latexu.
Samozřejmě se jedná o m-file, takže si jej můžete poupravit k obrazu svému. Já třeba nepoužívám matice v matematickém režimu, takže jsem dal na řádce 39 prázdný mathstr, abych nemusel funkci volat s parametrem.
Příklad použití:
A = [1 2; 5 6]; latex(A)
Získáme v konzoli přímo:
1.0000 & 2.0000 \\ 5.0000 & 6.0000
Reference latex - File Exchange - MATLAB Central.
Pozor - Matlab sám obsahuje od roku 2005 funkci "latex", která ale umí zobrazit jen symbolický zápis. Umí jej ale i včetně zlomků.
function s = latex(M,varargin)
%LATEX Print a matrix in LaTeX tabular format.
% LATEX(M) prints out the numeric matrix M in a LaTeX tabular
% format. The '&' character appears between entries in a row, '\\'
% is appended to the ends of rows, and each entry is set in math
% mode. Complex numbers are understood, and exponentials will be
% converted to a suitable format.
%
% LATEX(M,'nomath') does not include the $$ needed to put each
% entry in math mode (e.g., for use with the amsmath matrix modes).
%
% LATEX(M,FMT) uses a format specifier FMT of the SPRINTF type for
% each entry.
%
% LATEX(M,FMT1,FMT2,...) works through the given format specifiers
% on each row of M. If fewer are given than the column size of M,
% the last is used repeatedly for the rest of the row.
%
% S = LATEX(M,...) does not display output but returns a character
% array S.
%
% Examples:
% latex( magic(4) )
% latex( magic(4), '%i', 'nomath' )
% latex( magic(4), '%i', '%.2f' )
%
% See also SPRINTF, SYM/LATEX.
% Copyright 2002 by Toby Driscoll. Last updated 12/06/02.
% Changes: JANKO, 2012/10/24
if ~isa(M,'double')
error('Works only for arrays of numbers.')
elseif ndims(M) > 2
error('Works only for 2D arrays.')
end
% start JANKO
if nargin < 2
fmt = {'%g'}; % changed from %#.5g
mathstr = ''; % changed from $
else
fmt = varargin;
idx = strmatch('nomath',fmt);
if isempty(idx)
mathstr = ''; % changed from $
else
mathstr = '';
fmt = fmt([1:idx-1 idx+1:end]);
if isempty(fmt), fmt = {'%#.5g'}; end
end
end
% Extend the format specifiers.
[m,n] = size(M);
if n > length(fmt)
[fmt{end:n}] = deal(fmt{end});
end
% Create one format for a row.
rowfmt = '';
for p = 1:n
% Remove blanks.
thisfmt = deblank(fmt{p});
% Add on imaginary part if needed.
if ~isreal(M(:,p))
% Use the same format as for the real part, but force a + sign for
% positive numbers.
ifmt = thisfmt;
j = findstr(ifmt,'%');
if ~any(strcmp(ifmt(j+1),['-';'+';' ';'#']))
ifmt = [ifmt(1:j) '+' ifmt(j+1:end)];
else
ifmt(j+1) = '+';
end
ifmt = [ifmt 'i'];
thisfmt = [thisfmt ifmt];
end
% Add to row.
rowfmt = [rowfmt mathstr thisfmt mathstr ' & '];
end
% After last column, remove column separator and put in newline.
rowfmt(end-1:end) = [];
rowfmt = [rowfmt '\\\\\n'];
% Use it.
A = M.';
if isreal(M)
S = sprintf(rowfmt,A);
else
S = sprintf(rowfmt,[real(A(:)) imag(A(:))].');
end
% Remove extraneous imaginary part for real entries.
if ~isreal(M)
zi = sprintf(ifmt,0);
S = strrep(S,zi,blanks(length(zi)));
end
% Remove NaNs.
S = strrep(S,'$NaN$','--');
S = strrep(S,'NaN','--');
% Convert 'e' exponents to LaTeX form. This is probably really slow, but
% what can you do without regular expressions?
S = strrep(S,'e','E');
ex = min(findstr(S,'E'));
while ~isempty(ex)
% Find first non-digit character. Where is ISDIGIT?
j = ex+2;
while ~isempty(str2num(S(j))) & ~strcmp(S(j),'i')
j = j+1;
end
% This strips off leading '+' and zeros.
num = sprintf('%i',str2num(S(ex+1:j-1)));
ee = ['\times 10^{' num '}'];
S = [S(1:ex-1) ee S(j:end)];
ex = ex + min(findstr(S(ex+1:end),'E'));
end
% For good form, remove that last '\\'.
S(end-2:end-1) = ' ';
% start JANKO, inserted code
cc = '';
for i=1:size(M,2)
cc = [cc 'c'];
end
S = [sprintf('\\left[\n\\begin{array}{') cc sprintf('}\n') S sprintf('\\end{array}\n\\right]')];
% end JANKO
% Display or output?
if nargout==0
disp(S)
else
s = S;
end
editor = matlab.desktop.editor.getActive;
% vezmeme vybrany text
selected = editor.SelectedText;
% spustime vybrany kus kodu
data = latex(eval(selected));
clipboard('copy', data); .