summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/tikzscale/tikzscale.dtx
blob: d5f7a29bb9ee8b6fb4ba75b6ef527631eef8543f (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
% \iffalse meta-comment
% !TEX program  = pdfLaTeX
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
tikzscale --- Absolute resizing of TikZ pictures and PGF plots without
              scaling text
E-mail: pat_h@web.de Released under the LaTeX Project
Public License v1.3c or later See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

The tikzscale package extends the includegraphics command to support
tikzpictures. It allows scaling of TikZ images and PGFPlots to a provided
width or height without changing the text size.

Usage: \usepackage{tikzscale}
    or \usepackage[loadFromCurrentDirectory]{tikzscale}

As an example write
\includegraphics{myTikZFile.tikz}
instead of
\includegraphics{myJPEGFile.jpeg},
with myTikZFile.tikz being the file name of a text file containing
everything from \begin{tikzpicture} to \end{tikzpicture}. To actually
do some scaling of the included TikZ file, give either an absolute width
or an absolute height in the optional argument, e.g.
\tikzscale@includetikz[width=0.5\linewidth]{myTikZFile.tikz}.
If the file contains a plot created with the PGFPlots package, set both
width and height via the optional argument, e.g.
\tikzscale@includetikz[width=\linewidth,height=0.4\linewidth]{myPGFPlot.tikz}.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
tikzscale --- Absolute resizing of TikZ pictures and PGF plots without scaling text
E-mail: pat_h@web.de
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

\endpreamble
\postamble

Copyright (C) 2012 by Patrick Häcker <pat_h@web.de>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version.  The latest version of this license is in the file:

http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
Patrick Häcker.

This work consists of the file  tikzscale.dtx
and the derived files           tikzscale.ins,
                                tikzscale.pdf and
                                tikzscale.sty.

\endpostamble
\usedir{tex/latex/tikzscale}
\generate{
  \file{tikzscale.sty}{\from{tikzscale.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/tikzscale}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/tikzscale}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tikzscale}[2012/11/02 v0.1.2 tikzscale LaTeX package]
\RequirePackage{graphicx}
\RequirePackage{etoolbox}
\RequirePackage{pgfkeys}
\RequirePackage{xparse}
\RequirePackage{letltxmacro}
\RequirePackage{xstring}
%</package>
%<*driver>
\documentclass{ltxdoc}
% \documentclass{ydoc}
\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
\usepackage{subfig}
\usepackage{booktabs}
\usepackage{tabulary}
\usepackage{tikzscale}
\usepackage{tikz}
\usepackage{pgfplots}
% Load the MWE package, although its functionality is not needed. The loading highlights, that some graphics used below are part of the MWE package.
\usepackage{mwe}
\usepackage{xcolor}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage[numbered]{hypdoc}
\hypersetup{pdftitle=The tikzscale package}
\newcommand{\cell}[2][c]{%
	\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand{\rcell}[2][c]{%
	\begin{tabular}[#1]{@{}r@{}}#2\end{tabular}%
}
% \usepackage{ydoc-desc}
% \optionaloff % otherwise the optional arguments are displayed lighter as the normal text
\def\xcmd#1{%
	\cmd#1%
	\futurelet\tmp\arglook%
}
\def\arglook{%
	\let\next\relax
	\ifx[%
		\tmp%
		\let\next\xoarg%
	\fi
	\ifx\bgroup%
		\tmp%
		\let\next\xmarg%
	\fi
	\ifx(%
		\tmp%
		\let\next\xparg%
	\fi
	\next
}

\def\xoarg[#1]{%
	\oarg{#1}\futurelet\tmp\arglook%
}
\def\xmarg#1{%
	\marg{#1}\futurelet\tmp\arglook%
}
\def\xparg(#1){%
	\parg{#1}\futurelet\tmp\arglook%
}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
%\GetFileInfo{\jobname.sty}
%
%\title{^^A
%  \textsf{tikzscale} --- Absolute resizing of TikZ pictures and PGF plots without scaling text\thanks{^^A
%    This file describes version \fileversion, last revised \filedate.^^A
%  }^^A
%}
%\author{^^A
%  Patrick Häcker\thanks{E-mail: pat\_h@web.de}^^A
%}
%\date{Released \filedate}
%
%\maketitle
%
%\changes{v0.1}{2012/10/31}{First public release}
%\changes{v0.1.1}{2012/11/02}{Fix some bugs, increase robustness, regenerate externalized files less often}
%\changes{v0.1.2}{2012/11/02}{Fix whitespace issues}
% \section{Introduction}
% When dealing with graphics, there are different scaling demands. For \emph{absolute} scaling, a width and/or height is given. Opposed to that, for relativ scaling, a horizontal and/or vertical scaling factor is needed. This package only is about absolute scaling of tikzpicture environments. The different absolute scaling demands and their solutions are shown in table \ref{scalingDemands}.
% \begin{table}
	% \centering
	% \caption[Graphic scaling methods.]{Absolute graphic scaling methods. If multiple methods are available, the most native one is shown. Methods which \textcolor{orange}{approximate} the scaling are shown in orange text color. \textcolor{blue}{Recommended} methods are shown in blue textcolor.}%^^A Note, that the first three methods in each table are absolute ones, whereas the last three methods are relative ones.}%
	% \label{scalingDemands}
	% \subfloat[Scaling with scaled text and line widths.]{^^A
		% \begin{tabular}{rccc}%^^A{1.1\linewidth}{RCCC}
			% \toprule
			% scale & Images & TikZ/PGFPlots\\
			% \midrule
			% \rcell{to width\\proportionally} & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit}]}}} & \cell{\cmd{\resizebox}\\\texttt{\{\emph{width}\}\{!\}}}\\[0.8em]
			% \rcell{to width\\keeping height} & \cell{\cmd{\resizebox}\\\texttt{\{\emph{width}\}\{\cmd{\height}\}}} & \cell{\cmd{\resizebox}\\\texttt{\{\emph{width}\}\{\cmd{\height}\}}}\\[0.8em]
			% \rcell{to height\\proportionally} & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[height=\emph{unit}]}}} & \cell{\cmd{\resizebox}\\\texttt{\{!\}\{\emph{height}\}}}\\[0.8em]
			% \rcell{to height\\keeping width} & \cell{\cmd{\resizebox}\\\texttt{\{\cmd{\width}\}\{\emph{height}\}}} & \cell{\cmd{\resizebox}\\\texttt{\{\cmd{\width}\}\{\emph{height}\}}}\\[0.8em]
			% \rcell{to width\\and height} & \cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit},height=\emph{unit}]}} & \cell{\cmd{\resizebox}\\\texttt{\{\emph{width}\}\{\emph{height}\}}}\\
			%^^A horizontally & \cell{\cmd{\scalebox}\\\texttt{\{\emph{factor}\}[1]}} & \cell{\texttt{[transform canvas=}\\\texttt{\{xscale=\emph{factor}\}]}} & \texttt{[xscale=\emph{factor}]}\\[0.8em]
			%^^A vertically & \cell{\cmd{\scalebox}\\\texttt{\{1\}[\emph{factor}]}} & \cell{\texttt{[transform canvas=}\\\texttt{\{yscale=\emph{factor}\}]}} & \texttt{[yscale=\emph{factor}]}\\[0.8em]
			%^^A proportionally & \cell{\cmd{\scalebox}\\\texttt{\{\emph{factor}\}}} & \cell{\texttt{[transform canvas=}\\\texttt{\{scale=\emph{factor}\}]}} & \texttt{[scale=\emph{factor}]}\\
			% \bottomrule
		% \end{tabular}
	% }\\%
	% \subfloat[Scaling with unscaled text and line widths without tikzscale.]{^^A
		% \begin{tabulary}{1.1\linewidth}{RCCC}
			% \toprule
			% scale & Images & TikZ & PGFPlots\\
			% \midrule
			% \rcell{to width\\proportionally} & -- & -- & \textcolor{orange}{\texttt{[width=\emph{unit}]}}\\[0.8em]
			% \rcell{to width\\keeping height} & -- & -- & --\\[0.8em]
			% \rcell{to height\\proportionally} & -- & -- & \textcolor{orange}{\texttt{[height=\emph{unit}]}}\\[0.8em]
			% \rcell{to height\\keeping width} & -- & -- & --\\[0.8em]
			% \rcell{to width\\and height} & -- & -- & \textcolor{orange}{\texttt{[width=\emph{unit},height=\emph{unit}]}}\\
			%^^A horizontally & -- & \texttt{[xscale=\emph{factor}]} & --\\
			%^^A vertically & -- & \texttt{[yscale=\emph{factor}]} & --\\
			%^^A proportionally & -- & \texttt{[scale=\emph{factor}]} & --\\
			% \bottomrule
		% \end{tabulary}
	% }\\%
	% \subfloat[Scaling with unscaled text and line widths with tikzscale.]{^^A
		% \begin{tabulary}{1.1\linewidth}{RCCC}
			% \toprule
			% scale & Images & TikZ & PGFPlots\\
			% \midrule
			% \rcell{to width\\proportionally} & -- & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit}]}}} & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit}]}}}\\[0.8em]
			% \rcell{to width\\keeping height} & -- & -- & --\\[0.8em]
			% \rcell{to height\\proportionally} & -- & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[height=\emph{unit}]}}} & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[height=\emph{unit}]}}}\\[0.8em]
			% \rcell{to height\\keeping width} & -- & -- & --\\[0.8em]
			% \rcell{to width\\and height} & -- & -- & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit},height=\emph{unit}]}}}\\
			%^^A horizontally & -- & \texttt{[xscale=\emph{factor}]} & --\\
			%^^A vertically & -- & \texttt{[yscale=\emph{factor}]} & --\\
			%^^A proportionally & -- & \texttt{[scale=\emph{factor}]} & --\\
			% \bottomrule
		% \end{tabulary}
	% }
