Skip to content

Commit fa005a7

Browse files
committed
Initial commit
0 parents  commit fa005a7

34 files changed

+2209
-0
lines changed

LICENSE

+674
Large diffs are not rendered by default.

README.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Create pdf figures using the TikZ/pgf LaTeX package
2+
3+
`tikzfig` is a toolbox to programmatically create figure files in pdf format using the [TikZ/pgf LaTeX package](https://ctan.org/pkg/pgf?lang=en). It exposes part of the TikZ interface to pgf as a Matlab API, so that graphics statements lead to the construction of a LaTeX file which is transparently processed into pdf, including a simple preview mechanism. Additionally, functionality to easily create multi-panel figure layouts is provided.
4+
5+
6+
## Example
7+
8+
```matlab
9+
% create a figure named "hello" (associated with a Matlab figure window)
10+
tfInit('hello')
11+
% define a color to be used later
12+
tfColor('kitty', 'rgb', [1 0 0])
13+
% define a style to be used later
14+
tfStyle('pretty', 'mark=+,draw=orange')
15+
% define a layout with two boxes side-by-side,
16+
% the first with a 4:3 aspect ratio and the second one square
17+
tfLayout({1 1 4/3; 1 2 1})
18+
19+
% select layout box #1 and define coordinate system
20+
tfView(1, [0 3], [0 3])
21+
tfIsoView
22+
% draw an arrow from (1, 2) to (2, 1)
23+
tfPath('green,thick,draw,->', [1 2 ; 2 1])
24+
% make a text label
25+
tfPath('', [0.1 0.1], ...
26+
'node [kitty,anchor=south west,draw,fill=yellow] {Hello Kitty! $E = mc^2$}')
27+
% close layout box and show standard decorations: viewbox and scales
28+
tfDeco
29+
30+
% select layout box #2 and define coordinate system
31+
tfView(2, [0.5 9.5], [-0.1 1.1])
32+
% plot some random data
33+
tfPlot('pretty', [1 : 9 ; rand(1, 9)]')
34+
% close layout box and use scales and a title as decorations
35+
tfDeco scales
36+
tfDeco title graph
37+
38+
% generate pdf and show a preview in the associated figure window
39+
tfRender
40+
```
41+
42+
43+
## Usage
44+
45+
The basic structure of a `tikzfig` script is
46+
- `tfInit`: initialize TikZ figure
47+
- style commands
48+
- `tfLayout`: organize the layout of a TikZ figure using boxes arranged over a grid
49+
- for each view:
50+
- `tfView`: prepare layout box as current view
51+
- optionally `tfIsoView`: impose isoscaling on the current view
52+
- plot commands
53+
- optionally `tfDeco`: draw decorations for the current view
54+
- `tfRender`: render TikZ figure to pdf file and show a preview
55+
56+
Style commands:
57+
- `tfColor`: define color for later use in drawing or filling
58+
- `tfStyle`: define TikZ style for later use in options
59+
60+
Plot commands:
61+
tfPath - insert path in current view
62+
tfPlot - plot data in current view
63+
tfScatter - generate scatter plot in current view
64+
tfHeatMap - display matrix as a heat map
65+
tfImage - insert image into the current view
66+
tfContour - display matrix as a contour plot
67+
tfEllipse - draw an circle or ellipse in the current view
68+
tfArrow - draw arrow
69+
tfColorBar - fill pre-defined layout box with colorbar
70+
tfGrid - draw a coordinate grid into the current view
71+
72+
Helper functions:
73+
tfPrintCode - print TikZ code
74+
tfLimits - compute fitting view limits for discretely sampled data (e.g. images)
75+

tfArrow.m

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function tfArrow(edge, xyA, xyB)
2+
3+
% draw arrow
4+
%
5+
% tfArrow(edge, xyA, xyB)
6+
%
7+
% edge: edge options
8+
% xyA: coordinates or node anchor of start point
9+
% xyB: coordinates or node anchor of end point
10+
%
11+
% Examples:
12+
% - double arrow between two node anchors, bent to the left
13+
% tfArrow('<->,bend left', 'd11.south east', 'd12.north east')
14+
% - arrow from a node anchor to numeric coordinates, shortened at the start
15+
% tfArrow('-latex,shorten <=1pt', 'comp.east', [8.4 1.5])
16+
%
17+
% Arrow tips:
18+
% Standard
19+
% latex / reversed long, pointed
20+
% stealth / reversed hollowed at the base
21+
% to / reversed curled outwards; also < / >
22+
% | line perpendicular to arrow
23+
% Triangular
24+
% latex' / reversed
25+
% stealth' / reversed
26+
% triangle 90 / reversed 90° filled triangle
27+
% triangle 60 / reversed 60° filled triangle
28+
% triangle 45 / reversed 45° filled triangle
29+
% open triangle 90 / reversed 90° opened triangle
30+
% open triangle 60 / reversed 60° opened triangle
31+
% open triangle 45 / reversed 45° opened triangle
32+
% Barbed
33+
% angle 90 / reversed 90° straight lines
34+
% angle 60 / reversed 60° straight lines
35+
% angle 45 / reversed 45° straight lines
36+
% hooks / reversed hooks
37+
% Bracket-Like
38+
% [ / ]
39+
% ( / )
40+
% Circle and Diamond
41+
% o open circle
42+
% * filled circle
43+
% diamond filled diamond
44+
% open diamond open diamond
45+
% Partial
46+
% left to / reversed
47+
% right to / reversed
48+
% left hook / reversed
49+
% right hook / reversed
50+
51+
52+
if ~ischar(xyA)
53+
xyA = tf_transform_data2rel(xyA);
54+
xyA = sprintf('%.6f,%.6f', xyA);
55+
end
56+
57+
if ~ischar(xyB)
58+
xyB = tf_transform_data2rel(xyB);
59+
xyB = sprintf('%.6f,%.6f', xyB);
60+
end
61+
62+
tf_append(sprintf('\\path (%s) edge[%s] (%s);', xyA, edge, xyB))

tfColor.m

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function tfColor(name, colorspace, components)
2+
3+
% define color for later use in drawing or filling
4+
%
5+
% tfColor(name, colorspace, components)
6+
%
7+
% implemented using the \definecolor command of the xcolor package
8+
%
9+
% Examples:
10+
% tfColor('yellow', 'HTML', 'FFFF00')
11+
% tfColor('yellow', 'rgb', [1 1 0])
12+
% tfColor('yellow', 'RGB', [255 255 0])
13+
% tfColor('yellow', 'cmyk', [0 0 1 0])
14+
% tfColor('yellow', 'Hsb', [60 1 1])
15+
%
16+
% Instead of defining colors explicitly like this, TikZ also supports the
17+
% xcolor syntax for mixing colors, e.g. 'blue!50!yellow'.
18+
%
19+
% The following colors are predefined by xcolor:
20+
% red, green, blue
21+
% cyan, magenta, yellow
22+
% black, darkgray, gray, lightgray, white
23+
% brown, lime, olive, orange, pink, purple, teal, violet
24+
%
25+
% tfColor without arguments defines additional colors that complete the
26+
% RGB color circle:
27+
% red
28+
% Mandarin
29+
% yellow
30+
% Lime
31+
% green
32+
% Cyanotic
33+
% cyan
34+
% Nautical
35+
% blue
36+
% Purple
37+
% magenta
38+
% Crimson
39+
% red
40+
41+
if nargin == 0
42+
tfColor('Mandarin', 'HTML', 'FFA600')
43+
tfColor('Lime', 'HTML', 'A6FF00')
44+
tfColor('Cyanotic', 'HTML', '00FFA6')
45+
tfColor('Nautical', 'HTML', '00A6FF')
46+
tfColor('Purple', 'HTML', 'A600FF')
47+
tfColor('Crimson', 'HTML', 'FF00A6')
48+
return
49+
end
50+
51+
if ischar(components)
52+
col = components;
53+
elseif all(round(components) == components)
54+
col = sprintf('%d,', components);
55+
col = col(1 : end - 1);
56+
else
57+
col = sprintf('%.4f,', components);
58+
col = col(1 : end - 1);
59+
end
60+
61+
tf_append(sprintf('\\definecolor{%s}{%s}{%s}', name, colorspace, col))

tfColorBar.m

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function tfColorBar(bi, clim)
2+
3+
% fill pre-defined layout box with colorbar
4+
%
5+
% tfColorBar(bi, clim)
6+
%
7+
% bi: index of predefined layout box
8+
% clim: range of values mapped to the colormap
9+
10+
% get view box dimensions
11+
olim = tf_get('olim', 'No boxes defined!');
12+
olim = olim(bi, :);
13+
14+
% determine values that are mapped
15+
vals = linspace(clim(1), clim(2), size(colormap, 1));
16+
vlim = tfLimits(vals);
17+
dlim = tfLimits([1 1]);
18+
19+
if olim(2) - olim(1) > olim(4) - olim(3)
20+
% horizontal
21+
tfView(bi, vlim, dlim)
22+
tfHeatMap('', vals', clim, vals, [1 1])
23+
else
24+
% vertical
25+
tfView(bi, dlim, vlim)
26+
tfHeatMap('', vals, clim, [1 1], vals)
27+
end

tfContour.m

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function tfContour(options, A, levels, xval, yval)
2+
3+
% display matrix as a contour plot
4+
%
5+
% tfContour(options, A, levels, xval, yval)
6+
%
7+
% A is visualized as a set of contour lines at the given levels.
8+
% The two two-element vectors xval and yval specify the coordinates
9+
% corresponding to A(1, 1) and A(end, end), respectively.
10+
%
11+
% options are passed on as-is to tfPath.
12+
13+
[m, n] = size(A);
14+
15+
if (nargin < 3) || isempty(levels)
16+
levels = quantile(A(:), [0.25 0.5 0.75]);
17+
end
18+
if (nargin < 4) || isempty(xval)
19+
xval = [1 m];
20+
end
21+
if (nargin < 5) || isempty(yval)
22+
yval = [1 n];
23+
end
24+
25+
% determine coordinate grid
26+
xs = linspace(xval(1), xval(end), m);
27+
ys = linspace(yval(1), yval(end), n);
28+
29+
% determine contour lines
30+
if numel(levels) == 1
31+
% accomodate quirk of contourc interface
32+
levels = [levels, levels];
33+
end
34+
C = contourc(xs, ys, A .', levels);
35+
36+
% extract contour lines from contourc output and plot them
37+
j = 1;
38+
while j < size(C, 2)
39+
nc = C(2, j);
40+
tfPath(options, C(:, j + (1 :2: nc))')
41+
j = j + nc + 1;
42+
end

0 commit comments

Comments
 (0)