使用 pgfopts 定义所需选项

使用 pgfopts 定义所需选项

我想用 定义一个必需的包选项pgfopts。到目前为止,我已经完成了以下工作:

\begin{filecontents}{\jobname.sty}
    \ProvidesPackage{\jobname}[2018-01-24 v1.0 Test package]
    \RequirePackage{etoolbox, pgfopts}

    \pgfkeys{
        testproj/.cd,
        required entry/.store in = \myValue,
        required entry = {} % <-- Set default to empty
    }

    \newrobustcmd\mysetup[1]{\pgfqkeys{/testproj}{#1}}
    \ProcessPgfOptions{/testproj}

    \ifcsempty{myValue}{%
        \PackageError{\jobname}{Option required entry was not set}{help text}%
    }{}%

    \endinput
\end{filecontents}

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{\jobname}

\mysetup{%
    required entry = test
}

\begin{document}
    myValue: \myValue
\end{document}

问题是 PackageError 总是会引发。我想要的是,只有当required entrywithin\mysetup未设置或等于空时才会引发错误。

答案1

您检查得太早了:在阅读序言的其余部分之前,先阅读了全文.sty。因此,请推迟检查:

\begin{filecontents}{\jobname.sty}
  \ProvidesPackage{\jobname}[2018-01-24 v1.0 Test package]
  \RequirePackage{etoolbox, pgfopts}

  \pgfkeys{
    testproj/.cd,
    required entry/.store in = \myValue,
    required entry = {} % <-- Set default to empty
  }

  \newrobustcmd\mysetup[1]{\pgfqkeys{/testproj}{#1}}
  \ProcessPgfOptions{/testproj}

  \AtBeginDocument{%
    \ifcsempty{myValue}{%
      \PackageError{\jobname}{Option required entry was not set}{help text}%
    }{}%
  }

  \endinput
\end{filecontents}

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{\jobname}

\mysetup{%
    required entry = test
}

\begin{document}
    myValue: \myValue
\end{document}

相关内容