Debian Codegraph @

texmf-dist/tex/latex/tikzscale/tikzscale.sty

raw
1 %%
2 %% This is file `tikzscale.sty',
3 %% generated with the docstrip utility.
4 %%
5 %% The original source files were:
6 %%
7 %% tikzscale.dtx (with options: `package')
8 %% ----------------------------------------------------------------
9 %% tikzscale --- Absolute resizing of TikZ pictures and PGF plots without scaling text
10 %% E-mail: [email protected]
11 %% Released under the LaTeX Project Public License v1.3c or later
12 %% See http://www.latex-project.org/lppl.txt
13 %% ----------------------------------------------------------------
14 %%
15 \NeedsTeXFormat{LaTeX2e}
16 \ProvidesPackage{tikzscale}[2013/05/22 v0.2.6 tikzscale LaTeX package]
17 \RequirePackage{graphicx}
18 \RequirePackage{etoolbox}
19 \RequirePackage{pgfkeys}
20 \RequirePackage{xparse}
21 \RequirePackage{letltxmacro}
22 \RequirePackage{xstring}
23
24 \def\pgfmathsetglobalmacro#1#2{%
25 \pgfmathparse{#2}%
26 \global\let#1\pgfmathresult%
27 }
28 \def\ifTikzLibraryLoaded#1#2#3{%
29 \ifcsdef{tikz@library@#1@loaded}{%
30 #2%
31 }{%
32 #3%
33 }%
34 }
35 \def\ifExternalizationLoaded#1#2{%
36 \ifTikzLibraryLoaded{external}{#1}{#2}%
37 }
38 \def\ifedefequal#1#2{%
39 \edef\etoolbox@ifedefequal@first{#1}%
40 \edef\etoolbox@ifedefequal@second{#2}%
41 \ifdefequal{\etoolbox@ifedefequal@first}{\etoolbox@ifedefequal@second}%
42 }
43 \def\edocsvlist#1{%
44 \edef\tikzscale@edocsvlist{#1}%
45 \expandafter\docsvlist\expandafter{\tikzscale@edocsvlist}%
46 }
47 \def\eforcsvlist#1#2{%
48 \edef\tikzscale@eforcsvlist{#2}%
49 \expandafter\forcsvlist\expandafter{\expandafter#1\expandafter}\expandafter{\tikzscale@eforcsvlist}%
50 }
51 \def\forgrouplist#1#2{%
52 \def\grouplistbreak{\def\breakFor{}}%
53 \tikzscale@forGroupListElement{#1}#2\tikzscale@endList%
54 \undef\breakFor
55 }
56 \NewDocumentCommand{\tikzscale@forGroupListElement}{mgu{\tikzscale@endList}}{%
57 \ifundef{\breakFor}{%
58 \IfValueTF{#2}{%
59 #1{#2}%
60 \tikzscale@forGroupListElement{#1}#3\tikzscale@endList%
61 }{%
62 #1{#3}%
63 }%
64 }{}%
65 }
66 \def\eforgrouplist#1#2{%
67 \edef\tikzscale@grouplist{#2}%
68 \expandafter\forgrouplist\expandafter{\expandafter#1\expandafter}\expandafter{\tikzscale@grouplist}%
69 }
70 \def\tikzscale@trim#1{%
71 \ignorespaces#1\unskip
72 }%
73 \def\tikzscale@trimMacro#1{%
74 \expandafter\IfBeginWith\expandafter{#1}{ }{%
75 \expandafter\StrGobbleLeft\expandafter{#1}{1}[#1]%
76 }{}%
77 \expandafter\IfEndWith\expandafter{#1}{ }{%
78 \expandafter\StrGobbleRight\expandafter{#1}{1}[#1]%
79 }{}%
80 }
81 \NewDocumentCommand{\elseif}{mm}{%
82 \ifboolexpr{#1}{%
83 #2%
84 \elseif@absorb
85 }{%
86 \elseif@optional
87 }%
88 }
89 \NewDocumentCommand{\elseif@optional}{gg}{%
90 \IfValueTF{#1}{%
91 \IfValueTF{#2}{%
92 \ifboolexpr{#1}{%
93 #2%
94 \elseif@absorb
95 }{%
96 \elseif@optional
97 }%
98 }{%
99 #1%
100 }%
101 }{}%
102 }
103 \NewDocumentCommand{\elseif@absorb}{g}{%
104 \IfValueTF{#1}{%
105 \elseif@absorb
106 }{}%
107 }
108 \ExplSyntaxOn
109 \NewDocumentCommand{\IfNoValueOrSplitEmptyTF}{mmm}{
110 \ifboolexpr{test {\IfNoValueTF{#1}} or test {\tl_if_eq:nnTF{#1}{{}}}}{
111 #2
112 }{
113 #3
114 }
115 }
116 \ExplSyntaxOff
117 \def\tikzscale@ifExternalizationActive#1#2{%
118 \ifExternalizationLoaded{%
119 \ifdefequal{\tikz}{\tikzexternal@tikz@replacement}{%
120 #1%
121 }{%
122 \ifdefequal{\tikz}{\tikzexternal@origtikz}{%
123 }{%
124 \PackageWarning{tikzscale}{Status of externalization is unknown, thus I assume it is deactivated.}%
125 }%
126 #2%
127 }%
128 }{%
129 #2%
130 }%
131 }%
132 \def\tikzscale@debug#1{}%
133 \AtEndPreamble{%
134 \def\tikzscale@tikzFileExtensions{.tikz,.TIKZ,.TikZ,.pgf,.PGF}%
135 \DeclareGraphicsExtensions{\tikzscale@tikzFileExtensions,\Gin@extensions}%
136 \LetLtxMacro{\tikzscale@oldincludegraphics}{\includegraphics}%
137 \tikzscale@useEnhancedIncludegraphics
138 \tikzscale@patchTikzpictureIncludegraphics
139 \tikzscale@patchEndtikzpictureIncludegraphics
140 }
141 \AtEndPreamble{%
142 \ifExternalizationLoaded{%
143 \pgfkeys{/pgf/images/external info}%
144 }{}%
145 \@ifpackageloaded{tikz}{%
146 \newlength{\tikzscale@accuracy}%
147 \setlength{\tikzscale@accuracy}{\hfuzz}%
148 \def\maxTestIterations{10}%
149 }{}%
150 \ifExternalizationLoaded{%
151 \apptocmd{\tikzexternaldisable}{%
152 \tikzscale@useEnhancedIncludegraphics
153 \tikzscale@patchTikzpictureIncludegraphics
154 }{}{\PackageError{tikzscale}{Patching tikzexternaldisable failed}}%
155 \apptocmd{\tikzexternalenable}{%
156 \tikzscale@useEnhancedIncludegraphics
157 \tikzscale@patchTikzpictureIncludegraphics
158 }{}{\PackageError{tikzscale}{Patching tikzexternalenable failed}}%
159 \LetLtxMacro{\tikzscale@externalend@storeshifts}{\pgf@externalend@storeshifts}%
160 \def\pgf@externalend@storeshifts#1{%
161 \tikzscale@externalend@storeshifts{#1}%
162 \ifpgfexternal@info
163 \tikzscale@writeToDpth{#1}{\tikzscale@oldAxisRatio}{\requestedAxisRatio}%
164 \fi
165 }%
166 }{}%
167 }
168 \def\tikzscale@writeToDpth#1#2#3{%
169 \ifdef{#3}{%
170 \immediate\write#1{\noexpand\pgfexternal@restore{\noexpand\def\noexpand#2{#3}}}%
171 }{}%
172 }
173 \NewDocumentCommand{\tikzscale@includegraphics}{O{}m}{%
174 \begingroup
175 \LetLtxMacro{\includegraphics}{\tikzscale@oldincludegraphics}%
176 \tikzscale@FixEndLine
177 \tikzscale@findExactFileName{tikzscale@fileName}{#2}%
178 \tikzscale@isTikzFile{tikzscale@testTikzFile}{\tikzscale@fileName}%
179 \ifcsdef{tikzscale@testTikzFile}{%
180 \tikzscale@includetikz[#1]{\tikzscale@fileName}%
181 }{%
182 \tikzscale@restoreEndLineChar
183 \tikzscale@oldincludegraphics[#1]{\tikzscale@fileName}%
184 }%
185 \endgroup
186 }%
187 \def\tikzscale@useEnhancedIncludegraphics{%
188 \LetLtxMacro{\includegraphics}{\tikzscale@includegraphics}%
189 }
190 \def\tikzscale@FixEndLine{%
191 \edef\tikzscale@restoreEndLineChar{\endlinechar=\the\endlinechar\relax}%
192 \endlinechar=-1%
193 \tikzscale@addRestoreEndLineCharToTikzpicture
194 \apptocmd{\endtikzpicture}{%
195 \endlinechar=-1%
196 }{}{\PackageError{tikzscale}{Patching endtikzpicture failed}}%
197 }%
198 \def\tikzscale@addRestoreEndLineCharToTikzpicture{%
199 \pretocmd{\tikzpicture}{%
200 \tikzscale@restoreEndLineChar
201 }{}{\PackageError{tikzscale}{Patching tikzpicture failed}}%
202 }
203 \def\tikzscale@patchTikzpictureIncludegraphics{%
204 \pretocmd{\tikzpicture}{%
205 \LetLtxMacro{\includegraphics}{\tikzscale@oldincludegraphics}%
206 }{}{\PackageError{tikzscale}{Patching tikzpicture failed}}%
207 }
208 \def\tikzscale@patchEndtikzpictureIncludegraphics{%
209 \apptocmd{\endtikzpicture}{%
210 \LetLtxMacro{\includegraphics}{\tikzscale@includegraphics}%
211 }{}{\PackageError{tikzscale}{Patching endtikzpicture failed}}%
212 }
213 \NewDocumentCommand{\tikzscale@findExactFileName}{mm}{%
214 \csundef{#1}%
215 \def\tikzscale@checkDirectory##1{%
216 \def\tikzscale@checkExtension####1{%
217 \IfFileExists{##1#2####1}{%
218 \csedef{#1}{##1#2####1}%
219 \listbreak
220 }{}%
221 }%
222 \eforcsvlist{\tikzscale@checkExtension}{{},\Gin@extensions}%
223 \ifcsdef{#1}{%
224 \grouplistbreak
225 }{}%
226 }%
227 \tikzscale@setGraphicsPath
228 \eforgrouplist{\tikzscale@checkDirectory}{\tikzscale@graphicspath}%
229 \ifcsundef{#1}{%
230 \csdef{#1}{#2}%
231 }{}%
232 }
233 \NewDocumentCommand{\tikzscale@setGraphicsPath}{}{%
234 \ifdef{\Ginput@path}{%
235 \tikzscale@trimMacro{\Ginput@path}%
236 }{}%
237 \ifdef{\currfiledir}{%
238 \ifdef{\Ginput@path}{%
239 \def\tikzscale@graphicspath{{\currfiledir}\Ginput@path{}}%
240 }{%
241 \def\tikzscale@graphicspath{{\currfiledir}{}}%
242 }%
243 }{%
244 \ifdef{\Ginput@path}{%
245 \def\tikzscale@graphicspath{\Ginput@path{}}%
246 }{%
247 \def\tikzscale@graphicspath{{}}%
248 }%
249 }%
250 }%
251 \NewDocumentCommand{\tikzscale@isTikzFile}{mm}{%
252 \def\do##1{%
253 \IfEndWith{#2}{##1}{%
254 \csdef{#1}{}%
255 \listbreak
256 }{}%
257 }%
258 \csundef{#1}%
259 \edocsvlist{\tikzscale@tikzFileExtensions}%
260 }
261 \pgfkeys{
262 /tikzscale/.is family, /tikzscale,
263 width/.code = {\pgfmathsetmacro{\requestedWidth}{#1}},
264 width/.value required,
265 height/.code = {\pgfmathsetmacro{\requestedHeight}{#1}},
266 height/.value required,
267 axisratio/.code = {\pgfmathsetmacro{\requestedAxisRatio}{#1}},
268 axisratio/.value required
269 }
270 \newcommand{\tikzscale@includetikz}[2][]{%
271 \pgfkeys{/tikzscale, #1}%
272 \tikzscale@ifExternalizationActive{%
273 \tikzifexternalizingnext{%
274 \tikzscale@debug{Externalizing the file #2}%
275 \tikzscale@includetikzUnexternalized{#2}%
276 }{%
277 \tikzscale@includetikzWithExternalization{#2}%
278 }%
279 }{%
280 \tikzscale@includetikzUnexternalized{#2}%
281 }%
282 }
283 \def\tikzscale@includetikzUnexternalized#1{%
284 \elseif{test {\ifundef{\requestedWidth}} and test {\ifundef{\requestedHeight}} and test {\ifundef{\requestedAxisRatio}}}{%
285 \tikzscale@debug{no option given}%
286 \tikzscale@trim{\input{#1}}%
287 }{test {\ifdef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{%
288 \tikzscale@debug{width and height given}%
289 \tikzscale@resizePlotTo{#1}%
290 }{test {\ifdef{\requestedAxisRatio}}}{%
291 \tikzscale@debug{axis ratio given}%
292 \tikzscale@includeAxisRatio{#1}%
293 }{test {\ifundef{\requestedAxisRatio}}}{%
294 \tikzscale@debug{width or height given}%
295 \ifdef{\pgfplotsset}{%
296 \def\requestedAxisRatio{1}%
297 \tikzscale@includeAxisRatio{#1}%
298 }{%
299 \tikzscale@debug{no pgfplots loaded}%
300 \tikzscale@includeNormalTikzpicture{#1}%
301 }%
302 }{%
303 \tikzscale@invalidKeyError{#1}%
304 }%
305 }
306
307 \NewDocumentCommand{\tikzscale@includetikzWithExternalization}{m}{%
308 \tikzexternalgetnextfilename{\tikzscale@externalizationName}%
309 \pgfexternalreaddpth{\tikzscale@externalizationName}%
310 \IfFileExists{\[email protected]}{%
311 \tikzscale@checkRequestedSizeChanges
312 }{}%
313 \tikzscale@trim{\input{#1}}%
314 }
315 \NewDocumentCommand{\tikzscale@checkRequestedSizeChanges}{}{%
316 \ifdef{\requestedWidth}{%
317 \ifdef{\pgfexternalwidth}{%
318 \tikzscale@ifSizeDifference{\requestedWidth - \pgfexternalwidth}{%
319 \tikzset{external/remake next}%
320 \tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of width difference \MessageBreak (requestedWidth=\requestedWidth, pgfexternalwidth=\pgfexternalwidth)}%
321 }{}%
322 }{%
323 \tikzset{external/remake next}%
324 \tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of no external width}%
325 }%
326 }{}%
327 \ifdef{\requestedHeight}{%
328 \ifdef{\pgfexternalheight}{%
329 \tikzscale@ifSizeDifference{\requestedHeight - \pgfexternalheight}{%
330 \tikzset{external/remake next}%
331 \tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of height difference \MessageBreak (requestedHeight=\requestedHeight, pgfexternalheight=\pgfexternalheight)}%
332 }{}%
333 }{%
334 \tikzset{external/remake next}%
335 \tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of no external height}%
336 }%
337 }{}%
338 \ifdef{\requestedAxisRatio}{%
339 \ifdef{\tikzscale@oldAxisRatio}{%
340 \tikzscale@ifSizeDifference{\requestedAxisRatio - \tikzscale@oldAxisRatio}{%
341 \tikzset{external/remake next}%
342 \tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of axis ratio difference \MessageBreak (requestedAxisRatio=\requestedAxisRatio, oldAxisRatio=\tikzscale@oldAxisRatio)}%
343 }{}%
344 \undef{\tikzscale@oldAxisRatio}%
345 }{%
346 \tikzset{external/remake next}%
347 \tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of no external axis ratio}%
348 }%
349 }{}%
350 }
351 \NewDocumentCommand{\tikzscale@preparePlot}{}{%
352 \pgfplotsset{every axis/.append style={width=\tikzscale@width,height=\tikzscale@height,every axis/.style={}}}%
353 }
354 \NewDocumentCommand{\tikzscale@prepareTikzpicture}{}{%
355 \tikzset{every picture/.style={scale=\tikzscale@scale,every picture/.style={}}}%
356 }
357 \NewDocumentCommand{\tikzscale@includeNormalTikzpicture}{m}{%
358 \tikzscale@prepareTikzpicture
359 \elseif{test {\ifdef{\requestedWidth}} and test {\ifundef{\requestedHeight}}}{%
360 \def\requestedSize{\requestedWidth}%
361 \tikzscale@scaleTikzpictureTo{\wd}{\tikzscale@trim{\input{#1}}}{#1}%
362 }{test {\ifundef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{%
363 \def\requestedSize{\requestedHeight}%
364 \tikzscale@scaleTikzpictureTo{\ht}{\tikzscale@trim{\input{#1}}}{#1}%
365 }{%
366 \tikzscale@invalidKeyError{#1}%
367 }%
368 }
369 \NewDocumentCommand{\tikzscale@invalidKeyError}{m}{%
370 \PackageError{tikzscale}{Invalid key for TikZ graphic}{Change key #1 into a valid key.}%
371 }
372 \NewDocumentCommand{\tikzscale@includeAxisRatio}{m}{%
373 \pgfplotsset{every axis/.append style={scale only axis,every axis/.style={}}}%
374 \elseif{test {\ifdef{\requestedWidth}} and test {\ifundef{\requestedHeight}}}{%
375 \let\requestedSize\requestedWidth
376 \def\tikzscale@width{\requestedWidth}%
377 \pgfmathsetmacro{\tikzscale@height}{\requestedWidth / \requestedAxisRatio}%
378 \tikzscale@resizePlotWithAxesRatioTo{\wd}{\tikzscale@width}{\tikzscale@trim{\input{#1}}}{#1}%
379 }{test {\ifundef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{%
380 \let\requestedSize\requestedHeight
381 \def\tikzscale@height{\requestedHeight}%
382 \pgfmathsetmacro{\tikzscale@width}{\requestedHeight * \requestedAxisRatio}%
383 \tikzscale@resizePlotWithAxesRatioTo{\ht}{\tikzscale@height}{\tikzscale@trim{\input{#1}}}{#1}%
384 }{%
385 \tikzscale@invalidKeyError{#1}%
386 }%
387 }
388 \NewDocumentCommand{\tikzscale@scaleTikzpictureTo}{mmm}{%
389 \tikzscale@debug{At beginning scale, requestedWidth=\requestedWidth}%
390 \tikzscale@conditionalDisableExternalization
391 \def\tikzscale@scale{1}%
392 \tikzscale@measureSize{\measuredFirst}{#1}{#2}%
393 \pgfmathsetmacro{\tikzscale@scale}{\requestedSize/\measuredFirst}%
394 \tikzscale@measureSize{\measuredSecond}{#1}{#2}%
395 \tikzscale@ifSizeDifference{\measuredSecond - \requestedSize}{%
396 \tikzscale@ifSizeDifference{\measuredFirst - \measuredSecond}{%
397 }{%
398 \PackageError{tikzscale}{Requested to scale unscalable graphic}{Do not set width or height for graphic in\MessageBreak #3}%
399 }%
400 \pgfmathsetmacro{\fixedSize}{(\tikzscale@scale*\measuredFirst - \measuredSecond) / (\tikzscale@scale - 1)}%
401 \pgfmathsetmacro{\tikzscale@scale}{(\requestedSize - \fixedSize) / (\measuredFirst - \fixedSize)}%
402 \foreach \l in {1,...,\maxTestIterations}{%
403 \tikzscale@measureSize{\measuredIntermediate}{#1}{#2}%
404 \tikzscale@ifSizeDifference{\measuredIntermediate-\requestedSize}{%
405 \pgfmathsetmacro{\errorRatio}{\measuredIntermediate/\requestedSize}%
406 \tikzscale@debug{errorRatio=\errorRatio\MessageBreak for #3}%
407 \pgfmathsetglobalmacro{\tikzscale@scale}{\tikzscale@scale/\errorRatio}%
408 }{%
409 \breakforeach%
410 }%
411 }%
412 % Do a last measurement to be able to warn if the size does not fit good enough. This measurement has to be done before possibly reactivating the externalization, as measurements with activated externalization can lead to wrong measurement results (possibly due to calling \cmd{\shipout} inside of the measurement). The assumption is, that the real graphic size does not change if the externalization gets activated, which all tests seem to confirm.
413 \tikzscale@measureSize{\measuredFinal}{#1}{#2}%
414 \tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{#3}%
415 \tikzscale@testGraphicFileForRetry{#1}{#2}{#3}{\measuredFinal}%
416 }{%
417 \tikzscale@testGraphicFileForRetry{#1}{#2}{#3}{\measuredSecond}%
418 }%
419 }
420 \def\tikzscale@testGraphicFileForRetry#1#2#3#4{%
421 \tikzscale@conditionalEnableExternalization{#3}%
422 \tikzscale@ifExternalizationActive{%
423 \tikzexternalgetnextfilename{\tikzscale@externalizationName}%
424 }{}%
425 #2%
426 \tikzscale@ifExternalizationActive{%
427 \pgfexternalreaddpth{\tikzscale@externalizationName}%
428 \tikzscale@ifIsWidth{#1}{%
429 \edef\pgfexternalsize{\pgfexternalwidth}%
430 }{%
431 \edef\pgfexternalsize{\pgfexternalheight}%
432 }%
433 \ifundef{\originalRequestedSize}{%
434 \let\originalRequestedSize\requestedSize
435 }{}%
436 \tikzscale@debug{originalRequestedSize=\originalRequestedSize, requestedSize=\requestedSize, pgfexternalsize=\pgfexternalsize}%
437 \tikzscale@ifSizeDifference{\originalRequestedSize-\pgfexternalsize}{%
438 \pgfmathsetmacro{\requestedSize}{\requestedSize + #4 - \pgfexternalsize}%
439 \tikzscale@decreaseFigureCounter
440 \tikzscale@scaleTikzpictureTo{#1}{#2}{#3}%
441 }{}%
442 }{}%
443 }
444 \def\tikzscale@decreaseFigureCounter{%
445 \begingroup
446 \edef\macroName{c@tikzext@no@\pgfkeysvalueof{/tikz/external/figure name}}%
447 \c@pgf@counta=\csuse{\macroName}\relax
448 \advance\c@pgf@counta -1\relax
449 \expandafter\xdef\csname\macroName\endcsname{\the\c@pgf@counta}%
450 \endgroup
451 }%
452 \NewDocumentCommand{\tikzscale@resizePlotTo}{m}{%
453 \def\fileName{#1}%
454 \def\content{\tikzscale@trim{\input{#1}}}%
455 \tikzscale@preparePlot
456 \def\tikzscale@width{\requestedWidth}%
457 \def\tikzscale@height{\requestedHeight}%
458 \tikzscale@conditionalDisableExternalization
459 \foreach \l in {1,...,\maxTestIterations}{%
460 \sbox{\tikzscale@measuredSize}{\content}%
461 \pgfmathsetmacro{\widthDifference}{\wd\tikzscale@measuredSize - \requestedWidth}%
462 \pgfmathsetmacro{\heightDifference}{\ht\tikzscale@measuredSize - \requestedHeight}%
463 \ifboolexpr{test {\tikzscale@ifSizeDifference{\widthDifference}} or test {\tikzscale@ifSizeDifference{\heightDifference}}}{%
464 \pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@width - \widthDifference}%
465 \pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@height - \heightDifference}%
466 }{%
467 \breakforeach
468 }%
469 }%
470 \sbox{\tikzscale@measuredSize}{\content}%
471 \tikzscale@warnIfSizeDifference{\requestedWidth}{\wd\tikzscale@measuredSize}{\fileName's width}%
472 \tikzscale@warnIfSizeDifference{\requestedHeight}{\ht\tikzscale@measuredSize}{\fileName's height}%
473 \tikzscale@conditionalEnableExternalization{\fileName}%
474 \content%
475 }
476
477 \NewDocumentCommand{\tikzscale@resizePlotWithAxesRatioTo}{mmmm}{%
478 \def\dimension{#1}%
479 \def\variable{#2}%
480 \def\content{#3}%
481 \def\fileName{#4}%
482 \gdef\tikzscale@oldSizeDifference{0pt}%
483 \tikzscale@preparePlot
484 \tikzscale@conditionalDisableExternalization
485 \foreach \l in {1,...,\maxTestIterations}{%
486 \tikzscale@measureSize{\measuredSize}{\dimension}{\content}%
487 \pgfmathsetmacro{\sizeDifference}{\measuredSize - \requestedSize}%
488 \tikzscale@ifSizeDifference{\sizeDifference}{%
489 \tikzscale@ifIsWidth{\dimension}{%
490 \pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@width - \sizeDifference}%
491 \pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@width / \requestedAxisRatio}%
492 }{%
493 \pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@height - \sizeDifference}%
494 \pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@height * \requestedAxisRatio}%
495 }%
496 \tikzscale@ifSizeDifference{\sizeDifference-\tikzscale@oldSizeDifference}{%
497 }{%
498 \tikzscale@conditionalEnableExternalization{\fileName}%
499 \tikzscale@includeNormalTikzpicture{#4}%
500 \gdef\tikzscale@alreadyIncluded{true}%
501 \breakforeach
502 }%
503 \pgfmathsetglobalmacro{\tikzscale@oldSizeDifference}{\sizeDifference}%
504 }{%
505 \breakforeach
506 }%
507 }%
508 \ifdef{\tikzscale@alreadyIncluded}{%
509 \global\undef\tikzscale@alreadyIncluded%
510 }{%
511 % Do a last measurement to be able to warn if the size does not fit good enough. This measurement has to be done before possibly reactivating the externalization, as measurements with activated externalization can lead to wrong measurement results (possibly due to calling \cmd{\shipout} inside of the measurement). The assumption is, that the real graphic size does not change if the externalization gets activated, which all tests seem to confirm.
512 \tikzscale@measureSize{\measuredFinal}{\dimension}{\content}%
513 \tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{\fileName}%
514 \tikzscale@conditionalEnableExternalization{\fileName}%
515 \content%
516 }%
517 }
518 \def\tikzscale@ifIsWidth#1{%
519 \ifedefequal{#1}{\wd}%
520 }
521 \newsavebox{\tikzscale@measuredSize}
522 \def\tikzscale@measureSize#1#2#3{%
523 \sbox{\tikzscale@measuredSize}{#3}%
524 \pgfmathsetmacro{#1}{#2\tikzscale@measuredSize}%
525 }
526 \def\tikzscale@ifSizeDifference#1#2#3{%
527 \pgfmathparse{abs(#1)}%
528 \ifdimgreater{\pgfmathresult pt}{\tikzscale@accuracy}{%
529 #2%
530 }{%
531 #3%
532 }%
533 }%
534
535 \def\tikzscale@warnIfSizeDifference#1#2#3{%
536 \tikzscale@ifSizeDifference{#1-#2}{%
537 \PackageWarning{tikzscale}{Scaling of #3 was only\MessageBreak accurate to \pgfmathresult pt}%
538 }{}%
539 }
540 \NewDocumentCommand{\tikzscale@conditionalDisableExternalization}{}{%
541 \tikzscale@ifExternalizationActive{%
542 \tikzexternaldisable
543 \def\tikzscale@externalizationWasDisabled{}%
544 }{}%
545 \ifExternalizationLoaded{%
546 \tikzscale@addRestoreEndLineCharToTikzpicture
547 }{}%
548 \ifdef{\pause}{%
549 \LetLtxMacro{\tikzscale@oldpause}{\pause}%
550 \RenewDocumentCommand{\pause}{o}{}%
551 }{}%
552 }
553 \NewDocumentCommand{\tikzscale@conditionalEnableExternalization}{m}{%
554 \ifdef{\tikzscale@externalizationWasDisabled}{%
555 \tikzexternalenable
556 \undef\tikzscale@externalizationWasDisabled
557 }{}%
558 \ifExternalizationLoaded{%
559 \tikzscale@addRestoreEndLineCharToTikzpicture
560 }{}%
561 \ifdef{\pause}{%
562 \LetLtxMacro{\pause}{\tikzscale@oldpause}%
563 }{}%
564 }
565 %%
566 %% Copyright (C) 2012 by Patrick Häcker <[email protected]>
567 %%
568 %% This work may be distributed and/or modified under the
569 %% conditions of the LaTeX Project Public License (LPPL), either
570 %% version 1.3c of this license or (at your option) any later
571 %% version. The latest version of this license is in the file:
572 %%
573 %% http://www.latex-project.org/lppl.txt
574 %%
575 %% This work is "maintained" (as per LPPL maintenance status) by
576 %% Patrick Häcker.
577 %%
578 %% This work consists of the file tikzscale.dtx
579 %% and the derived files tikzscale.ins,
580 %% tikzscale.pdf and
581 %% tikzscale.sty.
582 %%
583 %%
584 %% End of file `tikzscale.sty'.