summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/stex/source/extensions/tikzinput.dtx
blob: acb6e4bc94bc87165527968a71dd44b6323410c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
% \iffalse meta-comment
% An Infrastructure for Semantic Macros and Module Scoping
% Copyright (c) 2019 Michael Kohlhase, all rights reserved
%                this file is released under the
%                LaTeX Project Public License (LPPL)
% 
% The original of this file is in the public repository at 
% http://github.com/sLaTeX/sTeX/
%
% TODO update copyright  
%
%<*driver>
\def\libfolder#1{../../lib/#1}
\input{../../doc/stex-docheader}

\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{Tikzinput: Treating TIKZ code as images
% 	\thanks{Version {\fileversion} (last revised {\filedate})} 
% }
%
% \author{Michael Kohlhase, Dennis Müller\\
% 	FAU Erlangen-Nürnberg\\
% 	\url{http://kwarc.info/}
% }
%
% \maketitle
%
% \ifinfulldoc\else
% \begin{abstract}
%   This is the documentation for the \pkg{tikzinput} package.  For a more high-level
%   introduction, see \href{\basedocurl/manual.pdf}{the \sTeX Manual} or the
%   \href{\basedocurl/stex.pdf}{full \sTeX documentation}.
% 
%   In some situations it is more efficient externalize the TIKZ pictures into separate
%   (standalone) files, to let {\LaTeX} handle the TIKZ pictures to generate an image, and
%   just load it via the usual {\LaTeX} graphics packages. The |tikzinput| package
%   supports this workflow, and allows to switch back to native TIKZ via a package option.
% \end{abstract}
%
% \tableofcontents
% 
% \input{../../doc/packages/stex-tikzinput}
% \fi
%
% \begin{documentation}\label{pkg:tikzinput:doc}
%
%
% \section{Macros and Environments}\label{pkg:tikzinput:doc:macros}
%
% \end{documentation}
%
% \begin{implementation}\label{pkg:tikzinput:impl}
%
% \section{Tikzinput Implementation}
%
%    \begin{macrocode}
%<@@=tikzinput>
%<*package>

%%%%%%%%%%%%%   tikzinput.dtx   %%%%%%%%%%%%%

%    \end{macrocode}
%
%    \begin{macrocode}
\ProvidesExplPackage{tikzinput}{2022/09/14}{3.2.0}{tikzinput package}
\RequirePackage{l3keys2e}

\keys_define:nn { tikzinput } {
  image   .bool_set:N   = \c_tikzinput_image_bool,
  image   .default:n    = false ,
  unknown   .code:n       = {}
}

\ProcessKeysOptions { tikzinput }

\bool_if:NTF \c_tikzinput_image_bool {
  \RequirePackage{graphicx}

  \providecommand\usetikzlibrary[]{}
  \newcommand\tikzinput[2][]{\includegraphics[#1]{#2}}
}{
  \RequirePackage{tikz}
  \RequirePackage{standalone}

  \newcommand \tikzinput [2] [] {
    \setkeys{Gin}{#1}
    \ifx \Gin@ewidth \Gin@exclamation
      \ifx \Gin@eheight \Gin@exclamation
        \input { #2 }
      \else
        \resizebox{!}{ \Gin@eheight }{
          \input { #2 }
        }
      \fi
    \else
      \ifx \Gin@eheight \Gin@exclamation
        \resizebox{ \Gin@ewidth }{!}{
          \input { #2 }
        }
      \else
        \resizebox{ \Gin@ewidth }{ \Gin@eheight }{
          \input { #2 }
        }
      \fi
    \fi
  }
}

\newcommand \ctikzinput [2] [] {
  \begin{center}
    \tikzinput [#1] {#2}
  \end{center}
}

\@ifpackageloaded{stex}{
  \RequirePackage{stex-tikzinput}
}{}

%    \end{macrocode}
%
%
%    \begin{macrocode}
%</package>
%<*stex>
%    \end{macrocode}
%
%    \begin{macrocode}
\ProvidesExplPackage{stex-tikzinput}{2022/09/14}{3.2.0}{stex-tikzinput}
\RequirePackage{stex}
\RequirePackage{tikzinput}

\newcommand\mhtikzinput[2][]{%
  \def\Gin@mhrepos{}\setkeys{Gin}{#1}%
  \stex_in_repository:nn\Gin@mhrepos{
    \tikzinput[#1]{\mhpath{##1}{#2}}
  }
}
\newcommand\cmhtikzinput[2][]{\begin{center}\mhtikzinput[#1]{#2}\end{center}}

\cs_new_protected:Nn \_@@_usetikzlibrary:nn {
  \pgfkeys@spdef\pgf@temp{#1}
  \expandafter\ifx\csname tikz@library@\pgf@temp @loaded\endcsname\relax%
  \expandafter\global\expandafter\let\csname tikz@library@\pgf@temp @loaded\endcsname=\pgfutil@empty%
  \expandafter\edef\csname tikz@library@#1@atcode\endcsname{\the\catcode`\@}
  \expandafter\edef\csname tikz@library@#1@barcode\endcsname{\the\catcode`\|}
  \expandafter\edef\csname tikz@library@#1@dollarcode\endcsname{\the\catcode`\$}
  \catcode`\@=11
  \catcode`\|=12
  \catcode`\$=3
  \pgfutil@InputIfFileExists{#2}{}{}
  \catcode`\@=\csname tikz@library@#1@atcode\endcsname
  \catcode`\|=\csname tikz@library@#1@barcode\endcsname
  \catcode`\$=\csname tikz@library@#1@dollarcode\endcsname
}


\newcommand\libusetikzlibrary[1]{
  \prop_if_exist:NF \l_stex_current_repository_prop {
    \msg_error:nnn{stex}{error/notinarchive}\libusetikzlibrary
  } 
  \prop_get:NnNF \l_stex_current_repository_prop {id} \l_tmpa_str {
    \msg_error:nnn{stex}{error/notinarchive}\libusetikzlibrary
  }
  \seq_clear:N \l_@@_libinput_files_seq
  \seq_set_eq:NN \l_tmpa_seq \c_stex_mathhub_seq
  \seq_set_split:NnV \l_tmpb_seq / \l_tmpa_str

  \bool_while_do:nn { ! \seq_if_empty_p:N \l_tmpb_seq }{
    \str_set:Nx \l_tmpa_str {\stex_path_to_string:N \l_tmpa_seq / meta-inf / lib / tikzlibrary #1 .code.tex}
    \IfFileExists{ \l_tmpa_str }{
      \seq_put_right:No \l_@@_libinput_files_seq \l_tmpa_str
    }{}
    \seq_pop_left:NN \l_tmpb_seq \l_tmpa_str
    \seq_put_right:No \l_tmpa_seq \l_tmpa_str
  }

  \str_set:Nx \l_tmpa_str {\stex_path_to_string:N \l_tmpa_seq / lib / tikzlibrary #1 .code.tex}
  \IfFileExists{ \l_tmpa_str }{
    \seq_put_right:No \l_@@_libinput_files_seq \l_tmpa_str
  }{}

  \seq_if_empty:NTF \l_@@_libinput_files_seq {
    \msg_error:nnxx{stex}{error/nofile}{\exp_not:N\libusetikzlibrary}{tikzlibrary #1 .code.tex}
  }{
    \int_compare:nNnTF {\seq_count:N \l_@@_libinput_files_seq} = 1 {
      \seq_map_inline:Nn \l_@@_libinput_files_seq {
        \_@@_usetikzlibrary:nn{#1}{ ##1 }
      }
    }{
      \msg_error:nnxx{stex}{error/twofiles}{\exp_not:N\libusetikzlibrary}{tikzlibrary #1 .code.tex}
    }
  }
}
%    \end{macrocode}
%
%    \begin{macrocode}
%</stex>
%    \end{macrocode}
%
% \end{implementation}
% \ifinfulldoc\else\printbibliography\fi
%
% \PrintIndex

% \endinput
% Local Variables:
% mode: doctex
% TeX-master: t
% End:

%  LocalWords:  libfolder jobname.dtx tikzinput.dtx usetikzlibrary Gin@ewidth Gin@eheight
%  LocalWords:  ctikzinput mhpath pgfkeys@spdef pgfutil@empty 1@atcode catcode l_tmpa_str
%  LocalWords:  pgfutil@InputIfFileExists libusetikzlibrary msg_error:nnn notinarchive
%  LocalWords:  seq_clear:N seq_set_eq:NN l_tmpa_seq c_stex_mathhub_seq seq_set_split:NnV
%  LocalWords:  l_tmpb_seq seq_if_empty_p:N tikzlibrary seq_put_right:No seq_pop_left:NN