Seltsamer Fehler im benutzerdefinierten Paket anstelle des Fehlers, den ich auslösen wollte?

Seltsamer Fehler im benutzerdefinierten Paket anstelle des Fehlers, den ich auslösen wollte?

Ich erstelle ein benutzerdefiniertes Paket für alle meine persönlichen Befehle und versuche, einige Schlüsselwortargumente an das Paket zu übergeben, wie in Abschnitt 4.4 von beschrieben.Abonnieren. Mein Anwendungsfall ist, dass ich Mathematik- und Physikkurse belege und deshalb oft die Notation ändern muss (in der Mathematik ist es beispielsweise üblich, die komplex Konjugierte mit einem Strich zu kennzeichnen, während in der Physik die gebräuchlichere Notation ein Sternchen ist). Ich habe diese kleine Vorlage erstellt, die fast so funktioniert, wie ich wollte:

% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]

\RequirePackage{xstring}

\DeclareKeys[demo]
{
    notation.store = \@demo@notation,
    notation.usage = load
}

\SetKeys[demo]{notation=physics} % Default to physics notation

\ProcessKeyOptions[demo]

\IfStrEqCase{\@demo@notation}{
    {physics}{}
    {math}{}
}[
    \PackageError{demo-pkg}{Invalid notation value: \@demo@notation}
    {Choose value of: physics, math}
]

\RequirePackage{xparse}

