%% abb.sty 27.Juli 2017 Vers 4.0
%
% Makros zur Einbindung von Figuren und sonstigem Material
%
% Erlaeuterungen im Dokument abb.tex
%
% Autor
% Daniel Baumgartner
% Kantonsschule Schaffhausen
%
% www.geometria.ch
% eMail bg@geometria.ch
%

\ProvidesPackage{abb}[2017/06/04 v4.0]
%
\catcode`\@=11

\def\Abb{%
	\let\abb@alg\Abb@%
	\@ifnextchar[{\filter@opt@}{\Abb@[]}%
}%
\def\AbbTxt{%
	\let\abb@alg\AbbTxt@%
	\@ifnextchar[{\filter@opt@}{\AbbTxt@[]}%
}%
\def\filter@opt@[#1]{%
	\def\@arg{#1}
	\ifx\@arg\empty\let\next\abb@alg%
	\else\let\next\filter@opts\fi
	\next[#1]
}%
	
\def\filter@opts[#1=#2]{%
	\flt@opt[#1=#2,\relax]
	}
\def\flt@opt[#1=#2,#3]{%
	\@ifundefined{#1@opt}{\let\next\flt@opt@i}{%
		\expandafter\xdef\csname #1@opt\endcsname{#2}%
		\ifx#3\relax\let\next\flt@opt@ii%
		\else%
		\let\next\flt@opt@iii%
		\fi%
		}%
	\next[#1=#2,#3]%
}%
\def\flt@opt@i[#1,\relax]{%
	\abb@alg[#1]%
}%
\def\flt@opt@ii[#1,\relax]{%
	\abb@alg[]%
}%
\def\flt@opt@iii[#1=#2,#3]{%
	\flt@opt[#3]%
}%

\newbox\bild@box%
\long\def\Abb@[#1]#2{%
	\erzeuge@abb@box[#1]{#2}%
	\box\bild@box%
}%
\newdimen\text@breite
\newdimen\temp@linewidth
\newbox\text@box%
\newcount\@flagge%

%4358 \def\list
\long\def\AbbTxt@[#1]#2{%
	\global\@flagge=0%
	\if\ort@opt l%
		\global\@flagge=1%
	\fi%
	\erzeuge@abb@box[#1]{#2}%
	\text@breite=\linewidth%
	\advance\text@breite by-\wd\bild@box%
	\advance\text@breite by-\abst@opt%
	\let\temp@linewidth=\linewidth%
	\let\linewidth=\text@breite%
	\ifnum \@listdepth >0%
		\advance\@totalleftmargin-\leftmargin%
	\fi%
	\setbox\text@box=\vbox\bgroup\hsize=\text@breite%
}%
\def\endAbbTxt{%
	\egroup%
	\let\linewidth=\temp@linewidth%
	\bgroup
	\setbox\@flagge=\box\bild@box%
	\multiply\@flagge by-1%
	\advance\@flagge by1%
	\setbox\@flagge=\vbox{\unvbox\text@box}%
	\par%
	\setbox3=\hbox{I}%
	\rightline{%
	\hbox to\linewidth{%
		\raise\ht3\vtop{\hrule height 0pt\box1
		\hrule height0pt}%*
		\hfill%
		\raise\ht3\vtop{\hrule height 0pt\box0}%
		}%
	}%
	\vskip2\lineskip%
	\egroup
	\ruecksetz@opts{ort,abst}%
	\Abbfootnote@%
}%

\def\erzeuge@abb@box[#1]#2{%
	\expandafter\@einlesen[#1]#2|%
	\begingroup%!!
	\dimen1=\ht\bild@box%
	\advance\dimen1by\dp\bild@box%*
	\dp\bild@box=0pt%*
	\ht\bild@box=\dimen1%*
	\csname @AbbNum@\num@opt\endcsname%
	\if@AbbNum@\@abbleg@true\fi%
	\dimen2=0pt%
	\if@abbleg@%
		\erzeuge@leg@box%
		\dimen2=\ht\leg@box%
		\advance\dimen2 by\dp\leg@box%
	\fi%
	\ifdim\wd\leg@box>\wd\bild@box%
		\dimen3=\wd\leg@box%
	\else%
		\dimen3=\wd\bild@box%
	\fi%
	\global\setbox\bild@box=\vbox{%!!
		\hsize=\dimen3%
		\hbox to\hsize{%
			\if\legpos@opt l\else\hfill\fi
			\box\bild@box
			\if\legpos@opt r\else\hfill\fi%
			}%
		\par%
		\vskip\lineskip%
		\hbox to \hsize{%
			\if\legpos@opt l\else\hfill\fi
			\box\leg@box%
			\if\legpos@opt r\else\hfill\fi%
			}%
		}%
	\if\pos@opt t%
		\setbox0=\hbox{I}%
		\advance\dimen2 by\dimen1%
		\advance\dimen2 by-\ht0%
		\dimen1=\ht0%
	\fi%
	\global\ht\bild@box=\dimen1%!!
	\global\dp\bild@box=\dimen2%!!
	\endgroup%!!
	\if@AbbNum@\else\@abbleg@false\fi%
	\xdef\leg@txt{\empty}%
	\ruecksetz@opts{pos,ort,num,label,breite,legpos}%
}%

\def\@einlesen[#1]#2.#3|{%
	\setbox\bild@box=\vbox{%
		\hbox{\csname @#3@\endcsname[#1]{#2}}%
		}%
}%


\def\@pst@[#1]#2{\input{\the\@pfad#2.pst}}%
\def\@tex@[#1]#2{\input{\the\@pfad#2.tex}}%
\def\@def@[#1]#2{\csname#2\endcsname#1}%
\def\@eps@[#1]#2{\includegraphics[#1]{\the\@pfad#2.eps}}%
\def\@psf@[#1]#2{\input{\the\@pfad#2.psf}\includegraphics[#1]{\the\@pfad#2.eps}}%
\def\@ps@[#1]#2{\includegraphics[#1]{\the\@pfad#2.ps}}%
\def\@jpg@[#1]#2{\includegraphics[#1]{\the\@pfad#2.jpg}}%
\def\@jpeg@[#1]#2{\includegraphics[#1]{\the\@pfad#2.jpeg}}%
\def\@png@[#1]#2{\includegraphics[#1]{\the\@pfad#2.png}}%
\def\@pdf@[#1]#2{\includegraphics[#1]{\the\@pfad#2.pdf}}%

\newtoks\@pfad
\@pfad={}%
\def\AbbPfad#1{%
\@pfad={#1}}%

\newbox\leg@box%
\setbox\leg@box=\hbox{\empty}
\newif\if@abbleg@
\@abbleg@false%

\newif\if@AbbNum@
\newcount\figurNr
\figurNr=0

\def\AbbFigurNum{%
	\number\figurNr%
}%
	
\def\AbbNumForm#1{%
	Figur \AbbFigurNum%
	\ifdim#1=0pt\else:\,\fi
}%

\def\setze@label{
	\ifx\label@opt\empty
	\else
		\immediate\write\@mainaux{\string\newlabel{\label@opt}{{\AbbFigurNum}{\thepage}}}%
	\fi%
	\gdef\label@opt{}%
}

\newdimen\leg@breite
\def\leg@txt{\empty}%

\def\AbbLegende{%
	\@abbleg@true%
	\let\abb@alg\AbbLegende@%
	\@ifnextchar[{\filter@opt@}{\AbbLegende@[]}%
}
\def\AbbLegende@[#1]#2{%
	\gdef\leg@txt{#2}%
}


\def\erzeuge@leg@box{%
	\begingroup%!!
	\setbox0=\hbox{\leg@txt}%
	\setbox1=\hbox{}%
	\if@AbbNum@%
		\global\advance\figurNr by 1%
		\setbox1=\hbox{%
			\AbbNumForm{\wd0}}%
		\setze@label%
	\fi%
	\dimen0=\wd0%
	\advance\dimen0by\wd1%
	\setbox3=\hbox{I}%
	\leg@breite=\wd\bild@box%
	\ifx\breite@opt\empty%
		\leg@breite=\dimen0%
	\else%
		\def\@arg{abb}%
		\ifx\@arg\breite@opt%%
		\else%
			\ifdim\dimen0<\breite@opt%
				\leg@breite=\dimen0%
			\else%
				\leg@breite=\breite@opt%
			\fi%
		\fi%
	\fi%
	\global\setbox\leg@box=\vbox{\hsize=\leg@breite%
		\vskip6\lineskip%
		\ifdim\dimen0>\wd\bild@box%
			\if@AbbNum@\AbbNumForm{\wd0}\fi\leg@txt%!!
		\else%
			\hbox to\hsize{%
			\if@AbbNum@\AbbNumForm{\wd0}\fi\leg@txt}%
		\fi%
		\vskip\ht3}%
	\endgroup%!!
	\Abbfootnote@%
}

\def\setz@lok@opt#1#2{%
	\expandafter\xdef\csname #1\endcsname{\csname#2\endcsname}%
}

\def\opts@lokal[#1,#2]#3#4{%
	\setz@lok@opt{#3#1@opt}{#4#1@opt}%
	\ifx#2\relax\let\next\@optii%
	\else%
		\let\next\opts@lokal%
	\fi%
	\next[#2]{#3}{#4}%
}%
\def\@optii[#1]#2#3{}

\def\abbset#1{%
	\def\@arg{#1}%
	\ifx\@arg\empty\let\next\abbset@@i%
	\else\let\next\abbset@@\fi%
	\next[#1,\relax]%
}%


\def\abbset@@[#1=#2,#3]{
	\expandafter\xdef\csname gb@#1@opt\endcsname{#2}%
	\expandafter\xdef\csname lk@#1@opt\endcsname{#2}%
	\expandafter\xdef\csname #1@opt\endcsname{#2}%
	\ifx#3\relax\else\abbset@@[#3]\fi%
}%

\def\abbset@@i[#1]{}

\abbset{pos=b,ort=r,num=false,label=,abst=10pt,breite=,legpos=c,abblaenge=\hsize}

\def\setz@opts#1{%
	\opts@lokal[#1,\relax]{}{gb@}%
	\opts@lokal[#1,\relax]{lk@}{gb@}%
}%

\def\ruecksetz@opts#1{%
	\opts@lokal[#1,\relax]{}{gb@}%
	\opts@lokal[#1,\relax]{}{lk@}%
}%

\newbox\multileg@box

\def\MultiAbb{%
	\@ifnextchar[{\MultiAbb@}{\MultiAbb@[]}%
}

\long\def\MultiAbb@[#1]{%
	\opts@multiabb[#1]%
	\csname @AbbNum@\num@opt\endcsname%
	\if@abbleg@%
		\edef\label@opt{\lk@label@opt}%
		\setze@label%
		\ifx\breite@opt\empty%
			\edef\breite@opt{\abblaenge@opt}
		\fi
		\setbox\bild@box=\hbox to\abblaenge@opt{}%
		\erzeuge@leg@box%
		\setbox\multileg@box=\hbox to\abblaenge@opt{%
			\if\legpos@opt l\else\hfill\fi%
			\box\leg@box%
			\if\legpos@opt r\else\hfill\fi%
			}%
		\global\@abbleg@false%
		\edef\lk@num@opt{false}%
		\edef\num@opt{false}%
		\setz@opts{legpos}%
	\fi%
	\vbox\bgroup\hsize=\abblaenge@opt%
}

\long\def\endMultiAbb{%
	\egroup%
	\par%
	\vbox{\hsize=\abblaenge@opt\box\multileg@box}%
	\setz@opts{pos,ort,num,label,abst,breite,legpos,abblaenge}%
}%

\def\opts@multiabb[#1]{%
	\edef\@arg{#1}%
	\ifx\@arg\empty\let\next\m@flt@optii%
	\else\let\next\m@filter@opt\fi%
	\next[#1,\relax]
}


\def\m@filter@opt[#1=#2,#3]{%
	\@ifundefined{#1@opt}{}{%
	\expandafter\xdef\csname lk@#1@opt\endcsname{#2}
	\expandafter\xdef\csname #1@opt\endcsname{#2}%
	\ifx#3\relax%
		\let\next\m@flt@optii%
	\else
		\let\next\m@flt@optiii
	\fi}%
	\next[#1=#2,#3]%
}%

\def\m@flt@optii[#1]{}%
\def\m@flt@optiii[#1=#2,#3]{%
	\m@filter@opt[#3]
}%

\newcount\abb@fn%
\abb@fn=0

\def\AbbFootnote#1{%
	\addtocounter{\@mpfn}{1}%
	$^{\thempfn}$%
	\global\advance\abb@fn by1%
	\count1=\abb@fn%
	\expandafter\gdef\csname Abb@fntxt\number\count1\endcsname{#1}%
}%

\def\Abbfootnote@{%
	\count0=\abb@fn%
	\count1=1%
	\addtocounter{\@mpfn}{-\number\count0}%
	\loop%
		\ifnum\count0>0
		\addtocounter{\@mpfn}{1}%
		\xdef\@thefnmark{\thempfn}%
		\@footnotetext{\csname Abb@fntxt\number\count1\endcsname}%
		\advance\count0 by-1%
		\advance\count1by1%
	\repeat%
	\global\abb@fn=0%
}%

\catcode`\@=12
%END abb
\endinput