使用 pgfkeys 的第一个字符语法

使用 pgfkeys 的第一个字符语法

我正在学习如何使用first char syntaxpdfkeys。我想让宏选项更人性化,然后将它们转换为数字,以便于进行 if-then 分析。但现在我遇到了一些麻烦:

以下 MWE

\documentclass{article}

\usepackage{xparse}
\usepackage{expl3}
\usepackage{pgfkeys}

\makeatletter

\pgfkeys{
    /handlers/first char syntax = true,
    /MakePage/.is family,
    /MakePage,
        PageNumbering/.style            =   {ResetPageTF/#1/.get = \@@ResetPageNumbering},
        Footer/.style                   =   {FooterTypes/#1/.get = \@@FooterType},
        ResetPageTF/.cd,
            Reset/.initial      =   1,
            Continue/.initial   =   0,
        FooterTypes/.cd,
            Blank/.initial  =   4,
            Empty/.initial  =   0,
            First/.initial  =   1,
            Last/.initial   =   2,
            Middle/.initial =   3,
}

\NewDocumentCommand{\MakePage}{ o m }{
    \pgfkeys{/MakePage, #1}%

    The \textbf{footer type} will be%
        \if\@@FooterType0
            Empty
        \fi
        \if\@@FooterType1
            First
        \fi
        \if\@@FooterType2
            Last
        \fi
        \if\@@FooterType3
            Middle
        \fi
        \if\@@FooterType4
            Blank
        \fi

    The \textbf{page numbering will be}
        \if\@@ResetPageNumbering1%
            Reset
        \else%
            Continued
        \fi
}

\makeatother

\begin{document}

    \MakePage[Footer = Blank, PageNumbering = Reset]{Hello}

\end{document} 

产生了一些错误。

在此处输入图片描述

出了什么问题?

答案1

\pgfkeys您调用FooterTypes/.cdafter 时ResetPageTF/.cd,这会将您引导至关键路径/MakePage/ResetPageTF/FooterTypes并且/MakePage/FooterTypes保持未定义状态。添加/MakePage,beforeFooterTypes/.cd将引导您进入正确的路径。

我还自由地实现了单个 char 语法。它基本上来自手册(第 880 页)。这里最重要的部分是要知道,传递给宏的参数包含关键字符,例如使用:Middle \singlechar@footergets:Middle作为其参数。这个宏的工作就是摆脱它。这里用 来完成\singlechar@@footer

备注:代码中的这些\typeout行仅用于调试,并会将一些信息写入日志文件。

代码:

\documentclass{article}

\usepackage{xparse}
\usepackage{expl3}
\usepackage{pgfkeys}

\makeatletter

\pgfkeys{
    /handlers/first char syntax = true,
    /handlers/first char syntax/the character !/.initial=\singlechar@pagenumbering,
    /handlers/first char syntax/the character :/.initial=\singlechar@footer,
    /MakePage/.is family,
    /MakePage,
        PageNumbering/.style            =   {ResetPageTF/#1/.get = \@@ResetPageNumbering},
        Footer/.style                   =   {FooterTypes/#1/.get = \@@FooterType},
        ResetPageTF/.cd,
            Reset/.initial      =   1,
            Continue/.initial   =   0,
    % go up again!
    /MakePage,
    % without it, the next line will lead to /MakePage/ResetPageTF/FooterTypes
        FooterTypes/.cd,
            Blank/.initial  =   4,
            Empty/.initial  =   0,
            First/.initial  =   1,
            Last/.initial   =   2,
            Middle/.initial =   3,
}

\newcommand\singlechar@pagenumbering[1]{%
    \typeout{XXXXXXXXXXXXXXXXXXXX page @: |#1|}%
    \singlechar@@pagenumbering#1\@@scend
}
\def\singlechar@@pagenumbering#1#2\@@scend{%
    \typeout{XXXXXXXXXXXXXXXXXXXX page @@: |#1|#2|}%
    \pgfkeysalso{PageNumbering={#2}}%
}

\newcommand\singlechar@footer[1]{%
    \typeout{XXXXXXXXXXXXXXXXXXXX footer @: #1}%
    \singlechar@@footer#1\@@scend
}
\def\singlechar@@footer#1#2\@@scend{%
    \typeout{XXXXXXXXXXXXXXXXXXXX footer @@: |#1|#2|}%
    \pgfkeysalso{Footer={#2}}%
}


\NewDocumentCommand{\MakePage}{ o m }{
    \pgfkeys{/MakePage, #1}%

    The \textbf{footer type} will be%
        \typeout{XXXXXXXXXXXXXXXXXXXX footer: \meaning\@@FooterType}
        \if\@@FooterType0
            Empty
        \fi
        \if\@@FooterType1
            First
        \fi
        \if\@@FooterType2
            Last
        \fi
        \if\@@FooterType3
            Middle
        \fi
        \if\@@FooterType4
            Blank
        \fi

    The \textbf{page numbering will be}
        \typeout{XXXXXXXXXXXXXXXXXXXX page: \meaning\@@ResetPageNumbering}
        \if\@@ResetPageNumbering1%
            Reset
        \else%
            Continued
        \fi
}

\makeatother

\begin{document}

    \MakePage[Footer = Blank, PageNumbering = Reset]{Hello}

    \MakePage[:Empty,!Continue]{Hello}

\end{document}

结果:

在此处输入图片描述

相关内容