%%
%% This is file `tikzscale.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% tikzscale.dtx  (with options: `package')
%% ----------------------------------------------------------------
%% 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
%% ----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tikzscale}[2012/10/31 v0.1 tikzscale LaTeX package]
\RequirePackage{graphicx}
\RequirePackage{etoolbox}
\RequirePackage{pgfkeys}
\RequirePackage{xparse}
\RequirePackage{letltxmacro}
\RequirePackage{xstring}

\AtEndPreamble{%
\ifdef{\tikzifexternalizing}{%
\pgfkeys{/pgf/images/external info}%
}{}%
\ProvideDocumentCommand{\tikzsetnextfilename}{m}{}%
\ProvideDocumentCommand{\tikzsetexternalprefix}{m}{}%
\ProvideDocumentCommand{\tikzexternaldisable}{}{}%
\ProvideDocumentCommand{\tikzexternalenable}{}{}%
\ProvideDocumentCommand{\tikzifexternalizingnext}{mm}{#1}%
\@ifpackageloaded{tikz}{%
\newlength{\tikzscale@accuracy}%
\setlength{\tikzscale@accuracy}{0.01pt}%
\def\maxTestIterations{10}%
}{}%
}
\def\pgfmathsetglobalmacro#1#2{%
\pgfmathparse{#2}%
\global\let#1\pgfmathresult%
}
\def\edocsvlist#1{%
\edef\tikzscale@edocsvlist{#1}%
\expandafter\docsvlist\expandafter{\tikzscale@edocsvlist}%
}
\def\eforcsvlist#1#2{%
\edef\tikzscale@eforcsvlist{#2}%
\expandafter\forcsvlist\expandafter{\expandafter#1\expandafter}\expandafter{\tikzscale@eforcsvlist}%
}
\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
}{}%
}
\ExplSyntaxOn
\NewDocumentCommand{\IfNoValueOrSplitEmptyTF}{mmm}{
\ifboolexpr{test {\IfNoValueTF{#1}} or test {\tl_if_eq:nnTF{#1}{{}}}}{
#2
}{
#3
}
}
\ExplSyntaxOff

\AtEndPreamble{%
\def\tikzscale@tikzFileExtensions{.tikz,.TIKZ,.TikZ,.pgf,.PGF}%
\DeclareGraphicsExtensions{\tikzscale@tikzFileExtensions,\Gin@extensions}%
\ifdef{\currfiledir}{%
\def\tikzscale@graphicspath{\currfiledir,{}}%
}{%
\def\tikzscale@graphicspath{{}}%
}%
\LetLtxMacro{\tikzscale@oldincludegraphics}{\includegraphics}%
\LetLtxMacro{\includegraphics}{\tikzscale@includegraphics}%
}
\NewDocumentCommand{\tikzscale@includegraphics}{O{}m}{%
\begingroup
\edef\tikzscale@restoreEndLineChar{\endlinechar=\the\endlinechar\relax}%
\endlinechar=-1%
\tikzscale@findExactFileName{tikzscale@fileName}{#2}%
\tikzscale@isTikzFile{tikzscale@testTikzFile}{\tikzscale@fileName}%
\ifcsdef{tikzscale@testTikzFile}{%
\LetLtxMacro{\includegraphics}{\tikzscale@oldincludegraphics}%
\tikzscale@includetikz[#1]{\tikzscale@fileName}%
}{%
\tikzscale@oldincludegraphics[#1]{\tikzscale@fileName}%
}%
\tikzscale@restoreEndLineChar
\endgroup
}%
\NewDocumentCommand{\tikzscale@findExactFileName}{mm}{%
\csundef{#1}%
\def\tikzscale@checkDirectory##1{%
\def\tikzscale@checkExtension####1{%
\IfFileExists{##1#2####1}{%
\csdef{#1}{##1#2####1}%
\listbreak
}{}%
}%
\eforcsvlist{\tikzscale@checkExtension}{{},\Gin@extensions}%
}%
\eforcsvlist{\tikzscale@checkDirectory}{\tikzscale@graphicspath}%
\ifcsundef{#1}{%
\csdef{#1}{#2}%
}{}%
}
\NewDocumentCommand{\tikzscale@isTikzFile}{mm}{%
\def\do##1{%
\IfEndWith{#2}{##1}{%
\csdef{#1}{}%
\listbreak
}{}%
}%
\csundef{#1}%
\edocsvlist{\tikzscale@tikzFileExtensions}%
}
\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
}
\NewDocumentCommand{\tikzscale@includetikz}{O{}m}{%
\ifdef{\tikzexternalgetnextfilename}{%
\tikzexternalgetnextfilename{\tikzscale@externalizationName}%
}{}%
\pgfkeys{/tikzscale, #1}%
\tikzifexternalizingnext{%
\tikzscale@includetikzUnexternalized[#1]{#2}%
}{%
\sbox{\tikzscale@measuredSize}{%
\IfFileExists{\tikzscale@externalizationName.dpth}{%
\input{\tikzscale@externalizationName.dpth}%
\xdef\tikzscale@measuredWidth{\pgfexternalwidth}%
\xdef\tikzscale@measuredHeight{\pgfexternalheight}%
}{}%
}%
\ifdef{\requestedWidth}{%
\ifdef{\tikzscale@measuredWidth}{%
\tikzscale@ifSizeDifference{\requestedWidth - \tikzscale@measuredWidth}{%
\tikzset{external/remake next}%
}{}%
\global\undef{\tikzscale@measuredWidth}%
}{%
\tikzset{external/remake next}%
}%
}{}%
\ifdef{\requestedHeight}{%
\ifdef{\tikzscale@measuredHeight}{%
\tikzscale@ifSizeDifference{\requestedHeight - \tikzscale@measuredHeight}{%
\tikzset{external/remake next}%
}{}%
\global\undef{\tikzscale@measuredHeight}%
}{%
\tikzset{external/remake next}%
}%
}{}%
\ifdef{\requestedAxisRatio}{%
\ifdef{\tikzscale@oldAxisRatio}{%
\tikzscale@ifSizeDifference{\requestedAxisRatio - \tikzscale@oldAxisRatio}{%
\tikzset{external/remake next}%
}{}%
\global\undef{\tikzscale@oldAxisRatio}%
}{%
\tikzset{external/remake next}%
}%
}{}%
\input{#2}%
}%
}
\NewDocumentCommand{\tikzscale@includetikzUnexternalized}{O{}m}{%
\elseif{test {\ifundef{\requestedWidth}} and test {\ifundef{\requestedHeight}} and test {\ifundef{\requestedAxisRatio}}}{%
\input{#2}%
}{test {\ifdef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{%
\tikzscale@resizePlotTo{#2}%
}{test {\ifdef{\requestedAxisRatio}}}{%
\tikzscale@includeAxisRatio{#2}%
}{test {\ifundef{\requestedAxisRatio}}}{%
\def\requestedAxisRatio{1}%
\tikzscale@includeAxisRatio{#2}%
}{%
\tikzscale@invalidKeyError{#2}%
}%
}
\NewDocumentCommand{\tikzscale@preparePlot}{}{%
\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={}}}%
}
\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}%
}%
}
\NewDocumentCommand{\tikzscale@invalidKeyError}{m}{%
\PackageError{tikzscale}{Invalid key for TikZ graphic}{Change key #1 into a valid key.}%
}
\NewDocumentCommand{\tikzscale@includeAxisRatio}{m}{%
\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}%
}%
}
\NewDocumentCommand{\tikzscale@scaleTikzpictureTo}{mmm}{%
\tikzscale@conditionalDisableExternalization
\def\tikzscale@scale{1}%
\tikzscale@measureSize{\measuredFirst}{#1}{#2}%
\pgfmathsetmacro{\tikzscale@scale}{\requestedSize/\measuredFirst}%
\tikzscale@measureSize{\measuredSecond}{#1}{#2}%
\tikzscale@ifSizeDifference{\measuredSecond - \requestedSize}{%
\tikzscale@ifSizeDifference{\measuredFirst - \measuredSecond}{%
}{%
\PackageError{tikzscale}{Requested to scale unscalable graphic}{Do not set width or height for graphic in\MessageBreak #3}%
}%
\pgfmathsetmacro{\fixedSize}{(\tikzscale@scale*\measuredFirst - \measuredSecond) / (\tikzscale@scale - 1)}%
\pgfmathsetmacro{\tikzscale@scale}{(\requestedSize - \fixedSize) / (\measuredFirst - \fixedSize)}%
\foreach \l in {1,...,\maxTestIterations}{%
\tikzscale@measureSize{\measuredIntermediate}{#1}{#2}%
\tikzscale@ifSizeDifference{\measuredIntermediate-\requestedSize}{%
\pgfmathparsemacro{\errorRatio}{\measuredIntermediate/\requestedSize}%
\pgfmathsetglobalmacro{\tikzscale@scale}{\tikzscale@scale/\errorRatio}%
}{%
\breakforeach%
}%
}%
\tikzscale@conditionalEnableExternalization{#3}%
\tikzscale@measureSize{\measuredFinal}{#1}{#2}%
\usebox{\tikzscale@measuredSize}%
\tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{#3}%
}{%
\tikzscale@conditionalEnableExternalization{#3}%
#2%
}%
}
\NewDocumentCommand{\tikzscale@resizePlotTo}{m}{%
\def\fileName{#1}%
\def\content{\input{#1}}%
\tikzscale@preparePlot
\def\tikzscale@width{\requestedWidth}%
\def\tikzscale@height{\requestedHeight}%
\tikzscale@conditionalDisableExternalization
\foreach \l in {1,...,\maxTestIterations}{%
\sbox{\tikzscale@measuredSize}{\content}%
\pgfmathsetmacro{\widthDifference}{\wd\tikzscale@measuredSize - \requestedWidth}%
\pgfmathsetmacro{\heightDifference}{\ht\tikzscale@measuredSize - \requestedHeight}%
\ifboolexpr{test {\tikzscale@ifSizeDifference{\widthDifference}} or test {\tikzscale@ifSizeDifference{\heightDifference}}}{%
\pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@width - \widthDifference}%
\pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@height - \heightDifference}%
}{%
\breakforeach
}%
}%
\tikzscale@conditionalEnableExternalization{\fileName}%
\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}%
}

\NewDocumentCommand{\tikzscale@resizePlotWithAxesRatioTo}{mmmm}{%
\def\dimension{#1}%
\def\variable{#2}%
\def\content{#3}%
\def\fileName{#4}%
\gdef\tikzscale@oldSizeDifference{0pt}%
\tikzscale@preparePlot
\tikzscale@conditionalDisableExternalization
\foreach \l in {1,...,\maxTestIterations}{%
\tikzscale@measureSize{\measuredSize}{\dimension}{\content}%
\pgfmathsetmacro{\sizeDifference}{\measuredSize - \requestedSize}%
\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%
}{%
\tikzscale@conditionalEnableExternalization{\fileName}%
\tikzscale@measureSize{\measuredFinal}{\dimension}{\content}%
\usebox{\tikzscale@measuredSize}%
\tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{\fileName}%
}%
}
\newsavebox{\tikzscale@measuredSize}
\def\tikzscale@measureSize#1#2#3{%
\sbox{\tikzscale@measuredSize}{#3}%
\pgfmathsetmacro{#1}{#2\tikzscale@measuredSize}%
}
\def\tikzscale@ifSizeDifference#1#2#3{%
\pgfmathparse{abs(#1)}%
\ifdimgreater{\pgfmathresult pt}{\tikzscale@accuracy}{%
#2%
}{%
#3%
}%
}%

\def\tikzscale@warnIfSizeDifference#1#2#3{%
\tikzscale@ifSizeDifference{#1-#2}{%
\PackageWarning{tikzscale}{Scaling of #3 was only\MessageBreak accurate to \pgfmathresult pt}%
}{}%
}
\NewDocumentCommand{\tikzscale@conditionalDisableExternalization}{}{%
\tikzexternaldisable
}
\NewDocumentCommand{\tikzscale@conditionalEnableExternalization}{m}{%
\tikzexternalenable
}
%% 
%% 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.
%% 
%%
%% End of file `tikzscale.sty'.
