%% hgb.sty
%% This file is part of the Hagenberg Thesis package for creating bachelors's and masters's theses
%% Author: Wilhelm Burger (wilbur@ieee.org)
%% GitHub: https://github.com/Digital-Media/HagenbergThesis
%% File encoding: UTF-8

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{hgb}[2026/10/22]%%AUTO
\typeout{******************* hgb.sty  (options=\@nameuse{opt@hgb.sty}) ***********************}

%% Class option helpers (defined in hgbvars.sty; \providecommand here is a no-op).
%% \@currname expands to the calling class name at call time.

% Register a permissible key=value option pair.
% Usage: \@RegisterClassOption{type}{master}
\providecommand{\@RegisterClassOption}[2]{%
  \expandafter\def\csname hgb@keyvalueoption-#1-#2\endcsname{\relax}%
}

% Validate a key=value option pair; \ClassError reports the actual calling class name.
% Usage: \@ValidateClassOption{type}{master}
\providecommand{\@ValidateClassOption}[2]{%
  \ifcsname hgb@keyvalueoption-#1-#2\endcsname%
    {}%
  \else\ClassError{\@currname}{illegal key/value option: #1=#2}\relax%
  \fi%
}

% Warn about a deprecated class option; \ClassWarning reports the actual calling class name.
% Usage: \@WarnDeprecatedOption{english}{language=english}
\providecommand{\@WarnDeprecatedOption}[2]{%
  \ClassWarning{\@currname}{option '#1' is deprecated, use '#2' instead!}%
}

%% Required Packages ----------------------------------------------------------
\RequirePackage[utf8]{inputenc}
\RequirePackage{xifthen}
\RequirePackage{hgbvars}	% also declares hgb@SmartQuotesOn and hgb@UpdateCheck booleans
\RequirePackage{hgbdict}

% Handle package options passed from the class file
% Language options are not passed through by hgbthesis.cls!
\DeclareOption{english}{\renewcommand{\hgb@MainLanguage}{english}\typeout{hgb.sty: option 'english' used}}
\DeclareOption{german}{\renewcommand{\hgb@MainLanguage}{german}\typeout{hgb.sty: option 'german' used}}
\DeclareOption{ngerman}{\renewcommand{\hgb@MainLanguage}{german}\typeout{hgb.sty: option 'ngerman' used}}

\DeclareOption{smartquotes}{\setboolean{hgb@SmartQuotesOn}{true}\typeout{hgb.sty: option 'smartquotes' used}}
\DeclareOption{noupdatecheck}{\setboolean{hgb@UpdateCheck}{false}}% deprecated, use updateCheck=false
\DeclareOption*{\PackageWarning{hgb}{Unknown option '\CurrentOption'.}}
\ProcessOptions*



%% Babel language support -----------------------------------------------------
%% hgb.sty loads AFTER \ProcessKeyOptions in the cls file, so \hgb@MainLanguage
%% is already final here — Babel can be loaded immediately with the correct language.

\ifthenelse{\equal{\hgb@MainLanguage}{german}}%
  {\RequirePackage[main=ngerman,english,noconfigs]{babel}}{}%
\ifthenelse{\equal{\hgb@MainLanguage}{english}}%
  {\RequirePackage[main=english,ngerman,noconfigs]{babel}}{}%
% If title language has not been set, default to the main language:
\ifthenelse{\equal{\hgb@TitleLanguage}{}}%
  {\renewcommand{\hgb@TitleLanguage}{\hgb@MainLanguage}}{}%

%% Setup for smart quotes (csquotes package) ----------------------------------

\newcommand{\SmartQuotesActive}{'csquotes' not loaded}

% Turn smartquotes on depending on document settings:
\newcommand{\@SmartQuotesRefresh}{%
	\ifthenelse{\boolean{hgb@SmartQuotesOn}}{\@enablequotes}{}%	instead of '\EnableQuotes' to allow smartquotes in preamble
}

% Load csquotes immediately if smartquotes are enabled.
% hgb.sty loads after \ProcessKeyOptions (so hgb@SmartQuotesOn is final) and
% after Babel (loaded above), so \languageshorthands is available.
\ifthenelse{\boolean{hgb@SmartQuotesOn}}{%
  \RequirePackage[autostyle=true,german=quotes]{csquotes}%
  \MakeOuterQuote{"}%
  \renewcommand{\SmartQuotesActive}{\iftoggle{csq@active}{true}{false}}%
  \languageshorthands{none}%
  \@SmartQuotesRefresh%
}{}%


%% Language setting commands and environments (babel) -------------------------

% Switch to the specified language. This is only a wrapper around babel's \selectlanguage{} command,
% replacing 'german' by 'ngerman'.
\newcommand{\SetLanguage}[1]{% usage example: \SetLanguage{english} 
	\edef\@tmpLang{#1}%	must expand argument!
	\ifthenelse{\equal{\@tmpLang}{german}}{\def\@tmpLang{ngerman}}{}%	 needed to make babel treat 'german' as 'ngerman'
	\selectlanguage{\@tmpLang}%
	%\ifthenelse{\boolean{hgb@SmartQuotesOn}}{\@enablequotes}{}
	\@SmartQuotesRefresh\ignorespaces%		needed!
}

% Temporarily switch to another language
\newenvironment{SwitchLanguage}[1]{% usage example: \begin{SwitchLanguage}{english} ... \end{SwitchLanguage}
	\edef\@tmpLang{#1}%	must expand argument!
	\ifthenelse{\equal{\@tmpLang}{german}}{\def\@tmpLang{ngerman}}{}%	 needed to make babel treat 'german' as 'ngerman'
	\begin{otherlanguage}{\@tmpLang}%
	%\ifthenelse{\boolean{hgb@SmartQuotesOn}}{\@enablequotes}{}%
	\@SmartQuotesRefresh\ignorespaces% 	needed!
}%
{\end{otherlanguage}\ignorespacesafterend}%

% Temporarily switch to English (short for '\begin{SwitchLanguage}{english} ... \end{SwitchLanguage}')
\newenvironment{english}%
	{\begin{SwitchLanguage}{english}}%
	{\end{SwitchLanguage}\ignorespacesafterend}%

% Temporarily switch to German (short for '\begin{SwitchLanguage}{german} ... \end{SwitchLanguage}')
\newenvironment{german}%
	{\begin{SwitchLanguage}{german}}%
	{\end{SwitchLanguage}\ignorespacesafterend}%


%% Date-related stuff ---------------------------------------------------------

\edef\hgbDate{\csname ver@hgb.sty\endcsname}	% get this package's build date

\RequirePackage{xstring}			% for string operations
\RequirePackage[english,ngerman,calc,useregional,showdow=false]{datetime2}	% replaces obsolete 'datetime' package

% https://tex.stackexchange.com/questions/316253/latex-convert-date-string-format-from-mm-dd-yyyyy-to-dd-mm-yyyy/316257#316257
\newcommand{\PackageToDTMdate}[2]{%	converts date in 'YYYY/MM/DD' format to DTMdate and saves to #2
    \StrLeft{#1}{4}[\myYear]%
    \StrRight{#1}{2}[\myDay]%
    \StrMid{#1}{6}{7}[\myMonth]%
		\DTMsavenoparsedate{#2}{\myYear}{\myMonth}{\myDay}{-1}%
    %\DTMdate{\myYear-\myMonth-\myDay}%
}

\PackageToDTMdate{\hgbDate}{hgb@PackageDate}	% converts package date to DTM format: \DTMusedate{hgbPackageDate}
\DTMsavenow{hgb@Today} % today's date in DTM format: \DTMusedate{hgbToday}

\newcounter{hgb@AgeLimit}
\setcounter{hgb@AgeLimit}{365}	% warn if package is older than 1 year

\newcount\hgb@Age
\DTMsaveddatediff{hgb@Today}{hgb@PackageDate}{\hgb@Age}

\newcommand{\hgb@WarnOldPackage}[1]{\PackageWarning{hagenberg-thesis}{%
^^J****************************************************************
^^JNOTE: 'hagenberg-thesis' is #1 days old - pls. check for updates 
^^Jat https://github.com/Digital-Media/HagenbergThesis
^^J****************************************************************
^^J} % https://tex.stackexchange.com/questions/6529/newline-linebreak-in-message
}

\ifthenelse{\hgb@Age>\value{hgb@AgeLimit} \AND \boolean{hgb@UpdateCheck}}%
	{\hgb@WarnOldPackage{\the\hgb@Age}}{}


%%% ----------------------------------------------------------------------------

\RequirePackage{upquote} 			% uses "right" quotes in the verbatim environment
\RequirePackage{marvosym}
\providecommand{\euro}[0]{\EUR\xspace}  % for compatibility with 'eurosym' macro

\RequirePackage{soul}           % for inserting additional space between letters
  \let\@addspaces\so            % rename soul's \so command to @addspaces
  \let\so\relax                 % shadow \so command (already defined in hgbabbrev.sty)

\RequirePackage{graphicx}
\RequirePackage[percent]{overpic}	% to allow text/graphics overlays on pictures
\RequirePackage{pict2e}				% extends the LaTeX 'picture' environment		

\RequirePackage{xcolor}
\newcommand{\@MissingArg}[1]{\textcolor{red}{#1}}

\RequirePackage[T1,hyphens,obeyspaces,spaces,lowtilde]{url}
% \urlstyle{sf} % Moved to AtBeginDocument to avoid being overridden by citation styles

\RequirePackage{verbatim}
\RequirePackage{moreverb}
\def\verbatimtabsize{2\relax}

%\RequirePackage{ifpdf}
%\ifpdf % LaTeX is in native PDF mode (pdflatex) 
  %\RequirePackage{epstopdf}			% converting EPS to PDF for pdflatex %wilbur: still needed?
%%  \pdfcompresslevel=0				% 0 = no compression, 9 = max. compression
%%  \pdfimageresolution=300
%\else								% LaTeX is in emulation mode (DVI-PS-PDF)
  %\RequirePackage[hyphenbreaks]{breakurl} % allows line breaks in URLs without pdflatex (DVI, PS, PDF) %wilbur: check!
%\fi	


%% Generic hyperref setup -------------------------------------------------------
\RequirePackage[unicode]{hyperref}	% utf8-change
\hypersetup{
	linktocpage=true,				  % make page number, not text, be link on TOC, LOF and LOT 
	colorlinks=false,				  % don't uses colored links (because this shows up in printing!) 
	pdfborder={0 0 0.5},			% use a thinner (0.5pt) border around all PDF links
	allbordercolors={blue},	  % this only works with a named color, any effect?
	%breaklinks = true				% allow links to break over lines by making links over multiple lines (obsolete)
	pdfdisplaydoctitle=true,	% PDF viewers show the document title instead of the file name (required by PDF/UA)
}

% Redefine certain commands for bookmarks and meta-data
\let\oldand\and
\pdfstringdefDisableCommands{%
	\def\\{}%
	\def\url#1{<#1>}%
	\def\and{\texorpdfstring{\oldand}{, }}%
}

\RequirePackage[figure,table,table*]{hypcap}% to adjust anchors of captions (https://ctan.org/pkg/hypcap)

%% Miscellaneous Utility Commands ---------------------------------------------

\newcommand\trennstrich{%
  \nopagebreak[4]
  \vskip 1.5ex %
%  \nopagebreak[4]
  \noindent\makebox[\textwidth]{\rule{4cm}{0.4pt}}
  \vskip 2.5ex}

\newcommand{\SuperPar}[0]{%
\PackageWarning{hgb}{Due to frequent misuse, macro \protect\SuperPar\space has been removed.}
}

\newcommand{\email}[1]{%
\texorpdfstring{\href{mailto:#1}{\nolinkurl{#1}}}{<#1>}%
}

% Test box for print proofing
\newcommand{\calibrationbox}[2]{% parameters: #1=width, #2=height
	\setlength{\unitlength}{1.0mm}%
	\begin{picture}(#1,#2)%
	\linethickness{0.05mm}%
	\put(0,0){\dashbox{0.2}(#1,#2)%
	{\parbox{#1mm}{%
	\centering\footnotesize 
	width $ = #1 \textrm{mm}$\\
	height $ = #2 \textrm{mm}$
	}}}\end{picture}
}

% Test box for print proofing (obsolete - use \calibrationbox)
\newcommand{\Messbox}[2]{
	\calibrationbox{#1}{#2}
}

\newcommand{\ShowParameter}[1]{\the#1\showthe#1} % to display TeX parameters
%Beispiel: clubpenalty = \ShowParameter{\clubpenalty}

% The supplied text is formatted either justified or centered, depending on its length
% (modeled after internal \@makecaption macro).
\newcommand{\hgbJustifyOrCenter}[1]{
	\sbox\@tempboxa{#1}%
	\ifdim \wd\@tempboxa >\hsize
		\begin{sloppypar}\noindent#1\end{sloppypar}  % long text, format as block
	\else
		\global \@minipagefalse
		\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%  % short text, format centered
	\fi
}

%% Layout Settings ------------------------------------------------------------

\frenchspacing						% creates normal spacing between sentences
\setlength{\parskip}{0pt plus 0.1pt}
\raggedbottom						%\flushbottom

\RequirePackage{float}
\floatstyle{plain}

% Captions with font size "small" and additional margin spacing
\RequirePackage[small,bf]{caption}
\DeclareCaptionStyle{ruled}{labelfont=bf,labelsep=colon} 
\setlength{\captionmargin}{5mm}
\setlength{\abovecaptionskip}{10pt}
\setlength{\belowcaptionskip}{10pt}

%% Disable \footnote in captions:
\newcommand{\@WarnFootnoteInCaption}{%
\GenericError{}{Package hgb Error: footnotes in captions are disabled}%
{How to fix: do not place footnotes in captions!}{}}%
\DeclareCaptionTextFormat{nofootnotes}{%
	\renewcommand{\footnote}[1]{\@WarnFootnoteInCaption\relax}#1}
\captionsetup{textformat=nofootnotes}

%% Make float placement easier    
\renewcommand{\floatpagefraction}{.9}	% previously: .5
\renewcommand{\textfraction}{.1}		% previously: .2
\renewcommand{\topfraction}{.9}			% previously: .7
\renewcommand{\bottomfraction}{.7}		% previously: .3
\setcounter{topnumber}{3}				% previously: 2
\setcounter{bottomnumber}{2}			% previously: 1
\setcounter{totalnumber}{5}				% previously: 3

\RequirePackage{enumitem}				% fix excessive vertical spacing in lists
\setlist{%
	partopsep = .0ex plus .1ex, 
	topsep = .6ex plus .3ex, 
	itemsep = .0ex plus .1ex, 
	parsep = .4ex plus .2ex
}

\newenvironment{nowidows}%
	{\clubpenalty=10000%
	\widowpenalty=10000%
	\displaywidowpenalty=10000}%
	{}
%Example:
% \begin{nowidows}
% Any text where no widows are allowed
% \end{nowidows}

\RequirePackage{lengthconvert}	% for showing the current textwidth in mm


% Setup for Table of Contents (ToC)

\RequirePackage{tocbasic}%				%% tocbasic is part of koma script
\DeclareTOCStyleEntry[entryformat=\bfseries\textsf]{tocline}{chapter}%

%% Program environments  ------------------------------------------------------

%% Define the program float environment:
\@ifundefined{chapter}%
   {\newfloat{program}{tbp}{lop}}%
	 {\newfloat{program}{tbp}{lop}[chapter]}

\floatstyle{plain}
\restylefloat*{program}


%% PDF Metadata settings -----------------------------------------------------

\newcommand{\hgb@SetDocumentMetadata}{%
	\hypersetup{
			pdftitle={\@title},
			pdfauthor={\@author},
			pdflang={\hgbDictionaryGet{iso-language-code}{\hgb@MainLanguage}},
			pdfsubject={},
			pdfcreator={LaTeX using 'hagenberg-thesis' package [\hgbDate]},
			pdfproducer={pdflatex},
			pdfkeywords={}
		}%
}

\AtBeginDocument{%	set name of 'program' environments according to document language
	\floatname{program}{\hgbDictionaryGet{program-env-name}{\hgb@MainLanguage}}%
}%

%% Experimental items ---------------------------------------------------------

\newenvironment{NarrowList}%
{\begin{list}{}%
{\setlength{\leftmargin}{2.5em}
 \setlength{\labelwidth}{1em}
 \setlength{\labelsep}{0.5em}
 \setlength{\itemsep}{0.5ex}
 \setlength{\parsep}{0ex}
 \setlength{\topsep}{0ex}
\raggedright
}}%
{\end{list}}

\providecommand{\hgb@PathName}{Pfad}

\newenvironment{FileList}[1]%
{%\paragraph{Pfad:} \url{#1}
\ifthenelse{\equal{\hgb@MainLanguage}{german}}
	{\renewcommand{\hgb@PathName}{Pfad}}%
	{\renewcommand{\hgb@PathName}{Path}}%
\paragraph{\hgb@PathName: \nolinkurl{#1}}
\begin{list}{}%
{\setlength{\leftmargin}{5cm}
 \setlength{\labelwidth}{4cm}
 \setlength{\labelsep}{0.25cm}
 \setlength{\rightmargin}{0cm}
 \setlength{\itemindent}{0cm}
 \setlength{\itemsep}{0.5ex}
 \setlength{\parsep}{0ex}
 \setlength{\topsep}{1ex}
\raggedright
}}%
{\end{list}}

\def\widedotfill{\leaders\hbox to 8pt{\hfil.\hfil}\hfill}

\newcommand{\fitem}[1]{%
\item[\nolinkurl{#1}\widedotfill]
}

% A generic environment that does nothing, e.g., for temporary variable settings:
% \begin{block}
% \setlength{\tabcolsep}{10pt}
%    \renewcommand{\arraystretch}{1.50}
%    ... 
% \end{block}
\newenvironment{block}{}{}

% Gives access to the most recent label assigned (used in longtable example):
\newcommand{\getcurrentlabel}{\@currentlabel}


%% For inserting foreign PDF pages --------------------------------------------

\RequirePackage{pdfpages}

%% Tables ---------------------------------------------------------------------

\RequirePackage{booktabs}
\RequirePackage{longtable}
\RequirePackage{multirow}

%% Execute at the start of the document ---------------------------------------

\AtBeginDocument{%
  % Set the URL style to sans serif for the document
  \urlstyle{sf}
  
  % The subfigure package is DEPRECATED (see https://ctan.org/pkg/subfigure)!
  % We strongly advise against its use since it creates poor output which is not 
  % compatible with our quality standards. Loading this package is therefore blocked
  % by default. We recommend to use the 'tabular' environment instead.
  \@ifpackageloaded{subfigure}{%
    \PackageError{hgb}{Use of the 'subfigure' package is not supported in this setup, because it is obsolete}{}
	\let\subfigure\undefined% just to make sure ...
  }%
}%

%% ----------------------------------------------------------------------------

\setlength{\fboxsep}{0mm}			%globally zero fbox separator
