%% The LaTeX package tcolorbox - version 4.11 (2017/09/14)
%% tcblistingscode.code.tex: Auxiliary code for colorboxes with listings
%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2006-2017 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.11}

\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{tcbcol@lower}\kvtcb@fontlower\kvtcb@halignlower\kvtcb@before@lower\ignorespaces}%
  {\ifvmode\else\unskip\fi\kvtcb@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 2 args={%
    \let\tcb@inputlisting\tcb@inputlisting@inside%
    \let\tcb@listing@process=#1%
    \let\tcb@use@listing@other=#2},%
  listing@process@outside/.code 2 args={%
    \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,
}

