命令“根”和“子命令”

命令“根”和“子命令”

来自瑞士的问候,

(对于这个措辞不当的问题,我深感抱歉)

是否可以创建一个命令“根”(例如\law,具有如下所述的固定结构,从未在文档本身中使用过),然后根据我的需要创建其他“子命令” (例如,,\lawbueg) ?\lawbuev\lawkbueg

以下是 MWE:

\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[2]{\createlaw}{<unsure how to proceed here>}
   % #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}

如能得到任何帮助我将非常感激。

答案1

这是一种适用于 MWE 的方法,但我不知道它是否适合您(满足您的实际需求):

\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}

输出:

您好,我是 Art. 21 Abs. 2 lit. b BG,我是 Art. 43 Abs. 1 KBG。

答案2

我会避免使用g参数类型。无论如何,这里有一个解决方案,也\IfIntegerTF可以代替 来\IfInteger实现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}

在此处输入图片描述

答案3

\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}

相关内容