-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix2latex.m
41 lines (38 loc) · 1.19 KB
/
matrix2latex.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function latex = matrix2latex( A )
%MATRIX2LATEX converts a matrix into LaTeX code.
%
% Arguments:
% A is the matrix to be converted.
%
% Copyright 2021, C. Minz. BSD 3-Clause License.
% fractionformat = "{}^{%d}/_{%d}";
fractionformat = "\\frac{%d}{%d}";
latex = "\begin{pmatrix}" + newline;
n = size( A );
for i = 1 : n( 1 )
latex = latex + char( 9 );
latex = latex + sprintf( '%0.3f', A( i, 1 ) );
for j = 2 : n( 2 )
latex = latex + sprintf( ' & %0.3f', A( i, j ) );
% if A( i, j ) == 0
% latex = latex + "\cdot";
% else
% [ nom, denom ] = rat( A( i, j ) );
% if denom == 1
% latex = latex + nom;
% else
% latex = latex + sprintf( fractionformat, nom, denom );
% end
% end
% if j < n( 2 )
% latex = latex + " & ";
% end
end
if i < n( 1 )
latex = latex + " \\" + newline;
else
latex = latex + newline;
end
end
latex = latex + "\end{pmatrix}";
end