% \end{table}
%
% The tikzscale package adds and improves certain forms of absolute scaling for TikZ and PGFPlots, respectively. These scaling methods are the ones which are most useful, maybe even the only ones which are needed. During the scaling, the text sizes and line widths are left unscaled, which avoids inconsistency and visual distraction. PGFPlots itself can scale absolutely, but an approximation is used to achieve that. The tikzscale package uses optimization algorithms and warns if the scaling is not exact.
%
% Using tikzscale all relevant scaling methods share the same user interface with the well known \cmd{\includegraphics} command, enabling some of its features like automatic file extension detection for TikZ and PGFPlots, too. Furthermore, the \cmd{\includegraphics} is improved to look-up relative paths in the correct subdirectory, if a \LaTeX\ project is organized in subdirectories.
%
% Relative scaling methods are mostly useless, as the sizes of the used images are often arbitrary, either determined by some resolution for rastered images or some arbitrary unit vector size for vector images, TikZ  and PGFPlots. For traditional images and TikZ pictures, only proportional scaling methods giving either a width or a height make sense, as otherwise they get heavily distorted if the original aspect ratio is changed. As PGFPlots can handle different aspect ratios and aspect ratios are normally not predefined for plots, its requirement is the opposite: A width and a height are needed to avoid getting arbitrary sizes. For some special plots, the axis ratio can be given, as well. These requirements lead to the marked blue colors in table \ref{scalingDemands}.
%
% \section{Usage and Examples}
% Loading the tikzscale package without loading other packages, does not do anything useful.
%
% \subsection{TikZ}
% If the tikzscale and the tikz packages are loaded, the \cmd{\includegraphics} command can be used to input and scale a tikzpicture environment located in a separate file.
%
% As an example create the following .tex-file.
%
% \vspace{0.5em}
% \noindent\cmd{\documentclass\{minimal\}}\\
% \cmd{\usepackage\{tikz\}}\\
% \cmd{\usepackage\{tikzscale\}}\\
% \cmd{\begin\{document\}}\\
% \indent\texttt{\cmd{\includegraphics}[width=0.5\cmd{\linewidth}]\{linewidth.tikz\}}\\
% \cmd{\end\{document\}}
% \vspace{0.5em}
%
% Furthermore create the following .tikz-file and save it as linewidth.tikz in the same directory as the above .tex-file.
%
% \vspace{0.5em}
% \noindent\cmd{\begin\{tikzpicture\}}\\
% \indent\texttt{\cmd{\draw} (0,0) -- node \{center\} (\cmd{\linewidth},1);}\\
% \cmd{\end\{tikzpicture\}}
% \vspace{0.5em}
%
% The result of the complied .tex-file should look like this.\\%^^A
%\noindent\includegraphics[width=0.5\linewidth]{linewidth.tikz}
%
% So although the original tikzpicture itself has the width of a complete line, it gets proportionally scaled down to half the width while being loaded from the \cmd{\includegraphics} command. Neither the line's thickness nor the text \texttt{center} are scaled. Compare the output to\\
% \indent\texttt{\cmd{\input}\{linewidth.tikz\}}\\
% \input{linewidth.tikz}\\
% and\\
% \indent\texttt{\cmd{\resizebox}\{0.5\cmd{\linewidth}\}\{!\}\{\cmd{\input}\{linewidth.tikz\}\}}\\
% \resizebox{0.5\linewidth}{!}{\input{linewidth.tikz}}\\
% to see tikzscale's benefit.
%
% \subsection{PGFPlots}
% \subsubsection{Scaling of width and height}
% If the pgfplots package is loaded together with the tikzscale package, the user interface is the same. Instead of giving either a width or a height, both have to be given for pgfplots. So
%
% \vspace{0.5em}
% \cmd{\input\{pgfplots-test.tikz\}}
%
% \vspace{0.5em}
% \texttt{\cmd{\begin}\{tikzpicture\}\cmd{\begin}\{axis\}[width=3cm,height=2cm] \dots}
% \vspace{0.5em}
%
% \noindent becomes
%
% \vspace{0.5em}
% \cmd{\includegraphics[width=3cm,height=2cm]\{pgfplots-test.tikz\}}
%
% \vspace{0.5em}
% \texttt{\cmd{\begin}\{tikzpicture\}\cmd{\begin}\{axis\} \dots}
% \vspace{0.5em}
%
% The benefit is a more accurate scaling algorithm, as the scaling with PGFPlots can be quite coarse. Another win is the unified interface, which simplifies the sharing of plots between projects enormously, as one file and thus one plot can be included in different projects with different sizes.
%
% \subsubsection{Scaling using axis ratio}
% The scaling described in the previous section scales the whole plot including all axis descriptions and legends to the given width and height. It can thus happen, that the plotted figure has a different size ratio than expected, if the x and y descriptions have different sizes as shown in figure \ref{width=height}.
% \begin{figure}
% \centering
% \frame{\includegraphics[width=0.4\linewidth,height=0.4\linewidth]{testgraphic2D.tikz}}
% \caption{Using options \texttt{width=0.4\cmd{\linewidth}} and \texttt{height=0.4\cmd{\linewidth}} results in an overall quadratic graphic with overall width and height set to 40\% of the linewidth.}
% \label{width=height}
% \end{figure}
% Sometimes, the x-axis and the y-axis should have a specific ratio, e.g. being equal, ignoring the axis description and other things. This is normally achieved by using PGFPlots' option \texttt{scale only axis}. Unfortunately, if this option would be used, a plot might be unsharable between two projects, if they have different requirements for the axis ratio. Thus, this option should not be used in this case. Instead, in \cmd{\includegraphics} there is a new option \texttt{axisratio} which must be used together with either width or height. It scales the whole plot including the axis description to the given width or height as in figure \ref{axisratio} while keeping the graphical part at a given axis ratio, where the ratio is defined by width divided by height. The graphical part is thus not quadratic in general.
% \begin{figure}
% \centering
% \frame{\includegraphics[width=0.4\linewidth,axisratio=1]{testgraphic2D.tikz}}
% \caption{Using options \texttt{width=0.4\cmd{\linewidth}} and \texttt{axisratio=1} results in an quadratic graphic area with overall width set to 40\% of the linewidth. The height follows from these constraints, so that the overall plot is not quadratic in general.}
% \label{axisratio}
% \end{figure}


