PoC

PoC

Ich möchte wissen, ob ich l3keysmehrere Untermodule verarbeiten kann, wie es mit pgfoption möglich ist?

Hier ist ein MWE zur Veranschaulichung meiner Frage:

  • eine Paketdatei mypkg.sty(die im Kommentar den Fehler enthält, den ich erhalte, wenn ich versuche, die LaTeX-Datei zu kompilieren):
\ProvidesExplPackage{mypkg}{2024/01/01}{v0.0.0}{mypkg test}

\keys_define:nn { mypkg / title }
{
    title .choice:,
    title .usage:n = { general },
    title .default:n = { regular },
    title / regular .code:n = 
    {
        \PackageWarning {mypkg} {We~use~regular~title!}
    },
    title / smallcaps .code:n = 
    {
        \PackageWarning {mypkg} {We~use~smallcaps~title!}
    },
    title / unknown .code:n = { \PackageWarning {mypkg} {unknown~title~option!} },
}

\keys_define:nn { mypkg / font }
{
    font .choice:,
    font .usage:n = { general },
    font .default:n = { regular },
    font / regular .code:n = 
    {
        \PackageWarning {mypkg} {We~use~regular~font!}
    },
    font / bold .code:n = 
    {
        \PackageWarning {mypkg} {We~use~bold~font!}
    },
    font / unknown .code:n = { \PackageWarning {mypkg} {unknown~font~option!} },
}

% \ProcessKeyOptions % -> ! LaTeX Error: Unknown option 'title' for package mypkg.
\ProcessKeyOptions[mypkg/title, mypkg/font] % -> ! LaTeX Error: Unknown option 'title' for package mypkg.
% \ProcessKeyOptions[mypkg/title] % -> ! LaTeX Error: Unknown option 'font' for package mypkg.
% \ProcessKeyOptions[mypkg/font] % -> ! LaTeX Error: Unknown option 'title' for package mypkg.
  • eine LaTeX-Datei test.tex,:
\documentclass{article}
\usepackage[title=smallcaps, font=regular]{mypkg}

\begin{document}
TEST
\end{document}

Antwort1

Im Folgenden wird ein unbekannter Handler eines weiteren Untermoduls verwendet, um die Schlüssel Ihrer Untermodule zu laden. Dies zerstört globale Schlüssel (also die an gegebenen Optionen \documentclass), funktioniert aber ansonsten einigermaßen gut.

\ProvidesExplPackage{mypkg}{2024/01/01}{v0.0.0}{mypkg test}

