Debian Codegraph @

texmf-dist/source/latex/tikzinclude/tikzinclude.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2012 by Ferdinand Schwenk ([email protected]).
4 % Copyright (C) 2012 by Benjamin Berg ([email protected]).
5 %
6 % This file may be distributed and/or modified under the
7 % conditions of the LaTeX Project Public License, either
8 % version 1.3c of this license or (at your option) any later
9 % version. The latest version of this license is available at
10 % http://www.latex-project.org/lppl/.
11 %
12 % \fi
13 %
14 % \iffalse
15 %<*driver>
16 \ProvidesFile{tikzinclude.dtx}
17 %</driver>
18 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
19 %<package>\ProvidesPackage{tikzinclude}
20 %<*package>
21 [2012/22/02 v1.0 package for including only a specified tikz pictures from a file]
22 %</package>
23 %
24 %<*driver>
25 \documentclass[a4paper]{ltxdoc}
26 \usepackage{tikzinclude}[2012/12/23]
27 \EnableCrossrefs
28 \CodelineIndex
29 \RecordChanges
30 \begin{document}
31 \DocInput{tikzinclude.dtx}
32 \PrintChanges
33 \PrintIndex
34 \end{document}
35 %</driver>
36 % \fi
37 %
38 % \CheckSum{70}
39 %
40 % \CharacterTable
41 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
42 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
43 % Digits \0\1\2\3\4\5\6\7\8\9
44 % Exclamation \! Double quote \" Hash (number) \#
45 % Dollar \$ Percent \% Ampersand \&
46 % Acute accent \' Left paren \( Right paren \)
47 % Asterisk \* Plus \+ Comma \,
48 % Minus \- Point \. Solidus \/
49 % Colon \: Semicolon \; Less than \<
50 % Equals \= Greater than \> Question mark \?
51 % Commercial at \@ Left bracket \[ Backslash \\
52 % Right bracket \] Circumflex \^ Underscore \_
53 % Grave accent \` Left brace \{ Vertical bar \|
54 % Right brace \} Tilde \~}
55 %
56 %
57 % \changes{v1.0}{2012/12/23}{Initial version}
58 %
59 % \GetFileInfo{tikzinclude.dtx}
60 %
61 % \title{The \textsf{tikzinclude} package\thanks{This document
62 % corresponds to \textsf{tikzinclude}~\fileversion, dated \filedate.}}
63 % \author{Ferdinand Schwenk \texttt{[email protected]} \and
64 % Benjamin Berg \texttt{[email protected]}}
65 %
66 % \maketitle
67 %
68 % \begin{abstract}
69 % This package addresses the problem of importing only one TikZ-image from
70 % a file holding multiple images (i.e. different versions of the same picture).
71 % \end{abstract}
72 %
73 % \section{Introduction}
74 % Normaly I use one file per TikZ-image. This simplifies reusage of the images in
75 % different documents.
76 %
77 % When drawing different versions of the same image, for example to highlight
78 % parts of the image or provide localized versions, it is not practical to put
79 % each of the version in a separate file. Doing this would increase the risk
80 % of version mismatch.
81 %
82 % Because of this it can make sense to have all versions of one image or even
83 % different images inside the same source file. However, simply doing this
84 % makes it impossible to use the |\input| command as this would insert all
85 % images at the same time. This package solves the issue by allowing the user
86 % to only insert a single tikzpicture from a file.
87 %
88 % \section{Usage}
89 %
90 % To be able to select an image it necessary to name each drawing. This is done
91 % by assigning a figure name to the TikZ-Key |/tikzinclude/figure| at the
92 % beginning of the picture.
93 %
94 % \begin{verbatim}
95 % \begin{tikzpicture}[/tikzinclude/figure=foo]
96 % \node{foo};
97 % \end{tikzpicture}
98 % \end{verbatim}
99 %
100 % \DescribeMacro{\includetikzgraphics}
101 % After naming the images it is now possible to only include a specific image
102 % using the |\includetikzgraphics|\oarg{name}\marg{imagefile} command.\\
103 % \meta{name} is the name of the image that should be included. If \meta{name}
104 % is provided then only the picture with the given name is included, all other
105 % pictures are dropped.
106 % If \meta{name} is omitted all pictures in \meta{imagefile} are included. This
107 % gives the same result as if |\input| was used.\\
108 %
109 % \StopEventually{}
110 %
111 % \section{Implementation}
112 %
113 % The Package is depending on |ifthen| and |etoolbox|
114 % \begin{macrocode}
115 \RequirePackage{tikz}
116 \RequirePackage{ifthen}
117 \RequirePackage{etoolbox}
118 % \end{macrocode}
119 %
120 % Provide a new if condition that states if tikzinclude should be active or if
121 % all pictures should be included.
122 % \begin{macrocode}
123 \newif\if@tikzinclude@active\@tikzinclude@activefalse
124 % \end{macrocode}
125 %
126 % Store the original definition of |\pgfsys@typesetpicturebox|
127 % \begin{macrocode}
128 \let\@tikzinclude@typsetpicturebox\pgfsys@typesetpicturebox
129 % \end{macrocode}
130 %
131 % \begin{macro}{\@tikzinclude@picture@started}
132 % This internal macro sets |\par| to its original definition, if tikzinclude is
133 % used. Otherwise it should not have been changed.
134 % \begin{macrocode}
135 \newcommand{\@tikzinclude@picture@started}{%
136 \if@tikzinclude@active%
137 \let\par\@tikzinclude@par%
138 \fi%
139 }
140 % \end{macrocode}
141 % \end{macro}
142 %
143 % \begin{macro}{\@tikzinclude@picture@ended}
144 % This internal macro overwrites |\pgfsys@typesetpicturebox| with a macro that
145 % drops the next box entirely and then resets the definition of
146 % |\pgfsys@typesetpicturebox|. The overwrite only happens if the image should
147 % be suppressed.
148 % \begin{macrocode}
149 \newcommand{\@tikzinclude@picture@ended}{%
150 \if@tikzinclude@active%
151 \ifthenelse{%
152 \equal{\pgfkeysvalueof{/tikzinclude/figure}}{\pgfkeysvalueof{/tikzinclude/select}}%
153 }%
154 {}%
155 {%
156 \global\def\pgfsys@typesetpicturebox##1{%
157 \global\let\pgfsys@typesetpicturebox\@tikzinclude@typsetpicturebox%
158 }%
159 }%
160 \fi%
161 }
162 % \end{macrocode}
163 % \end{macro}
164 %
165 % Some hooks need to be installed.
166 % \begin{macrocode}
167 \BeforeBeginEnvironment{tikzpicture}{\if@tikzinclude@active%
168 \whileboolexpr{test{\ifdimgreater{\lastskip}{0pt}}}{\unskip}\fi}%
169 \AtBeginEnvironment{tikzpicture}{\@tikzinclude@picture@started%
170 }
171 \AtEndEnvironment{tikzpicture}{\@tikzinclude@picture@ended}
172 \AfterEndEnvironment{tikzpicture}{\ignorespaces}
173 % \end{macrocode}
174 %
175 % Set the TikZ-Keys to empty values. This is necessary to suppress some
176 % TikZ-warnings
177 % \begin{macrocode}
178 \pgfkeyssetvalue{/tikzinclude/figure}{}
179 \pgfkeyssetvalue{/tikzinclude/select}{}
180 % \end{macrocode}
181 %
182 % \begin{macro}{\includetikzgraphics}
183 % First it is checked if \meta{name} is provided or not. If no name is given a
184 % simple |\input| is performed.\\
185 % If \meta{name} is given it needs to be assigned to |/tikzinclude/select|.
186 % Any whitespace in the image file needs to be ignored, but whitespace inside
187 % the images should be unchanged. Therefore the definition of |\par| is stored
188 % and |\par| is overridden outside of any TikZ-environment.\par
189 % Then tikzinclude is activated and the image file is included using the |\input|
190 % command.
191 % After the picture is included some additional whitespace needs to be erased.
192 % To have a defined starting position and to avoid the deletion of whitespace
193 % added by the user a 0pt kerning is inserted prior to the inclusion of the
194 % image file.\\
195 % The whitespace that needs to be removed is caused by newlines at the end
196 % of TikZ-environments.\par
197 % In the end all settings are restored.
198 % \begin{macrocode}
199 \newcommand\includetikzgraphics[2][]{%
200 \begingroup%
201 \ifthenelse{\equal{#1}{}}%
202 {%
203 \input{#2}%
204 }%
205 {%
206 \let\@tikzinclude@par\par%
207 \def\par{}%
208 \pgfkeyssetvalue{/tikzinclude/select}{#1}%
209 \@tikzinclude@activetrue%
210 \kern0pt\input{#2}%
211 \whileboolexpr{test{\ifdimgreater{\lastskip}{0pt}}}{\unskip}%
212 \@tikzinclude@activefalse%
213 \let\par\@tikzinclude@par%
214 }%
215 \endgroup%
216 }
217 % \end{macrocode}
218 % \end{macro}
219 %
220 % \Finale
221 \endinput
222