%%
%% This is file `rgltxdoc.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% rgltxdoc.dtx  (with options: `package')
%% 
%% Copyright (C) 2018-2019 by Richard Grewe <r-g+tex@posteo.net>
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.2 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.2 or later is part of all distributions of LaTeX version
%% 1999/12/01 or later.
%% 
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{rgltxdoc}
    [2019/05/04 v1.2 Common documentation code used for Richard Grewe's packages]
\RequirePackage{ifluatex}
\RequirePackage{etoolbox}
\ifbool{luatex}{
  \RequirePackage[utf8]{luainputenc}
  \RequirePackage{polyglossia}
  \setmainlanguage[variant=american]{english}
}{
  \RequirePackage[utf8]{inputenc}
  \@ifpackageloaded{babel}{}
    {\RequirePackage[english]{babel}}
}
\RequirePackage[a4paper,twoside=false]{geometry}
\RequirePackage[DIV=9,BCOR=2.25cm]{typearea}
\ifbool{luatex}{
  \RequirePackage{fontspec}
  \setmainfont[SmallCapsFont={* Caps}]{Latin Modern Roman}
  \setsansfont{Latin Modern Sans}
  \setmonofont[Scale=MatchLowercase,
               SmallCapsFont={Latin Modern Mono Caps}]
    {Latin Modern Mono Light}
}{
  \RequirePackage[T1]{fontenc}
  \RequirePackage[lighttt]{lmodern}
  \bgroup\setbox\z@\hbox{\ttfamily ignore}\egroup
  \DeclareFontShape{T1}{lmtt}{m}{it}{<->sub*lmtt/m/sl}{}
}
\RequirePackage{microtype}
\RequirePackage[autostyle=true]{csquotes}
\MakeOuterQuote{"}
\RequirePackage[inline]{enumitem}
\RequirePackage[columns=2]{idxlayout}
\RequirePackage{varioref}
\RequirePackage{hypdoc}
\RequirePackage[capitalise,noabbrev,nameinlink]{cleveref}
\RequirePackage{doc}
\newcommand\NewNiceDescription[4]{%
  \expandafter\newcommand\csname NiceDescribe#1\endcsname{%
    \csname NiceDescribe#1s\endcsname{1}}%
  \expandafter\newcommand\csname NiceDescribe#1s\endcsname{%
    \rgltxdoc@Desc
      {\csuse{rgltxdoc@@efmt@#1}}
      {\csuse{rgltxdoc@@afmt@#1}}
      {#4}}%
  \csdef{rgltxdoc@@efmt@#1}##1{#2}%
  \csdef{rgltxdoc@@afmt@#1}##1{#3}}
\NewNiceDescription{Macro}{\string#1}{#1}{\SpecialUsageIndex}
\NewNiceDescription{Env}
  {\textcolor{gray}{\cs{begin}}\cmarg{#1}\\
   \textcolor{gray}{\cs{end}}\cmarg{#1}}
  {#1}{\SpecialEnvIndex}
\NewNiceDescription{Counter}{#1}{#1}
                   {\SpecialOtherIndex{counter}{counters}}
\NewNiceDescription{Key}{#1\,\textrm{=}\,\null}
  {\begingroup
   \newcommand\vmeta[1]{{\normalfont\meta{##1}}}%
   \setkeys[rgltxdoc]{DescOpt}{#1}%
   \setbox\z@\hbox{\qquad\let\rgltxdoc@@sep\@empty
     \rgltxdoc@opt{default}{\cmdrgltxdoc@DescOpt@def}%
     \rgltxdoc@opt{initially}{\cmdrgltxdoc@DescOpt@init}}%
   \parbox[t]{\linewidth-\wd\z@}{%
     \raggedright\cmdrgltxdoc@DescOpt@vals}%
   \box\z@\endgroup}
  {\SpecialOtherIndex{option-key}{option-keys}}
\usepackage{xkeyval,calc}
\define@key[rgltxdoc]{DescOpt}{vals}{%
  \def\cmdrgltxdoc@DescOpt@vals{}\let\rgltxdoc@@sep\@empty%
  \def\do##1{%
    \protected@eappto\cmdrgltxdoc@DescOpt@vals{\rgltxdoc@@sep\texttt{##1}}%
    \def\rgltxdoc@@sep{, }}%
  \docsvlist{#1}}
\define@cmdkey[rgltxdoc]{DescOpt}{init}{}
\define@cmdkey[rgltxdoc]{DescOpt}{def}{}
\newcommand\rgltxdoc@opt[2]{\ifdef{#2}
  {\rgltxdoc@@sep\textsl{#1: }\texttt{#2}%
   \def\rgltxdoc@@sep{, }}
  {}}
\newcommand*\SpecialOtherIndex[3]{%
  \@bsphack
  \begingroup
    \HD@target
    \let\HDorg@encapchar\encapchar
    \edef\encapchar usage{%
      \HDorg@encapchar hdclindex{\the\c@HD@hypercount}{usage}%
    }%
    \rgltxdoc@nohyp@SpecialOtherIndex{#1}{#2}{#3}%
  \endgroup
  \@esphack}
\newcommand\rgltxdoc@nohyp@SpecialOtherIndex[3]{%
  \index{#3\actualchar{\protect\ttfamily#3}
    (#1)\encapchar usage}%
  \index{#2:\levelchar#3\actualchar
    {\protect\ttfamily#3}\encapchar usage}}
\RequirePackage{pbox}
\newcommand\rgltxdoc@Desc{\medskip\par\noindent\rgltxdoc@DescRec}
\newcommand\rgltxdoc@DescRec[4]{%
  \@ifnextchar[%]
    {\rgltxdoc@DescRec@i{#1}{#2}{#3}{#4}}%
    {\rgltxdoc@DescRec@i{#1}{#2}{#3}{#4}[]}}
\def\rgltxdoc@DescRec@i#1#2#3#4[#5]#6#7{%
  \rgltxdoc@inmargin{\ttfamily #1{#6}}%
    {\ifstrempty{#7}{\quad}{}}%
  #2{#7}\relax
  \ifstrempty{#5}%
    {#3{#6}}%
    {\forcsvlist{#3}{#5}}%
  \ifnumgreater{#4}{1}%
    {\\\rgltxdoc@DescRec{#1}{#2}{#3}{#4-1}}%
    {\ifstrempty{#7}{}{\smallskip\par\noindent}\ignorespaces}}
\newcommand\rgltxdoc@inmargin[2]{%
  \leavevmode\null\hbox to\z@{\hss%
    \pbox[t]{3\marginparwidth}{#1}%
    #2}}
\RequirePackage{keyvaltable}
\kvtSet{headbg=black!10,rowbg=white..black!5}
\NewKeyValTable{KeyDesc}{%
  key:  align=l,    format=\texttt, head=\textbf{Key};
  desc: align=X,    head=\textbf{Description and Possible Values};
  default: align=l, format=\texttt, head=\textbf{Default};
}
\NewKeyValTable[showhead=false]{ValDesc}{%
  val:  align=l,    format=\texttt, head=\textbf{Value};
  desc: align=X,                    head=\textbf{Description};
}
\NewKeyValTable[showhead=false]{ArgDesc}{%
  arg:  align=l, head=\textbf{Argument};
  desc: align=X, head=\textbf{Description};
}
\newcommand\env[1]{\texttt{#1}}
\newrobustcmd\pkgname[1]{%
  \IfFileExists{#1.sty}
    {\textsf{#1}}
    {\rgltxdoc@err{Package `#1' not found. Spelling?}}}
\newcommand\pkgnames{%
  \def\do##1{\pkgname{##1}\def\do####1{, \pkgname{####1}}}%
  \docsvlist}
\newcommand\cmarg[1]{\mbox{\texttt{\string{#1\string}}}}
\newcommand\coarg[1]{\mbox{\texttt{[#1]}}}
\RequirePackage{hologo}
\RequirePackage{showexpl}
\lstset{%
  gobble=2,
  frame=trbl,
  backgroundcolor=\color{black!5!white},
  explpreset={%
    numbers=none, columns=flexible, basicstyle=\footnotesize\ttfamily},
  numbers=none, columns=flexible, basicstyle=\footnotesize\ttfamily,
  preset={\rgltxdoc@ExampleFix\rgltxdoc@SaveSecs\small\sffamily},
  overhang=2cm,
  pos=r,
  captionpos=b}
\crefname{lstlisting}{Listing}{Listings}
\lst@Key{morepreset}\relax{\appto\SX@preset{#1}}
\newcommand\rgltxdoc@ExampleFix{%
  \renewcommand\label[2][]{%
    \global\csletcs{rgltxdoc@@lbl@##2}{@currentlabel}%
    \ifstrempty{##1}
      {\csxdef{rgltxdoc@@lbltype@##2}{\rgltxdoc@curlbltype}}
      {\csgdef{rgltxdoc@@lbltype@##2}{##1}}}%
  \def\ref##1{\csuse{rgltxdoc@@lbl@##1}}%
  \def\cref##1{%
    \csuse{cref@\csuse{rgltxdoc@@lbltype@##1}@name}~\ref{##1}}%
  \let\marginpar=\rgltxdoc@@marginpar
}
\let\rgltxdoc@@marginpar=\marginpar
\def\rgltxdoc@curlbltype{%
  \@ifundefined{cref@currentlabel}{}
    {\expandafter\rgltxdoc@curlbltype@i\cref@currentlabel\@nil}}
\def\rgltxdoc@curlbltype@i[#1][#2][#3]#4\@nil{#1}
\newcommand\rgltxdoc@SaveSecs{%
  \def\addcontentsline##1##2##3{}%
  \@for\SC:=chapter,section,subsection,subsubsection\do{%
    \@ifundefined{c@\SC}{}
      {\csedef{rgltx@@ctr@\SC}{\the\value{\SC}}%
       \setcounter{\SC}{0}}}}
\newcommand\rgltxdoc@RestoreSecs{%
  \@for\SC:=chapter,section,subsection,subsubsection\do{%
    \@ifundefined{c@\SC}{}
      {\setcounter{\SC}{\csuse{rgltx@@ctr@\SC}}}}}
\patchcmd{\SX@resultInput}{\par}{\rgltxdoc@RestoreSecs\par}
  {}
  {\rgltxdoc@warn{Could not patch showexpl to reset section counters.}}
\newcommand\rgltxdoc@err[1]{%
  \PackageError{rgltxdoc}{#1}{}}
\newcommand\rgltxdoc@warn[1]{%
  \PackageWarning{rgltxdoc}{#1}{}}
\endinput
%%
%% End of file `rgltxdoc.sty'.
