概念驗證

概念驗證

我想知道是否可以l3keys像 pgfoption 那樣處理多個子模組?

這是一個 MWE 來說明我的問題:

  • 一個套件檔案mypkg.sty(在註解中包含我嘗試編譯 LaTeX 檔案時遇到的錯誤):
\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.
  • 一個乳膠文件,test.tex
\documentclass{article}
\usepackage[title=smallcaps, font=regular]{mypkg}

\begin{document}
TEST
\end{document}

答案1

下面使用另一個子模組的未知處理程序來載入子模組的金鑰。這會破壞全域鍵(因此給定的選項\documentclass),但在其他方面工作得很好。

\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(以便全域選項起作用),但是透過我們的load-submodule 與未知密鑰處理程序一起使用的任何未來選項,所有這些都只解析一次每個選項。

誠然,這有點令人費解。

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

答案2

我不確定你為什麼要這樣定義鍵。但您可以簡單地在每個模組中新增對未知密鑰的管理。

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

相關內容