Command "root" and "subcommands"
I would avoid the g
argument type. Anyway, here's a solution that also implements \IfIntegerTF
in place of \IfInteger
of xstring
.
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\law{ m m g g}{%
Art.~#2%
\IfNoValueF{#3}{%
\IfIntegerTF{#3}%
{~Abs. #3}%
{~lit. #3}%
}%
\IfNoValueF{#4}{%
\IfIntegerTF{#4}%
{~Ziff. #4}%
{~lit. #4}%
}%
~#1%
}
\ExplSyntaxOn
% law-command "generator"
\NewDocumentCommand{\createlaw}{mm}
{
% creates \law#1
% #2 is the first argument to \law
\cs_new_protected:cpn { law#1 } { \law{#2} }
}
\NewDocumentCommand{\IfIntegerTF}{mmm}
{% [\+\-]? is zero or one sign; \d+ is one or more digits
\regex_match:nnTF { \A [\+\-]? \d+ \Z } { #1 } { #2 } { #3 }
}
\ExplSyntaxOff
\createlaw{bueg}{BüG}
\createlaw{kbueg}{KBüG}
\begin{document}
Hello, I'm \lawbueg{21}{2}{b}, and I'm \lawkbueg{43}{1}.
\end{document}
Here is a way that works for this MWE but I don't really know if it works for you (for your real needs):
\documentclass{article}
\usepackage{xstring}
\usepackage{xifthen}
\usepackage{xparse}
\DeclareDocumentCommand\law{ m m g g}{%
{Art.~#2%
\IfNoValueF {#3} {%
\IfInteger{#3}%
{~Abs. #3}%
{~lit. #3}%
}%
\IfNoValueF {#4} {%
\IfInteger{#4}%
{~Ziff. #4}%
{~lit. #4}%
}%
~#1}%
}
% law-command "generator"
\newcommand{\createlaw}[2]{%
\edef\temp{\law{#2}}
\expandafter\expandafter\expandafter\global\expandafter\let\csname law#1\endcsname\temp
}
% #1 needs to be stuck right after \law and before the first {, in order to create a new separate command every time I use \createlaw
% #2 this argument needs to correspond to #1 in \law above
%Examples:
\createlaw{bueg}{BüG}
\createlaw{kbueg}{KBüG}
\begin{document}
Hello, I'm \lawbueg{21}{2}{b}, and I'm \lawkbueg{43}{1}.
\end{document}
Output:
Hello, I’m Art. 21 Abs. 2 lit. b BG, and I’m Art. 43 Abs. 1 KBG.
\documentclass{article}
\csname @ifundefined\endcsname{XeTeXrevision}{%
\csname @ifundefined\endcsname{luatexversion}{%
\usepackage[utf8]{inputenc}%
}{}%
}{}%
\usepackage{xstring}
\usepackage{xifthen}
\usepackage{xparse}
% The periods in your commands do _not_ denote sentence endings.
% Thus something needs to be done for avoiding the spacefactor-thingie
% with punctuation-marks -- see TeXBook, Chapter 12: Glue:
%
% | Abbreviations present problems too. For example, the short story in
% | Chapter 6 referred to Mr. `Droofnats'; TeX must be told somehow that
% | the period after `Mr.' or `Mrs.' or `Prof.' or `Dr.' or `Rt. Hon.'
% | etc., doesn't count as a sentence-ending full stop.
% | [...]
\DeclareDocumentCommand\law{ m m g g}{%
Art.~#2%
\IfNoValueF {#3} {%
\IfInteger{#3}%
{ Abs.~#3}%
{ lit.~#3}%
}%
\IfNoValueF {#4} {%
\IfInteger{#4}%
{ Ziff.~#4}%
{ lit.~#4}%
} %
#1%
}
% law-command "generator"
\newcommand\createlaw[2]{%
\expandafter\DeclareDocumentCommand\csname law#1\endcsname{}{\law{#2}}%
}%
%Examples:
\createlaw{bueg}{BüG}
\createlaw{kbueg}{KBüG}
\begin{document}
Hello, I'm \lawbueg{21}{2}{b}, and I'm \lawkbueg{43}{1}.
\end{document}