0

Using pgfkeys, I'd like to initialize keys as in setting attributes with an object constructor.

Such that I can create keys /Photo/May, /Photo/June etc. based on some initial properties of /Photo, and set some default values.

Apparently this should be easy, but I don't seem to interpret the manual correctly:

! Package pgfkeys Error: I do not know the key '/Photo/May/defaults' and I am going to ignore it. Perhaps you misspelled it.

MWE:

\documentclass{article}

\makeatletter

\usepackage{pgfkeys}

\pgfkeys{
  /Photo/.is family, /Photo,
  init/.style = {
    defaults/.style = {file = {}, bleed = 0pt},
    file/.initial = {},
    bleed/.initial = {},
  }
}

% #1 = options, #2 = photo name
\newcommand\SetPhoto[2][]{%
  \pgfkeys{%
    /Photo/#2/.is family, /Photo/#2,
    /Photo/#2/.append style=/Photo/init,
    defaults, file={#2},
    #1%
  }%
}

\newcommand\DoPhoto[1]{%
  % suppose it was an image...
  \hskip -\pgfkeysvalueof{/Photo/#1/bleed}%
  \frame{\pgfkeysvalueof{/Photo/#1/file}}%
}

\setlength{\parskip}{0pt}%
\setlength{\parindent}{0pt}%

\makeatother

\begin{document}

% somewhere early in the document
\SetPhoto{May}%
\SetPhoto[bleed=3mm]{June}%
\SetPhoto[bleed=5mm]{July}%

% typeset the photos with the given options 

\DoPhoto{May}

\DoPhoto{June}

\DoPhoto{July}

\end{document}

1 Answer 1

1

Gotcha! You don't .append style=/Photo/init, you just just call /Photo/init to apply the style set in it.

% #1 = options, #2 = photo name
\newcommand\SetPhoto[2][]{%
  \pgfkeys{%
    /Photo/#2/.is family, /Photo/#2,
    /Photo/init,
    defaults, file={#2},
    #1%
  }%
}

So the correct mwe:

\documentclass{article}

\makeatletter

\usepackage{pgfkeys}

\pgfkeys{
  /Photo/.is family, /Photo,
  init/.style = {
    defaults/.style = {file = {}, bleed = 0pt},
    file/.initial = {},
    bleed/.initial = {},
  }
}

% #1 = options, #2 = photo name
\newcommand\SetPhoto[2][]{%
  \pgfkeys{%
    /Photo/#2/.is family, /Photo/#2,
    /Photo/init,
    defaults, file={#2},
    #1%
  }%
}

\newcommand\DoPhoto[1]{%
  % suppose it was an image...
  \hskip -\pgfkeysvalueof{/Photo/#1/bleed}%
  \frame{\pgfkeysvalueof{/Photo/#1/file}}%
}

\setlength{\parskip}{0pt}%
\setlength{\parindent}{0pt}%

\makeatother

\begin{document}

% somewhere early in the document
\SetPhoto{May}%
\SetPhoto[bleed=3mm]{June}%
\SetPhoto[bleed=5mm]{July}%

% typeset the photos with the given options 

\DoPhoto{May}

\DoPhoto{June}

\DoPhoto{July}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .