4

Based on this pgfkeys method, I tried to collect options in a macro for later re-use:

% Set options for a photo
% #1 = options, #2 = photo name, i.e. June
\newcommand\SetPhoto[2][]{%
  \expandafter\def\csname #2photoOptions\endcsname{#1}%
}

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

% Place the photo using eariler options
% #1 = photo name
\newcommand\DoPhoto[1]{%
  \pgfkeys{
    /Photo, defaults,
    \csname #1photoOptions\endcsname,
  }%

  % ...
  % ... deal with placing the photo, \includegraphics and so on
  % ...
}

And use it like this:

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

% typeset the photos with the given options 
\DoPhoto{June}
\DoPhoto{July}

But pgfkeys sees the whole content of the macro (bleed=3mm) as a key:

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

How to interpret the contents of a macro as key=value assignment?

2

Either you have to expand the control sequence inside the definition with any kind of \expandafter,\edef combo, or you use keys for everything. Since the control sequence holds the style keys there is no point on defining a macro. Instead use the key system and dump the options in a style:

\newcommand*\SetPhoto[2][]{\pgfkeys{/Photo/#2photoOptions/.style={#1}}

and then when you have

\newcommand*\DoPhoto[1]{\pgfkeys{/Photo, defaults,#1photoOptions}}

or the expanded version of this definition.

  • The problem is that the key in \SetPhoto will be local, and \DoPhoto won't see it. I tried expanding the macro, but I didn't have success with that either. – Gambhiro Oct 15 '15 at 19:51
  • @Nyiti Shouldn't be local. Are you setting the key in some environment? – percusse Oct 15 '15 at 19:51
  • you're right, it compiles in a separate mwe. I'm messing it up somewhere else. – Gambhiro Oct 15 '15 at 20:11
  • I need this in a different situation where passing through a macro is really what I need. I cannot devise the "any kind of \expandafter,\edef combo". Can you give me any hint about it? – gigabytes Nov 30 '17 at 5:18
2

Based on percusse's answer:

\documentclass{article}

\makeatletter

\usepackage{pgfkeys}

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

\newcommand\SetPhoto[2][]{\pgfkeys{/Photo/#2opts/.style={#1}}}

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

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

photo placeholders

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.