# Using first char syntax with pgfkeys

I am learning how to use first char syntax with pdfkeys. I want to make macros options more human, but then turn them into numbers for easier if-then analysis. But right now I am having some troubles:

The following 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}


Produces something wrong.

What is going wrong?

• orthogonal comment: You probably would enjoy yax package more intuitive and easy to use. If you have this much of nesting. – percusse May 24 '18 at 19:15

In \pgfkeys you called FooterTypes/.cd after ResetPageTF/.cd, which leads you to the key path /MakePage/ResetPageTF/FooterTypes and /MakePage/FooterTypes stays undefined. Adding /MakePage, before FooterTypes/.cd will lead you to the correct path.

I also took the freedom to implement the single char syntax. It's basically from the manual (page 880). The important part here is to know, that the argument given to the macro contains the key char, e.g. with :Middle \singlechar@footer gets :Middle as its argument. It's the job of this macro to get rid of it. Here it is done with \singlechar@@footer.

Remark: in the code the \typeout lines are just for debugging and will write some information to the log file.

The code:

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


The result: