summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/tcolorbox/tcblistingscore.code.tex
blob: 1b76e30fbe3c13b007359da710063332c1171e46 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
%% The LaTeX package tcolorbox - version 4.31 (2020/07/31)
%% tcblistingscode.code.tex: Auxiliary code for colorboxes with listings
%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2006-2020 by Prof. Dr. Dr. Thomas F. Sturm <thomas dot sturm at unibw dot de>
%% -------------------------------------------------------------------------------------------
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `author-maintained'.
%%
%% This work consists of all files listed in README
%%
%\makeatletter
\tcb@set@library@version{4.31}

\tcbuselibrary{pro@cessing}

\def\tcbuselistingtext@input{\input{\kvtcb@listingfile}}%
\def\tcbuselistingtext{\tcbuselistingtext@input}%
\def\tcbuselistingcomment{\kvtcb@listingcomment}%
\def\tcb@listing@listing{\tcbuselistinglisting}

\def\tcb@listing@listingAndOther{%
   \tcbuselistinglisting%
   \tcblower%
   \tcb@use@listing@other%
}

\def\tcb@listing@otherAndListing{%
   \tcb@use@listing@other%
   \tcblower%
   \tcbuselistinglisting%
}

\newenvironment{tcb@lower@outside}[1]{%
  \tcbset{savedelimiter=tcb@lower@outside}\tcb@minipage{#1}%
  \color{tcbcollower}\kvtcb@fontlower\kvtcb@halignlower\tcb@insert@before@lower}%
  {\tcb@insert@after@lower\endminipage}

\def\tcb@use@listing@other@reset{%
  \tcbset{every box on layer 1/.style={reset,every box}}%
  \tcb@use@listing@other}

\def\tcb@use@lower@outside#1{%
  \setbox\z@=\hbox{\begin{tcb@lower@outside}{#1}%
    \tcb@use@listing@other@reset%
  \end{tcb@lower@outside}}%
  \tcbdimto{\tcb@temp@wd}{\wd\z@}%
  \unhbox\z@%
}

\def\tcb@inputlisting@out@otherAboveListing{%
  {\kvtcb@beforebox}\noindent%
  \iftcb@lowervisible%
    \tcb@use@lower@outside{\kvtcb@width}%
    \par\vskip-\parskip\vspace*{\kvtcb@middle}\noindent%
  \fi%
  \begin{tcolorbox}[before=,savedelimiter=tcolorbox]\tcbuselistinglisting\end{tcolorbox}%
}

\def\tcb@inputlisting@out@otherAboveListing@Decoupled{%
  \iftcb@lowervisible%
    {\par\tcb@use@listing@other@reset\par}%
  \fi%
  \begin{tcolorbox}[savedelimiter=tcolorbox]\tcbuselistinglisting\end{tcolorbox}%
}

\def\tcb@inputlisting@out@listingAboveOther{%
  \begin{tcolorbox}[after=,savedelimiter=tcolorbox]\tcbuselistinglisting\end{tcolorbox}%
  \iftcb@lowervisible\par\vskip-\parskip\vspace*{\kvtcb@middle}\noindent%
    \tcb@use@lower@outside{\kvtcb@width}%
  \fi%
  {\kvtcb@afterbox}%
}

\def\tcb@inputlisting@out@listingAboveOther@Decoupled{%
  \begin{tcolorbox}[savedelimiter=tcolorbox]\tcbuselistinglisting\end{tcolorbox}%
  \iftcb@lowervisible%
    {\par\tcb@use@listing@other@reset\par}%
  \fi%
}


\newenvironment{tcb@outside@environment}{%
  \tcb@before@unbroken%
  \begin{minipage}{\tcb@width}}%
{%
  \end{minipage}%
  \tcb@after@unbroken}

\def\tcb@inputlisting@out@prepare{%
  \tcbdimto\tcb@width{\kvtcb@width}%
  \tcbdimto\tcb@innerwidth{\tcb@width-\kvtcb@left@rule-\kvtcb@right@rule}%
  \tcbdimto\tcb@temp{\tcb@innerwidth-\kvtcb@boxsep*2}%
  \tcbdimto\tcb@w@upper{\tcb@temp-\kvtcb@leftupper-\kvtcb@rightupper}%
  \tcbset{breakable@false}%
  \def\tcb@minipage{\csname tcb@minipage@\kvtcb@sbs@align\endcsname}%
  \tcbdimto\tcb@w@upper@real{\tcb@w@upper}%
  \tcbdimto\tcb@w@sbs{\tcb@w@upper@real-\kvtcb@sbs@gap}%
  \tcb@sbs@quota%
  \ifx\kvtcb@float\@empty%
    \let\tcb@before@unbroken=\kvtcb@beforebox%
    \let\tcb@after@unbroken=\kvtcb@afterbox%
  \else%
    \edef\tcb@before@unbroken{\noexpand\tcb@float@env@begin{tcbfloat}[\kvtcb@float]}%
    \let\tcb@after@unbroken=\tcb@float@env@end%
  \fi%
}

\def\tcb@use@lower@outside@side#1{%
  \setbox\z@=\hbox{\begin{tcb@lower@outside}{#1}%
    \tcb@use@listing@other@reset%
  \end{tcb@lower@outside}}%
  \tcb@box@align\z@%
  \tcbdimto{\tcb@temp@wd}{\wd\z@}%
  \unhbox\z@%
}

\def\tcb@inputlisting@out@listingAndOther{%
  \tcb@inputlisting@out@prepare%
  \tcbdimto\tcb@w@outside{\tcb@w@lower}%
  \tcbdimto\tcb@w@inside{\tcb@width-\tcb@w@lower-\kvtcb@sbs@gap}%
  \begin{tcb@outside@environment}%
  \setbox\z@=\hbox{\tcb@minipage{\tcb@w@inside}%
  \begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false,savedelimiter=tcolorbox]\tcbuselistinglisting\end{tcolorbox}%
  \endminipage}%
  \tcb@box@align\z@%
  \unhbox\z@%
  \kern\kvtcb@sbs@gap%
  \iftcb@lowervisible%
    \tcb@use@lower@outside@side{\tcb@w@outside}%
  \fi%
  \end{tcb@outside@environment}%
}

\def\tcb@inputlisting@out@otherAndListing{%
  \tcb@inputlisting@out@prepare%
  \tcbdimto\tcb@w@outside{\tcb@w@upper}%
  \tcbdimto\tcb@w@inside{\tcb@width-\tcb@w@upper-\kvtcb@sbs@gap}%
  \begin{tcb@outside@environment}%
  \iftcb@lowervisible%
    \tcb@use@lower@outside@side{\tcb@w@outside}%
    \tcbdimto{\tcb@temp@wd}{\tcb@w@outside-\tcb@temp@wd+\kvtcb@sbs@gap}%
  \else%
    \tcbdimto{\tcb@temp@wd}{\tcb@w@outside+\kvtcb@sbs@gap}%
  \fi%
  \kern\tcb@temp@wd%
  \setbox\z@=\hbox{\tcb@minipage{\tcb@w@inside}%
  \begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false,savedelimiter=tcolorbox]\tcbuselistinglisting\end{tcolorbox}%
  \endminipage}%
  \tcb@box@align\z@%
  \unhbox\z@%
  \end{tcb@outside@environment}%
}

\def\tcb@inputlisting@inside{%
  \begin{tcolorbox}[capture=\tcb@listing@capture,savedelimiter=tcolorbox]\tcb@listing@process\end{tcolorbox}%
}

\long\def\tcb@run@system@command#1{%
  \ifcase\pdf@shellescape\relax
    \tcb@error{You must invoke LaTeX with the -shell-escape flag}{Pass the -shell-escape flag to LaTeX.}%
  \or\or
    \tcb@warning{System call with restricted shell escape may fail}%
  \fi%
  \ShellEscape{#1}%
  \relax%
}


\tcbset{%
  listing file/.store in=\kvtcb@listingfile,%
  comment/.store in=\kvtcb@listingcomment,%
  listing@process@inside/.code n args={2}{%
    \let\tcb@inputlisting\tcb@inputlisting@inside%
    \let\tcb@listing@process=#1%
    \let\tcb@use@listing@other=#2},%
  listing@process@outside/.code n args={2}{%
    \let\tcb@inputlisting=#1%
    \let\tcb@use@listing@other=#2},%
  listing only/.style={listing@process@inside={\tcb@listing@listing}{\tcbuselistingtext}},%
  text only/.style={listing@process@inside={\tcbuselistingtext}{\tcbuselistingtext}},%
  comment only/.style={listing@process@inside={\tcbuselistingcomment}{\tcbuselistingcomment}},%
  listing and text/.style={listing@process@inside={\tcb@listing@listingAndOther}{\tcbuselistingtext}},%
  text and listing/.style={listing@process@inside={\tcb@listing@otherAndListing}{\tcbuselistingtext}},%
  text above listing/.style={listing@process@outside={\tcb@inputlisting@out@otherAboveListing}{\tcbuselistingtext}},%
  text above* listing/.style={listing@process@outside={\tcb@inputlisting@out@otherAboveListing@Decoupled}{\tcbuselistingtext}},%
  listing above text/.style={listing@process@outside={\tcb@inputlisting@out@listingAboveOther}{\tcbuselistingtext}},%
  listing above* text/.style={listing@process@outside={\tcb@inputlisting@out@listingAboveOther@Decoupled}{\tcbuselistingtext}},%
  text outside listing/.style={listing@process@outside={\tcb@inputlisting@out@otherAndListing}{\tcbuselistingtext}},%
  listing outside text/.style={listing@process@outside={\tcb@inputlisting@out@listingAndOther}{\tcbuselistingtext}},%
  text side listing/.style={sidebyside,text and listing},%
  listing side text/.style={sidebyside,listing and text},%
  listing and comment/.style={listing@process@inside={\tcb@listing@listingAndOther}{\tcbuselistingcomment}},%
  comment and listing/.style={listing@process@inside={\tcb@listing@otherAndListing}{\tcbuselistingcomment}},%
  comment above listing/.style={listing@process@outside={\tcb@inputlisting@out@otherAboveListing}{\tcbuselistingcomment}},%
  comment above* listing/.style={listing@process@outside={\tcb@inputlisting@out@otherAboveListing@Decoupled}{\tcbuselistingcomment}},%
  listing above comment/.style={listing@process@outside={\tcb@inputlisting@out@listingAboveOther}{\tcbuselistingcomment}},%
  listing above* comment/.style={listing@process@outside={\tcb@inputlisting@out@listingAboveOther@Decoupled}{\tcbuselistingcomment}},%
  comment outside listing/.style={listing@process@outside={\tcb@inputlisting@out@otherAndListing}{\tcbuselistingcomment}},%
  listing outside comment/.style={listing@process@outside={\tcb@inputlisting@out@listingAndOther}{\tcbuselistingcomment}},%
  comment side listing/.style={sidebyside,comment and listing},%
  listing side comment/.style={sidebyside,listing and comment},%
  listing engine/.is choice,%
  image comment/.style 2 args={comment={\includegraphics[#1]{#2}}},%
  comment style/.store in=\kvtcb@commentstyle,%
  tcbimage comment/.style={comment={\let\tcb@trans@commentstyle\kvtcb@commentstyle\tcbincludegraphics[code={\pgfkeysalsofrom\tcb@trans@commentstyle}]{#1}}},
  pdf extension/.store in=\kvtcb@pdf@extension,%
  pdf comment/.style={comment={\let\tcb@trans@commentstyle\kvtcb@commentstyle\filename@parse{#1}\edef\tcb@trans@commentfile{\filename@area\filename@base.\kvtcb@pdf@extension}%
    \begin{tcbraster}[raster halign=center,sharp corners,size=tight,colback=white,code={\pgfkeysalsofrom\tcb@trans@commentstyle}]\tcbincludepdf{\tcb@trans@commentfile}\end{tcbraster}}},
  pdf comment/.default={\kvtcb@listingfile},
  process code/.code={\appto\tcb@listing@process@hook{#1}},%
  no process/.code={\def\tcb@listing@process@hook{}},%
  compilable listing/.style={listing file={\jobname-listing-\thetcblistingcount.tex}},
  run system command/.style={process code={\tcb@run@system@command{#1}}},%
  run@system@command/.style 2 args={run system command={#1\space#2\space\filename@area\filename@base}},%
  run pdflatex/.style={run@system@command={pdflatex}{#1}},%
  run xelatex/.style={run@system@command={xelatex}{#1}},%
  run lualatex/.style={run@system@command={lualatex}{#1}},%
  run makeindex/.style={run@system@command={makeindex}{#1}},%
  run bibtex/.style={run@system@command={bibtex}{#1}},%
  run biber/.style={run@system@command={biber}{#1}},%
  run arara/.style={run@system@command={arara}{#1}},%
  run latex/.style={run@system@command={latex}{#1}},%
  run dvips/.style={run@system@command={dvips}{#1}},%
  run ps2pdf/.style={run system command={ps2pdf\space#1\space\filename@area\filename@base.ps}},%
  freeze file/.code={\def\tcbprocess@condition{1}\def\tcbprocess@target{#1}},%
  freeze none/.code={\def\tcbprocess@condition{0}\def\tcbprocess@target{}},%
  freeze extension/.style={freeze file={\filename@area\filename@base#1}},%
  freeze pdf/.style={freeze extension={.pdf}},
  freeze png/.style={freeze extension={.png}},
  freeze jpg/.style={freeze extension={.jpg}},
}

\def\tcb@listing@process@core{%
  \filename@parse{\kvtcb@listingfile}%
  \tcbiffileprocess{\tcbprocess@condition}%
    {\kvtcb@listingfile}{\filename@area\filename@base.md5}{\tcbprocess@target}%
    {\tcb@listing@process@hook}{}%
}

\def\tcbinputlisting@core{%
  \ifdefempty\tcb@listing@process@hook{}{\tcb@listing@process@core}%
  \let\tcb@listing@capture=\kvtcb@capture%
  \tcb@inputlisting%
}

\newcounter{tcblisting}

\newenvironment{tcblisting}[1]{%
  \stepcounter{tcblisting}\edef\thetcblistingcount{\thetcblisting}%
  \tcb@layer@pushup%
  \tcb@apply@box@options{capture=minipage,#1}%
  \begingroup\tcbverbatimwrite{\kvtcb@listingfile}%
}{\endtcbverbatimwrite\endgroup%
  \tcbinputlisting@core%
}

\long\def\tcbinputlisting#1{%
  \begingroup%
  \stepcounter{tcblisting}\edef\thetcblistingcount{\thetcblisting}%
  \tcb@layer@pushup%
  \tcb@apply@box@options{capture=minipage,#1}%
  \tcbinputlisting@core%
  \endgroup%
}

\newenvironment{tcboutputlisting}{%
  \begingroup\tcbverbatimwrite{\kvtcb@listingfile}%
}{\endtcbverbatimwrite\endgroup}

\long\def\tcb@newtcblisting#1#2{\tcb@newenvironment{#1}{\tcblisting{#2,options@for=#1}}{\endtcblisting}}

\long\def\tcb@newtcblisting@#1[#2]#3{\tcb@newenvironment{#1}[#2]{\tcblisting{#3,options@for=#1}}{\endtcblisting}}

\long\def\tcb@newtcblisting@@#1[#2][#3]#4{\tcb@newenvironment{#1}[#2][#3]{\tcblisting{#4,options@for=#1}}{\endtcblisting}}

\long\def\tcb@new@tcblisting@#1[#2]{%
  \@ifnextchar[{\tcb@newtcblisting@@{#1}[#2]}{\tcb@newtcblisting@{#1}[#2]}}

\def\tcb@new@tcblisting#1{%
  \@ifnextchar[{\tcb@new@tcblisting@{#1}}{\tcb@newtcblisting{#1}}}

\def\newtcblisting{%
  \let\tcb@newenvironment\newenvironment%
  \let\tcb@process@newtcolorbox@continue\tcb@new@tcblisting%
  \tcb@process@newenvironment}

\def\renewtcblisting{%
  \let\tcb@newenvironment\renewenvironment%
  \let\tcb@process@newtcolorbox@continue\tcb@new@tcblisting%
  \tcb@process@newenvironment}

\long\def\tcb@newtcbinputlisting#1#2{\expandafter\tcb@newcommand\csname#1\endcsname{\tcbinputlisting{#2,options@for=#1}}}

\long\def\tcb@newtcbinputlisting@#1[#2]#3{\expandafter\tcb@newcommand\csname#1\endcsname[#2]{\tcbinputlisting{#3,options@for=#1}}}

\long\def\tcb@newtcbinputlisting@@#1[#2][#3]#4{\expandafter\tcb@newcommand\csname#1\endcsname[#2][#3]{\tcbinputlisting{#4,options@for=#1}}}

\long\def\tcb@new@tcbinputlisting@#1[#2]{%
  \@ifnextchar[{\tcb@newtcbinputlisting@@{#1}[#2]}{\tcb@newtcbinputlisting@{#1}[#2]}}

\def\tcb@new@tcbinputlisting#1{%
  \@ifnextchar[{\tcb@new@tcbinputlisting@{#1}}{\tcb@newtcbinputlisting{#1}}}

\def\newtcbinputlisting{%
  \let\tcb@newcommand\newcommand%
  \let\tcb@process@newtcolorbox@continue\tcb@new@tcbinputlisting%
  \tcb@process@newcommand}

\def\renewtcbinputlisting{%
  \let\tcb@newcommand\renewcommand%
  \let\tcb@process@newtcolorbox@continue\tcb@new@tcbinputlisting%
  \tcb@process@newcommand}

\tcbset{%
  reset@listingscore/.style={%
    listing file=\jobname.listing,
    comment=, listing and text,
    no process,freeze none,
    comment style=,pdf extension=pdf,
  },
  initialize@reset=reset@listingscore,
}