% \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}

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}[2013/05/22 v0.2.6 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}
%\changes{v0.1.3}{2012/11/02}{Fix error when loading TikZ but not PGFPlots; Fix error when scaling complicated TikZ graphics}
%\changes{v0.2}{2013/01/02}{Add default axis ratio; save axis ratio; support \cmd{\externaldisable} and \cmd{\externalenable}; improve robustness; increase speed}
%\changes{v0.2.1}{2013/01/09}{Fix graphics with data from text file due to wrong line breaks; fix plain old \LaTeX support}
%\changes{v0.2.2}{2013/01/13}{Fully support plain (dvi) \LaTeX; mind graphicspath}
%\changes{v0.2.3}{2013/01/23}{Fix graphicspath bug; fix endlinechar bug, improve compatibility with beamer}
%\changes{v0.2.4}{2013/03/10}{Fix another graphicspath bug; support Beamer's \cmd{\pause} command}
%\changes{v0.2.5}{2013/03/30}{Do not accidently activate externalization}
%\changes{v0.2.6}{2013/05/22}{Fix whitespace issue; correctly scale with externalization even if it scales incorrectly}
% \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} command 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: Both width and 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. Otherwise a default axis ratio is assumed (see section \ref{axisRatio}).\\
% So,\nopagebreak
%
% \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}\label{axisRatio}
% 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 such a 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. If \texttt{axisratio} is omitted, i.e. only either height or width are given, it is assumed to be \texttt{1}.
% \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 optionally 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}
%
% \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.
%
% 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.
%
% Using \cmd{\tikzexternalenable} or \cmd{\tikzexternaldisable} inside of a tikzpicture leads to undefined behaviour when using tikzscale. It's not clear, what the correct behaviour should be and what the externalization library does withouth tikzscale.
%
% Note, that there was a \href{http://tex.stackexchange.com/a/88158/7323}{bug} in the externalization library, which has been fixed on 25th of December in 2012, so you might want to use a more recent version of TikZ or PGFPlots.
%
% \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.
%
% \subsection{Fitting library}
% Due to a \href{http://sourceforge.net/tracker/index.php?func=detail&aid=2991312&group_id=142562&atid=752792}{known bug in the fitting library}, nodes with a \texttt{fit} option also need a \texttt{transform shape} option in order to be scalable. If they are not scalable, they normally do not contain the nodes as specified when tikzscale is used.
%
% \section{Further Ideas}
% \begin{itemize}
%	\item With careful considerations, it should be possible to reduce the average number of needed figure renderings, which should speed-up runs with very complicated figures.
%	\item Add the external file optimization loop to axis ratio and pgfplots, as well.
%	\item It might be a good idea to use the file names as figure names, but probably only if the name was not already set by the user. Additionally, there must be taken care to not try to write into a directory where there is no write access (e.g. reading a graphic from a system wide TeX installation)
%	\item if graphic files are located in a subdirectory, the externalized files should also be in that subdirectory.
%	\item allow in-file graphics by redefining the tikzpicture environment and accepting tikzscale and tikz options. The tikzscale options are evaluated using key filtering (tikz library) and the tikz options are forwarded.
%	\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. The purpose is similar to the externalization library.
%	\item Using something like [x=5pt] as an argument to the axis environment, e.g. to scale the units in bar plots, is problematic, as tikzscale changes the behaviour, i.e. stops the scaling.
% \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.
%             \item Fixed problems in the externalization library when used with tikzscale.
%         \end{itemize}
%     \item David Carlisle
%         \begin{itemize}
%             \item Created the \cmd{\xcmd} macro for this package, which is used in the documentation.
%         \end{itemize}
%     \item Prof. Kai Arzheimer
%         \begin{itemize}
%             \item Reported a bug when not using TikZ without PGFPlots, which lead to a fix.
%             \item Reported a bug that a non-existent macro is used, which lead to a fix.
%         \end{itemize}
%     \item devendra
%         \begin{itemize}
%             \item Reported bugs when using the externalization library together with tikzscale, which lead to a fix.
%             \item Reported a problem when using data files, which lead to a fix regarding \cmd{\endlinechar}.
%         \end{itemize}
%     \item Mohammad Reza Keshtkaran
%         \begin{itemize}
%             \item Reported a bug when using plain old \LaTeX with an eps file, which lead to a fix.
%             \item Reported another bug when using plain old \LaTeX, which lead to some rework to fully support \LaTeX without additional code.
%             \item Reported a bug when using \cmd{\graphicspath}, which lead to a fix.
%             \item Reported the bug when using \cmd{\graphicspath} again, which lead to a correct fix even if currfile is not used.
%         \end{itemize}
%     \item Andreas Tharang
%         \begin{itemize}
%             \item Reported that the beamer class is incompatible with tikzscale, which lead to a change in tikzscale to fix this incompatibility.
%             \item Reported that the fitting library is incompatible with tikzscale due to a bug in the fitting library, which lead to a note in the documentation.
%             \item Created tests to improve the compatibility between beamer and tikzscale, which lead to support of Beamer's \cmd{\pause} command.
%         \end{itemize}
%     \item Klaus Pribil
%         \begin{itemize}
%             \item Reported an incompatibility with the pdfpages package, which lead to a fix in tikzscale.
%         \end{itemize}
%     \item Christoph Schmidpeter
%         \begin{itemize}
%             \item Reported a problem when accidently adding a superfluous space into the graphics path, which lead to a detection and fix of that case in tikzscale.
%         \end{itemize}
%     \item Jose Hissa Ferreira
%         \begin{itemize}
%             \item Reported a bug when using a graphics path with multiple path entries, which lead to a fix.
%         \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
%
% 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}{\ifTikzLibraryLoaded}
% This is a general command, which might be useful for inclusion into the tikz package. This is taken from \href{http://tex.stackexchange.com/a/48472}{stackexchange} and simplified.
%    \begin{macrocode}
\def\ifTikzLibraryLoaded#1#2#3{%
	\ifcsdef{tikz@library@#1@loaded}{%
		#2%
	}{%
		#3%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\ifExternalizationLoaded}
%    \begin{macrocode}
\def\ifExternalizationLoaded#1#2{%
	\ifTikzLibraryLoaded{external}{#1}{#2}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\ifedefequal}
% \xcmd\ifedefequal{first expression}{second expression}{true}{false}
% This is a general command, which might be useful for inclusion into the etoolbox package. It executes the true code if both expressions expand to the same and otherwise the false code. This test is often wanted, as the order of the arguments is not important and it does not matter if the user saves a value in another macro without expanding it.
%    \begin{macrocode}
\def\ifedefequal#1#2{%
	\edef\etoolbox@ifedefequal@first{#1}%
	\edef\etoolbox@ifedefequal@second{#2}%
	\ifdefequal{\etoolbox@ifedefequal@first}{\etoolbox@ifedefequal@second}%
}
%    \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}
% This 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}{\forgrouplist}
% This is a general command, which might be useful for inclusion into the etoolbox package. It works similar to \cmd{\forcsvlist} but uses TeX groups to separate elements instead of a comma separated list.
%    \begin{macrocode}
\def\forgrouplist#1#2{%
%    \end{macrocode}
	% Use \cmd{\grouplistbreak} instead of \cmd{\forcsvlist}'s \cmd{\listbreak}, because the function given in the first argument can contain a call to \forcsvlist. In this case \cmd{\listbreak} is executed, although no break has been called, which lead to an error in the program, if \cmd{\listbreak} were used.
%    \begin{macrocode}
	\def\grouplistbreak{\def\breakFor{}}%
	\tikzscale@forGroupListElement{#1}#2\tikzscale@endList%
%    \end{macrocode}
	% Delete \cmd{\breakFor} in case it has been set.
%    \begin{macrocode}
	\undef\breakFor
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@forGroupListElement}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@forGroupListElement}{mgu{\tikzscale@endList}}{%
%    \end{macrocode}
	% Only do list processing if \cmd{\listbreak} has not been called.
%    \begin{macrocode}
	\ifundef{\breakFor}{%
		\IfValueTF{#2}{%
			#1{#2}%
			\tikzscale@forGroupListElement{#1}#3\tikzscale@endList%
		}{%
			#1{#3}%
		}%
	}{}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\eforgrouplist}
% This is a general command, which might be useful for inclusion into the etoolbox package. It works similar to \cmd{\forgrouplist} 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\eforgrouplist#1#2{%
	\edef\tikzscale@grouplist{#2}%
	\expandafter\forgrouplist\expandafter{\expandafter#1\expandafter}\expandafter{\tikzscale@grouplist}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@trim}
% These is a general command to trim leading and trailing spaces, which might be useful for inclusion into another package taken from the following \href{http://www.matijs.net/blog/2006/07/20/how-to-trim-spaces-in-tex}{homepage}.
%    \begin{macrocode}
\def\tikzscale@trim#1{%
	\ignorespaces#1\unskip
}%
%    \end{macrocode}
%\end{macro}
%
% \begin{macro}{\tikzscale@trimMacro}
	% A possible present leading or trailing space in the macro's content is removed from the macro.
%    \begin{macrocode}
\def\tikzscale@trimMacro#1{%
	\expandafter\IfBeginWith\expandafter{#1}{ }{%
		\expandafter\StrGobbleLeft\expandafter{#1}{1}[#1]%
	}{}%
	\expandafter\IfEndWith\expandafter{#1}{ }{%
		\expandafter\StrGobbleRight\expandafter{#1}{1}[#1]%
	}{}%
}
%    \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{macro}
%
%\begin{macro}
	% The check \cmd{\tikzifexternalizehasbeencalled} from file tikzexternalshared.code.tex is not exactly what is needed in tikzscale, as it always stays true after it has been set by \cmd{\tikzexternalize}. Instead, add a check whether externalization is active and set it to false if externalization has not been loaded for simplification. Thus, whether externalization is active can be checked without checking if it has been loaded at all. The initial state of \cmd{\tikzscale@externalizationActive} is not trivially known, as an arbitrary combination and order of \cmd{\tikzexternaldisable} and \cmd{\tikzexternalenable} commands could have been used before the end of the preamble. In the long run, tikzexternalshared.code.tex should offer that check. Until then, we can check whether \cmd{\tikz}=\cmd{\tikzexternal@origtikz} (i.e. externalization disabled) or \cmd{\tikz}=\cmd{\tikzexternal@tikz@replacement} (i.e. externalization enabled) holds (if neither of both holds, this indicates a problem, e.g. another package redefining the command). This is, of course, only needed if the externalization library has been loaded at all. Please note, the implementation of this check as a macro is possible, because tikzscale redefines tikzpicture, whereas the externalization library redefines tikz, so there is no conflict.
%    \begin{macrocode}
\def\tikzscale@ifExternalizationActive#1#2{%
	\ifExternalizationLoaded{%
		\ifdefequal{\tikz}{\tikzexternal@tikz@replacement}{%
			#1%
		}{%
			\ifdefequal{\tikz}{\tikzexternal@origtikz}{%
			}{%
				\PackageWarning{tikzscale}{Status of externalization is unknown, thus I assume it is deactivated.}%
			}%
%    \end{macrocode}
			% It's important, that this code is below the above code, as the below code can change the meaning of \cmd{\tikz} through side effects.
%    \begin{macrocode}
			#2%
		}%
	}{%
		#2%
	}%
}%
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@debug}
% Activate and deactivate debugging by commenting and uncommenting the folowing code.
%    \begin{macrocode}
% \def\tikzscale@debug#1{%
% 	\PackageWarning{tikzscale}{#1}%
% }
\def\tikzscale@debug#1{}%
%    \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}
%    Save the \cmd{\includegraphics} \href{ftp://ftp.tu-chemnitz.de/pub/tex/macros/latex/required/graphics/grfguide.pdf}{command}.
%    \begin{macrocode}
	\LetLtxMacro{\tikzscale@oldincludegraphics}{\includegraphics}%
%    \end{macrocode}
	% Activate the enhanced includegraphics command at end of preamble, so that no other package is interfering (besides on purpose).
%    \begin{macrocode}
	\tikzscale@useEnhancedIncludegraphics
%    \end{macrocode}
	% Also patch tikzpicture environment to temporarily deactivate the enhanced includegraphics command inside the tikzpicture environment in case the tikzpicture environment is called directly (without includegraphics being called) and loading another graphic (like a PNG file inside of a pgfplot).
%    \begin{macrocode}
	\tikzscale@patchTikzpictureIncludegraphics
%    \end{macrocode}
	% As \cmd{\endtikzpicture} does not seem to be redefined, patch it here (once) to activate tikzscale's \cmd{\includegraphics} again. This is probably not necessary, but might be handy if there are two tikzpicture environments in one includegraphics environment.
%    \begin{macrocode}
	\tikzscale@patchEndtikzpictureIncludegraphics
%    \end{macrocode}
%    \begin{macrocode}
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzexternal}
%    \begin{macrocode}
\AtEndPreamble{%
%    \end{macrocode}
	% Activate the output of the graphics sizes into the dpth files (one file per graphic) if externalization might be used (known at the end of preamble). 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}
	\ifExternalizationLoaded{%
		\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}{}%
%    \end{macrocode}
%
%    \begin{macrocode}
	\@ifpackageloaded{tikz}{%
%    \end{macrocode}
		% Set a minimum accuracy tikzscale tries to achieve. TeX's accuracy is limited, thus, e.g. 0.04 pt, cannot always be achieved independent of the number of iterations. Use the \href{http://en.wikibooks.org/wiki/TeX/hfuzz}{value} (0.1 pt in an experiment) which is used for overfull paragraph warnings, too.
%    \begin{macrocode}
		\newlength{\tikzscale@accuracy}%
		\setlength{\tikzscale@accuracy}{\hfuzz}%
%    \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}
%	If the externalization library has been loaded, prepare it for use together with tikzscale.
%    \begin{macrocode}
	\ifExternalizationLoaded{%
%    \end{macrocode}
%	\cmd{\tikzexternaldisable} and \cmd{\tikzexternalenable} normally unintentionally deactivate the tikzscale commands (as they restore the original TikZ commands), so let them restore the tikzscale commands instead. The idea is to get the tikzscale's includegraphics command being called and then redefine tikzpicture and do the rest of the work there. Do the patching always when \cmd{\tikzexternaldisable} or \cmd{\tikzexternalenable} is called, as the patching should also be done when \cmd{\includegraphics} is not used, but \cmd{\tikzpicture} is called directly.
%    \begin{macrocode}
		\apptocmd{\tikzexternaldisable}{%
			\tikzscale@useEnhancedIncludegraphics
			\tikzscale@patchTikzpictureIncludegraphics
		}{}{\PackageError{tikzscale}{Patching tikzexternaldisable failed}}%
		%
		\apptocmd{\tikzexternalenable}{%
			\tikzscale@useEnhancedIncludegraphics
			\tikzscale@patchTikzpictureIncludegraphics
		}{}{\PackageError{tikzscale}{Patching tikzexternalenable failed}}%
%    \end{macrodode}
		% Patch the externalization command to also save the axis ratio if given. Unfortunately, \cmd{\apptocmd} cannot be used, as patching fails due to "nested patching command and parameters in patch", thus, manual patching is in order.
%    \begin{macrocode}
		\LetLtxMacro{\tikzscale@externalend@storeshifts}{\pgf@externalend@storeshifts}%
		\def\pgf@externalend@storeshifts#1{%
			\tikzscale@externalend@storeshifts{#1}%
			\ifpgfexternal@info
%    \end{macrocode}
				% Write the axis ratio into the dpth file into variable \cmd{\tikzscale@oldAxisRatio}. The existence of the variable in the dpth file indicates if the axis ratio has been given in the last run.
%    \begin{macrocode}
				\tikzscale@writeToDpth{#1}{\tikzscale@oldAxisRatio}{\requestedAxisRatio}%
			\fi
		}%
	}{}%
%    \end{macrocode}
%    \begin{macrocode}
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@writeToDpth}
% Write the value of the third argument, if it exists, into the dpth file (write handle in the first argument) of the current figure, accessible by the second argument. The second and the third argument must contain macro names (including the backslash).
%    \begin{macrocode}
\def\tikzscale@writeToDpth#1#2#3{%
	\ifdef{#3}{%
%    \end{macrocode}
		% This is copied from the macro \cmd{\pgf@externalend@storeshifts} from file pgfcoreexternal.code.tex.
%    \begin{macrocode}
		\immediate\write#1{\noexpand\pgfexternal@restore{\noexpand\def\noexpand#2{#3}}}%
	}{}%
}
%    \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}
		% Do the patching of endlinechar and tikzpicture here, as tikzpicture should not be changed if not called via the new \cmd{\includegraphics} command.
%    \begin{macrocode}
		\tikzscale@FixEndLine
%    \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}%
		}{%
%    \end{macrocode}
		% Restore \cmd{\endlinechar} before calling code from other packages. This is not only cleaner, but really avoids an error when using the plain old latex (with dvi output) with an eps graphic.
%    \begin{macrocode}
			\tikzscale@restoreEndLineChar
			\tikzscale@oldincludegraphics[#1]{\tikzscale@fileName}%
		}%
	\endgroup
}%
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@useEnhancedIncludegraphics}
	% Replace the \cmd{\includegraphics} command by tikzscale's more generic command, to provide a consistent user interface.
