%% This is file `hvextern.sty',
%%
%% Copyright (C) 2016-22
%% Herbert Voss
%%
%% It may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% 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.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.

\def\hvexternFileversion{0.23}
\ProvidesFile{hvextern}[2022/05/08 v\hvexternFileversion: package for running external documents (HV)]

\RequirePackage{shellesc,xkeyval,graphicx,marginnote,fancyvrb,tikz,listings,ifplatform}
\RequirePackage{tcolorbox,xparse}
\tcbuselibrary{skins,breakable}

\ifwindows
  \def\hv@move{move }%
  \def\hv@rm{del }%
\else
  \def\hv@move{mv }
  \def\hv@rm{rm }
\fi

\newcounter{hv@extern@runs}
\newcounter{@@@runs} 
\newsavebox\hv@extern@box
\newlength\hv@LineWidth
\AtBeginDocument{\setlength\hv@LineWidth{\dimexpr\linewidth-2\fboxsep-2\fboxrule}}
\newlength\hv@extern@mpwidth \setlength\hv@extern@mpwidth{\z@}

\define@key{hv}{progpath}{\def\hv@extern@progpath{#1}}
\define@key{hv}{runsequence}[]{\def\hv@extern@runsequence{#1}}
\define@key{hv}{runs}[1]{\setcounter{hv@extern@runs}{#1}}
\define@key{hv}{grfOptions}[]{\def\hv@extern@grfOptions{#1}}
\define@key{hv}{lstOptions}[]{\def\hv@extern@lstOptions{#1}}
\define@key{hv}{BGpreamble}[black!12]{\def\hv@extern@BGpreamble{#1}}
\define@key{hv}{BGbody}[black!8]{\def\hv@extern@BGbody{#1}}
\define@key{hv}{BOpreamble}[black!12]{\def\hv@extern@BOpreamble{#1}}
\define@key{hv}{BObody}[black!8]{\def\hv@extern@BObody{#1}}
\define@key{hv}{docType}[latex]{\def\hv@extern@docType{#1}}
\define@key{hv}{caption}[]{\def\hv@extern@caption{#1}}
\define@key{hv}{label}[]{\def\hv@extern@label{#1}}
\define@key{hv}{pages}[1]{\def\hv@extern@pages{#1}}
\define@key{hv}{cropmargin}[2]{\def\hv@extern@cropmargin{#1 }}
\define@key{hv}{mpwidth}[0pt]{\setlength\hv@extern@mpwidth{#1}}
\define@key{hv}{mpvalign}[t]{\def\hv@extern@mpvalign{#1}}
\define@key{hv}{ext}[tex]{\def\hv@extern@ext{.#1}}
\define@boolkey{hv}[hv@extern@]{redirect}[true]{}
\define@boolkey{hv}[hv@extern@]{usefancyvrb}[true]{}
\define@boolkey{hv}[hv@extern@]{showFilename}[true]{}
\define@boolkey{hv}[hv@extern@]{code}[true]{}
\define@boolkey{hv}[hv@extern@]{force}[true]{}
\define@boolkey{hv}[hv@extern@]{crop}[true]{}
\define@boolkey{hv}[hv@extern@]{tcbox}[true]{}
\define@boolkey{hv}[hv@extern@]{biber}[true]{}
\define@boolkey{hv}[hv@extern@]{xindex}[true]{}
\define@key{hv}{xindexOptions}[]{\def\hv@extern@xindexOptions{#1}}
\define@boolkey{hv}[hv@extern@]{includegraphic}[true]{}
\define@boolkey{hv}[hv@extern@]{inline}[true]{\hv@extern@codefalse}
\define@boolkey{hv}[hv@extern@]{frame}[true]{}
\define@boolkey{hv}[hv@extern@]{float}[true]{}
%\define@boolkey{hv}[hv@extern@]{cleanup}[true]{}
\define@key{hv}{cleanup}[{aux,log}]{\def\hv@extern@cleanup{#1}}% 
\define@boolkey{hv}[hv@extern@]{moveToExampleDir}[true]{}
\define@key{hv}{align}[\centering]{\def\hv@extern@align{#1}}
\define@key{hv}{ExampleDir}[Examples]{%
  \ifhv@extern@moveToExampleDir
    \def\hv@extern@ExamplesDir{#1/}%
  \else
    \def\hv@extern@ExamplesDir{}%
  \fi
}
\define@boolkey{hv}[hv@extern@]{eps}[true]{}
\define@boolkey{hv}[hv@extern@]{verbose}[true]{}
\def\hv@typeout#1{\ifhv@extern@verbose\typeout{#1}\fi}

%\define@key{hv}{compiler}[pdflatex]{\def\hv@extern@compiler{#1}}
\define@choicekey*+{hv}{compiler}[\val\nr]{mpost,tex,latex,luatex,python3,perl,lua,java,%
     xetex,pdflatex,lualatex,xelatex,context,sh,texlua}[pdflatex]{% 
  \hv@typeout{>>>> Compiler type \nr}%
  \def\hv@extern@compiler{\val}%
  \edef\hv@extern@compilerNo{\nr}%
}{\PackageWarning{hvextern}{erroneous input (#1) for compiler ignored. Using pdflatex.}%
  \def\hv@extern@compiler{pdflatex}%
  \def\hv@extern@compilerNo{9}%
 }

\def\ResetKeys{%
 \setkeys{hv}{
  showFilename=false,%  Dateinamen im Rand angeben
  ext=tex,%             file extension
  code=false,%          show Code
  crop=false,%		erzeugte PDF "croppen"
  compiler=pdflatex,%	zu verwendener Compiler
  redirect=false,%      write output into a file *.txt
  tcbox=true,%          use tcolorbox
  grfOptions={},%	Optionen der einzubindenden Grafik
  lstOptions={},%	Optionen für das Listing
  BGpreamble=black!12,% Backgroundcolor for preamble
  BGbody=black!8,%      Backgroundcolor for body
  BOpreamble=black!12,% Bodercolor for preamble
  BObody=black!8,%      Bodercolor for body
  includegraphic=true,% Grafik einbinden oder User überlassen
  inline=false,%        Grafik nicht in derselben Zeile
  runs=1,%		Anzahl Compiler-Durchläufe
  runsequence={},%	Im Moment nicht aktiv
  biber=false,%		Biber laufen lassen?
  xindex=false,%	xindex laufen lassen?
  xindexOptions={},%     options for xindex
  force=false,%		Compiler, auch wenn PDF existiert?
  frame=false,%		keinen Rahmen um Abbildung
  float=false,%		nicht als Gleitumgebung
  caption=,%	        keine Caption
  label=,%		kein Label
  pages=1,%		welche Seiten auszugeben sind
  docType=latex,%	LaTeX example Code
  cleanup={},% 	        Hilfsdateien nicht löschen
  moveToExampleDir=false,%  Verschieben nach ExamplesDir
  ExampleDir=Examples,% Unterverzeichnis für Beispiele
  align=\centering,%    Ausgabe zentrieren
  mpwidth=0pt,%         no minipage
  mpvalign=t,%          if side by side output the vertical alignment          
  usefancyvrb=false,%   use VerbatimInput instead of listings
  progpath={},
  cropmargin=2,%        2pb margin
  eps=false,%           create an eps output?          
  }%
}

\ResetKeys

{\catcode`\%=12
 \catcode`\#=12 
 \gdef\perCent{%}
 \gdef\DoubleperCent{%%}
 \gdef\NumChar{#}
 \gdef\DoubleNumChar{##}
}

\ExplSyntaxOn
\NewDocumentCommand\run@hv@extern@cleanup{ m }
 {
  \clist_map_inline:nn {#1}{\ShellEscape{\hv@rm \hv@extern@ExamplesDir\hvExternDateiname.##1}} 
  \ifnum\hv@extern@compilerNo=0\relax   % we have metapost
    \hv@rm epsf.*
  \fi
 }
\NewDocumentCommand\run@hv@extern@sequenceList{ m }
 {
  \clist_map_inline:nn {#1}{%
    \ShellEscape{\hv@extern@progpath##1\space\hvExternDateiname}
    %\hv@typeout{>>>> sequencerun: ##1}
  } 
 }
\ExplSyntaxOff

\lstset{belowskip=0pt,aboveskip=0pt}% global
\tcbset{breakable,left=2pt,right=2pt,top=2pt,bottom=2pt,boxsep=0pt}

\newcommand\PreambleVerbatim[2][]{%
  \ifhv@extern@tcbox
    \begin{tcolorbox}\VerbatimInput[#1]{#2}\end{tcolorbox}%
  \else
    \VerbatimInput[#1]{#2}
  \fi}

\newcommand\BodyVerbatim[2][]{%
  \ifhv@extern@tcbox
    \begin{tcolorbox}\VerbatimInput[#1]{#2}\end{tcolorbox}%
  \else
    \VerbatimInput[#1]{#2}
  \fi}

\newcommand\PreambleListing[2][]{%
  \ifhv@extern@tcbox 
    \begin{tcolorbox}[top=-2pt]\expandafter\lstinputlisting\expandafter[#1]{#2}\end{tcolorbox}%
  \else
    \expandafter\lstinputlisting\expandafter[#1]{#2}%
  \fi}

\newcommand\BodyListing[2][]{%
  \ifhv@extern@tcbox 
    \begin{tcolorbox}[top=-2pt]\expandafter\lstinputlisting\expandafter[#1]{#2}\end{tcolorbox}%
  \else
    \expandafter\lstinputlisting\expandafter[#1]{#2}%
  \fi}

\newcounter{hv@example@counter}

\newcommand\hv@extern@ExampleType[5]{%
  \@namedef{#1@initTextFancy}{\edef\FancyVerbStartString{#2}\edef\FancyVerbStopString{#3}}% text
  \@namedef{#1@initPreambleFancy}{\edef\FancyVerbStartString{#4}\edef\FancyVerbStopString{#5}}% code
  \@namedef{#1@initText}{linerange={#2}-{#3},includerangemarker=false}% text
  \@namedef{#1@initPreamble}{linerange={#4}-{#5},includerangemarker=false}% code
}%                                   {} ^^to prevent problems with lua comments

%%------------------   the config part --------------------
%\input{hvextern-mp.cfg}
%----  MP
\hv@extern@ExampleType{mp}
  {beginfig(1)}
  {endfig;}
  {\perCent StartVisiblePreamble}
  {\perCent StopVisiblePreamble}

\def\hv@extern@runMP#1#2#3#4{% path compiler file extension
  \hv@typeout{>>>> running #1#2 #3#4}%
  \ShellEscape{#1#2\space -tex=tex\space #3#4}%
  \hv@typeout{>>>> running #1tex #3}%
  \ShellEscape{#1tex\space "\string\input\space epsf\string\relax\string\nopagenumbers\string\epsfbox{#3.1}\string\bye"}%
  \hv@typeout{>>>> running #1dvips #3}%
  \ShellEscape{#1dvips\space -j\space -E\space -o\space #3.eps\space epsf.dvi}%
  \hv@typeout{>>>> running #1epstopdf #3}%
  \ShellEscape{#1epstopdf\space #3.eps}%
}

%----  TeX
%\input{hvextern-tex.cfg}
\hv@extern@ExampleType{tex}
  {\perCent StartBody}
  {\string\bye}
  {\perCent StartVisiblePreamble}
  {\perCent StopVisiblePreamble}

\def\hv@extern@runTEX#1#2#3#4{% path compiler file extension
  \hv@typeout{>>>> running #1#2 #3#4}%
  \ShellEscape{#1tex\space #3#4}%
  \hv@typeout{>>>> running #1dvips #3}%
  \ShellEscape{#1dvips\space #3.dvi}%
  \hv@typeout{>>>> running ps2pdf #3.ps}%
  \ShellEscape{#1ps2pdf\space -dAutoRotatePages=/None\space -dALLOWPSTRANSPARENCY\space #3.ps}%
}

%----  LaTeX
%\input{hvextern-latex.cfg}
\hv@extern@ExampleType{latex}%                      
  {\string\begin\string{document\string}}%       
  {\string\end\string{document\string}}%         
  {\perCent StartVisiblePreamble}%               
  {\perCent StopVisiblePreamble}%                

% only for the sequence latex->dvips->ps2pdf
\def\hv@extern@runLATEX#1#2#3#4{% path-compiler-file-extension
  \hv@typeout{>>>> running #1#2 #3#4}%
  \ShellEscape{#1#2\space #3#4}%
  \hv@typeout{>>>> running #1dvips #3}%
  \ShellEscape{#1dvips\space #3.dvi}%
  \hv@typeout{>>>> running ps2pdf #3.ps}%
  \ShellEscape{#1ps2pdf\space -dAutoRotatePages=/None\space -dALLOWPSTRANSPARENCY\space  #3.ps}%
}

%---- ConTeXt
%\input{hvextern-context.cfg}
\hv@extern@ExampleType{context}
  {\string\starttext}
  {\string\stoptext}
  {\perCent StartVisiblePreamble}
  {\perCent StopVisiblePreamble}

%---- Python
%\input{hvextern-py.cfg}
\hv@extern@ExampleType{py}
  {\NumChar StartVisibleMain}
  {\NumChar StopVisibleMain}
  {\NumChar StartVisiblePreamble}
  {\NumChar StopVisiblePreamble}


%----  Perl
%\input{hvextern-pl.cfg}
\hv@extern@ExampleType{pl}
  {\NumChar StartVisibleMain}
  {\NumChar StopVisibleMain}
  {\NumChar StartVisiblePreamble}
  {\NumChar StopVisiblePreamble}


%----  Lua
%\input{hvextern-lua.cfg}
\hv@extern@ExampleType{lua}
  {--StartVisibleMain}
  {--StopVisibleMain}
  {--StartVisiblePreamble}
  {--StopVisiblePreamble}


%----  Java
\hv@extern@ExampleType{java}
  {//StartVisibleMain}
  {//StopVisibleMain}
  {//StartVisiblePreamble}
  {//StopVisiblePreamble}


%----  Shell
%\input{hvextern-sh.cfg}
\hv@extern@ExampleType{sh}
  {\NumChar StartVisibleMain}
  {\NumChar StopVisibleMain}
  {\NumChar StartVisiblePreamble}
  {\NumChar StopVisiblePreamble}


%%%---------------------------------  end config part ------------------

%%
%% [#1]: Optionen  #2: Dateiname
%%
\newenvironment{externalDocument}[2][]{%
   \xdef\hvExternDateiname{#2-\arabic{hv@example@counter}}%
   \setkeys{hv}{#1}%                   \begin
   \hv@typeout{External filename: \hvExternDateiname}%
   \expandafter\IfFileExists\expandafter{\hv@extern@ExamplesDir\hvExternDateiname.pdf}{}{%  
     \expandafter\IfFileExists\expandafter{\hv@extern@ExamplesDir\hvExternDateiname.png}{}{%
       \expandafter\IfFileExists\expandafter{\hv@extern@ExamplesDir\hvExternDateiname.txt}{}{%
         \global\hv@extern@forcetrue}}}%   set force, if no pdf or png exists
   \begingroup
   \hv@typeout{>>> writing file \hvExternDateiname\hv@extern@ext ...}%
   \filecontents[force,noheader]{\hvExternDateiname\hv@extern@ext}
  }
  {\endfilecontents%         \end
   \hv@typeout{>>> ... done}%
   \global\stepcounter{hv@example@counter}%
   \endgroup
   \ifhv@extern@force
     \ifx\hv@extern@runsequence\@empty
       \hv@typeout{>>>> force=true: running \hv@extern@progpath\hv@extern@compiler~ \hvExternDateiname\hv@extern@ext ...}%
       \setcounter{@@@runs}{\value{hv@extern@runs}}%
       \loop\ifnum\the@@@runs > 0\relax
         \ifcase\hv@extern@compilerNo  \relax   % we have metapost
           \hv@typeout{>>>> running mpost \hvExternDateiname\hv@extern@ext}%
           \hv@extern@runMP{\hv@extern@progpath}{\hv@extern@compiler}{\hvExternDateiname}{\hv@extern@ext}%       
         \or %1
           \hv@typeout{>>>> running tex \hvExternDateiname\hv@extern@ext}%
           \hv@extern@runTEX{\hv@extern@progpath}{\hv@extern@compiler}{\hvExternDateiname}{\hv@extern@ext}%       
         \or %2
           \hv@typeout{>>>> running \hv@extern@compiler~ \hvExternDateiname\hv@extern@ext}%
           \hv@extern@runLATEX{\hv@extern@progpath}{\hv@extern@compiler}{\hvExternDateiname}{\hv@extern@ext}%       
         \else
           \ifhv@extern@redirect
             \hv@typeout{>>>> running \hv@extern@compiler~ \hvExternDateiname\hv@extern@ext > \hvExternDateiname.txt}%
             \ShellEscape{\hv@extern@progpath\hv@extern@compiler\space\hvExternDateiname\hv@extern@ext\space > \hvExternDateiname.txt}%
           \else
             \hv@typeout{>>>> running \hv@extern@compiler~ \hvExternDateiname\hv@extern@ext}%
             \ShellEscape{\hv@extern@progpath\hv@extern@compiler\space\hvExternDateiname\hv@extern@ext}%
           \fi
         \fi
         \addtocounter{@@@runs}{-1}%
       \repeat
       \hv@typeout{>>> ... done}%
       \ifhv@extern@biber
         \ShellEscape{biber  \hvExternDateiname}%  NAch biber nochmal latex
         \ShellEscape{\hv@extern@progpath\hv@extern@compiler\space\hvExternDateiname}%
       \fi
       \ifhv@extern@xindex 
         \ShellEscape{xindex \hv@extern@xindexOptions\space \hvExternDateiname.idx}%  NAch xindex nochmal latex
         \ShellEscape{\hv@extern@progpath\hv@extern@compiler\space\hvExternDateiname}%
       \fi
     \else % runsequence
       \expandafter\run@hv@extern@sequenceList\expandafter{\hv@extern@runsequence}%
     \fi  
     \ifhv@extern@crop
       \ShellEscape{pdfcrop --margins \hv@extern@cropmargin \hvExternDateiname}%
       \ShellEscape{\hv@move \hvExternDateiname-crop.pdf \hvExternDateiname.pdf}%
     \fi
     \ifhv@extern@eps
       \ShellEscape{pdftops -eps -f 1 -l 1 \hvExternDateiname.pdf}%
     \fi
   \fi%    end force
   \ifhv@extern@code%--------------        Code Part
     \hv@typeout{>>>>Starting Code part ...}%
     \par\noindent
     \begingroup
      \ifdim\hv@extern@mpwidth>\z@ \minipage[\hv@extern@mpvalign]{\hv@extern@mpwidth}\vspace{0pt}\fi
      \ifhv@extern@usefancyvrb
         \@nameuse{\hv@extern@docType @initPreambleFancy}%
         \edef\@@@temp{\@nameuse{\hv@extern@docType @initPreamble}}%
         % we need lstinputlisting, because \VerbatimInput doesn't work in a box
         \savebox\hv@extern@box{\expandafter\lstinputlisting\expandafter[\@@@temp]{\hvExternDateiname\hv@extern@ext}}%
%         \savebox\hv@extern@box{\VerbatimInput{\hvExternDateiname\hv@extern@ext}}%
         \ifdim\wd\hv@extern@box > \z@
           \expandafter\fvset\expandafter{\hv@extern@lstOptions}%
           \tcbset{colback=\hv@extern@BGpreamble,colframe=\hv@extern@BOpreamble}%
           \PreambleVerbatim{\hvExternDateiname\hv@extern@ext}%
         \fi
      \else
        \edef\@@@temp{\@nameuse{\hv@extern@docType @initPreamble}}%
        \savebox\hv@extern@box{\expandafter\lstinputlisting\expandafter[\@@@temp]{\hvExternDateiname\hv@extern@ext}}%
        \ifdim\wd\hv@extern@box>\z@
          \expandafter\lstset\expandafter{\hv@extern@lstOptions}%
          \tcbset{colback=\hv@extern@BGpreamble,colframe=\hv@extern@BOpreamble}%
          \PreambleListing[\@@@temp]{\hvExternDateiname\hv@extern@ext}%
        \fi
      \fi
      \ifhv@extern@showFilename
         \marginnote{\rotatebox{90}{\hvExternDateiname\hv@extern@ext}}%[-15mm]%
      \fi
%     
       \ifhv@extern@usefancyvrb
         \@nameuse{\hv@extern@docType @initTextFancy}%
         \expandafter\fvset\expandafter{\hv@extern@lstOptions}%
         \tcbset{colback=\hv@extern@BGbody,colframe=\hv@extern@BObody}%
         \expandafter\BodyVerbatim\expandafter[\hv@extern@lstOptions]{\hvExternDateiname\hv@extern@ext}%
       \else
         \expandafter\lstset\expandafter{\hv@extern@lstOptions}%
         \edef\@@@temp{\@nameuse{\hv@extern@docType @initText}}%
         \tcbset{colback=\hv@extern@BGbody,colframe=\hv@extern@BObody}%
         \BodyListing[\@@@temp]{\hvExternDateiname\hv@extern@ext}%
       \fi
     \ifdim\hv@extern@mpwidth > \z@\relax \endminipage\fi
     \endgroup
   \fi % end code part
   \ifhv@extern@code\else  % only if no code
      \ifhv@extern@showFilename
        \hv@typeout{>>>> Set filename in the margin!}%
        \marginnote{\rotatebox{90}{\hvExternDateiname}}%
      \fi
   \fi
   \ifhv@extern@moveToExampleDir
%     \ShellEscape{mkdir\space\hv@extern@ExamplesDir/}%
     \hv@typeout{>>>> Move file into example dir}% 
     \hv@typeout{>>>>>>\hvExternDateiname ---- \hv@extern@ExamplesDir}%
     \ShellEscape{\hv@move \hvExternDateiname.*\space \hv@extern@ExamplesDir}%
   \fi
   \ifhv@extern@includegraphic
     \ifhv@extern@inline
       \expandafter\includegraphics\expandafter[\hv@extern@grfOptions]{\hv@extern@ExamplesDir\hvExternDateiname}%
     \else
       \ifhv@extern@float
         \hv@typeout{>>>> Floating environment}% 
         \begin{figure}[!htb]
       \else
         \hv@typeout{>>>> No floating environment}% 
         \ifdim\hv@extern@mpwidth>\z@ 
           \hfill\minipage[\hv@extern@mpvalign]{\dimexpr\linewidth-\hv@extern@mpwidth-1em\relax}\vspace{0pt}%
         \else
           \par\noindent\begingroup
         \fi
       \fi
       \hv@extern@align
       \hv@typeout{>>>> Input image \hv@extern@ExamplesDir\hvExternDateiname}% 
       \ifhv@extern@frame
         \expandafter\@for\expandafter\next\expandafter:\expandafter=\hv@extern@pages\do{%
           \fbox{\expandafter\includegraphics\expandafter[\hv@extern@grfOptions,page=\next]{\hv@extern@ExamplesDir\hvExternDateiname}}%
              \hspace{5pt}}\hspace*{-5pt}%
       \else
         \expandafter\@for\expandafter\next\expandafter:\expandafter=\hv@extern@pages\do{%
           \expandafter\includegraphics\expandafter[\hv@extern@grfOptions,page=\next]%
             {\hv@extern@ExamplesDir\hvExternDateiname}%
              \hspace{5pt}}\hspace*{-5pt}%
       \fi
       \ifx\hv@extern@caption\@empty\else\caption{\hv@extern@caption}\fi
       \ifx\hv@extern@label\@empty\else\label{\hv@extern@label}\fi
       \ifhv@extern@float
         \end{figure}%
       \else
         \ifdim\hv@extern@mpwidth>\z@  \endminipage\else\par\endgroup\fi
       \fi
     \fi
   \else% no graphic, only text
     \ifdim\hv@extern@mpwidth>\z@ 
       \hfill\minipage[t]{\dimexpr\linewidth-\hv@extern@mpwidth-1em\relax}\vspace{0pt}
       \noindent\BodyListing{\hv@extern@ExamplesDir\hvExternDateiname.txt}
       \endminipage
     \else
       \noindent\BodyListing{\hv@extern@ExamplesDir\hvExternDateiname.txt}
     \fi
   \fi
   \ifhv@extern@force\expandafter\run@hv@extern@cleanup\expandafter{\hv@extern@cleanup}\fi%   cleanup
}

\stepcounter{hv@example@counter}%


\newcommand\runExtCmd[3][]{%
  \begingroup
  \setkeys{hv}{code=false,includegraphic=false,#1}%
  \xdef\hvExternDateiname{#3-\arabic{hv@example@counter}}%
  \hv@typeout{>>>> running #2}%
  \ifhv@extern@redirect
    \ShellEscape{\hv@extern@progpath#2   > \hv@extern@ExamplesDir\hvExternDateiname.txt}%
  \else
    \ShellEscape{\hv@extern@progpath#2}%
  \fi
  \global\stepcounter{hv@example@counter}%
  %-------------------------------------  output -----------------------------------
  \ifhv@extern@includegraphic
     \ifhv@extern@inline
       \expandafter\includegraphics\expandafter[\hv@extern@grfOptions]{\hv@extern@ExamplesDir\hvExternDateiname}%
     \else
       \ifhv@extern@float
         \hv@typeout{>>>> Floating environment}% 
         \begin{figure}[!htb]
       \else
         \hv@typeout{>>>> No floating environment}% 
         \par\noindent\begingroup
       \fi
       \hv@extern@align
       \hv@typeout{>>>> Input image \hv@extern@ExamplesDir\hvExternDateiname}% 
       \ifhv@extern@frame
         \expandafter\@for\expandafter\next\expandafter:\expandafter=\hv@extern@pages\do{%
           \fbox{\expandafter\includegraphics\expandafter[\hv@extern@grfOptions,page=\next]{\hv@extern@ExamplesDir\hvExternDateiname}}%
              \hspace{5pt}}\hspace*{-5pt}%
       \else
         \expandafter\@for\expandafter\next\expandafter:\expandafter=\hv@extern@pages\do{%
           \expandafter\includegraphics\expandafter[\hv@extern@grfOptions,page=\next]%
             {\hv@extern@ExamplesDir\hvExternDateiname}%
              \hspace{5pt}}\hspace*{-5pt}%
       \fi
       \ifx\hv@extern@caption\@empty\else\caption{\hv@extern@caption}\fi
       \ifx\hv@extern@label\@empty\else\label{\hv@extern@label}\fi
       \ifhv@extern@float
         \end{figure}%
       \else
         \ifdim\hv@extern@mpwidth>\z@  \endminipage\else\par\endgroup\fi
       \fi
     \fi
   \else% no graphic, only text
     \ifhv@extern@float
       \hv@typeout{>>>> Floating environment}% 
       \begin{figure}[!htb]
       \noindent\BodyListing{\hv@extern@ExamplesDir\hvExternDateiname.txt}
       \ifx\hv@extern@caption\@empty\else\caption{\hv@extern@caption}\fi
       \ifx\hv@extern@label\@empty\else\label{\hv@extern@label}\fi
       \end{figure}
     \else
       \noindent\BodyListing{\hv@extern@ExamplesDir\hvExternDateiname.txt}
     \fi
   \fi
   \ifhv@extern@force\expandafter\run@hv@extern@cleanup\expandafter{\hv@extern@cleanup}\fi%   cleanup
  \endgroup
}
%
%%
\endinput
