From c7b0de93190a3c0d8e3ee9d88e79da7d21a27e31 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Wed, 31 Oct 2012 21:47:29 +0000 Subject: new pgf/tikz package tikzscale (31oct12) git-svn-id: svn://tug.org/texlive/trunk@28143 c570f23f-e606-0410-a88d-b1316a301751 --- .../source/latex/tikzscale/tikzscale.dtx | 1109 ++++++++++++++++++++ .../source/latex/tikzscale/tikzscale.ins | 72 ++ 2 files changed, 1181 insertions(+) create mode 100644 Master/texmf-dist/source/latex/tikzscale/tikzscale.dtx create mode 100644 Master/texmf-dist/source/latex/tikzscale/tikzscale.ins (limited to 'Master/texmf-dist/source/latex/tikzscale') diff --git a/Master/texmf-dist/source/latex/tikzscale/tikzscale.dtx b/Master/texmf-dist/source/latex/tikzscale/tikzscale.dtx new file mode 100644 index 00000000000..2c148abcce6 --- /dev/null +++ b/Master/texmf-dist/source/latex/tikzscale/tikzscale.dtx @@ -0,0 +1,1109 @@ +% \iffalse meta-comment +% !TEX program = pdfLaTeX +%<*internal> +\iffalse +% +%<*readme> +---------------------------------------------------------------- +tikzscale --- Absolute resizing of TikZ pictures and PGF plots without + scaling text +E-mail: pat_h@web.de Released under the LaTeX Project +Public License v1.3c or later See http://www.latex-project.org/lppl.txt +---------------------------------------------------------------- + +The tikzscale package extends the includegraphics command to support +tikzpictures. It allows scaling of TikZ images and PGFPlots to a provided +width or height without changing the text size. + +Usage: \usepackage{tikzscale} + or \usepackage[loadFromCurrentDirectory]{tikzscale} + +As an example write +\includegraphics{myTikZFile.tikz} +instead of +\includegraphics{myJPEGFile.jpeg}, +with myTikZFile.tikz being the file name of a text file containing +everything from \begin{tikzpicture} to \end{tikzpicture}. To actually +do some scaling of the included TikZ file, give either an absolute width +or an absolute height in the optional argument, e.g. +\tikzscale@includetikz[width=0.5\linewidth]{myTikZFile.tikz}. +If the file contains a plot created with the PGFPlots package, set both +width and height via the optional argument, e.g. +\tikzscale@includetikz[width=\linewidth,height=0.4\linewidth]{myPGFPlot.tikz}. +% +%<*internal> +\fi +\def\nameofplainTeX{plain} +\ifx\fmtname\nameofplainTeX\else + \expandafter\begingroup +\fi +% +%<*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 + +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}} +} +% +%\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 +% +%<*package> +\NeedsTeXFormat{LaTeX2e} +\ProvidesPackage{tikzscale}[2012/10/31 v0.1 tikzscale LaTeX package] +\RequirePackage{graphicx} +\RequirePackage{etoolbox} +\RequirePackage{pgfkeys} +\RequirePackage{xparse} +\RequirePackage{letltxmacro} +\RequirePackage{xstring} +% +%<*driver> +\documentclass{ltxdoc} +% \documentclass{ydoc} +\usepackage[utf8]{inputenx} +\usepackage[T1]{fontenc} +\usepackage{subfig} +\usepackage{booktabs} +\usepackage{tabulary} +\usepackage{tikzscale} +\usepackage{tikz} +\usepackage{pgfplots} +\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} +% +% \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} +% \section{Introduction} +% When dealing with graphics, there are different scaling demands. For \emph{absolute} scaling, a width and/or height is given. Opposed to that, for relativ scaling, a horizontal and/or vertical scaling factor is needed. This package only is about absolute scaling of tikzpicture environments. The different absolute scaling demands and their solutions are shown in table \ref{scalingDemands}. +% \begin{table} + % \centering + % \caption[Graphic scaling methods.]{Absolute graphic scaling methods. If multiple methods are available, the most native one is shown. Methods which \textcolor{orange}{approximate} the scaling are shown in orange text color. \textcolor{blue}{Recommended} methods are shown in blue textcolor.}%^^A Note, that the first three methods in each table are absolute ones, whereas the last three methods are relative ones.}% + % \label{scalingDemands} + % \subfloat[Scaling with scaled text and line widths.]{^^A + % \begin{tabular}{rccc}%^^A{1.1\linewidth}{RCCC} + % \toprule + % scale & Images & TikZ/PGFPlots\\ + % \midrule + % \rcell{to width\\proportionally} & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit}]}}} & \cell{\cmd{\resizebox}\\\texttt{\{\emph{width}\}\{!\}}}\\[0.8em] + % \rcell{to width\\keeping height} & \cell{\cmd{\resizebox}\\\texttt{\{\emph{width}\}\{\cmd{\height}\}}} & \cell{\cmd{\resizebox}\\\texttt{\{\emph{width}\}\{\cmd{\height}\}}}\\[0.8em] + % \rcell{to height\\proportionally} & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[height=\emph{unit}]}}} & \cell{\cmd{\resizebox}\\\texttt{\{!\}\{\emph{height}\}}}\\[0.8em] + % \rcell{to height\\keeping width} & \cell{\cmd{\resizebox}\\\texttt{\{\cmd{\width}\}\{\emph{height}\}}} & \cell{\cmd{\resizebox}\\\texttt{\{\cmd{\width}\}\{\emph{height}\}}}\\[0.8em] + % \rcell{to width\\and height} & \cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit},height=\emph{unit}]}} & \cell{\cmd{\resizebox}\\\texttt{\{\emph{width}\}\{\emph{height}\}}}\\ + %^^A horizontally & \cell{\cmd{\scalebox}\\\texttt{\{\emph{factor}\}[1]}} & \cell{\texttt{[transform canvas=}\\\texttt{\{xscale=\emph{factor}\}]}} & \texttt{[xscale=\emph{factor}]}\\[0.8em] + %^^A vertically & \cell{\cmd{\scalebox}\\\texttt{\{1\}[\emph{factor}]}} & \cell{\texttt{[transform canvas=}\\\texttt{\{yscale=\emph{factor}\}]}} & \texttt{[yscale=\emph{factor}]}\\[0.8em] + %^^A proportionally & \cell{\cmd{\scalebox}\\\texttt{\{\emph{factor}\}}} & \cell{\texttt{[transform canvas=}\\\texttt{\{scale=\emph{factor}\}]}} & \texttt{[scale=\emph{factor}]}\\ + % \bottomrule + % \end{tabular} + % }\\% + % \subfloat[Scaling with unscaled text and line widths without tikzscale.]{^^A + % \begin{tabulary}{1.1\linewidth}{RCCC} + % \toprule + % scale & Images & TikZ & PGFPlots\\ + % \midrule + % \rcell{to width\\proportionally} & -- & -- & \textcolor{orange}{\texttt{[width=\emph{unit}]}}\\[0.8em] + % \rcell{to width\\keeping height} & -- & -- & --\\[0.8em] + % \rcell{to height\\proportionally} & -- & -- & \textcolor{orange}{\texttt{[height=\emph{unit}]}}\\[0.8em] + % \rcell{to height\\keeping width} & -- & -- & --\\[0.8em] + % \rcell{to width\\and height} & -- & -- & \textcolor{orange}{\texttt{[width=\emph{unit},height=\emph{unit}]}}\\ + %^^A horizontally & -- & \texttt{[xscale=\emph{factor}]} & --\\ + %^^A vertically & -- & \texttt{[yscale=\emph{factor}]} & --\\ + %^^A proportionally & -- & \texttt{[scale=\emph{factor}]} & --\\ + % \bottomrule + % \end{tabulary} + % }\\% + % \subfloat[Scaling with unscaled text and line widths with tikzscale.]{^^A + % \begin{tabulary}{1.1\linewidth}{RCCC} + % \toprule + % scale & Images & TikZ & PGFPlots\\ + % \midrule + % \rcell{to width\\proportionally} & -- & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit}]}}} & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit}]}}}\\[0.8em] + % \rcell{to width\\keeping height} & -- & -- & --\\[0.8em] + % \rcell{to height\\proportionally} & -- & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[height=\emph{unit}]}}} & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[height=\emph{unit}]}}}\\[0.8em] + % \rcell{to height\\keeping width} & -- & -- & --\\[0.8em] + % \rcell{to width\\and height} & -- & -- & \textcolor{blue}{\cell{\cmd{\includegraphics}\\\texttt{[width=\emph{unit},height=\emph{unit}]}}}\\ + %^^A horizontally & -- & \texttt{[xscale=\emph{factor}]} & --\\ + %^^A vertically & -- & \texttt{[yscale=\emph{factor}]} & --\\ + %^^A proportionally & -- & \texttt{[scale=\emph{factor}]} & --\\ + % \bottomrule + % \end{tabulary} + % } +% \end{table} +% +% The tikzscale package adds and improves certain forms of absolute scaling for TikZ and PGFPlots, respectively. These scaling methods are the ones which are most useful, maybe even the only ones which are needed. During the scaling, the text sizes and line widths are left unscaled, which avoids inconsistency and visual distraction. PGFPlots itself can scale absolutely, but an approximation is used to achieve that. The tikzscale package uses optimization algorithms and warns if the scaling is not exact. +% +% Using tikzscale all relevant scaling methods share the same user interface with the well known \cmd{\includegraphics} command, enabling some of its features like automatic file extension detection for TikZ and PGFPlots, too. Furthermore, the \cmd{\includegraphics} is improved to look-up relative paths in the correct subdirectory, if a \LaTeX\ project is organized in subdirectories. +% +% Relative scaling methods are mostly useless, as the sizes of the used images are often arbitrary, either determined by some resolution for rastered images or some arbitrary unit vector size for vector images, TikZ and PGFPlots. For traditional images and TikZ pictures, only proportional scaling methods giving either a width or a height make sense, as otherwise they get heavily distorted if the original aspect ratio is changed. As PGFPlots can handle different aspect ratios and aspect ratios are normally not predefined for plots, its requirement is the opposite: A width and a height are needed to avoid getting arbitrary sizes. For some special plots, the axis ratio can be given, as well. These requirements lead to the marked blue colors in table \ref{scalingDemands}. +% +% \section{Usage and Examples} +% Loading the tikzscale package without loading other packages, does not do anything useful. +% +% \subsection{TikZ} +% If the tikzscale and the tikz packages are loaded, the \cmd{\includegraphics} command can be used to input and scale a tikzpicture environment located in a separate file. +% +% As an example create the following .tex-file. +% +% \vspace{0.5em} +% \noindent\cmd{\documentclass\{minimal\}}\\ +% \cmd{\usepackage\{tikz\}}\\ +% \cmd{\usepackage\{tikzscale\}}\\ +% \cmd{\begin\{document\}}\\ +% \indent\texttt{\cmd{\includegraphics}[width=0.5\cmd{\linewidth}]\{linewidth.tikz\}}\\ +% \cmd{\end\{document\}} +% \vspace{0.5em} +% +% Furthermore create the following .tikz-file and save it as linewidth.tikz in the same directory as the above .tex-file. +% +% \vspace{0.5em} +% \noindent\cmd{\begin\{tikzpicture\}}\\ +% \indent\texttt{\cmd{\draw} (0,0) -- node \{center\} (\cmd{\linewidth},1);}\\ +% \cmd{\end\{tikzpicture\}} +% \vspace{0.5em} +% +% The result of the complied .tex-file should look like this.\\%^^A +%\noindent\includegraphics[width=0.5\linewidth]{linewidth.tikz} +% +% So although the original tikzpicture itself has the width of a complete line, it gets proportionally scaled down to half the width while being loaded from the \cmd{\includegraphics} command. Neither the line's thickness nor the text \texttt{center} are scaled. Compare the output to\\ +% \indent\texttt{\cmd{\input}\{linewidth.tikz\}}\\ +% \input{linewidth.tikz}\\ +% and\\ +% \indent\texttt{\cmd{\resizebox}\{0.5\cmd{\linewidth}\}\{!\}\{\cmd{\input}\{linewidth.tikz\}\}}\\ +% \resizebox{0.5\linewidth}{!}{\input{linewidth.tikz}}\\ +% to see tikzscale's benefit. +% +% \subsection{PGFPlots} +% \subsubsection{Scaling of width and height} +% If the pgfplots package is loaded together with the tikzscale package, the user interface is the same. Instead of giving either a width or a height, both have to be given for pgfplots. So +% +% \vspace{0.5em} +% \cmd{\input\{pgfplots-test.tikz\}} +% +% \vspace{0.5em} +% \texttt{\cmd{\begin}\{tikzpicture\}\cmd{\begin}\{axis\}[width=3cm,height=2cm] \dots} +% \vspace{0.5em} +% +% \noindent becomes +% +% \vspace{0.5em} +% \cmd{\includegraphics[width=3cm,height=2cm]\{pgfplots-test.tikz\}} +% +% \vspace{0.5em} +% \texttt{\cmd{\begin}\{tikzpicture\}\cmd{\begin}\{axis\} \dots} +% \vspace{0.5em} +% +% The benefit is a more accurate scaling algorithm, as the scaling with PGFPlots can be quite coarse. Another win is the unified interface, which simplifies the sharing of plots between projects enormously, as one file and thus one plot can be included in different projects with different sizes. +% +% \subsubsection{Scaling using axis ratio} +% The scaling described in the previous section scales the whole plot including all axis descriptions and legends to the given width and height. It can thus happen, that the plotted figure has a different size ratio than expected, if the x and y descriptions have different sizes as shown in figure \ref{width=height}. +% \begin{figure} +% \centering +% \frame{\includegraphics[width=0.4\linewidth,height=0.4\linewidth]{testgraphic2D.tikz}}\\ +%^^A \includegraphics[width=0.4\linewidth,height=0.4\linewidth]{testgraphic2D.tikz} +% \caption{Using options \texttt{width=0.4\cmd{\linewidth}} and \texttt{height=0.4\cmd{\linewidth}} results in an overall quadratic graphic with overall width and height set to 40\% of the linewidth.} +% \label{width=height} +% \end{figure} +% Sometimes, the x-axis and the y-axis should have a specific ratio, e.g. being equal, ignoring the axis description and other things. This is normally achieved by using PGFPlots' option \texttt{scale only axis}. Unfortunately, if this option would be used, a plot might be unsharable between two projects, if they have different requirements for the axis ratio. Thus, this option should not be used in this case. Instead, in \cmd{\includegraphics} there is a new option \texttt{axisratio} which must be used together with either width or height. It scales the whole plot including the axis description to the given width or height as in figure \ref{axisratio} while keeping the graphical part at a given axis ratio, where the ratio is defined by width divided by height. The graphical part is thus not quadratic in general. +% \begin{figure} +% \centering +% \frame{\includegraphics[width=0.4\linewidth,axisratio=1]{testgraphic2D.tikz}}\\ +%^^A \includegraphics[width=0.4\linewidth,axisratio=1]{testgraphic2D.tikz}\\ +% \caption{Using options \texttt{width=0.4\cmd{\linewidth}} and \texttt{axisratio=1} results in an quadratic graphic area with overall width set to 40\% of the linewidth. The height follows from these constraints, so that the overall plot is not quadratic in general.} +% \label{axisratio} +% \end{figure} + + +% +% \subsection{Hints for TikZ and PGFPlots} +% The whole tikzpicture environment must be in a separate file. This allows sharing of graphics between different \TeX\ projects and a unified user interface via \cmd{\includegraphics}. Having tikzpicture environments directly in a .tex-file is not supported, i.e.\ they do not benefit from the tikzscale package. Multiple tikzpicture environments in one .tikz-file are not supported, either. Put things which always belong together in a shared tikzpicture environment and things which might be used separately in the future in separate files for code sharing across projects. The file ending may be ommited in the \cmd{\includegraphics} command, if it is one of .tikz, .TIKZ, .TikZ, .pgf or .PGF. At the moment, use only \emph{either} width \emph{or} height for normal (i.e.\ non-PGFPlots) tikzpicture environments and use width \emph{and} height or one of both together with \emph{axisratio} for tikzpicture environments containing a PGFPlots' axis environment. +% +% \subsection{currfile} +% If the tikzpicture package is loaded together with the \href{http://www.ctan.org/pkg/currfile}{currfile} package, another feature is activated. Suppose you have your project organized in the following directory tree with \textcolor{blue}{directories} shown in blue color: +% +% \vspace{0.5em} +% \noindent \textcolor{blue}{projectDirectory}\\ +% \indent main.tex\\ +% \indent \textcolor{blue}{firstChapter}\\ +% \indent\indent firstChapter.tex\\ +% \indent\indent firstGraphicOfFirstChapter.jpeg\\ +% \indent\indent secondGraphicOfFirstChapter.tikz\\ +% \indent \textcolor{blue}{secondChapter}\\ +% \indent\indent secondChapter.tex\\ +% \indent\indent firstGraphicOfSecondChapter.tikz\\ +% \indent\indent secondGraphicOfSecondChapter.jpeg +% \vspace{0.5em} +% +% Further suppose the chapter.tex files are \cmd{\input}ted in main.tex. Calling\\ +% \cmd{\includegraphics\{firstGraphicOfFirstChapter.jpeg\}}\\ +% in firstChapter.tex normally does not work. The reason is that the\\ +% \cmd{\input\{firstChapter.tex\}}\\ +% command in main.tex copies the content of firstChapter.tex into main.tex, so when the \cmd{\includegraphics} command is called, it is called from within projectDirectory, thus the relative path lookup of firstGraphicOfFirstChapter.jpeg fails. Instead the command\\ +% \cmd{\includegraphics\{firstChapter/firstGraphicOfFirstChapter.jpeg\}}\\ +% can be used (example for a Unix system), but this is tedious and counter-intuitive. +% +% If both tikzscale and currfile are loaded, the limitation is fixed, so that both \cmd{\includegraphics} commands succeed. Note, that this functionality supports the traditional graphic formats, too, and is also available without loading the TikZ or PGFPlots packages, although the package's name might imply otherwise. +% +% \section{Compatibility} +% Using both the externalization library and tikzscale seems to have a race condition when a make file is used with multiple jobs (-j$X$ with $X > 1$). The probability of getting errors increases with the number of jobs and for $X = 1$ and $X = 2$ no race condition could be observed, yet. You should either avoid using mode \emph{list and make} or have only one job if you want to be on the safe side. +% +% \subsection{Load Order} +% There is no constraint regarding the load order known, yet. TikZ, PGFPlots and currfile might all be loaded or not in all possible combinations and orders before or after tikzscale. +% +% \subsection{Externalization library} +% TikZ' externalization library is supported. Its use is highly recommended, as tikzscale renders some graphics multiple times to get the correct size. The savings by using the externalization library can thus be huge. +% +% \section{Further Ideas} +% \begin{itemize} +% \item When the externalization library is used, the graphic files get regenerated more often than necessary. This is, for example, because the axis ratio is not saved, thus assumed unknown and the graphic is thus regenerated. +% \item if graphic files are located in a subdirectory, the externalized files should also be in that subdirectory. +% \item the package can test if a pgfplot is used (needed if normal TikZ graphics should be stretchable) by changing \cmd{\tikzscale@width} and or \cmd{\tikzscale@height} and measuring. If nothing changes, it must be a normal tikzpicture (the argument does not hold the other way round). +% \item it may be better to use the \href{http://tex.stackexchange.com/a/22957}{depth as well} +% \item The final sizing parameters should be saved per figure in the aux file. The first rendering each run should be performed with the aux file's parameters into an sbox. The scaling algorithms should only be called, if the sizing requirements are not met. +% \end{itemize} +% +% \section{Contributions} +% \begin{itemize} +% \item Jake +% \begin{itemize} +% \item Encouraged the author to create this package. +% \end{itemize} +% \item Dr.\ Christian Feuersänger +% \begin{itemize} +% \item Encouraged the author to create this package and created PGFPlots. +% \item Answered many questions and had a lot of good ideas regarding the externalization. +% \end{itemize} +% \item David Carlisle +% \begin{itemize} +% \item Created the \cmd{\xcmd} macro for this package, which is used in the documentation. +% \end{itemize} +% \end{itemize} +% +% +% \section{Implementation} +% The basic idea is to first get the correct file name (i.e. find the path and the file extension), then determine the graphic type (i.e. TikZ or something else) and call either the original includegraphics command or the tikzscale command. Tikzpictures are then plotted into an invisible box and their size is measured. If their measured size differs from the requested size, they are replotted with corrected parameters to get the requested size. The correctly sized plots are then really plotted. +% +% \iffalse +%<*package> +% \fi +% \makeatletter +% +%\begin{macro}{\tikzexternal} +% \begin{macrocode} +\AtEndPreamble{% +% \end{macrocode} + % Activate the output of the graphics sizes into the dpth files (one file per graphic). This key is used if the externalization library is activated to check if the scaling is correct, otherwise the code is not needed +% \begin{macrocode} + \ifdef{\tikzifexternalizing}{% + \pgfkeys{/pgf/images/external info}% + }{}% +% \end{macrocode} + % Provide dummy commands, if the externalization library has not been loaded during the preamble. +% \begin{macrocode} + \ProvideDocumentCommand{\tikzsetnextfilename}{m}{}% + \ProvideDocumentCommand{\tikzsetexternalprefix}{m}{}% + \ProvideDocumentCommand{\tikzexternaldisable}{}{}% + \ProvideDocumentCommand{\tikzexternalenable}{}{}% + \ProvideDocumentCommand{\tikzifexternalizingnext}{mm}{#1}% +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@scale} +% \begin{macrocode} + \@ifpackageloaded{tikz}{% +% \end{macrocode} + % Set a minimum accuracy tikzscale tries to achieve. TeX's accuracy is limited, thus 0.001 pt cannot always be achieved independent of the number of iterations. The current value is chosen arbitrarily, but it might have to be increased in case a plot is found where the given accuracy cannot be achieved. +% \begin{macrocode} + \newlength{\tikzscale@accuracy}% + \setlength{\tikzscale@accuracy}{0.01pt}% +% \end{macrocode} + % This is needed in normal TikZ pictures and in PGFPlots, but as the pgfplots package loads the tikz package, it is fine to define it here. +% \begin{macrocode} + \def\maxTestIterations{10}% + }{}% +} +% \end{macrocode} +%\end{macro} +% +% This command draws the plot's border at the right text border, so that thick points or label descriptions can reach into the margin. This should be limited to PGFPlots only if activated. +%^^A \tikzset{every picture/.style={trim axis right}} +% +% With the option below, the labels can be moved a bit to the left so that they reach to the text margin. +% yticklabel style={align=right,inner sep=0pt,xshift=-0.1cm} +% +%\begin{macro}{\pgfmathsetglobalmacro} +% This is a general command, which might be useful for inclusion into the tikz package. It works similar to \cmd{\pgfmathsetglobalmacro} but has global scope. +% \begin{macrocode} +\def\pgfmathsetglobalmacro#1#2{% + \pgfmathparse{#2}% + \global\let#1\pgfmathresult% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\edocsvlist} +% This is a general command, which might be useful for inclusion into the etoolbox package. It works similar to \cmd{\docsvlist} but expands its argument similar to \cmd{\def} vs.\ \cmd{\edef}, which is useful if the list is stored in a macro/variable. +% \begin{macrocode} +\def\edocsvlist#1{% + \edef\tikzscale@edocsvlist{#1}% + \expandafter\docsvlist\expandafter{\tikzscale@edocsvlist}% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\eforcsvlist} +% These is a general command, which might be useful for inclusion into the etoolbox package. It works similar to \cmd{\forcsvlist} but expands its argument similar to \cmd{\def} vs.\ \cmd{\edef}, which is useful if the list is stored in a macro/variable. +% \begin{macrocode} +\def\eforcsvlist#1#2{% + \edef\tikzscale@eforcsvlist{#2}% + \expandafter\forcsvlist\expandafter{\expandafter#1\expandafter}\expandafter{\tikzscale@eforcsvlist}% +} +% \end{macrocode} +%\end{macro} +% +% \begin{macro}{\elseif} +% This macro provides a conditional which supports an if with an arbitrary amount of elseif (none is also ok) and an optional else. With a simplified syntax (remove the tests and the grouping) this would be worth a separate package. +% \begin{macrocode} +\NewDocumentCommand{\elseif}{mm}{% + \ifboolexpr{#1}{% + #2% + \elseif@absorb + }{% + \elseif@optional + }% +} +\NewDocumentCommand{\elseif@optional}{gg}{% + \IfValueTF{#1}{% + \IfValueTF{#2}{% + \ifboolexpr{#1}{% + #2% + \elseif@absorb + }{% + \elseif@optional + }% + }{% + #1% + }% + }{}% +} +\NewDocumentCommand{\elseif@absorb}{g}{% + \IfValueTF{#1}{% + \elseif@absorb + }{}% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro} +% This command is from \href{http://tex.stackexchange.com/a/63248/7323}{Bruno Le Floch}. +% \begin{macrocode} +\ExplSyntaxOn +\NewDocumentCommand{\IfNoValueOrSplitEmptyTF}{mmm}{ + \ifboolexpr{test {\IfNoValueTF{#1}} or test {\tl_if_eq:nnTF{#1}{{}}}}{ + #2 + }{ + #3 + } +} +\ExplSyntaxOff +% \end{macrocode} +%\end{macro} + +% +%\begin{macro}{\activatetikzscale} +% \begin{macrocode} +\AtEndPreamble{% +% \end{macrocode} + % Add the TikZ file extensions to the \href{http://tex.stackexchange.com/a/45502}{graphicx file extensions}. +% \begin{macrocode} + \def\tikzscale@tikzFileExtensions{.tikz,.TIKZ,.TikZ,.pgf,.PGF}% +% \def\tikzscale@tikzFileExtensions{.tikz,.TIKZ,.TikZ,.pgf,.PGF,.tex,.TEX}% + \DeclareGraphicsExtensions{\tikzscale@tikzFileExtensions,\Gin@extensions}% +% \end{macrocode} + % The \cmd{graphicspath} command is used to set additional directories, which are searched for graphics. \cmd{\Ginput@path} is used to get the \href{http://tex.stackexchange.com/a/58404}{current content}. +% \begin{macrocode} + \ifdef{\currfiledir}{% +% \graphicspath{{\currfiledir}{}\Ginput@path}% + \def\tikzscale@graphicspath{\currfiledir,{}}% + }{% + \def\tikzscale@graphicspath{{}}% + }% +% \end{macrocode} +% Save the \cmd{\includegraphics} \href{ftp://ftp.tu-chemnitz.de/pub/tex/macros/latex/required/graphics/grfguide.pdf}{command} and replace it by a new more generic command, to have a consistent user interface. +% \begin{macrocode} + \LetLtxMacro{\tikzscale@oldincludegraphics}{\includegraphics}% + \LetLtxMacro{\includegraphics}{\tikzscale@includegraphics}% +} +% \end{macrocode} +%\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} + % There is a leading space character introduced by the externalization library, if the file is input directly. Thus use a trick to avoid that space. Furthermore, TikZ introduces with a specific version an trailing space character. To get rid of all space character issues, just solve the problem here once an for all. +% \begin{macrocode} + \edef\tikzscale@restoreEndLineChar{\endlinechar=\the\endlinechar\relax}% + \endlinechar=-1% +% \end{macrocode} + % Find the exact file name, as the ending and the path could be omitted. +% \begin{macrocode} + \tikzscale@findExactFileName{tikzscale@fileName}{#2}% +% \end{macrocode} + % Check if the found file is a TikZ file. + % \begin{macrocode} + \tikzscale@isTikzFile{tikzscale@testTikzFile}{\tikzscale@fileName}% + \ifcsdef{tikzscale@testTikzFile}{% +% \end{macrocode} + % Deactivate the new includegraphics command, as a tikzpicture might load a PNG graphic or something and this should not be scaled by tikzscale but by TikZ or PGFPlots. +% \begin{macrocode} + \LetLtxMacro{\includegraphics}{\tikzscale@oldincludegraphics}% + \tikzscale@includetikz[#1]{\tikzscale@fileName}% + }{% + \tikzscale@oldincludegraphics[#1]{\tikzscale@fileName}% + }% + \tikzscale@restoreEndLineChar + \endgroup +}% +% \end{macrocode} +%\end{macro} +% +% \begin{macro}{\tikzscale@findExactFileName} +% Find the exact file name of a graphic file by testing several paths and file endings if there are degrees of freedom. The file name is saved in the command sequence name given by the first argument. +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@findExactFileName}{mm}{% + % \end{macrocode} + % Delete the return variable if it already exists to allow checking if a file has been found. + % \begin{macrocode} + \csundef{#1}% + % \end{macrocode} + % Create a helper function used inside the file ending evaluation. + % \begin{macrocode} + \def\tikzscale@checkDirectory##1{% + \def\tikzscale@checkExtension####1{% + \IfFileExists{##1#2####1}{% + \csdef{#1}{##1#2####1}% + \listbreak + }{}% + }% +% \end{macrocode} + % Test all possible file extensions and do not forget that the extension might already be given. \cmd{\Gin@extensions} returns the \href{http://tex.stackexchange.com/a/45502}{current content} set by \cmd{\DeclareGraphicsExtensions}. +% \begin{macrocode} + \eforcsvlist{\tikzscale@checkExtension}{{},\Gin@extensions}% + }% + \eforcsvlist{\tikzscale@checkDirectory}{\tikzscale@graphicspath}% +% \end{macrocode} + % If no file has been found, return the given file name, as includegraphics should try its best. +% \begin{macrocode} + \ifcsundef{#1}{% + \csdef{#1}{#2}% + }{}% +} +% \end{macrocode} +%\end{macro} +% +% \begin{macro}{\tikzscale@findExactFileName} +% The first argument is the macro name (without backslash), which gets defined if the file is a tikzfile. +% The second argument is the file name. +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@isTikzFile}{mm}{% + % \end{macrocode} + % Create a helper function used inside the evaluation. + % \begin{macrocode} + \def\do##1{% + \IfEndWith{#2}{##1}{% + \csdef{#1}{}% + \listbreak + }{}% + }% +% \end{macrocode} + % Delete macro so that defining it is really indicating something. +% \begin{macrocode} + \csundef{#1}% + \edocsvlist{\tikzscale@tikzFileExtensions}% +} +% \end{macrocode} +%\end{macro} +% +% \begin{macro}{\pgfkeys} +% This is \href{http://tex.stackexchange.com/a/34318}{similarly} done. +% \begin{macrocode} +\pgfkeys{ + /tikzscale/.is family, /tikzscale, + width/.code = {\pgfmathsetmacro{\requestedWidth}{#1}}, + width/.value required, + height/.code = {\pgfmathsetmacro{\requestedHeight}{#1}}, + height/.value required, + axisratio/.code = {\pgfmathsetmacro{\requestedAxisRatio}{#1}}, + axisratio/.value required +} +% \end{macrocode} +%\end{macro} +% +% \begin{macro}{\tikzscale@includetikz} +% \xcmd\tikzscale@includetikz{filename}\\ +% \xcmd\tikzscale@includetikz[width=1cm]{filename}\\ +% \xcmd\tikzscale@includetikz[height=1cm]{filename}\\ +% \xcmd\tikzscale@includetikz[height=1cm,width=1cm]{filename}\\ +% \xcmd\tikzscale@includetikz[width=1cm,height=1cm]{filename}\\ +% This command allows the inclusion of a tikz file like a graphics file. Thus instead of writing +% \cmd{\includegraphics}[width=\cmd{\linewidth}]{fileWithoutEnding} +% write +% \cmd{\tikzscale@includetikz}[width=\cmd{\linewidth}]{fileWithoutEnding} +% If only one of width or height are given, scale proportionally to fulfill the requirement. If both are given, scale non-proportionally to required width and height. Therefore, for normal tikzpictures only give either width or height, as the aspect ratio is already determined by the coordinate limits in the tikzpicture, but give width and height for PGFPlots, as the aspect ratio is unknown for these plots. \cmd{\NewEnviron} could be used to handle something like verbose in a tikzpicture, but at the moment, this is unsupported. +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@includetikz}{O{}m}{% +% \end{macrocode} + % TODO: Is it really necessary to execute this command here or can it be moved to a more specialized code path? +% \begin{macrocode} + \ifdef{\tikzexternalgetnextfilename}{% + \tikzexternalgetnextfilename{\tikzscale@externalizationName}% + }{}% +% \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}% + \tikzifexternalizingnext{% + \tikzscale@includetikzUnexternalized[#1]{#2}% + }{% +% \end{macrocode} + % Load this thing in an environment, where it does not get printed, as there is some garbage in the file, too. As the sbox creates a new group, use a global variable to get the content out of the group. +% \begin{macrocode} + \sbox{\tikzscale@measuredSize}{% + \IfFileExists{\tikzscale@externalizationName.dpth}{% + \input{\tikzscale@externalizationName.dpth}% + \xdef\tikzscale@measuredWidth{\pgfexternalwidth}% + \xdef\tikzscale@measuredHeight{\pgfexternalheight}% + }{}% + }% +% \end{macrocode} + % Check if the sizes are still correct, i.e. agree with the sizes of the externalized PDF graphic. The axis ratio does not have to be checked, as is is always correct. This is because if it gets changed, the width and the height get changed, too, and one of these changes gets detected. +% \begin{macrocode} + \ifdef{\requestedWidth}{% + \ifdef{\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}% + }% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@includetikzUnexternalized} +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@includetikzUnexternalized}{O{}m}{% + \elseif{test {\ifundef{\requestedWidth}} and test {\ifundef{\requestedHeight}} and test {\ifundef{\requestedAxisRatio}}}{% +% \end{macrocode} + % If no option is given, directly load the content, as nothing should get scaled. +% \begin{macrocode} + \input{#2}% + }{test {\ifdef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{% +% \end{macrocode} + % If width and height are given, the content must be a pgfplot, so scale it. The plot currently only had approximately the given size without calling the resizeTo macro, due to a (known) bug in PGFPlots. +% \begin{macrocode} + \tikzscale@resizePlotTo{#2}% + }{test {\ifdef{\requestedAxisRatio}}}{% + \tikzscale@includeAxisRatio{#2}% + }{test {\ifundef{\requestedAxisRatio}}}{% +% \end{macrocode} + % If only either width or height is given it can be a normal tikzpicture or a plot with axisratio=1. Let's guess that it is a plot with default axisratio. If the guess is wrong, the called function detects that scaling the plot does not work and automatically calls \cmd{\tikzscale@includeNormalTikzpicture}. +% \begin{macrocode} + \def\requestedAxisRatio{1}% + \tikzscale@includeAxisRatio{#2}% + }{% + % Everything else results in an error. + \tikzscale@invalidKeyError{#2}% + }% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@preparePlot} +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@preparePlot}{}{% +% \end{macrocode} + % Set a scaling factor or a width and height for the plot, which will be loaded. The \cmd{\tikzset} and \cmd{\pgfplotsset} commands have local scope. The internal redefinition of the style is correct, because if one tikzpicture includes another one, the scaling factor is reset so that it does not get \href{http://tex.stackexchange.com/questions/38605/scaling-a-tikz-figure-from-an-external-file}{squared} in the inner one. Note that if a user-defined style thus is ignored in this special case. The styles are defined here, so that files which are inputted without the \cmd{includegraphics} command are not affected. +% \begin{macrocode} + \pgfplotsset{every axis/.append style={width=\tikzscale@width,height=\tikzscale@height,every axis/.style={}}}% +} +\NewDocumentCommand{\tikzscale@prepareTikzpicture}{}{% + \tikzset{every picture/.style={scale=\tikzscale@scale,every picture/.style={}}}% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@includeNormalTikzpicture} +% \xcmd\tikzscale@includeNormalTikzpicture{file name} +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@includeNormalTikzpicture}{m}{% + \tikzscale@prepareTikzpicture + \elseif{test {\ifdef{\requestedWidth}} and test {\ifundef{\requestedHeight}}}{% + \def\requestedSize{\requestedWidth}% + \tikzscale@scaleTikzpictureTo{\wd}{\input{#1}}{#1}% + }{test {\ifundef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{% + \def\requestedSize{\requestedHeight}% + \tikzscale@scaleTikzpictureTo{\ht}{\input{#1}}{#1}% + }{% + \tikzscale@invalidKeyError{#1}% + }% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@invalidKeyError} +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@invalidKeyError}{m}{% + \PackageError{tikzscale}{Invalid key for TikZ graphic}{Change key #1 into a valid key.}% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@includeAxisRatio} +% \xcmd\tikzscale@includeAxisRatio{file name} +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@includeAxisRatio}{m}{% +% \end{macrocode} + % Try to set initial sizes close to the requested sizes, to improve the optimization's speed. +% \begin{macrocode} + \pgfplotsset{every axis/.append style={scale only axis,every axis/.style={}}}% + \elseif{test {\ifdef{\requestedWidth}} and test {\ifundef{\requestedHeight}}}{% + \let\requestedSize\requestedWidth + \def\tikzscale@width{\requestedWidth}% + \pgfmathsetmacro{\tikzscale@height}{\requestedWidth / \requestedAxisRatio}% + \tikzscale@resizePlotWithAxesRatioTo{\wd}{\tikzscale@width}{\input{#1}}{#1}% + }{test {\ifundef{\requestedWidth}} and test {\ifdef{\requestedHeight}}}{% + \let\requestedSize\requestedHeight + \def\tikzscale@height{\requestedHeight}% + \pgfmathsetmacro{\tikzscale@width}{\requestedHeight * \requestedAxisRatio}% + \tikzscale@resizePlotWithAxesRatioTo{\ht}{\tikzscale@height}{\input{#1}}{#1}% + }{% + \tikzscale@invalidKeyError{#1}% + }% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@scaleTikzpictureTo} +% \xcmd\scalteTo{\cmd{\wd} or \cmd{\ht}}{to-be-scaled content}{file name} +% The first argument determines if a specific width or a specific height should be achieved by scaling. +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@scaleTikzpictureTo}{mmm}{% +% \end{macrocode} + % Deactivate the externalization, as the measurements to determine the correct size should not be externalized. +% \begin{macrocode} + \tikzscale@conditionalDisableExternalization +% \end{macrocode} + % When scaling a tikzpicture, only the drawings are scaled, but nodes are not scaled. So in general, there are horizontal or vertical areas, where the picture contains only unscaled nodes, and areas where the picture contains scalable drawings. Mathematically all scaled and all unscaled areas can be combined, so that there is one are area of fixed size and one variable sized area. Thus scaling only by multiplication of a factor is incorrect in general. To do the correct scaling, the fixed area size must be known. As there are two unknown parameters, i.e. fixed area size and variable area size, the fixed area size can be calculated by measuring the tikzpicture with two different scalings. A special scaling factor is used, to get the size close to the final size minimizing numerical and logical errors. +% \begin{macrocode} + \def\tikzscale@scale{1}% + \tikzscale@measureSize{\measuredFirst}{#1}{#2}% + \pgfmathsetmacro{\tikzscale@scale}{\requestedSize/\measuredFirst}% + \tikzscale@measureSize{\measuredSecond}{#1}{#2}% +% \end{macrocode} + % It can happen, that there are no variable areas. Furthermore, the original size could already fit. Avoid numerical problems in both cases by directly drawing the picture. Do not compare the float values directly, as TeX's precision is quite limited. +% \begin{macrocode} + \tikzscale@ifSizeDifference{\measuredSecond - \requestedSize}{% +% \end{macrocode} + % If a plot is not scalable (e.g. consisting of a node only), but is not correctly scaled, exit with an error. +% \begin{macrocode} + \tikzscale@ifSizeDifference{\measuredFirst - \measuredSecond}{% + }{% + \PackageError{tikzscale}{Requested to scale unscalable graphic}{Do not set width or height for graphic in\MessageBreak #3}% + }% +% \end{macrocode} + % We know, that the variable sized area scales with the scaling factor, thus it holds + % \cmd{\scale} * \cmd{\variableFirst} = \cmd{\variableSecond}, + % with \cmd{\variableFirst} = \cmd{\measuredFirst} - \cmd{\fixedSize} + % and \cmd{\variableSecond} = \cmd{\measuredSecond} - \cmd{\fixedSize}, + % which can be solved by substituttion and results in +% \begin{macrocode} + \pgfmathsetmacro{\fixedSize}{(\tikzscale@scale*\measuredFirst - \measuredSecond) / (\tikzscale@scale - 1)}% +% \end{macrocode} + % Now, to get the correct scaling factor, only take the variable areas into account, as it holds + % \cmd{\scaleFinal} = \cmd{\variableSizeFinal} / \cmd{\variableSizeOriginal} + % with \cmd{\variableSizeFinal} = \cmd{\requestedSize} - \cmd{\fixedSize} + % and \cmd{\variableSizeOriginal} = \cmd{\measuredFirst} - \cmd{\fixedSize}, + % which results in +% \begin{macrocode} + \pgfmathsetmacro{\tikzscale@scale}{(\requestedSize - \fixedSize) / (\measuredFirst - \fixedSize)}% +% \end{macrocode} + % Additionally or alternatively the brute force approach to iteratively improve the solution can be used. +% \begin{macrocode} + \foreach \l in {1,...,\maxTestIterations}{% + \tikzscale@measureSize{\measuredIntermediate}{#1}{#2}% +% \end{macrocode} + % Optimize until the absolute difference is small enough, although the (relative) size ratios are used to calculate a new scaling factor. +% \begin{macrocode} + \tikzscale@ifSizeDifference{\measuredIntermediate-\requestedSize}{% +% \end{macrocode} + % First divide before multiply to avoid overflowing (at 16384). +% \begin{macrocode} + \pgfmathparsemacro{\errorRatio}{\measuredIntermediate/\requestedSize}% + \pgfmathsetglobalmacro{\tikzscale@scale}{\tikzscale@scale/\errorRatio}% + }{% + \breakforeach% + }% + }% +% \end{macrocode} + % Externalize the graphic with the final size. +% \begin{macrocode} + \tikzscale@conditionalEnableExternalization{#3}% +% \end{macrocode} + % Finally, include the picture. Do it via a new measurement to be able to warn if it does not fit good enough. +% \begin{macrocode} + \tikzscale@measureSize{\measuredFinal}{#1}{#2}% + \usebox{\tikzscale@measuredSize}% + \tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{#3}% + }{% +% \end{macrocode} + % Externalize the graphic with the final size. +% \begin{macrocode} + \tikzscale@conditionalEnableExternalization{#3}% + #2% + }% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@resizePlotTo} +%\xcmd\tikzscale@resizePlotTo{file name} +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@resizePlotTo}{m}{% + \def\fileName{#1}% + \def\content{\input{#1}}% + \tikzscale@preparePlot + \def\tikzscale@width{\requestedWidth}% + \def\tikzscale@height{\requestedHeight}% +% \end{macrocode} + % Deactivate the externalization, as the measurements to determine the correct size should not be externalized. +% \begin{macrocode} + \tikzscale@conditionalDisableExternalization +% \end{macrocode} + % Improve the solution iteratively until it is good enough. +% \begin{macrocode} + \foreach \l in {1,...,\maxTestIterations}{% +% \end{macrocode} + % Using the box allows measuring the width and height with one rendering run. +% \begin{macrocode} + \sbox{\tikzscale@measuredSize}{\content}% +% \end{macrocode} + % Determine the remaining error and check if it is larger than a threshold. +% \begin{macrocode} + \pgfmathsetmacro{\widthDifference}{\wd\tikzscale@measuredSize - \requestedWidth}% + \pgfmathsetmacro{\heightDifference}{\ht\tikzscale@measuredSize - \requestedHeight}% +% \end{macrocode} + % Output error in current iterion for debugging. +% \begin{macrocode} +% widthDifference: \widthDifference, heightDifference: \heightDifference\\% Debugging +% \end{macrocode} + % Check if the remaining error is larger than a threshold. +% \begin{macrocode} + \ifboolexpr{test {\tikzscale@ifSizeDifference{\widthDifference}} or test {\tikzscale@ifSizeDifference{\heightDifference}}}{% +% \end{macrocode} + % Correct the dimension by the error. Use a global assignment, as each iteration in the loop is put into a separate group. +% \begin{macrocode} + \pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@width - \widthDifference}% + \pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@height - \heightDifference}% + }{% + \breakforeach + }% + }% +% \end{macrocode} + % Externalize the graphic with the final size. +% \begin{macrocode} + \tikzscale@conditionalEnableExternalization{\fileName}% +% \end{macrocode} + % Finally, include the picture. Do it via a new measurement to be able to warn if it does not fit good enough. +% \begin{macrocode} + \sbox{\tikzscale@measuredSize}{\content}% + \usebox{\tikzscale@measuredSize}% + \tikzscale@warnIfSizeDifference{\requestedWidth}{\wd\tikzscale@measuredSize}{\fileName's width}% + \tikzscale@warnIfSizeDifference{\requestedHeight}{\ht\tikzscale@measuredSize}{\fileName's height}% +} +% \end{macrocode} +%\end{macro} +% + +%\begin{macro}{\tikzscale@resizePlotWithAxesRatioTo} +% \xcmd\tikzscale@resizePlotWithAxesRatioTo{\textbackslash wd or \textbackslash ht}{\textbackslash tikzscale@width or \textbackslash tikzscale@height}{to-be-scaled content}{file name} +% The first argument determines if a specific width or a specific height should be achieved by resizing. +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@resizePlotWithAxesRatioTo}{mmmm}{% + \def\dimension{#1}% + \def\variable{#2}% + \def\content{#3}% + \def\fileName{#4}% + \gdef\tikzscale@oldSizeDifference{0pt}% + \tikzscale@preparePlot +% \end{macrocode} + % Deactivate the externalization, as the measurements to determine the correct size should not be externalized. +% \begin{macrocode} + \tikzscale@conditionalDisableExternalization +% \end{macrocode} + % Improve the solution iteratively until it is good enough. +% \begin{macrocode} + \foreach \l in {1,...,\maxTestIterations}{% + \tikzscale@measureSize{\measuredSize}{\dimension}{\content}% +% \end{macrocode} + % Determine the remaining error and check if it is larger than a threshold. +% \begin{macrocode} + \pgfmathsetmacro{\sizeDifference}{\measuredSize - \requestedSize}% +% \end{macrocode} + % Output error in current iterion for debugging. +% \begin{macrocode} +% sizeDifference: \sizeDifference\\% Debugging +% \end{macrocode} + % Optimize if the absolute difference is too large. +% \begin{macrocode} + \tikzscale@ifSizeDifference{\sizeDifference}{% + \ifdefstring{\dimension}{\wd}{% + \pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@width - \sizeDifference}% + \pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@width / \requestedAxisRatio}% + }{% + \pgfmathsetglobalmacro{\tikzscale@height}{\tikzscale@height - \sizeDifference}% + \pgfmathsetglobalmacro{\tikzscale@width}{\tikzscale@height * \requestedAxisRatio}% + }% + \tikzscale@ifSizeDifference{\sizeDifference-\tikzscale@oldSizeDifference}{% + }{% + \tikzscale@includeNormalTikzpicture{#4}% + \gdef\tikzscale@alreadyIncluded{true}% + \breakforeach + }% + \pgfmathsetglobalmacro{\tikzscale@oldSizeDifference}{\sizeDifference}% + }{% + \breakforeach + }% + }% + \ifdef{\tikzscale@alreadyIncluded}{% + \global\undef\tikzscale@alreadyIncluded% + }{% +% \end{macrocode} + % Externalize the graphic with the final size. +% \begin{macrocode} + \tikzscale@conditionalEnableExternalization{\fileName}% +% \end{macrocode} + % Finally, include the picture. Do it via a new measurement to be able to warn if it does not fit good enough. +% \begin{macrocode} + \tikzscale@measureSize{\measuredFinal}{\dimension}{\content}% + \usebox{\tikzscale@measuredSize}% + \tikzscale@warnIfSizeDifference{\measuredFinal}{\requestedSize}{\fileName}% + }% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@measuredSize} +% \begin{macrocode} +\newsavebox{\tikzscale@measuredSize} +% \end{macrocode} +% \xcmd\measureSize{result variable name}{\cmd{\wd} or \cmd{\ht}}{to-be-measured content} +% \begin{macrocode} +\def\tikzscale@measureSize#1#2#3{% + \sbox{\tikzscale@measuredSize}{#3}% + \pgfmathsetmacro{#1}{#2\tikzscale@measuredSize}% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@ifSizeDifference} +% \xcmd\tikzscale@ifSizeDifference{size}{executed if true}{executed if false} +% \begin{macrocode} +\def\tikzscale@ifSizeDifference#1#2#3{% + \pgfmathparse{abs(#1)}% + \ifdimgreater{\pgfmathresult pt}{\tikzscale@accuracy}{% + #2% + }{% + #3% + }% +}% +% \end{macrocode} +%\end{macro} + + +%\begin{macro}{\tikzscale@measuredSize} +% \xcmd\tikzscale@warnIfSizeDifference{firstSize}{secondSize}{file name} +% \begin{macrocode} +\def\tikzscale@warnIfSizeDifference#1#2#3{% + \tikzscale@ifSizeDifference{#1-#2}{% + \PackageWarning{tikzscale}{Scaling of #3 was only\MessageBreak accurate to \pgfmathresult pt}% + }{}% +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@conditionalDisableExternalization} +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@conditionalDisableExternalization}{}{% + \tikzexternaldisable +} +% \end{macrocode} +%\end{macro} +% +%\begin{macro}{\tikzscale@conditionalEnableExternalization} +% Activate externalization of TikZ graphics iff it had been active before definitely disabling it for measurement purposes. The argument contains the file name. +% \begin{macrocode} +\NewDocumentCommand{\tikzscale@conditionalEnableExternalization}{m}{% +% \end{macrocode} + % For the externalization, set correct file name and only externalize the graphic with the final size. This produces a \href{http://old.nabble.com/minor-comment-about-TikZ-external-library-usage-tt31042245.html#a31042245}{known bug} +% \begin{macrocode} +% \tikzsetnextfilename{#1}% +% \edef\myprefix{\pwd} +% \end{macrocode} + % Get the current directory as a string and use it as an prefix, so that the graphic's PDF is generated in a subdirectory if the tikz file is located in a subdirectory, too. This is necessary, as the PDF file is searched for in the subdirectory in this case. This might be unnecessary due to the newly created path lookup logic. +% \begin{macrocode} +% \expandafter\tikzsetexternalprefix\expandafter{\tikzscale@pwd}% +% \expandnext{\tikzsetexternalprefix}{\tikzscale@pwd}% + \tikzexternalenable +} +% \end{macrocode} +%\end{macro} +% +% \makeatother +% \iffalse +% +% \fi +% +%\StopEventually{^^A +% \PrintChanges +%^^A \PrintIndex +%} +% +%\Finale \ No newline at end of file diff --git a/Master/texmf-dist/source/latex/tikzscale/tikzscale.ins b/Master/texmf-dist/source/latex/tikzscale/tikzscale.ins new file mode 100644 index 00000000000..273fa3d7c81 --- /dev/null +++ b/Master/texmf-dist/source/latex/tikzscale/tikzscale.ins @@ -0,0 +1,72 @@ +%% +%% This is file `tikzscale.ins', +%% generated with the docstrip utility. +%% +%% The original source files were: +%% +%% tikzscale.dtx (with options: `install') +%% ---------------------------------------------------------------- +%% 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 +%% ---------------------------------------------------------------- +%% +\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 + +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}} +} +\endbatchfile + +%% +%% Copyright (C) 2012 by Patrick Häcker +%% +%% 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.ins'. -- cgit v1.2.3