Skip to content

Commit 96ed210

Browse files
Update to most recent version
1 parent f852623 commit 96ed210

File tree

4 files changed

+94
-58
lines changed

4 files changed

+94
-58
lines changed

test-tikzscale.tex

+12-8
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@
3030
% \includegraphics{example-grid-100x100bp.eps}%
3131
\item Use includegraphics with column width\\%
3232
\includegraphics[width=\linewidth]{example-grid-100x100bp.jpg}%
33+
\item Use \texttt{input} with pgfplots\\%
34+
\input{pgfplots-test.tikz}%
3335
\item Use pgfplots without optional parameter\\%
3436
\includegraphics{pgfplots-test}%
3537
\item Use pgfplots with given width and height\\%
3638
\includegraphics[width=\linewidth,height=0.3\linewidth]{pgfplots-test}%
37-
\item Use \texttt{input} with pgfplots and given width and height\\%
38-
\makeatletter%
39-
\gdef\tikzscale@width{\linewidth}%
40-
\gdef\tikzscale@height{0.3\linewidth}%
41-
\makeatother%
42-
\input{pgfplots-test.tikz}%
39+
\item Use includegraphics with only a node\\%
40+
\includegraphics{testNode.tikz}%
41+
% \item Use includegraphics with scaling only a node results in an error\\%
42+
% \includegraphics[width=\linewidth]{testNode.tikz}%
43+
\item Use includegraphics with a two dimensional plot\\%
44+
% TODO: Die Skalierung ändert sich in Abhängigkeit ob die Plots davor auskommentiert sind
45+
\includegraphics{testgraphic2D.tikz}%
46+
\item Use includegraphics with a scaled two dimensional plot\\%
47+
\includegraphics[width=0.4\linewidth,height=0.4\linewidth]{testgraphic2D.tikz}%
4348
\end{itemize}
44-
\end{document}
45-
49+
\end{document}

testNode.tikz

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\begin{tikzpicture}
2+
\node[draw] (0,0) {Node};
3+
\end{tikzpicture}

testgraphic2D.tikz

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
\begin{tikzpicture}
2+
\begin{axis}[%
3+
scale only axis,
4+
xmin=-25, xmax=25,
5+
ymin=-25, ymax=25]
6+
\addplot graphics [xmin=-25, xmax=25, ymin=-25, ymax=25] {example-grid-100x100bp.png};
7+
\end{axis}
8+
\end{tikzpicture}

tikzscale.dtx

+71-50
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ and the derived files tikzscale.ins,
303303
% 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.
304304
%
305305
% \subsection{Externalization library}
306-
% TikZ externalization library is supported.
306+
% 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.
307307
%
308308
% \section{Further Ideas}
309309
% \begin{itemize}
310-
% \item At least give a clear error message for unscalable plots.
311-
% \item the package can test if a pgfplot is used (needed due to keepAspectRatio in pgfplots) by changing the width and or height and measuring. If nothing changes, it must be a normal tikzpicture (the argument does not hold the other way round).
310+
% \item How to support for imagesc plots something like "scale only axis, width=\cmd{\linewidth}, height=\cmd{\linewidth}" without writing into the margin?
311+
% \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).
312312
% \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.
313313
% \end{itemize}
314314
%
@@ -346,27 +346,17 @@ and the derived files tikzscale.ins,
346346
%\end{macro}
347347
%
348348
%\begin{macro}{\tikzscale@scale}
349-
% Set these as default values to allow a preview of tikzpicture environments in ktikz or the like. Global definitions are necessary, as local definitions result in errors in some situations.
350-
%
351-
% Use a scale factor for all tikzpicture environments. Set the factor to 1 as default, so if there are no additional scaling demands, just keep the tikzpicture unscaled. 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 squared in the inner one: http://tex.stackexchange.com/questions/38605/scaling-a-tikz-figure-from-an-external-file
352349
% \begin{macrocode}
353350
\@ifpackageloaded{tikz}{%
354-
\gdef\tikzscale@scale{1}%
355-
\tikzset{every picture/.style={scale=\tikzscale@scale,every picture/.style={}}}%
356351
% \end{macrocode}
357352
% 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.
358353
% \begin{macrocode}
359354
\newlength{\tikzscale@accuracy}%
360355
\setlength{\tikzscale@accuracy}{0.01pt}%
361356
%
357+
% 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.
362358
\def\maxTestIterations{10}%
363359
}{}%
364-
%
365-
\@ifpackageloaded{pgfplots}{%
366-
\gdef\tikzscale@width{\linewidth}%
367-
\gdef\tikzscale@height{\linewidth}%
368-
\pgfplotsset{every axis/.style={width=\tikzscale@width,height=\tikzscale@height,every axis/.style={}}}%
369-
}{}%
370360
}
371361
% \end{macrocode}
372362
%\end{macro}
@@ -408,7 +398,7 @@ and the derived files tikzscale.ins,
408398
%\end{macro}
409399

