-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix2tikz.m
114 lines (111 loc) · 4.76 KB
/
matrix2tikz.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function tikz = matrix2tikz( A, options )
%MATRIX2TIKZ converts a matrix into TikZ code.
%
% Arguments:
% A is the matrix to be converted.
%
% Optional arguments:
% OPTIONS can be "hidegrid", "nogridlabels", "nocelllabels", "nozeros" or
% combinations of those separated by spaces.
%
% Copyright 2021, C. Minz. BSD 3-Clause License.
% default options or input:
showgrid = true;
showgridlabels = true;
showcelllabels = true;
showzeros = true;
if nargin > 1
showgrid = ~contains( options, "hidegrid" );
showgridlabels = ~contains( options, "nogridlabels" );
showcelllabels = ~contains( options, "nocelllabels" );
showzeros = ~contains( options, "nozeros" );
end
n = size( A );
tab = char( 9 );
% begin graphic
tikz = "\begin{tikzpicture}" + newline;
tikz = tikz + tab + "% ### This matrix graphic is generated by matrix2tikz.m" + newline;
tikz = tikz + tab + "% Parameters: " + newline;
tikz = tikz + tab + "\def\gridL{0.35cm}" + newline;
tikz = tikz + tab + "\colorlet{ScaleBot}{YorkBlue}" + newline;
tikz = tikz + tab + "\colorlet{ScaleZer}{white}" + newline;
tikz = tikz + tab + "\colorlet{ScaleTop}{YorkBlue}" + newline;
tikz = tikz + tab + "\colorlet{negInfinity}{YorkGrey}" + newline;
tikz = tikz + tab + "\colorlet{posInfinity}{YorkGrey}" + newline;
tikz = tikz + tab + "\colorlet{ErrorColor}{YorkRed}" + newline;
tikz = tikz + tab + "\colorlet{GridColor}{YorkLightGrey}" + newline;
tikz = tikz + tab + "\colorlet{GridLabelColor}{YorkBlackTint}" + newline;
tikz = tikz + tab + "\colorlet{CellLabelColor}{black}" + newline;
tikz = tikz + tab + newline;
% colour cells
tikz = tikz + tab + "% Matrix cells: " + newline;
Ascaled = A;
Ascaled( A == Inf ) = NaN;
Ascaled( A == -Inf ) = NaN;
AscaledPos = Ascaled;
AscaledNeg = -Ascaled;
AscaledPos( A < 0 ) = 0;
AscaledNeg( A >= 0 ) = 0;
AscaledPos = 100 * AscaledPos ./ max( max( AscaledPos ) );
AscaledNeg = 100 * AscaledNeg ./ max( max( AscaledNeg ) );
Ascaled = AscaledPos + AscaledNeg;
for i = 1 : n( 1 )
for j = 1 : n( 2 )
if isnan(A( i, j ))
cellcolor = "ErrorColor";
labeltext = "";
elseif A( i, j ) == Inf
cellcolor = "posInfinity";
labeltext = "$\infty$";
elseif A( i, j ) == -Inf
cellcolor = "negInfinity";
labeltext = "$-\infty$";
elseif A( i, j ) == 0
cellcolor = "ScaleZer";
if showzeros
labeltext = "$\cdot$";
else
labeltext = "";
end
elseif A( i, j ) < 0
cellcolor = sprintf( "ScaleBot!%0.3f!ScaleZer", Ascaled( i, j ) );
labeltext = sprintf( "%0.1f", A( i, j ) );
else
cellcolor = sprintf( "ScaleTop!%0.3f!ScaleZer", Ascaled( i, j ) );
labeltext = sprintf( "%0.1f", A( i, j ) );
end
tikz = tikz + tab + ...
sprintf( "\\draw[%s, fill=%s] ", cellcolor, cellcolor ) + ...
sprintf( "( %d*\\gridL, -%d*\\gridL ) ", j - 1, i - 1 ) + ...
"rectangle " + ...
sprintf( "( %d*\\gridL, -%d*\\gridL );", j, i ) + newline;
if showcelllabels && labeltext ~= ""
tikz = tikz + tab + "\draw[CellLabelColor] node at " + ...
sprintf( "( %d.5*\\gridL, -%d.5*\\gridL ) {\\tiny %s};", j - 1, i - 1, labeltext ) + ...
newline;
end
end
end
if showgrid
tikz = tikz + tab + newline + tab + "% Grid: " + newline;
% draw grid
tikz = tikz + tab + "\draw[step = \gridL, GridColor] " + ...
sprintf( "( 0, 0 ) grid ( %d*\\gridL, -%d*\\gridL );", n( 2 ), n( 1 ) ) + newline;
tikz = tikz + tab + "\draw[GridColor] ( 0, 0 ) -- " + ...
sprintf( "( %d*\\gridL, -%d*\\gridL );", n( 2 ), n( 1 ) ) + newline;
if showgridlabels
% draw row labels
for i = 1 : n( 1 )
tikz = tikz + tab + "\draw[GridLabelColor] node[left] at " + ...
sprintf( "( 0, -%d.5*\\gridL ) {\\tiny %d};", i - 1, i ) + newline;
end
% draw column labels
for j = 1 : n( 2 )
tikz = tikz + tab + "\draw[GridLabelColor] node[below left, rotate=45] at " + ...
sprintf( "( %d.5*\\gridL, -%d*\\gridL ) {\\tiny %d};", j - 1, n( 1 ), j ) + newline;
end
end
end
% end graphic
tikz = tikz + "\end{tikzpicture}" + newline;
end