%    \begin{macrocode}
\def\tikzscale@useEnhancedIncludegraphics{%
	\LetLtxMacro{\includegraphics}{\tikzscale@includegraphics}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@FixEndLine}
% tikzpicture environment gets redefined:
% - without external library: only inside tikzscale (once)
% - with external library: additionally, whenever \cmd{\externalenable} or \cmd{\externaldisable} is called (\cmd{\tikzpiture} and \cmd{\endtikzpicture})
% Use cases to patch tikzpicture:
% - to use tikzscale's includegraphics
% - to restore end of line character
% Constraints:
% - The patches have to be applied at the beginning of \cmd{\tikzpicture} and the end of \cmd{\endtikzpicture}, as \cmd{\tikzpicture} might not be executed completely when using external, as then the content of the tizkpicture environment is not executed at all.
% - The patches should not accumulate
% - A group might make sense to have a local scope
%    \begin{macrocode}
\def\tikzscale@FixEndLine{%
%    \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 a trailing space character. To get rid of all space character issues, just solve the problem here once an for all. Note, that the redefinition of \cmd{\endlinechar} is local to the current group, so it does not have to be restored at the end of the group.
%    \begin{macrocode}
	\edef\tikzscale@restoreEndLineChar{\endlinechar=\the\endlinechar\relax}%
	\endlinechar=-1%
%    \end{macrocode}
	% Restore the \cmd{\endlinechar} during the execution of the tikzpicture environment. This is necessary, for example, if data is read from a table and the data entries are separated by newline characters. Not restoring the \cmd{\endlinechar} would \href{http://tex.stackexchange.com/q/89053/7323}{distort the data}. Use \cmd{\apptocmd} to call the command inside the group opened by tikzpicture. Thus, nothing has to be done in \cmd{\endtikzpicture} regarding \cmd{\endlinechar}.
%    \begin{macrocode}
	\tikzscale@addRestoreEndLineCharToTikzpicture
	%
	\apptocmd{\endtikzpicture}{%
		\endlinechar=-1%
	}{}{\PackageError{tikzscale}{Patching endtikzpicture failed}}%
}%
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@addRestoreEndLineCharToTikzpicture}
%    \begin{macrocode}
\def\tikzscale@addRestoreEndLineCharToTikzpicture{%
	\pretocmd{\tikzpicture}{%
		\tikzscale@restoreEndLineChar
	}{}{\PackageError{tikzscale}{Patching tikzpicture failed}}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@patchTikzpictureIncludegraphics}