410400
%
411-
%\begin{macro}{\includegraphics}
401+
%\begin{macro}{\activatetikzscale}
412402
% \begin{macrocode}
413403
\AtEndPreamble{%
414404
% \end{macrocode}
@@ -427,30 +417,45 @@ and the derived files tikzscale.ins,
427417
\def\tikzscale@graphicspath{{}}%
428418
}%
429419
% \end{macrocode}
430-
% \item Save the \cmd{\includegraphics} \href{ftp://ftp.tu-chemnitz.de/pub/tex/macros/latex/required/graphics/grfguide.pdf}{command}, to have a consistent user interface.
420+
% \item 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.
431421
% \begin{macrocode}
432422
\LetLtxMacro{\tikzscale@oldincludegraphics}{\includegraphics}%
433-
\RenewDocumentCommand{\includegraphics}{om}{%
423+
\LetLtxMacro{\includegraphics}{\tikzscale@includegraphics}%
424+
}
425+
% \end{macrocode}
426+
%\end{macro}
427+
%
428+
%\begin{macro}{\includegraphics}
429+
% \begin{macrocode}
430+
\NewDocumentCommand{\tikzscale@includegraphics}{om}{%
434431
% \end{macrocode}
435-
% Find the exact file name, as the ending and the path could be omitted.
432+
% 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.
436433
% \begin{macrocode}
437-
\tikzscale@findExactFileName{tikzscale@fileName}{#2}%
434+
\begingroup
438435
% \end{macrocode}
439-
% Check if the found file is a TikZ file.
436+
% Find the exact file name, as the ending and the path could be omitted.
437+
% \begin{macrocode}
438+
\tikzscale@findExactFileName{tikzscale@fileName}{#2}%
439+
% \end{macrocode}
440+
% Check if the found file is a TikZ file.
440441
% \begin{macrocode}
441-
\tikzscale@isTikzFile{tikzscale@testTikzFile}{\tikzscale@fileName}
442-
\ifcsdef{tikzscale@testTikzFile}{
443-
\def\function{\tikzscale@includetikz}%
444-
}{%
445-
\def\function{\tikzscale@oldincludegraphics}%
446-
}%
447-
\IfNoValueTF{#1}{%
448-
\function{\tikzscale@fileName}%
449-
}{%
450-
\function[#1]{\tikzscale@fileName}%
451-
}%
442+
\tikzscale@isTikzFile{tikzscale@testTikzFile}{\tikzscale@fileName}
443+
\ifcsdef{tikzscale@testTikzFile}{
444+
% \end{macrocode}
445+
% 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.
446+
% \begin{macrocode}
447+
\LetLtxMacro{\includegraphics}{\tikzscale@oldincludegraphics}%
448+
\def\function{\tikzscale@includetikz}%
449+
}{%
450+
\def\function{\tikzscale@oldincludegraphics}%
452451
}%
453-
}
452+
\IfValueTF{#1}{%
453+
\function[#1]{\tikzscale@fileName}%
454+
}{%
455+
\function{\tikzscale@fileName}%
456+
}%
457+
\endgroup
458+
}%
454459
% \end{macrocode}
455460
%\end{macro}
456461
%
@@ -518,15 +523,10 @@ and the derived files tikzscale.ins,
518523
% If only one of width or height are given, scale proportionally to fullfil 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.
519524
% \begin{macrocode}
520525
\NewDocumentCommand{\tikzscale@includetikz}{>{\SplitList,}om}{%
521-
% \end{macrocode}
522-
% 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.
523-
% \begin{macrocode}
524526
\IfNoValueTF{#1}{%
525527
\input{#2}%
526528
}{%
527-
\begingroup
528529
\tikzscale@includeTikzSplitArguments{#2}#1%
529-
\endgroup
530530
}
531531
}
532532
% \end{macrocode}
@@ -542,9 +542,14 @@ and the derived files tikzscale.ins,
542542
%\begin{macro}{\tikzscale@includeTikzSplitArguments}
543543
% \begin{macrocode}
544544
\NewDocumentCommand{\tikzscale@includeTikzSplitArguments}{m>{\SplitList=}m>{\SplitList=}g}{%
545+
% \end{macrocode}
546+
% 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.
547+
% \begin{macrocode}
545548
\IfNoValueTF{#3}{%
549+
\tikzset{every picture/.style={scale=\tikzscale@scale,every picture/.style={}}}%
546550
\tikzscale@includeTikzOneArgument#2{#1}%
547551
}{%
552+
\pgfplotsset{every axis/.append style={width=\tikzscale@width,height=\tikzscale@height,every axis/.style={}}}%
548553
\tikzscale@includeTikzTwoArguments#2#3{#1}%
549554
}%
550555
}
@@ -556,13 +561,14 @@ and the derived files tikzscale.ins,
556561
% \begin{macrocode}
557562
\NewDocumentCommand{\tikzscale@includeTikzOneArgument}{mmm}{%
558563
% \end{macrocode}
559-
% Use \cmd{\pgfmathsetmacro}, to allow calculations in the given argument.
564+
% Use \cmd{\pgfmathsetmacro}, to allow calculations in the given argument. Do the calculation after checking the argument to output a useful error message in case the key is wrong.
560565
% \begin{macrocode}
561-
\pgfmathsetmacro{\tikzsize}{#2}%
562566
\ifstrequal{#1}{width}{%
567+
\pgfmathsetmacro{\tikzsize}{#2}%
563568
\tikzscale@scaleTo{\wd}{\input{#3}}{#3}%
564569
}{%
565570
\ifstrequal{#1}{height}{%
571+
\pgfmathsetmacro{\tikzsize}{#2}%
566572
\tikzscale@scaleTo{\ht}{\input{#3}}{#3}%
567573
}{%
568574
\tikzscale@invalidKeyError{#1}%
@@ -595,6 +601,9 @@ and the derived files tikzscale.ins,
595601
\tikzscale@invalidKeyError{#1 or #3}%
596602
}%
597603
}%
604+
% \end{macrocode}
605+
% Try to set initial sizes close to the requested sizes, to improve the optimization's speed.
606+
% \begin{macrocode}
598607
\def\tikzscale@width{\requestedWidth}%
599608
\def\tikzscale@height{\requestedHeight}%
600609
% \end{macrocode}
@@ -624,18 +633,14 @@ and the derived files tikzscale.ins,
624633
% \end{macrocode}
625634
% 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.
626635
% \begin{macrocode}
627-
\pgfmathparse{abs(\measuredSecond - \tikzsize)}%
636+
\tikzscale@ifSizeDifference{\measuredSecond - \tikzsize}{%
628637
% \end{macrocode}
629-
% Externalize the graphic with the final size.
638+
% If a plot is not scalable (e.g. consisting of a node only), but is not correctly scaled, exit with an error.
630639
% \begin{macrocode}
631-
\tikzscale@conditionalEnableExternalization{#3}
632-
\ifdimless{\pgfmathresult pt}{\tikzscale@accuracy}{%
633-
% \end{macrocode}
634-
% Reusing the box' content, instead of redrawing the graphic, were probably faster, but it would bypass an externalization.
635-
% \begin{macrocode}
636-
% \usebox{\tikzscale@measuredSize}%
637-
#2
638-
}{%
640+
\tikzscale@ifSizeDifference{\measuredFirst - \measuredSecond}{%
641+
}{%
642+
\PackageError{tikzscale}{Requested to scale unscalable graphic}{Do not set width or height for graphic in\MessageBreak #3}
643+
}
639644
% \end{macrocode}
640645
% We know, that the variable sized area scales with the scaling factor, thus it holds
641646
% \cmd{\scale} * \cmd{\variableFirst} = \cmd{\variableSecond},
@@ -657,7 +662,9 @@ and the derived files tikzscale.ins,
657662
% \begin{macrocode}
658663
\foreach \l in {1,...,\maxTestIterations}{%
659664
\tikzscale@measureSize{\measuredIntermediate}{#1}{#2}%
660-
%
665+
% \end{macrocode}
666+
% Optimize until the absolute difference is small enough, although the (relative) size ratios are used to calculate a new scaling factor.
667+
% \begin{macrocode}
661668
\tikzscale@ifSizeDifference{\measuredIntermediate-\tikzsize}{%
662669
% \end{macrocode}
663670
% First divide before multiply to avoid overflowing (at 16384).
@@ -668,12 +675,26 @@ and the derived files tikzscale.ins,
668675
\breakforeach%
669676
}%
670677
}%
678+
% \end{macrocode}
679+
% Externalize the graphic with the final size.
680+
% \begin{macrocode}
681+
\tikzscale@conditionalEnableExternalization{#3}%TODO: noch verschieben
671682
% \end{macrocode}
672683
% Finally, include the picture. Do it via a new measurement to be able to warn if it does not fit good enough.
673684
% \begin{macrocode}
674685
\tikzscale@measureSize{\measuredFinal}{#1}{#2}%
675686
\usebox{\tikzscale@measuredSize}%
676687
\tikzscale@warnIfSizeDifference{\measuredFinal}{\tikzsize}{#3}%
688+
}{%
689+
% \end{macrocode}
690+
% Externalize the graphic with the final size.
691+
% \begin{macrocode}
692+
\tikzscale@conditionalEnableExternalization{#3}%TODO: noch verschieben
693+
% \end{macrocode}
694+
% Reusing the box' content, instead of redrawing the graphic, were probably faster, but it would bypass an externalization.
695+
% \begin{macrocode}
696+
% \usebox{\tikzscale@measuredSize}%
697+
#2
677698
}%
678699
}
679700
% \end{macrocode}

0 commit comments

Comments
 (0)