%
% \subsection{Hints for TikZ and PGFPlots}
% The whole tikzpicture environment must be in a separate file. This allows sharing of graphics between different \TeX\ projects and a unified user interface via \cmd{\includegraphics}. Having tikzpicture environments directly in a .tex-file is not supported, i.e.\ they do not benefit from the tikzscale package. Multiple tikzpicture environments in one .tikz-file are not supported, either. Put things which always belong together in a shared tikzpicture environment and things which might be used separately in the future in separate files for code sharing across projects. The file ending may be ommited in the \cmd{\includegraphics} command, if it is one of .tikz, .TIKZ, .TikZ, .pgf or .PGF. At the moment, use only \emph{either} width \emph{or} height for normal (i.e.\ non-PGFPlots) tikzpicture environments and use width \emph{and} height or one of both together with \emph{axisratio} for tikzpicture environments containing a PGFPlots' axis environment.
%
% \subsection{currfile}
% If the tikzpicture package is loaded together with the \href{http://www.ctan.org/pkg/currfile}{currfile} package, another feature is activated. Suppose you have your project organized in the following directory tree with \textcolor{blue}{directories} shown in blue color:
%
% \vspace{0.5em}
% \noindent \textcolor{blue}{projectDirectory}\\
% \indent main.tex\\
% \indent \textcolor{blue}{firstChapter}\\
% \indent\indent firstChapter.tex\\
% \indent\indent firstGraphicOfFirstChapter.jpeg\\
% \indent\indent secondGraphicOfFirstChapter.tikz\\
% \indent \textcolor{blue}{secondChapter}\\
% \indent\indent secondChapter.tex\\
% \indent\indent firstGraphicOfSecondChapter.tikz\\
% \indent\indent secondGraphicOfSecondChapter.jpeg
% \vspace{0.5em}
%
% Further suppose the chapter.tex files are \cmd{\input}ted in main.tex. Calling\\
% \cmd{\includegraphics\{firstGraphicOfFirstChapter.jpeg\}}\\
% in firstChapter.tex normally does not work. The reason is that the\\
% \cmd{\input\{firstChapter.tex\}}\\
% command in main.tex copies the content of firstChapter.tex into main.tex, so when the \cmd{\includegraphics} command is called, it is called from within projectDirectory, thus the relative path lookup of firstGraphicOfFirstChapter.jpeg fails. Instead the command\\
% \cmd{\includegraphics\{firstChapter/firstGraphicOfFirstChapter.jpeg\}}\\
% can be used (example for a Unix system), but this is tedious and counter-intuitive.
%
% If both tikzscale and currfile are loaded, the limitation is fixed, so that both \cmd{\includegraphics} commands succeed. Note, that this functionality supports the traditional graphic formats, too, and is also available without loading the TikZ or PGFPlots packages, although the package's name might imply otherwise.
%
% \section{Compatibility}
% Using both the externalization library and tikzscale seems to have a race condition when a makefile is used with multiple jobs (-j$X$ with $X > 1$). The probability of getting errors increases with the number of jobs. For $X = 1$, obviously, no race condition could be observed. You should either avoid using mode \emph{list and make} or have only one job if you want to be on the safe side.
%
% \subsection{Load Order}
% There is no constraint regarding the load order known, yet. TikZ, PGFPlots and currfile might all be loaded or not in all possible combinations and orders before or after tikzscale.
%
% \subsection{Externalization library}
% TikZ' externalization library is supported. Its use is highly recommended, as tikzscale renders some graphics multiple times to get the correct size. The savings by using the externalization library can thus be huge.
%
% \section{Further Ideas}
% \begin{itemize}
%	\item When the externalization library is used, the graphic files get regenerated more often than necessary. This is, because the axis ratio is not saved, thus assumed unknown and the graphic is thus regenerated. For performance reasons if a default axis ratio is used, the plot is not regenerated. If a non-default axis ratio has been used for generating the externalized graphic and the default axis ratio is used in the current run, this is not detected and the graphic has the wrong size at the moment. You can, of course, delete the externalized graphic to generate a new one with the correct size.
%	\item if graphic files are located in a subdirectory, the externalized files should also be in that subdirectory.
%	\item the package can test if a pgfplot is used (needed if normal TikZ graphics should be stretchable) by changing \cmd{\tikzscale@width} and or \cmd{\tikzscale@height} and measuring. If nothing changes, it must be a normal tikzpicture (the argument does not hold the other way round).
%	\item it may be better to use the \href{http://tex.stackexchange.com/a/22957}{depth as well}
%	\item The final sizing parameters should be saved per figure in the aux file. The first rendering each run should be performed with the aux file's parameters into an sbox. The scaling algorithms should only be called, if the sizing requirements are not met.
% \end{itemize}
%
% \section{Contributions}
% \begin{itemize}
%     \item Jake
%         \begin{itemize}
%             \item Encouraged the author to create this package.
%         \end{itemize}
%     \item Dr.\ Christian Feuersänger
%         \begin{itemize}
%             \item Encouraged the author to create this package and created PGFPlots.
%             \item Answered many questions and had a lot of good ideas regarding the externalization and beyond.
%         \end{itemize}
%     \item David Carlisle
%         \begin{itemize}
%             \item Created the \cmd{\xcmd} macro for this package, which is used in the documentation.
%         \end{itemize}
% \end{itemize}
%
%
% \section{Implementation}
% The basic idea is to first get the correct file name (i.e. find the path and the file extension), then determine the graphic type (i.e. TikZ or something else) and call either the original includegraphics command or the tikzscale command. Tikzpictures are then plotted into an invisible box and their size is measured. If their measured size differs from the requested size, they are replotted with corrected parameters to get the requested size. The correctly sized plots are then really plotted.
%
% \iffalse
%<*package>
% \fi
% \makeatletter
%
%\begin{macro}{\tikzexternal}
%    \begin{macrocode}
\AtEndPreamble{%
%    \end{macrocode}
	% Activate the output of the graphics sizes into the dpth files (one file per graphic). This key is used if the externalization library is activated to check if the scaling is correct, otherwise the code is not needed
%    \begin{macrocode}
	\ifdef{\tikzifexternalizing}{%
		\pgfkeys{/pgf/images/external info}%
	}{}%
%    \end{macrocode}
	% Provide dummy commands, if the externalization library has not been loaded during the preamble.
%    \begin{macrocode}
	\ProvideDocumentCommand{\tikzsetnextfilename}{m}{}%
	\ProvideDocumentCommand{\tikzsetexternalprefix}{m}{}%
	\ProvideDocumentCommand{\tikzexternaldisable}{}{}%
	\ProvideDocumentCommand{\tikzexternalenable}{}{}%
	\ProvideDocumentCommand{\tikzexternalize@hasbeencalled}{}{0}%
%    \end{macrocode}
	% Define a test to check if the current graphic should be drawn and scaled.
%    \begin{macrocode}
	\ifboolexpr{test {\ifundef{\tikzexternalize}} or test {\ifdefstring{\tikzexternalize@hasbeencalled}{0}}}{%
%    \end{macrocode}
		% Always draw and scale the graphic if externalization library has not been loaded or not been activated.
%    \begin{macrocode}
		\def\tikzscale@ifDrawAndScale#1#2{#1}%
	}{%
%    \end{macrocode}
		% If externalization library has been loaded and activated, draw and scale the graphic if it is to be externalized.
%    \begin{macrocode}
		\def\tikzscale@ifDrawAndScale#1#2{%
			\tikzifexternalizingnext{#1}{#2}%
		}%
	}%
%\end{macro}
%
%\begin{macro}{\tikzscale@scale}
%    \begin{macrocode}
	\@ifpackageloaded{tikz}{%
%    \end{macrocode}
		% Set a minimum accuracy tikzscale tries to achieve. TeX's accuracy is limited, thus 0.001 pt cannot always be achieved independent of the number of iterations. The current value is chosen arbitrarily, but it might have to be increased in case a plot is found where the given accuracy cannot be achieved.
%    \begin{macrocode}
		\newlength{\tikzscale@accuracy}%
		\setlength{\tikzscale@accuracy}{0.01pt}%
%    \end{macrocode}
		% This is needed in normal TikZ pictures and in PGFPlots, but as the pgfplots package loads the tikz package, it is fine to define it here.
%    \begin{macrocode}
		\def\maxTestIterations{10}%
	}{}%
}
%    \end{macrocode}
%\end{macro}
%
% This command draws the plot's border at the right text border, so that thick points or label descriptions can reach into the margin. This should be limited to PGFPlots only if activated.
%^^A \tikzset{every picture/.style={trim axis right}}
%
% With the option below, the labels can be moved a bit to the left so that they reach to the text margin.
% yticklabel style={align=right,inner sep=0pt,xshift=-0.1cm}
%
%\begin{macro}{\pgfmathsetglobalmacro}
% This is a general command, which might be useful for inclusion into the tikz package. It works similar to \cmd{\pgfmathsetglobalmacro} but has global scope.
%    \begin{macrocode}
\def\pgfmathsetglobalmacro#1#2{%
	\pgfmathparse{#2}%
	\global\let#1\pgfmathresult%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\edocsvlist}
% This is a general command, which might be useful for inclusion into the etoolbox package. It works similar to \cmd{\docsvlist} but expands its argument similar to \cmd{\def} vs.\ \cmd{\edef}, which is useful if the list is stored in a macro/variable.
%    \begin{macrocode}
\def\edocsvlist#1{%
	\edef\tikzscale@edocsvlist{#1}%
	\expandafter\docsvlist\expandafter{\tikzscale@edocsvlist}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\eforcsvlist}
% These is a general command, which might be useful for inclusion into the etoolbox package. It works similar to \cmd{\forcsvlist} but expands its argument similar to \cmd{\def} vs.\ \cmd{\edef}, which is useful if the list is stored in a macro/variable.
%    \begin{macrocode}
\def\eforcsvlist#1#2{%
	\edef\tikzscale@eforcsvlist{#2}%
	\expandafter\forcsvlist\expandafter{\expandafter#1\expandafter}\expandafter{\tikzscale@eforcsvlist}%
}
%    \end{macrocode}
%\end{macro}
%
% \begin{macro}{\elseif}
% This macro provides a conditional which supports an if with an arbitrary amount of elseif (none is also ok) and an optional else. With a simplified syntax (remove the tests and the grouping) this would be worth a separate package.
%    \begin{macrocode}
\NewDocumentCommand{\elseif}{mm}{%
	\ifboolexpr{#1}{%
		#2%
		\elseif@absorb
	}{%
		\elseif@optional
	}%
}
\NewDocumentCommand{\elseif@optional}{gg}{%
	\IfValueTF{#1}{%
		\IfValueTF{#2}{%
			\ifboolexpr{#1}{%
				#2%
				\elseif@absorb
			}{%
				\elseif@optional
			}%
		}{%
			#1%
		}%
	}{}%
}
\NewDocumentCommand{\elseif@absorb}{g}{%
	\IfValueTF{#1}{%
		\elseif@absorb
	}{}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}
% This command is from \href{http://tex.stackexchange.com/a/63248/7323}{Bruno Le Floch}.
%    \begin{macrocode}
\ExplSyntaxOn
\NewDocumentCommand{\IfNoValueOrSplitEmptyTF}{mmm}{
	\ifboolexpr{test {\IfNoValueTF{#1}} or test {\tl_if_eq:nnTF{#1}{{}}}}{
		#2
	}{
		#3
	}
}
\ExplSyntaxOff
%    \end{macrocode}
%\end{macro}

%
%\begin{macro}{\activatetikzscale}
%    \begin{macrocode}
\AtEndPreamble{%
%    \end{macrocode}
	% Add the TikZ file extensions to the \href{http://tex.stackexchange.com/a/45502}{graphicx file extensions}.
%    \begin{macrocode}
	\def\tikzscale@tikzFileExtensions{.tikz,.TIKZ,.TikZ,.pgf,.PGF}%
% 	\def\tikzscale@tikzFileExtensions{.tikz,.TIKZ,.TikZ,.pgf,.PGF,.tex,.TEX}%
	\DeclareGraphicsExtensions{\tikzscale@tikzFileExtensions,\Gin@extensions}%
%    \end{macrocode}
	% The \cmd{graphicspath} command is used to set additional directories, which are searched for graphics. \cmd{\Ginput@path} is used to get the \href{http://tex.stackexchange.com/a/58404}{current content}.
%    \begin{macrocode}
	\ifdef{\currfiledir}{%
% 		\graphicspath{{\currfiledir}{}\Ginput@path}%
		\def\tikzscale@graphicspath{\currfiledir,{}}%
	}{%
		\def\tikzscale@graphicspath{{}}%
	}%
%    \end{macrocode}
%    Save the \cmd{\includegraphics} \href{ftp://ftp.tu-chemnitz.de/pub/tex/macros/latex/required/graphics/grfguide.pdf}{command} and replace it by a new more generic command, to have a consistent user interface.
%    \begin{macrocode}
	\LetLtxMacro{\tikzscale@oldincludegraphics}{\includegraphics}%
	\LetLtxMacro{\includegraphics}{\tikzscale@includegraphics}%
%    \end{macrocode}
	% Deactivate the new includegraphics command inside of tikzpictures, as a tikzpicture might load a PNG graphic or something and this should not be scaled by tikzscale but by TikZ or PGFPlots. Besides, the current implementation is not reentrant, so its not a good idea to call the macro recursively. The deactivation must be inside of tikzpicture, as a tikzpicture can be loaded without using includegraphics, thus it cannot be done there.
%    \begin{macrocode}
	\pretocmd{\tikzpicture}{\LetLtxMacro{\includegraphics}{\tikzscale@oldincludegraphics}}{}{\PackageWarning{tikzscale}{Patching tikzpicture not successful}}%
	\apptocmd{\endtikzpicture}{\LetLtxMacro{\includegraphics}{\tikzscale@includegraphics}}{}{\PackageWarning{tikzscale}{Patching endtikzpicture not successful}}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\includegraphics}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@includegraphics}{O{}m}{%
%    \end{macrocode}
	% This command uses an empty optional argument for compatibility with the traditional graphicx command.
	% Start a group, so that changed variables during processing the current tikzpicture due not influence other tikzpictures. This is much more convienient, than resetting every single variable. Use \cmd{\begingroup} instead of \cmd{\bgroup} to simplify finding unmatched braces.
%    \begin{macrocode}
	\begingroup
	%    \end{macrocode}
	% It happened at least once together with externalization, that the deactivation of the new includegraphics command did not work, so do it again to be safe (maybe reentrance problem with multiple tikzpicture calls?).
%    \begin{macrocode}
	\LetLtxMacro{\includegraphics}{\tikzscale@oldincludegraphics}%
%    \end{macrocode}
	% There is a leading space character introduced by the externalization library, if the file is input directly. Thus use a trick to avoid that space. Furthermore, TikZ introduces with a specific version an trailing space character. To get rid of all space character issues, just solve the problem here once an for all.
%    \begin{macrocode}
	\edef\tikzscale@restoreEndLineChar{\endlinechar=\the\endlinechar\relax}%
	\endlinechar=-1%
%    \end{macrocode}
	% Find the exact file name, as the ending and the path could be omitted.
%    \begin{macrocode}
	\tikzscale@findExactFileName{tikzscale@fileName}{#2}%
%    \end{macrocode}
	% Check if the found file is a TikZ file.
	%    \begin{macrocode}
	\tikzscale@isTikzFile{tikzscale@testTikzFile}{\tikzscale@fileName}%
	\ifcsdef{tikzscale@testTikzFile}{%
		\tikzscale@includetikz[#1]{\tikzscale@fileName}%
	}{%
		\tikzscale@oldincludegraphics[#1]{\tikzscale@fileName}%
	}%
	\tikzscale@restoreEndLineChar
	\endgroup
}%
%    \end{macrocode}
%\end{macro}
%
% \begin{macro}{\tikzscale@findExactFileName}
% Find the exact file name of a graphic file by testing several paths and file endings if there are degrees of freedom. The file name is saved in the command sequence name given by the first argument.
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@findExactFileName}{mm}{%
	%    \end{macrocode}
	% Delete the return variable if it already exists to allow checking if a file has been found.
	%    \begin{macrocode}
	\csundef{#1}%
	%    \end{macrocode}
	% Create a helper function used inside the file ending evaluation.
	%    \begin{macrocode}
	\def\tikzscale@checkDirectory##1{%
		\def\tikzscale@checkExtension####1{%
			\IfFileExists{##1#2####1}{%
				\csdef{#1}{##1#2####1}%
				\listbreak
			}{}%
		}%
%    \end{macrocode}
	% Test all possible file extensions and do not forget that the extension might already be given. \cmd{\Gin@extensions} returns the \href{http://tex.stackexchange.com/a/45502}{current content} set by \cmd{\DeclareGraphicsExtensions}.
%    \begin{macrocode}
		\eforcsvlist{\tikzscale@checkExtension}{{},\Gin@extensions}%
	}%
	\eforcsvlist{\tikzscale@checkDirectory}{\tikzscale@graphicspath}%
%    \end{macrocode}
	% If no file has been found, return the given file name, as includegraphics should try its best.
%    \begin{macrocode}
	\ifcsundef{#1}{%
		\csdef{#1}{#2}%
	}{}%
}
%    \end{macrocode}
%\end{macro}
%
% \begin{macro}{\tikzscale@findExactFileName}
% The first argument is the macro name (without backslash), which gets defined if the file is a tikzfile.
% The second argument is the file name.
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@isTikzFile}{mm}{%
	%    \end{macrocode}
	% Create a helper function used inside the evaluation.
	%    \begin{macrocode}
	\def\do##1{%
		\IfEndWith{#2}{##1}{%
			\csdef{#1}{}%
			\listbreak
		}{}%
	}%
%    \end{macrocode}
	% Delete macro so that defining it is really indicating something.
%    \begin{macrocode}
	\csundef{#1}%
	\edocsvlist{\tikzscale@tikzFileExtensions}%
}
%    \end{macrocode}
%\end{macro}
%
% \begin{macro}{\pgfkeys}
% This is \href{http://tex.stackexchange.com/a/34318}{similarly} done.
%    \begin{macrocode}
\pgfkeys{
	/tikzscale/.is family, /tikzscale,
	width/.code = {\pgfmathsetmacro{\requestedWidth}{#1}},
	width/.value required,
	height/.code = {\pgfmathsetmacro{\requestedHeight}{#1}},
	height/.value required,
	axisratio/.code = {\pgfmathsetmacro{\requestedAxisRatio}{#1}},
	axisratio/.value required
}
%    \end{macrocode}
%\end{macro}
%
% \begin{macro}{\tikzscale@includetikz}
% \xcmd\tikzscale@includetikz{filename}\\
% \xcmd\tikzscale@includetikz[width=1cm]{filename}\\
% \xcmd\tikzscale@includetikz[height=1cm]{filename}\\
% \xcmd\tikzscale@includetikz[height=1cm,width=1cm]{filename}\\
% \xcmd\tikzscale@includetikz[width=1cm,height=1cm]{filename}\\
% This command allows the inclusion of a tikz file like a graphics file. Thus instead of writing
% \cmd{\includegraphics}[width=\cmd{\linewidth}]{fileWithoutEnding}
% write
% \cmd{\tikzscale@includetikz}[width=\cmd{\linewidth}]{fileWithoutEnding}
% If only one of width or height are given, scale proportionally to fulfill the requirement. If both are given, scale non-proportionally to required width and height. Therefore, for normal tikzpictures only give either width or height, as the aspect ratio is already determined by the coordinate limits in the tikzpicture, but give width and height for PGFPlots, as the aspect ratio is unknown for these plots. \cmd{\NewEnviron} could be used to handle something like verbose in a tikzpicture, but at the moment, this is unsupported.
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@includetikz}{O{}m}{%
%    \end{macrocode}
	% Check the keys here already, as they are needed both to see if already externalized files fulfill their requirements and to handle unexternalized files.
%    \begin{macrocode}
	\pgfkeys{/tikzscale, #1}%
%    \end{macrocode}
	% Draw and scale the graphic if it can or should not be loaded from the externalized file.
%    \begin{macrocode}
	\tikzscale@ifDrawAndScale{%
		\tikzscale@includetikzUnexternalized[#1]{#2}%
	}{%
%    \end{macrocode}
		% Try to load a dpth file to get the sizes pgfexternalwidth and pgfexternalheight of the externalized graphic
%    \begin{macrocode}
		\ifboolexpr{test {\ifdef{\tikzexternalgetnextfilename}}}{%
			\tikzexternalgetnextfilename{\tikzscale@externalizationName}%
			\pgfexternalreaddpth{\tikzscale@externalizationName}%
%    \end{macrocode}
			% Check if the sizes are still correct, i.e. agree with the sizes of the externalized PDF graphic. The axis ratio does not have to be checked, as is is always correct. This is because if it gets changed, the width and the height get changed, too, and one of these changes gets detected.
%    \begin{macrocode}
			\ifdef{\requestedWidth}{%
				\ifdef{\pgfexternalwidth}{%
					\tikzscale@ifSizeDifference{\requestedWidth - \pgfexternalwidth}{%
						\tikzset{external/remake next}%
% 						\PackageWarning{tikzscale}{Regenerate \tikzscale@externalizationName \MessageBreak because of width difference}%
					}{}%
				}{%
					\tikzset{external/remake next}%
% 					\PackageWarning{tikzscale}{Regenerate \tikzscale@externalizationName \MessageBreak because of no external width}%
				}%
			}{}%
			\ifdef{\requestedHeight}{%
				\ifdef{\pgfexternalheight}{%
					\tikzscale@ifSizeDifference{\requestedHeight - \pgfexternalheight}{%
						\tikzset{external/remake next}%
% 						\PackageWarning{tikzscale}{Regenerate \tikzscale@externalizationName \MessageBreak because of height difference}%
					}{}%
				}{%
					\tikzset{external/remake next}%
% 					\PackageWarning{tikzscale}{Regenerate \tikzscale@externalizationName \MessageBreak because of no external height}%
				}%
			}{}%
			\ifdef{\requestedAxisRatio}{%
				\ifdef{\tikzscale@oldAxisRatio}{%
					\tikzscale@ifSizeDifference{\requestedAxisRatio - \tikzscale@oldAxisRatio}{%
						\tikzset{external/remake next}%
% 						\PackageWarning{tikzscale}{Regenerate \tikzscale@externalizationName \MessageBreak because of axis ratio difference}%
					}{}%
					\global\undef{\tikzscale@oldAxisRatio}%
				}{%
					\tikzset{external/remake next}%
% 					\PackageWarning{tikzscale}{Regenerate \tikzscale@externalizationName \MessageBreak because of no external axis ratio}%
				}%
			}{}%
		}{%
			\tikzset{external/remake next}%
			\PackageWarning{tikzscale}{Generate \tikzscale@externalizationName \MessageBreak because no file found}%
		}%
		\input{#2}%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@includetikzUnexternalized}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@includetikzUnexternalized}{O{}m}{%
	\elseif{test {\ifundef{\requestedWidth}} and test {\ifundef{\requestedHeight}} and test {\ifundef{\requestedAxisRatio}}}{%
%    \end{macrocode}
		% If no option is given, directly load the content, as nothing should get scaled.
%    \begin{macrocode}
		\input{#2}%
	}{test {\ifdef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{%
%    \end{macrocode}
		% If width and height are given, the content must be a pgfplot, so scale it. The plot currently only had approximately the given size without calling the resizeTo macro, due to a (known) bug in PGFPlots.
%    \begin{macrocode}
		\tikzscale@resizePlotTo{#2}%
	}{test {\ifdef{\requestedAxisRatio}}}{%
		\tikzscale@includeAxisRatio{#2}%
	}{test {\ifundef{\requestedAxisRatio}}}{%
%    \end{macrocode}
		% If only either width or height is given it can be a normal tikzpicture or a plot with axisratio=1. Let's guess that it is a plot with default axisratio. If the guess is wrong, the called function detects that scaling the plot does not work and automatically calls \cmd{\tikzscale@includeNormalTikzpicture}.
%    \begin{macrocode}
		\def\requestedAxisRatio{1}%
		\tikzscale@includeAxisRatio{#2}%
	}{%
		% Everything else results in an error.
		\tikzscale@invalidKeyError{#2}%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@preparePlot}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@preparePlot}{}{%
%    \end{macrocode}
	% Set a scaling factor or a width and height for the plot, which will be loaded. The \cmd{\tikzset} and \cmd{\pgfplotsset} commands have local scope. The internal redefinition of the style is correct, because if one tikzpicture includes another one, the scaling factor is reset so that it does not get \href{http://tex.stackexchange.com/questions/38605/scaling-a-tikz-figure-from-an-external-file}{squared} in the inner one. Note that if a user-defined style thus is ignored in this special case. The styles are defined here, so that files which are inputted without the \cmd{includegraphics} command are not affected.
%    \begin{macrocode}
	\pgfplotsset{every axis/.append style={width=\tikzscale@width,height=\tikzscale@height,every axis/.style={}}}%
}
\NewDocumentCommand{\tikzscale@prepareTikzpicture}{}{%
	\tikzset{every picture/.style={scale=\tikzscale@scale,every picture/.style={}}}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@includeNormalTikzpicture}
% \xcmd\tikzscale@includeNormalTikzpicture{file name}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@includeNormalTikzpicture}{m}{%
	\tikzscale@prepareTikzpicture
	\elseif{test {\ifdef{\requestedWidth}} and test {\ifundef{\requestedHeight}}}{%
		\def\requestedSize{\requestedWidth}%
		\tikzscale@scaleTikzpictureTo{\wd}{\input{#1}}{#1}%
	}{test {\ifundef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{%
		\def\requestedSize{\requestedHeight}%
		\tikzscale@scaleTikzpictureTo{\ht}{\input{#1}}{#1}%
	}{%
		\tikzscale@invalidKeyError{#1}%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@invalidKeyError}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@invalidKeyError}{m}{%
	\PackageError{tikzscale}{Invalid key for TikZ graphic}{Change key #1 into a valid key.}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@includeAxisRatio}
% \xcmd\tikzscale@includeAxisRatio{file name}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@includeAxisRatio}{m}{%
%    \end{macrocode}
	% Try to set initial sizes close to the requested sizes, to improve the optimization's speed.
%    \begin{macrocode}
	\pgfplotsset{every axis/.append style={scale only axis,every axis/.style={}}}%
	\elseif{test {\ifdef{\requestedWidth}} and test {\ifundef{\requestedHeight}}}{%
		\let\requestedSize\requestedWidth
		\def\tikzscale@width{\requestedWidth}%
		\pgfmathsetmacro{\tikzscale@height}{\requestedWidth / \requestedAxisRatio}%
		\tikzscale@resizePlotWithAxesRatioTo{\wd}{\tikzscale@width}{\input{#1}}{#1}%
	}{test {\ifundef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{%
		\let\requestedSize\requestedHeight
		\def\tikzscale@height{\requestedHeight}%
		\pgfmathsetmacro{\tikzscale@width}{\requestedHeight * \requestedAxisRatio}%
		\tikzscale@resizePlotWithAxesRatioTo{\ht}{\tikzscale@height}{\input{#1}}{#1}%
	}{%
		\tikzscale@invalidKeyError{#1}%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@scaleTikzpictureTo}
% \xcmd\scalteTo{\cmd{\wd} or \cmd{\ht}}{to-be-scaled content}{file name}
% The first argument determines if a specific width or a specific height should be achieved by scaling.
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@scaleTikzpictureTo}{mmm}{%
%    \end{macrocode}
	% Deactivate the externalization, as the measurements to determine the correct size should not be externalized.
%    \begin{macrocode}
	\tikzscale@conditionalDisableExternalization
%    \end{macrocode}
	% When scaling a tikzpicture, only the drawings are scaled, but nodes are not scaled. So in general, there are horizontal or vertical areas, where the picture contains only unscaled nodes, and areas where the picture contains scalable drawings. Mathematically all scaled and all unscaled areas can be combined, so that there is one are area of fixed size and one variable sized area. Thus scaling only by multiplication of a factor is incorrect in general. To do the correct scaling, the fixed area size must be known. As there are two unknown parameters, i.e. fixed area size and variable area size, the fixed area size can be calculated by measuring the tikzpicture with two different scalings. A special scaling factor is used, to get the size close to the final size minimizing numerical and logical errors.
%    \begin{macrocode}
	\def\tikzscale@scale{1}%
	\tikzscale@measureSize{\measuredFirst}{#1}{#2}%
	\pgfmathsetmacro{\tikzscale@scale}{\requestedSize/\measuredFirst}%
	\tikzscale@measureSize{\measuredSecond}{#1}{#2}%
%    \end{macrocode}
	% It can happen, that there are no variable areas. Furthermore, the original size could already fit. Avoid numerical problems in both cases by directly drawing the picture. Do not compare the float values directly, as TeX's precision is quite limited.
%    \begin{macrocode}
	\tikzscale@ifSizeDifference{\measuredSecond - \requestedSize}{%
%    \end{macrocode}
	% If a plot is not scalable (e.g. consisting of a node only), but is not correctly scaled, exit with an error.
%    \begin{macrocode}
		\tikzscale@ifSizeDifference{\measuredFirst - \measuredSecond}{%
		}{%
			\PackageError{tikzscale}{Requested to scale unscalable graphic}{Do not set width or height for graphic in\MessageBreak #3}%
		}%
%    \end{macrocode}
		% We know, that the variable sized area scales with the scaling factor, thus it holds
		% \cmd{\scale} * \cmd{\variableFirst} = \cmd{\variableSecond},
		% with \cmd{\variableFirst} = \cmd{\measuredFirst} - \cmd{\fixedSize}
		% and \cmd{\variableSecond} = \cmd{\measuredSecond} - \cmd{\fixedSize},
		% which can be solved by substituttion and results in
%    \begin{macrocode}
		\pgfmathsetmacro{\fixedSize}{(\tikzscale@scale*\measuredFirst - \measuredSecond) / (\tikzscale@scale - 1)}%
%    \end{macrocode}
		% Now, to get the correct scaling factor, only take the variable areas into account, as it holds
		% \cmd{\scaleFinal} = \cmd{\variableSizeFinal} / \cmd{\variableSizeOriginal}
		% with \cmd{\variableSizeFinal} = \cmd{\requestedSize} - \cmd{\fixedSize}
		% and \cmd{\variableSizeOriginal} = \cmd{\measuredFirst} - \cmd{\fixedSize},
		% which results in
%    \begin{macrocode}
		\pgfmathsetmacro{\tikzscale@scale}{(\requestedSize - \fixedSize) / (\measuredFirst - \fixedSize)}%
%    \end{macrocode}
		% Additionally or alternatively the brute force approach to iteratively improve the solution can be used.
%    \begin{macrocode}
		\foreach \l in {1,...,\maxTestIterations}{%
			\tikzscale@measureSize{\measuredIntermediate}{#1}{#2}%
%    \end{macrocode}
		% Optimize until the absolute difference is small enough, although the (relative) size ratios are used to calculate a new scaling factor.
%    \begin{macrocode}
			\tikzscale@ifSizeDifference{\measuredIntermediate-\requestedSize}{%
%    \end{macrocode}
				% First divide before multiply to avoid overflowing (at 16384).
%    \begin{macrocode}
				\pgfmathparsemacro{\errorRatio}{\measuredIntermediate/\requestedSize}%
				\pgfmathsetglobalmacro{\tikzscale@scale}{\tikzscale@scale/\errorRatio}%
			}{%
				\breakforeach%
			}%
		}%
%    \end{macrocode}
	% Externalize the graphic with the final size.
%    \begin{macrocode}
		\tikzscale@conditionalEnableExternalization{#3}%
%    \end{macrocode}
		% Finally, include the picture. Do it via a new measurement to be able to warn if it does not fit good enough.
%    \begin{macrocode}
		\tikzscale@measureSize{\measuredFinal}{#1}{#2}%
		\usebox{\tikzscale@measuredSize}%
		\tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{#3}%
	}{%
%    \end{macrocode}
	% Externalize the graphic with the final size.
%    \begin{macrocode}
		\tikzscale@conditionalEnableExternalization{#3}%
		#2%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@resizePlotTo}
%\xcmd\tikzscale@resizePlotTo{file name}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@resizePlotTo}{m}{%
	\def\fileName{#1}%
	\def\content{\input{#1}}%
	\tikzscale@preparePlot
	\def\tikzscale@width{\requestedWidth}%
	\def\tikzscale@height{\requestedHeight}%
%    \end{macrocode}
	% Deactivate the externalization, as the measurements to determine the correct size should not be externalized.
%    \begin{macrocode}
	\tikzscale@conditionalDisableExternalization
%    \end{macrocode}
	% Improve the solution iteratively until it is good enough.
%    \begin{macrocode}
	\foreach \l in {1,...,\maxTestIterations}{%
%    \end{macrocode}
		% Using the box allows measuring the width and height with one rendering run.
%    \begin{macrocode}
		\sbox{\tikzscale@measuredSize}{\content}%
%    \end{macrocode}
		% Determine the remaining error and check if it is larger than a threshold.
%    \begin{macrocode}
		\pgfmathsetmacro{\widthDifference}{\wd\tikzscale@measuredSize - \requestedWidth}%
		\pgfmathsetmacro{\heightDifference}{\ht\tikzscale@measuredSize - \requestedHeight}%
%    \end{macrocode}
		% Output error in current iterion for debugging.
%    \begin{macrocode}
% 		widthDifference: \widthDifference, heightDifference: \heightDifference\\% Debugging
%    \end{macrocode}
		% Check if the remaining error is larger than a threshold.
%    \begin{macrocode}
		\ifboolexpr{test {\tikzscale@ifSizeDifference{\widthDifference}} or test {\tikzscale@ifSizeDifference{\heightDifference}}}{%
%    \end{macrocode}
			% Correct the dimension by the error. Use a global assignment, as each iteration in the loop is put into a separate group.
%    \begin{macrocode}
			\pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@width - \widthDifference}%
			\pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@height - \heightDifference}%
		}{%
			\breakforeach
		}%
	}%
%    \end{macrocode}
	% Externalize the graphic with the final size.
%    \begin{macrocode}
	\tikzscale@conditionalEnableExternalization{\fileName}%
%    \end{macrocode}
	% Finally, include the picture. Do it via a new measurement to be able to warn if it does not fit good enough.
%    \begin{macrocode}
	\sbox{\tikzscale@measuredSize}{\content}%
	\usebox{\tikzscale@measuredSize}%
	\tikzscale@warnIfSizeDifference{\requestedWidth}{\wd\tikzscale@measuredSize}{\fileName's width}%
	\tikzscale@warnIfSizeDifference{\requestedHeight}{\ht\tikzscale@measuredSize}{\fileName's height}%
}
%    \end{macrocode}
%\end{macro}
%

%\begin{macro}{\tikzscale@resizePlotWithAxesRatioTo}
% \xcmd\tikzscale@resizePlotWithAxesRatioTo{\textbackslash wd or \textbackslash ht}{\textbackslash tikzscale@width or \textbackslash tikzscale@height}{to-be-scaled content}{file name}
% The first argument determines if a specific width or a specific height should be achieved by resizing.
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@resizePlotWithAxesRatioTo}{mmmm}{%
	\def\dimension{#1}%
	\def\variable{#2}%
	\def\content{#3}%
	\def\fileName{#4}%
	\gdef\tikzscale@oldSizeDifference{0pt}%
	\tikzscale@preparePlot
%    \end{macrocode}
	% Deactivate the externalization, as the measurements to determine the correct size should not be externalized.
%    \begin{macrocode}
	\tikzscale@conditionalDisableExternalization
%    \end{macrocode}
	% Improve the solution iteratively until it is good enough.
%    \begin{macrocode}
	\foreach \l in {1,...,\maxTestIterations}{%
		\tikzscale@measureSize{\measuredSize}{\dimension}{\content}%
%    \end{macrocode}
		% Determine the remaining error and check if it is larger than a threshold.
%    \begin{macrocode}
		\pgfmathsetmacro{\sizeDifference}{\measuredSize - \requestedSize}%
%    \end{macrocode}
		% Output error in current iterion for debugging.
%    \begin{macrocode}
% 		sizeDifference: \sizeDifference\\% Debugging
%    \end{macrocode}
		% Optimize if the absolute difference is too large.
%    \begin{macrocode}
		\tikzscale@ifSizeDifference{\sizeDifference}{%
			\ifdefstring{\dimension}{\wd}{%
				\pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@width - \sizeDifference}%
				\pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@width / \requestedAxisRatio}%
			}{%
				\pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@height - \sizeDifference}%
				\pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@height * \requestedAxisRatio}%
			}%
			\tikzscale@ifSizeDifference{\sizeDifference-\tikzscale@oldSizeDifference}{%
			}{%
				\tikzscale@includeNormalTikzpicture{#4}%
				\gdef\tikzscale@alreadyIncluded{true}%
				\breakforeach
			}%
			\pgfmathsetglobalmacro{\tikzscale@oldSizeDifference}{\sizeDifference}%
		}{%
			\breakforeach
		}%
	}%
	\ifdef{\tikzscale@alreadyIncluded}{%
		\global\undef\tikzscale@alreadyIncluded%
	}{%
%    \end{macrocode}
		% Externalize the graphic with the final size.
%    \begin{macrocode}
		\tikzscale@conditionalEnableExternalization{\fileName}%
%    \end{macrocode}
		% Finally, include the picture. Do it via a new measurement to be able to warn if it does not fit good enough.
%    \begin{macrocode}
		\tikzscale@measureSize{\measuredFinal}{\dimension}{\content}%
		\usebox{\tikzscale@measuredSize}%
		\tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{\fileName}%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@measuredSize}
%    \begin{macrocode}
\newsavebox{\tikzscale@measuredSize}
%    \end{macrocode}
% \xcmd\measureSize{result variable name}{\cmd{\wd} or \cmd{\ht}}{to-be-measured content}
%    \begin{macrocode}
\def\tikzscale@measureSize#1#2#3{%
	\sbox{\tikzscale@measuredSize}{#3}%
	\pgfmathsetmacro{#1}{#2\tikzscale@measuredSize}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@ifSizeDifference}
% \xcmd\tikzscale@ifSizeDifference{size}{executed if true}{executed if false}
%    \begin{macrocode}
\def\tikzscale@ifSizeDifference#1#2#3{%
	\pgfmathparse{abs(#1)}%
	\ifdimgreater{\pgfmathresult pt}{\tikzscale@accuracy}{%
		#2%
	}{%
		#3%
	}%
}%
%    \end{macrocode}
%\end{macro}


%\begin{macro}{\tikzscale@measuredSize}
% \xcmd\tikzscale@warnIfSizeDifference{firstSize}{secondSize}{file name}
%    \begin{macrocode}
\def\tikzscale@warnIfSizeDifference#1#2#3{%
	\tikzscale@ifSizeDifference{#1-#2}{%
		\PackageWarning{tikzscale}{Scaling of #3 was only\MessageBreak accurate to \pgfmathresult pt}%
	}{}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@conditionalDisableExternalization}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@conditionalDisableExternalization}{}{%
	\tikzexternaldisable
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@conditionalEnableExternalization}
% Activate externalization of TikZ graphics iff it had been active before definitely disabling it for measurement purposes. The argument contains the file name.
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@conditionalEnableExternalization}{m}{%
%    \end{macrocode}
	% For the externalization, set correct file name and only externalize the graphic with the final size. This produces a \href{http://old.nabble.com/minor-comment-about-TikZ-external-library-usage-tt31042245.html#a31042245}{known bug}
%    \begin{macrocode}
% 	\tikzsetnextfilename{#1}%
%    \end{macrocode}
	% Get the current directory as a string and use it as an prefix, so that the graphic's PDF is generated in a subdirectory if the tikz file is located in a subdirectory, too. This is necessary, as the PDF file is searched for in the subdirectory in this case. This might be unnecessary due to the newly created path lookup logic.
%    \begin{macrocode}
% 	\expandafter\tikzsetexternalprefix\expandafter{\tikzscale@pwd}%
% 	\expandnext{\tikzsetexternalprefix}{\tikzscale@pwd}%
	\tikzexternalenable
}
%    \end{macrocode}
%\end{macro}
%
% \makeatother
% \iffalse
%</package>
% \fi
%
%\StopEventually{^^A
%  \PrintChanges
%^^A  \PrintIndex
%}
%
%\Finale