\seq_const_from_clist:Nn \c__mypkg_load_submodules_seq { title, font }
\keys_define:nn { mypkg / load }
  {
    unknown .code:n =
      \__mypkg_keymodules:Vn \l_keys_key_str {#1}
  }
\cs_new_protected:Npn \__mypkg_keymodules:nn #1#2
  {
    \seq_map_inline:Nn \c__mypkg_load_submodules_seq
      {
        \keys_if_exist:nnT { mypkg / ##1 } {#1}
          {
            % missing feature in l3keys, we have no better way to detect an
            % omitted value, so we have to guess that all empty values are in
            % fact omitted values.
            \tl_if_empty:nTF {#2}
              { \keys_set:nn { mypkg / ##1 } { #1 } }
              { \keys_set:nn { mypkg / ##1 } { #1 = {#2} } }
            \prg_break:
          }
      }
    \msg_error:nnn { mypkg } { unknown-key } {#1}
    \prg_break_point:
  }
\cs_generate_variant:Nn \__mypkg_keymodules:nn { Vn }
\msg_new:nnn { mypkg } { unknown-key } { Unknown~ key~ #1~ encountered. }

\keys_define:nn { mypkg / title }
{
    title .choice:,
    title .usage:n = { general },
    title .default:n = { regular },
    title / regular .code:n = 
    {
        \PackageWarning {mypkg} {We~use~regular~title!}
    },
    title / smallcaps .code:n = 
    {
        \PackageWarning {mypkg} {We~use~smallcaps~title!}
    },
    title / unknown .code:n = { \PackageWarning {mypkg} {unknown~title~option!} },
}

\keys_define:nn { mypkg / font }
{
    font .choice:,
    font .usage:n = { general },
    font .default:n = { regular },
    font / regular .code:n = 
    {
        \PackageWarning {mypkg} {We~use~regular~font!}
    },
    font / bold .code:n = 
    {
        \PackageWarning {mypkg} {We~use~bold~font!}
    },
    font / unknown .code:n = { \PackageWarning {mypkg} {unknown~font~option!} },
}

\ProcessKeyOptions[mypkg/load] % -> ! LaTeX Error: Unknown option 'title' for package mypkg.

PoC

Dies ist ein PoC zum Parsen der anfänglichen Optionen über die Untermodule (damit globale Optionen funktionieren), aber aller zukünftigen Optionen über unser load-Untermodul, das mit einem Handler für unbekannte Schlüssel agiert, und das alles, während jede Option nur einmal geparst wird.

Das ist zugegebenermaßen etwas verworren.

\ProvidesExplPackage{mypkg}{2024/01/01}{v0.0.0}{mypkg test}

\seq_const_from_clist:Nn \c__mypkg_load_submodules_seq { title, font }
\cs_new_protected:Npn \__mypkg_keymodules:nn #1#2
  {
    \seq_map_inline:Nn \c__mypkg_load_submodules_seq
      {
        \keys_if_exist:nnT { mypkg / ##1 } {#1}
          {
            % missing feature in l3keys, we have no better way to detect an
            % omitted value, so we have to guess that all empty values are in
            % fact omitted values.
            \tl_if_empty:nTF {#2}
              { \keys_set:nn { mypkg / ##1 } { #1 } }
              { \keys_set:nn { mypkg / ##1 } { #1 = {#2} } }
            \prg_break:
          }
      }
    \msg_error:nnn { mypkg } { unknown-key } {#1}
    \prg_break_point:
  }
\cs_generate_variant:Nn \__mypkg_keymodules:nn { Vn }
\msg_new:nnn { mypkg } { unknown-key } { Unknown~ key~ #1~ encountered. }

\keys_define:nn { mypkg / title }
{
    title .choice:,
    title .usage:n = { general },
    title .default:n = { regular },
    title / regular .code:n = 
    {
        \PackageWarning {mypkg} {We~use~regular~title!}
    },
    title / smallcaps .code:n = 
    {
        \PackageWarning {mypkg} {We~use~smallcaps~title!}
    },
    title / unknown .code:n = { \PackageWarning {mypkg} {unknown~title~option!} },
    unknown .code:n = {}
}

\keys_define:nn { mypkg / font }
{
    font .choice:,
    font .usage:n = { general },
    font .default:n = { regular },
    font / regular .code:n = 
    {
        \PackageWarning {mypkg} {We~use~regular~font!}
    },
    font / bold .code:n = 
    {
        \PackageWarning {mypkg} {We~use~bold~font!}
    },
    font / unknown .code:n = { \PackageWarning {mypkg} {unknown~font~option!} },
    unknown .code:n = {}
}

\ProcessKeyOptions[mypkg/title]
\ProcessKeyOptions[mypkg/font]

\keys_define:nn { mypkg / load }
  {
    unknown .code:n = {}
  }
\ProcessKeyOptions[mypkg/load] % -> ! LaTeX Error: Unknown option 'title' for package mypkg.

\keys_define:nn { mypkg / load }
  {
     unknown .code:n =
      \__mypkg_keymodules:Vn \l_keys_key_str {#1}
  }
\clist_map_inline:nn { title, font }
  {
    \keys_define:nn { mypkg / #1 }
      {
        \unknown .code:n =
          \msg_error:nne { mypkg } { unknown-key } \l_keys_key_str
      }
  }

Antwort2

Ich bin mir nicht sicher, warum Sie Schlüssel auf diese Weise definieren möchten. Aber Sie können einfach in jedem Modul die Verwaltung unbekannter Schlüssel hinzufügen.

\ProvidesExplPackage{mypkg}{2024/01/01}{v0.0.0}{mypkg test}

\keys_define:nn { mypkg/title }
{
    title .choice:,
    title .usage:n = { general },
    title .default:n = { regular },
    title / regular .code:n = 
    {
        \PackageWarning {mypkg} {We~use~regular~title!}
    },
    title / smallcaps .code:n = 
    {
        \PackageWarning {mypkg} {We~use~smallcaps~title!}
    },
    title / unknown .code:n = { \PackageWarning {mypkg} {unknown~title~option!} },
    unknown .code:n = {},
}
\keys_define:nn { mypkg/font }
{
    font .choice:,
    font .usage:n = { general },
    font .default:n = { regular },
    font / regular .code:n = 
    {
        \PackageWarning {mypkg} {We~use~regular~font!}
    },
    font / bold .code:n = 
    {
        \PackageWarning {mypkg} {We~use~bold~font!}
    },
    font / unknown .code:n = { \PackageWarning {mypkg} {unknown~font~option!} },
    unknown .code:n = {},
}

\ProcessKeyOptions[mypkg/title]
\ProcessKeyOptions[mypkg/font]

verwandte Informationen