%    \begin{macrocode}
\def\tikzscale@patchTikzpictureIncludegraphics{%
	% 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. Using \cmd{\apptocmd} to do a local definition inside of the group started by \cmd{\tikzpicture} does not work. The \cmd{\includegraphics} command really has to be deactivated here, as a tikzpicture including a PNG file might be called directly without calling includegraphics.
%    \begin{macrocode}
	\pretocmd{\tikzpicture}{%
		\LetLtxMacro{\includegraphics}{\tikzscale@oldincludegraphics}%
	}{}{\PackageError{tikzscale}{Patching tikzpicture failed}}%
	%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@patchEndtikzpictureIncludegraphics}
%    \begin{macrocode}
\def\tikzscale@patchEndtikzpictureIncludegraphics{%
	\apptocmd{\endtikzpicture}{%
		\LetLtxMacro{\includegraphics}{\tikzscale@includegraphics}%
	}{}{\PackageError{tikzscale}{Patching endtikzpicture failed}}%
}
%    \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}{%
%    \end{macrocode}
				% Use \cmd{\csedef} instead of \cmd{\csdef} here, to be completely sure to only have a string left. This avoids problems when using tikzscale together with the pdfpages package and should generally be the right thing.
%    \begin{macrocode}
				\csedef{#1}{##1#2####1}%
%    \end{macrocode}
				% Break the inner (\cmd{\forcsvlist}) loop over file extensions.
%    \begin{macrocode}
				\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}%
		\ifcsdef{#1}{%
%    \end{macrocode}
			% Break the outer (\cmd{\forgrouplist}) loop over directories.
%    \begin{macrocode}
			\grouplistbreak
		}{}%
	}%
