-
Notifications
You must be signed in to change notification settings - Fork 0
/
includetikz.sty
266 lines (230 loc) · 7.38 KB
/
includetikz.sty
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Include tikz file.
%
% Author(s): Guillaume Dolle <gdolle at unistra.fr>
% (c) GNU GPLv2 2015
%
% Package required: package calc
% Options: scale, scalefnt
% Package options: external
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}[2014/05/01]
\ProvidesPackage{includetikz}[2015/05/01 Smart tikz include package]
\RequirePackage{ifthen}
\RequirePackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{external}
\RequirePackage{scalefnt}
\RequirePackage{xkeyval}
\RequirePackage{ifluatex}
\RequirePackage{xstring}
\RequirePackage{etoolbox}
\RequirePackage{morewrites}
\RequirePackage{currfile-abspath}
\RequirePackage{newfile}
\tikzexternaldisable
\newtoggle{itOrigin} \togglefalse{itOrigin}
\newtoggle{itDisable} \togglefalse{itDisable}
\newtoggle{itRebuild} \togglefalse{itRebuild}
%===============================================================================
% Enable external tikz (pdf) with checksum.
\DeclareOption{external}
{
\tikzexternalize
\tikzexternaldisable
}
%===============================================================================
% Optimize
\DeclareOption{itOptimize}
{
\tikzset{external/optimize=true}
}
%===============================================================================
\DeclareOption{origin}
{
\toggletrue{itOrigin}
}
%===============================================================================
\DeclareOption{disable}
{
\toggletrue{itDisable}
}
%===============================================================================
\DeclareOption{force}
{
\toggletrue{itRebuild}
}
\makeatletter
% Parameters
\define@key{includetikz}{scale}{\tikzset{every picture/.style={scale=#1}}}
\define@key{includetikz}{width}{\tikzset{every picture/.style={width=#1}}}
\define@key{includetikz}{height}{\tikzset{every picture/.style={height=#1}}}
\define@key{includetikz}{fill opacity}{\tikzset{every picture/.style={fill opacity=#1}}}
\define@key{includetikz}{font style}{\tikzset{every picture/.style={font=#1}}}
\define@key{includetikz}{font scale}{\scalefont{#1}}
\define@key{includetikz}{font keepratio}[1]{\tikzset{every node/.style={transform shape}}}
\define@key{includetikz}{drawscale}{
\pgfmathsetmacro{\fs}{2 - #1}
\scalefont{\fs}
\tikzset{every picture/.style={scale=#1}}
}
%-------------------------------------------------------------------------------
% NOTE: All it@<func> are made to be used inside \includetikz!
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
\def\it@figCounterName
{
it\filename@base
}
%-------------------------------------------------------------------------------
\def\it@pdfFileName{%
\filename@base-n\arabic{\it@figCounterName}%
}
%-------------------------------------------------------------------------------
\def\it@optChecksumFileName
{%
% \def\pdfoptsmdfivefilename{
\it@pdfFileName-opts.md5%
}
%-------------------------------------------------------------------------------
\def\it@stepFigCounter
{
% Create a counter for renaming.
% Note that tikz external might provide this value...
\ifcsname c@\it@figCounterName \endcsname
% Counter defined.
\stepcounter{\it@figCounterName}
\else
% counter not defined.
\newcounter{\it@figCounterName}
\fi
}
%-------------------------------------------------------------------------------
\def\it@defaultFigure
{
% Add default image
\begin{tikzpicture}
\draw[fill=black] (-3.5,2.5) rectangle (3.5,-2);
\node[color=white] at (0,0.5){\large FIGURE DISABLED !};
\node[color=white] at (0,0){\tiny\textit{(remove
\textbackslash{}includetikzdisable} to
reactivate this figure)};
\end{tikzpicture}
}
%-------------------------------------------------------------------------------
% Pdf path
\def\it@pdfPath%
{%
\iftoggle{itOrigin}{\filename@area/autogen/}{./autogen/}%
}
%-------------------------------------------------------------------------------
\def\it@optChecksumPathToFileName
{%
\it@pdfPath/\it@optChecksumFileName%
}
%-------------------------------------------------------------------------------
\def\it@optChecksumSave
{
\newoutputstream{\it@pdfFileName}
\openoutputfile{\it@optChecksumPathToFileName}{\it@pdfFileName}
\addtostream{\it@pdfFileName}{\@backslashchar def \@backslashchar optLastChecksum{\it@optCurrChecksum}\@percentchar}
\closeoutputstream{\it@pdfFileName}
}
%-------------------------------------------------------------------------------
\def\it@rebuild
{
\message{current path:\it@optChecksumPathToFileName ^^J}
\input{\it@optChecksumPathToFileName} % load md5 opts
%************ DEBUG ******************************
\message{old:\optLastChecksum ^^J}
\message{new:\it@optCurrChecksum ^^J}
%*************************************************
\ifnum\pdf@strcmp{\optLastChecksum}{\it@optCurrChecksum}=0
% Same MD5.
% \message{SAME MD5}
\else
% Different MD5, force rebuild tikz.
% \message{NOT SAME MD5}
\toggletrue{itRebuild}
\it@optChecksumSave
\fi
\iftoggle{itRebuild}
{
\tikzset{external/force remake=true}
\togglefalse{itRebuild}
}
{}
}
%-------------------------------------------------------------------------------
\def\it@debugInfo
{
\immediate\message{--------- BEGIN DEBUG --------^^J}
\immediate\message{Pdf opt name \it@pdfFileName ^^J}
\immediate\message{Path to opt MD5 checksum: \it@optChecksumPathToFileName^^J}
\immediate\message{--------- END DEBUG --------^^J}
}
%-------------------------------------------------------------------------------
\newcommand\includetikz[2][]
{
% Require -recorder option.
\getabspath{\jobname.aux}
%-------------------------------------------------------------------------------
\gdef\it@optCurrChecksum
{%
\pdf@mdfivesum{#1}%
}
\iftoggle{itDisable}
{
\it@defaultFigure
}
{
\bgroup
\filename@parse{#2}
\it@stepFigCounter
\immediate\write18{mkdir -p \it@pdfPath}
% If output-directory is different, this is required to avoid writing
% error...
\immediate\write18{mkdir -p \theabsdir\it@pdfPath}
% Set tikz external path and filename.
\tikzsetexternalprefix{\it@pdfPath}
\tikzsetnextfilename{\it@pdfFileName}
% Set figure options.
\setkeys{includetikz}{#1}
% Check md5file and force rebuild.
\IfFileExists{\it@optChecksumPathToFileName}
{% MD5 file exist.
\message{MD5 checksum differs : rebuild picture \it@pdfFileName ^^J}
\it@rebuild{toto}
}
{
% MD5 file does not exist.
\message{Create file \it@optChecksumPathToFileName checksum : \it@optCurrChecksum ^^J}
\it@optChecksumSave
}
\tikzexternalenable
% Set path and auto-generated figure name.
\input{#2}
\tikzexternaldisable
\tikzset{external/force remake=false}
\egroup
}
}
%-------------------------------------------------------------------------------
\newcommand\includetikzdisable
{
\toggletrue{itDisable}
}
%-------------------------------------------------------------------------------
\newcommand\includetikzorigin
{
\toggletrue{itOrigin}
}
%-------------------------------------------------------------------------------
\newcommand\includetikzforcerebuild
{
\toggletrue{itRebuild}
}
\makeatother
\ProcessOptions\relax
% vim: set ft=tex tw=80 sw=2: