# pgfkeys fails with initial blank line?

I am trying to learn pgfkeys based on the example code shown in How to create a command with key values?. Everything seems to be working as expected except that the code is sensitive to a blank line before the first parameter. Is there any way to get rid of this so that both beautiful and ugly work in the MWE below? (I was expecting this to already by answered, but a search did not seem to find it...)

\documentclass[10pt]{article}

\usepackage{pgfkeys}

\pgfkeys{ /mycmd/.is family, /mycmd,
default/.style = {cmdOpt = {}},
cmdOpt/.estore in = \mycmdOpt}

\newcommand\mycmd[1][]{%
\pgfkeys{/mycmd, default, #1}%
Hello \mycmdOpt World.
}

\begin{document}

\mycmd[cmdOpt = beautiful];

\mycmd[
cmdOpt = ugly];
\end{document}


EDIT:

Note that there are two workarounds that I know of:

\mycmd[%
cmdOpt = ugly];


and

\mycmd[,
cmdOpt = ugly];


I was hoping for something that would not require either of these...

• Key names are space sensitive because they are put inside a \csname...\endcsname pair so you need to create the key starting with a / to define thepath. Otherwise a whitespace character becomes the key root. – percusse Apr 26 '18 at 15:26
• I'm new to this. I tried just putting a /cmdOpt=ugly, and that did not work. I actually don't know in my code example above where the cmdOpt is being created. I don't see something that looks like a declaration. Can you post something with what you are suggesting as a solution? – Christopher Donham Apr 26 '18 at 15:32
• So you need to remove the whitespace before #1 – percusse Apr 26 '18 at 15:32
• Thanks. I don't know how to mark your comments as the "answer" to my question... – Christopher Donham Apr 26 '18 at 15:39
• No worries you can answer your own question. It's perfectly fine. I didn't have access to TeX so didn't test my own suggestion hence my comments. – percusse Apr 26 '18 at 15:40

Base on the comment from https://tex.stackexchange.com/users/3235/percusse, I tried the following which works. Note the subtle change from my original code -- there is no space between the comma and the #1. ",#1" instead of ", #1". This works.

\documentclass[10pt]{article}

\usepackage{pgfkeys}

\pgfkeys{ /mycmd/.is family, /mycmd,
default/.style = {cmdOpt = {}},
cmdOpt/.estore in = \mycmdOpt}

\newcommand\mycmd[1][]{%
\pgfkeys{/mycmd, default,#1}%
Hello \mycmdOpt World.
}

\begin{document}

\mycmd[cmdOpt = beautiful];

\mycmd[
cmdOpt = ugly];
\end{document}


If you want purely key processing and are not using them with TikZ, you could consider the keyval functions in expl3: this code strips spaces at the start and end of all key names automatically:

\documentclass[10pt]{article}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \mycmd { O { } }
{
\keys_set:nn { mycmd } { default , #1 }
Hello~\mycmdOpt\space World.
}
\keys_define:nn { mycmd }
{
default .meta:n   = { cmdOpt = { } } ,
cmdOpt  .tl_set:N = \mycmdOpt % You could use .tl_set_x:N ...
}
\ExplSyntaxOff

\begin{document}

\mycmd[cmdOpt = beautiful];

\mycmd[
cmdOpt = ugly];
\end{document}

• Actually, this is from deep inside some tikz code that automatically draws annotate Normal and Student T distributions for hypothesis testing. I did not post that because it didn't seem to be needed in an MWE to show the issue. Thanks though. – Christopher Donham Apr 26 '18 at 15:48