%    \end{macrocode}
	% Set the graphics path, to also find graphics in the last (current) input directory or in completely separate paths. Set it here to get updates if the user uses the \cmd{graphicspath} command inside of the document body.
%    \begin{macrocode}
	\tikzscale@setGraphicsPath
	\eforgrouplist{\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@setGraphicsPath}
	% 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}
\NewDocumentCommand{\tikzscale@setGraphicsPath}{}{%
%    \end{macrocode}
	% Remove possible leading or trailing spaces in the graphics path, as they lead to ugly string output before printing the graphic. Inserting such a space in the graphics path is a user's error, but it can happen easily as not all users are aware of TeX's newline issues. Fix the original path variable and not only tikzscale's variable, as this seems to be a general problem.
%    \begin{macrocode}
	\ifdef{\Ginput@path}{%
		\tikzscale@trimMacro{\Ginput@path}%
	}{}%
	\ifdef{\currfiledir}{%
		\ifdef{\Ginput@path}{%
			\def\tikzscale@graphicspath{{\currfiledir}\Ginput@path{}}%
		}{%
			\def\tikzscale@graphicspath{{\currfiledir}{}}%
		}%
	}{%
		\ifdef{\Ginput@path}{%
			\def\tikzscale@graphicspath{\Ginput@path{}}%
		}{%
			\def\tikzscale@graphicspath{{}}%
		}%
	}%
}%
%    \end{macrocode}
%\end{macro}
%
% \begin{macro}{\tikzscale@isTikzFile}
% 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. The used code is the same as the uncommented code, but also \href{http://tex.stackexchange.com/q/94781/7323}{compatible with class beamer}.
%    \begin{macrocode}
% \NewDocumentCommand{\tikzscale@includetikz}{O{}m}{%
\newcommand{\tikzscale@includetikz}[2][]{%
%    \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}
	% Check if the current graphic should be either drawn and scaled or simply included. As externalization can get activated or deactivated at any time (if the library has been loaded in the preamble), check in every call what to do.
%    \begin{macrocode}
	\tikzscale@ifExternalizationActive{%
%    \end{macrocode}
		% If externalization library has been loaded and is active, draw and scale the graphic if it is to be externalized.
%    \begin{macrocode}
		\tikzifexternalizingnext{%
			\tikzscale@debug{Externalizing the file #2}%
			\tikzscale@includetikzUnexternalized{#2}%
		}{%
			\tikzscale@includetikzWithExternalization{#2}%
		}%
	}{%
%    \end{macrocode}
		% Always draw and scale the graphic if externalization library has not been loaded or is deactivated.
%    \begin{macrocode}
		\tikzscale@includetikzUnexternalized{#2}%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@includetikzUnexternalized}
%    \begin{macrocode}
\def\tikzscale@includetikzUnexternalized#1{%
	\elseif{test {\ifundef{\requestedWidth}} and test {\ifundef{\requestedHeight}} and test {\ifundef{\requestedAxisRatio}}}{%
		\tikzscale@debug{no option given}%
%    \end{macrocode}
		% If no option is given, directly load the content, as nothing should get scaled.
%    \begin{macrocode}
		\tikzscale@trim{\input{#1}}%
	}{test {\ifdef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{%
		\tikzscale@debug{width and height given}%
%    \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{#1}%
	}{test {\ifdef{\requestedAxisRatio}}}{%
		\tikzscale@debug{axis ratio given}%
		\tikzscale@includeAxisRatio{#1}%
	}{test {\ifundef{\requestedAxisRatio}}}{%
		\tikzscale@debug{width or height given}%
%    \end{macrocode}
		% Use this test as a check if PGFPlots has been loaded.
%    \begin{macrocode}
		\ifdef{\pgfplotsset}{%
%    \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{#1}%
		}{%
			\tikzscale@debug{no pgfplots loaded}%
%    \end{macrocode}
			% If PGFPlots has not been loaded, it can only be a TikZPicture.
%    \begin{macrocode}
			\tikzscale@includeNormalTikzpicture{#1}%
		}%
	}{%
		% Everything else results in an error.
		\tikzscale@invalidKeyError{#1}%
	}%
}
%    \end{macrocode}
%\end{macro}

% \begin{macro}{\tikzscale@includetikzWithExternalization}
% This macro includes a tikzpicture file using the externalization library. As a precondition, the externalization must be loaded and active.
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@includetikzWithExternalization}{m}{%
%    \end{macrocode}
	% Try to load a dpth file to get the sizes pgfexternalwidth and pgfexternalheight as well as tikzscale@oldAxisRatio of the externalized graphic.
%    \begin{macrocode}
	\tikzexternalgetnextfilename{\tikzscale@externalizationName}%
	\pgfexternalreaddpth{\tikzscale@externalizationName}%
%    \end{macrocode}
	% Check if the next figure has to be remade. If no dpth file exist, it need not and must not be remade, as otherwise no md5-file is generated and thus one extra compilation run is necessary.
%    \begin{macrocode}
	\IfFileExists{\tikzscale@externalizationName.dpth}{%
		\tikzscale@checkRequestedSizeChanges
	}{}%
%    \end{macrocode}
	% The figure is either remade or the PDF is included with the following call. The former is correct if the file has been changed, the latter is correct if the last run was correct and nothing has changed since then. The only case left is if the figure has neither changed, nor is the exported size correct, as the file is then regenerated repeating the same error as last time. To change something, the new externalisation would need the result of the old externalization, but as the externalization is processed in a separate LaTeX process, it's non-trivial to push the information there.
%    \begin{macrocode}
	\tikzscale@trim{\input{#1}}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@checkRequestedSizeChanges}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@checkRequestedSizeChanges}{}{%
%    \end{macrocode}
	% Check if the sizes are still correct, i.e. agree with the sizes of the externalized PDF graphic. The saved axis ratio from the last run is checked, too, as it might have been changed by the user between the last run and the current run.
%    \begin{macrocode}
	\ifdef{\requestedWidth}{%
		\ifdef{\pgfexternalwidth}{%
			\tikzscale@ifSizeDifference{\requestedWidth - \pgfexternalwidth}{%
				\tikzset{external/remake next}%
				\tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of width difference \MessageBreak (requestedWidth=\requestedWidth, pgfexternalwidth=\pgfexternalwidth)}%
% 				\tikzscale@warnIfSizeDifference{\requestedWidth}{\pgfexternalwidth}{current file}%
%    \end{macrocode}
				% It would be possible to calculate a new size, if the old size did not fit, but this information would be needed to push into the externalization process, what is hard. The idea were requested = requested + measuredOld - fileSize.
%    \begin{macrocode}
			}{}%
		}{%
			\tikzset{external/remake next}%
			\tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of no external width}%
		}%
	}{}%
	\ifdef{\requestedHeight}{%
		\ifdef{\pgfexternalheight}{%
			\tikzscale@ifSizeDifference{\requestedHeight - \pgfexternalheight}{%
				\tikzset{external/remake next}%
				\tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of height difference \MessageBreak (requestedHeight=\requestedHeight, pgfexternalheight=\pgfexternalheight)}%
			}{}%
		}{%
			\tikzset{external/remake next}%
			\tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of no external height}%
		}%
	}{}%
	\ifdef{\requestedAxisRatio}{%
		\ifdef{\tikzscale@oldAxisRatio}{%
			\tikzscale@ifSizeDifference{\requestedAxisRatio - \tikzscale@oldAxisRatio}{%
				\tikzset{external/remake next}%
				\tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of axis ratio difference \MessageBreak (requestedAxisRatio=\requestedAxisRatio, oldAxisRatio=\tikzscale@oldAxisRatio)}%
			}{}%
			\undef{\tikzscale@oldAxisRatio}%
		}{%
			\tikzset{external/remake next}%
			\tikzscale@debug{Regenerate \tikzscale@externalizationName \MessageBreak because of no external axis ratio}%
		}%
	}{}%
}
%    \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}{\tikzscale@trim{\input{#1}}}{#1}%
	}{test {\ifundef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{%
		\def\requestedSize{\requestedHeight}%
		\tikzscale@scaleTikzpictureTo{\ht}{\tikzscale@trim{\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}{\tikzscale@trim{\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}{\tikzscale@trim{\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}{%
	\tikzscale@debug{At beginning scale, requestedWidth=\requestedWidth}%
%    \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}
				\pgfmathsetmacro{\errorRatio}{\measuredIntermediate/\requestedSize}%
				\tikzscale@debug{errorRatio=\errorRatio\MessageBreak for #3}%
				\pgfmathsetglobalmacro{\tikzscale@scale}{\tikzscale@scale/\errorRatio}%
			}{%
				\breakforeach%
			}%
		}%
%    \end{macrocode}
		 % 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.
%    \begin{macrocode}
		\tikzscale@measureSize{\measuredFinal}{#1}{#2}%
		\tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{#3}%
		\tikzscale@testGraphicFileForRetry{#1}{#2}{#3}{\measuredFinal}%
	}{%
		\tikzscale@testGraphicFileForRetry{#1}{#2}{#3}{\measuredSecond}%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@testGraphicFileForRetry}
% The macro tests the size of the generated graphic file. If the size is not as the requested size, the optimization is redone with an adapted optimization criterion to compensate the error done in the last run.
%
% \xcmd\tikzscale@testGraphicFileForRetry{\cmd{\wd} or \cmd{\ht}}{to-be-scaled content}{file name}{last measurement}
%
%    \begin{macrocode}
\def\tikzscale@testGraphicFileForRetry#1#2#3#4{%
%    \end{macrocode}
	% Reactivate externalization to prepare the figure to be really rendered.
%    \begin{macrocode}
	\tikzscale@conditionalEnableExternalization{#3}%
%    \end{macrocode}
	% Before externalizing the file, save the current externalization file name to be possibly used later. This code must be run after the reactivation of the externalization and before the rendering itself.
%    \begin{macrocode}
	\tikzscale@ifExternalizationActive{%
		\tikzexternalgetnextfilename{\tikzscale@externalizationName}%
	}{}%
%    \end{macrocode}
	% Finally, externalize and include the graphic with the final size. The graphic must not be included by reusing the measuredSize-box, as this leads to a subtly wrong behaviour when generating the PDF files. It can be observed by running the test suite with externalization and checking that no file is regenerated in the second run. Reusing the box can also lead to compile errors, if the problematic graphic is the last graphic in the document.
%    \begin{macrocode}
	#2%
%    \end{macrocode}
	% If the externalization is active, it might happen, that a figure was scaled correctly according to the request, but the externalized figure has a different size. The reason for that behaviour is unknown. The only chance we have is to calculate the error, rewind everything and try again with the compensated error.
%    \begin{macrocode}
	\tikzscale@ifExternalizationActive{%
%    \end{macrocode}
		% Read the size of the saved PDF file and save it on \cmd{\pgfexternalsize}.
%    \begin{macrocode}
		\pgfexternalreaddpth{\tikzscale@externalizationName}%
		\tikzscale@ifIsWidth{#1}{%
			\edef\pgfexternalsize{\pgfexternalwidth}%
		}{%
			\edef\pgfexternalsize{\pgfexternalheight}%
		}%
%    \end{macrocode}
		% Save the \cmd{\requestedSize} from the user in \cmd{\originalRequestedSize}, as the former value has to be overwritten for the next try. Do not use \cmd{\def}, as that would later be expanded to the current and not to the original value.
%    \begin{macrocode}
		\ifundef{\originalRequestedSize}{%
			\let\originalRequestedSize\requestedSize
		}{}%
		\tikzscale@debug{originalRequestedSize=\originalRequestedSize, requestedSize=\requestedSize, pgfexternalsize=\pgfexternalsize}%
%    \end{macrocode}
		% If a size difference is left, compensate it, decrease the counter and try the scaling again.
%    \begin{macrocode}
		\tikzscale@ifSizeDifference{\originalRequestedSize-\pgfexternalsize}{%
			\pgfmathsetmacro{\requestedSize}{\requestedSize + #4 - \pgfexternalsize}%
			\tikzscale@decreaseFigureCounter
			\tikzscale@scaleTikzpictureTo{#1}{#2}{#3}%
		}{}%
	}{}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@decreaseFigureCounter}
% Decrease the externalization figure counter. This way the last externalized figure will be overwritten when the next figure is externalized. This is useful if the same figure should be externalized again. This code is a modified copy of \cmd{\tikzexternal@getnextfilename@advancecount} from file tikzexternalshared.code.tex.
%    \begin{macrocode}
\def\tikzscale@decreaseFigureCounter{%
%    \end{macrocode}
% 		Use a group, so that the used counter register is not changed outside of this macro. This is necessary, as the global figure counter is not saved in a counter as one might think, but in a macro, so it must be copied to a local counter in order to be easily modified.
%    \begin{macrocode}
	\begingroup
%    \end{macrocode}
		% Get the name of the macro holding the figure counter value.
%    \begin{macrocode}
		\edef\macroName{c@tikzext@no@\pgfkeysvalueof{/tikz/external/figure name}}%
%    \end{macrocode}
		% Use a TeX counter and store the figure counter value therein.
%    \begin{macrocode}
% 		\c@pgf@counta=\csname\macroName\endcsname\relax
		\c@pgf@counta=\csuse{\macroName}\relax
%    \end{macrocode}
		% Decrease the value of the local counter.
%    \begin{macrocode}
		\advance\c@pgf@counta -1\relax
%    \end{macrocode}
		% Save the value of the local counter back to the global counter macro.
%    \begin{macrocode}
		\expandafter\xdef\csname\macroName\endcsname{\the\c@pgf@counta}%
	\endgroup
}%
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@resizePlotTo}
%\xcmd\tikzscale@resizePlotTo{file name}
%    \begin{macrocode}
\NewDocumentCommand{\tikzscale@resizePlotTo}{m}{%
	\def\fileName{#1}%
	\def\content{\tikzscale@trim{\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}
	% 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.
%    \begin{macrocode}
	\sbox{\tikzscale@measuredSize}{\content}%
	\tikzscale@warnIfSizeDifference{\requestedWidth}{\wd\tikzscale@measuredSize}{\fileName's width}%
	\tikzscale@warnIfSizeDifference{\requestedHeight}{\ht\tikzscale@measuredSize}{\fileName's height}%
%    \end{macrocode}
	% Finally, externalize and include the graphic with the final size. The graphic must not be include by reusing the measuredSize-box, as this leads to a subtly wrong behaviour when generating the PDF files. It can be observed by running the test suite with externalization and checking that no file is regenerated in the second run. Reusing the box can also lead to compile errors, if the problematic graphic is the last graphic in the document.
%    \begin{macrocode}
	\tikzscale@conditionalEnableExternalization{\fileName}%
	\content%
}
%    \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}{%
			\tikzscale@ifIsWidth{\dimension}{%
				\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}{%
			}{%
%    \end{macrocode}
				% Restore the externalization state in order to have strict enable-disable-call-pairing.
%    \begin{macrocode}
				\tikzscale@conditionalEnableExternalization{\fileName}%
				\tikzscale@includeNormalTikzpicture{#4}%
				\gdef\tikzscale@alreadyIncluded{true}%
				\breakforeach
			}%
			\pgfmathsetglobalmacro{\tikzscale@oldSizeDifference}{\sizeDifference}%
		}{%
			\breakforeach
		}%
	}%
	\ifdef{\tikzscale@alreadyIncluded}{%
		\global\undef\tikzscale@alreadyIncluded%
	}{%
%    \end{macrocode}
		 % 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.
%    \begin{macrocode}
		\tikzscale@measureSize{\measuredFinal}{\dimension}{\content}%
		\tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{\fileName}%
%    \end{macrocode}
		% Finally, externalize and include the graphic with the final size. The graphic must not be include by reusing the measuredSize-box, as this leads to a subtly wrong behaviour when generating the PDF files. It can be observed by running the test suite with externalization and checking that no file is regenerated in the second run. Reusing the box can also lead to compile errors, if the problematic graphic is the last graphic in the document.
%    \begin{macrocode}
		\tikzscale@conditionalEnableExternalization{\fileName}%
		\content%
	}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@ifIsWidth}
% \xcmd\tikzscale@ifIsWidth{condition}{true}{false}
%    \begin{macrocode}
\def\tikzscale@ifIsWidth#1{%
	\ifedefequal{#1}{\wd}%
}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\tikzscale@measureSize}
%    \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}{}{%
	\tikzscale@ifExternalizationActive{%
		\tikzexternaldisable
		\def\tikzscale@externalizationWasDisabled{}%
	}{}%
%    \end{macrocode}
	% Restore the endlinechar here and not in the general \cmd{\tikzexternaldisable} code, as it should only be restored if \cmd{\includegraphics} had been called and not if a tikzpicture was called directly without using \cmd{\includegraphics}. If the externalization has not been loaded, the endlinechar would be redefined twice (which would probably also do not much harm).
%    \begin{macrocode}
	\ifExternalizationLoaded{%
		\tikzscale@addRestoreEndLineCharToTikzpicture
	}{}%
%    \end{macrocode}
	% The pause command defined by the Beamer class creates additional slides when called multiple times due to tikzscale's scaling. Thus, deactivate it during the scaling tests, if it is defined.
%    \begin{macrocode}
	\ifdef{\pause}{%
		\LetLtxMacro{\tikzscale@oldpause}{\pause}%
		\RenewDocumentCommand{\pause}{o}{}%
	}{}%
}
%    \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}%
	\ifdef{\tikzscale@externalizationWasDisabled}{%
		\tikzexternalenable
		\undef\tikzscale@externalizationWasDisabled
	}{}%
%    \end{macrocode}
	% Restore the endlinechar here and not in the general \cmd{\tikzexternalenable} code, as it should only be restored if \cmd{\includegraphics} had been called and not if a tikzpicture was called directly without using \cmd{\includegraphics}. If the externalization has not been loaded, the endlinechar would be redefined twice (which would probably also do not much harm).
%    \begin{macrocode}
	\ifExternalizationLoaded{%
		\tikzscale@addRestoreEndLineCharToTikzpicture
	}{}%
%    \end{macrocode}
	% Reactivate Beamer's pause command if defined.
%    \begin{macrocode}
	\ifdef{\pause}{%
		\LetLtxMacro{\pause}{\tikzscale@oldpause}%
	}{}%
}
%    \end{macrocode}
%\end{macro}
%
% \makeatother
% \iffalse
%</package>
% \fi
%
%\StopEventually{^^A
%  \PrintChanges
%^^A  \PrintIndex
%}
%
%\Finale