\NewDocumentCommand{\Conjugate}{ m }{
    \IfStrEqCase{\@demo@notation}{
        {physics}{{#1}^{\ast}}
        {math}{\overline{#1}}
    }
}

% main.tex
\documentclass{article}

\usepackage[notation=math]{demo-pkg}

\begin{document}
\[ z = x + iy \Longleftrightarrow \Conjugate{z} = x - iy \]
\end{document}

Wenn ich einen gültigen Wert wie physicsoder matheingebe, funktioniert es wie erwartet. Wenn ich jedoch einen ungültigen Wert wie eingebe engineer, erhalte ich eine Fehlermeldung, aber es ist nicht der von mir angegebene Fehler:

Runaway argument?
 \PackageError {demo-pkg}{Invalid notation value: \@demo@notation } {\ETC.
! File ended while scanning use of \xs_testcase.
<inserted text> 
\par 
l.4 
  
I suspect you have forgotten a `}', causing me
to read past where you wanted me to stop.
I'll try to recover; but if the error is serious,
you'd better type `E' or `X' now and fix your file.

Anhand dieses Fehlers kann ich weder erkennen, was der ungültige Wert war, noch kann ich meine Fehlermeldung oder Hilfe sehen. Warum passiert das und wie kann ich einen Fehler korrekt ausgeben, wenn ich ein ungültiges Argument erhalte?

Antwort1

Sie haben zwischen den Vergleichsfällen einen Zwischenraum, der \IfStrEqCaseIhnen xstringnicht gefällt.

\begin{filecontents*}[overwrite]{demo-pkg.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]

\RequirePackage{xstring}

\DeclareKeys[demo]
{
    notation.store = \@demo@notation,
    notation.usage = load
}

\SetKeys[demo]{notation=physics} % Default to physics notation

\ProcessKeyOptions[demo]

\IfStrEqCase{\@demo@notation}{
    {physics}{}%
    {math}{}%
}[
    \PackageError{demo-pkg}{Invalid notation value: \@demo@notation}
    {Choose value of: physics, math}
]

\RequirePackage{xparse}

\NewDocumentCommand{\Conjugate}{ m }{%
    \IfStrEqCase{\@demo@notation}{%
        {physics}{{#1}^{\ast}}%
        {math}{\overline{#1}}%
    }%
}
\end{filecontents*}
\documentclass{article}

\usepackage[notation=aa]{demo-pkg}

\begin{document}
\[ z = x + iy \Longleftrightarrow \Conjugate{z} = x - iy \]
\end{document}

Sie können Ihren Code etwas vereinfachen, indem Sie einen Auswahlschlüssel verwenden (dies ist auch effizienter, da der Vergleich nicht bei jeder Verwendung von stattfindet \Conjugate):

\begin{filecontents*}[overwrite]{demo-pkg.sty}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]

\DeclareKeys[demo]
{
    notation.choice:,
    notation/physics.code = \DeclareDocumentCommand\Conjugate{ m }{{##1}^{\ast}},
    notation/math.code = \DeclareCommandCopy\Conjugate\overline,
    notation/unknown.code = 
        \PackageError{demo-pkg}{Invalid notation value: #1}
                     {Choose value of: physics, math},
    notation.initial:n = physics,
    notation.usage = load
}

\ProcessKeyOptions[demo]

\end{filecontents*}
\documentclass{article}

\usepackage[notation=math]{demo-pkg}

\begin{document}
\[ z = x + iy \Longleftrightarrow \Conjugate{z} = x - iy \]
\end{document}

Antwort2

Sie können mehr Schlüsseleigenschaften verwenden als in dokumentiert clsguidesind. Die weiteren sind in interface3.pdfAbschnitt 27.1 dokumentiert. Eine davon ist .choices:nn, für die in Abschnitt 27.3 ein Verwendungsbeispiel gegeben wird.

Die Fehlermeldung wird nicht exakt der in Ihrem Code angegebenen entsprechen, aber ich hoffe, sie ist gut genug.

% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[Demo]

\RequirePackage{xstring}

\DeclareKeys[demo]
{
  notation .choices:nn =
    {physics,math}
    {\ExpandArgs{Nc}\let\@demo@notation{l_keys_choice_tl}}
  ,notation.usage = load
}

\SetKeys[demo]{notation=physics} % Default to physics notation
\ProcessKeyOptions[demo]

% \RequirePackage{xparse} % not needed with recent kernel versions

\NewDocumentCommand{\Conjugate}{ m }{%
    \IfStrEqCase{\@demo@notation}{%
        {physics}{{#1}^{\ast}}%
        {math}{\overline{#1}}%
    }%
}

Ähnlich wie @UdiFogiel wird hier eine etwas andere Verwendung des Werts vorgeschlagen und Ihr Paket wird außerdem in die Verwendung der Programmiersprache L3 anstelle von konvertiert xstring.

% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{demo-pkg} {2024-01-06} {0.0} {Demo}

% assuming that you always need only two different notations, the following
% changes it to a boolean variable
\bool_new:N \l__demopkg_notation_physics_bool
\DeclareKeys[demo]
  {
    notation .choice:
    ,notation / physics .code:n =
      \bool_set_true:N  \l__demopkg_notation_physics_bool
    ,notation / maths   .code:n =
      \bool_set_false:N \l__demopkg_notation_physics_bool
    ,notation .usage:n   = load
    ,notation .initial:n = physics
  }

\ProcessKeyOptions[demo]

% since `load` doesn't allow changes after the package got loaded, we can just
% hard code the implementations here, instead of deciding on each call
\bool_if:NTF \l__demopkg_notation_physics_bool
  { \NewDocumentCommand \Conjugate { m } { #1 \sp { \ast } } }
  { \NewDocumentCommand \Conjugate { m } { \overline {#1} } }

\begin{advertisement}

Sie können expkvfür Ihre Optionenanalyse auch die -Familie von Paketen verwenden (deren Autor ich bin). Manche Dinge sind in möglicherweise einfacher expkv, andere schwieriger (oder eher nicht integriert). Ansonsten geht es eher darum, welche Syntax Sie bevorzugen.

% demo-pkg.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demo-pkg}[2024-01-06 v0.0 Demo]
\RequirePackage{expkv-def,expkv-opt}

\ekvdefinekeys{demo}
  {
     choice notation =
      {
        % expkv allows much freedom regarding the protected prefix because it
        % can work by expansion only (if it's not processing package
        % options...), therefore we need to set the protected prefix ourselves
        % here if we want to play nice.
         protected physics = \let\@demo@physicsTF\@firstoftwo
        ,protected maths   = \let\@demo@physicsTF\@secondoftwo
      }
    ,initial notation = physics
  }
\ekvoProcessGlobalOptions{demo} % options provided to \documentclass
\ekvoProcessLocalOptions{demo}  % options provided while loading this package
% future options will throw an option conflict error, since we only have a
% single load-time only option this is to be preferred over manually handling
% this. Else we could use `\ekvoProcessFutureOptions` (or combine all three
% calls by using `\ekvoProcessOptions` instead), but we'd need to redefine all
% the keys we don't want to be usable on a second time usage, because there is
% nothing like the `.usage`-property in `expkv`. This could look like the
% following:
%   \newcommand\@demo@aux[1]
%     {%
%       \protected\long\ekvdef{demo-pkg}{#1}
%         {%
%           \PackageError{demo-pkg}
%             {Option `#1' only usable on first package load}{}%
%         }%
%     }
%   \ekvcsvloop\@demo@aux{notation}% if you 
%   \let\@demo@aux\@demo@undefined

% since `load` doesn't allow changes after the package got loaded, we can just
% hard code the implementations here, instead of deciding on each call
\@demo@physicsTF
  {\NewDocumentCommand \Conjugate { m } {#1^{\ast}}}
  {\NewDocumentCommand \Conjugate { m } {\overline{#1}}}

\end{advertisement}

verwandte Informationen