%% The LaTeX package tcolorbox - version 6.0.4 (2023/06/19)
%% tcblistingscode.code.tex: Auxiliary code for colorboxes with listings
%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2006-2023 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{6.0.4}

\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}


\ExplSyntaxOn


\NewDocumentCommand \__tcobox_new_tcblisting:w { m +O{} >{\TrimSpaces} m o +o +m }
  {
    \__tcobox_process_newtcolorbox:nn { #2 }{ #3 }
    \tl_if_novalue:nTF { #4 }
      {
        #1 { #3 }{ \tcblisting{#6,options@for=#3} }{ \endtcblisting }
      }
      {
        \tl_if_novalue:nTF { #5 }
        {
          #1 { #3 }[ #4 ]{ \tcblisting{#6,options@for=#3} }{ \endtcblisting }
        }
        {
          #1 { #3 }[ #4 ][ #5 ]{ \tcblisting{#6,options@for=#3} }{ \endtcblisting }
        }
      }
  }
\NewDocumentCommand \newtcblisting { }
  {
    \bool_set_true:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_tcblisting:w \newenvironment
  }
\NewDocumentCommand \renewtcblisting { }
  {
    \bool_set_false:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_tcblisting:w \renewenvironment
  }


\NewDocumentCommand \__tcobox_new_TCBListing:w { m +O{} >{\TrimSpaces} m +m +m }
  {
    \__tcobox_process_newtcolorbox:nn { #2 }{ #3 }
    #1 { #3 }{ #4 }{ \tcblisting{#5,options@for=#3} }{ \endtcblisting }
  }
\NewDocumentCommand \DeclareTCBListing { }
  {
    \bool_set_false:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_TCBListing:w \DeclareDocumentEnvironment
  }
\NewDocumentCommand \NewTCBListing { }
  {
    \bool_set_true:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_TCBListing:w \NewDocumentEnvironment
  }
\NewDocumentCommand \RenewTCBListing { }
  {
    \bool_set_false:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_TCBListing:w \RenewDocumentEnvironment
  }
\NewDocumentCommand \ProvideTCBListing { }
  {
    \bool_set_true:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_TCBListing:w \ProvideDocumentEnvironment
  }


\NewDocumentCommand \__tcobox_new_tcbinputlisting_ii:w { m +m m o +o +m }
  {
    \__tcobox_process_newtcolorbox:nn { #2 }{ #3 }
    \tl_if_novalue:nTF { #4 }
      {
        \exp_args:Nc #1 { #3 }{ \tcbinputlisting{#6,options@for=#3} }
      }
      {
        \tl_if_novalue:nTF { #5 }
        {
          \exp_args:Nc #1 { #3 }[ #4 ]{ \tcbinputlisting{#6,options@for=#3} }
        }
        {
          \exp_args:Nc #1 { #3 }[ #4 ][ #5 ]{ \tcbinputlisting{#6,options@for=#3} }
        }
      }
  }
\NewDocumentCommand \__tcobox_new_tcbinputlisting_i:w { m +O{} m }
  {
    \__tcobox_set_backslash_removed:Nn \l_tmpa_tl { #3 }
    \exp_args:NNnV \__tcobox_new_tcbinputlisting_ii:w #1 {#2} \l_tmpa_tl
  }
\NewDocumentCommand \newtcbinputlisting { }
  {
    \bool_set_true:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_tcbinputlisting_i:w \newcommand
  }
\NewDocumentCommand \renewtcbinputlisting { }
  {
    \bool_set_false:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_tcbinputlisting_i:w \renewcommand
  }


\cs_new:Npn \__tcobox_new_TCBInputListing:Nnnnn #1#2#3#4#5
  {
    \__tcobox_process_newtcolorbox:nn { #2 }{ #3 }
    \exp_args:Nc #1 { #3 }{ #4 }{ \tcbinputlisting{#5,options@for=#3} }
  }
\NewDocumentCommand \__tcobox_new_TCBInputListing:w { m +O{} m }
  {
    \__tcobox_set_backslash_removed:Nn \l_tmpa_tl { #3 }
    \exp_args:NNnV \__tcobox_new_TCBInputListing:Nnnnn #1 {#2} \l_tmpa_tl
  }
\NewDocumentCommand \DeclareTCBInputListing { }
  {
    \bool_set_false:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_TCBInputListing:w \DeclareDocumentCommand
  }
\NewDocumentCommand \NewTCBInputListing { }
  {
    \bool_set_true:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_TCBInputListing:w \NewDocumentCommand
  }
\NewDocumentCommand \RenewTCBInputListing { }
  {
    \bool_set_false:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_TCBInputListing:w \RewewDocumentCommand
  }
\NewDocumentCommand \ProvideTCBInputListing { }
  {
    \bool_set_true:N \l__tcobox_prevent_init_overwrite_bool
    \__tcobox_new_TCBInputListing:w \ProvideDocumentCommand
  }


\ExplSyntaxOff


\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,
}

