1

I need to define a style contained in a macro without expanding this macro.

The problem is that if the macro contains several keys like or sets a value to a key like here:

\def\myconfiguration{text={The value of my great counter is \themycounter}}

it fails because pgfkey tries to first evaluate the macro, and then checks if a key is named text={The value ...}, which of course is wrong.

So what is the good way to define a pgfkey style inside a macro, and make sure that this macro is not expanded at definition time?

MWE:

\documentclass{article}

\usepackage{pgfkeys}

\newcounter{mycounter}
\def\myconfiguration{text={The value of my great counter is \themycounter}}

\pgfkeys{
  /prAtEnd/.cd, 
  % Text
  text/.code={\def\sayhello{#1}},
  configuration options/.style={
    text={The counter value is \themycounter},
    \myconfiguration %% This lines fails!
  },
}

\begin{document}

\pgfkeys{/prAtEnd/.cd, configuration options}

\sayhello

\stepcounter{mycounter}
\sayhello

\stepcounter{mycounter}
\sayhello
\end{document}

-- EDIT -- I would like to have at the end the same result as if \myconfiguration was replaced with it's definition, i.e. text={The value of my great counter is \number\value{mycounter}}. So the output must be:

enter image description here

2 Answers 2

1

\myconfiguration is not a style. Below is something that works, but if you tell us what you really have in mind there might very well be a better proposal.

\documentclass{article}

\usepackage{pgfkeys}

\newcounter{mycounter}
\def\myconfiguration{text={The value of my great counter is \number\value{mycounter}}}

\pgfkeys{/prAtEnd/.cd, 
  % Text 
  text/.code={\def\sayhello{#1}},
  configuration options/.style={
    text={The counter value is \number\value{mycounter}}},
  configuration options/.style/.expand once=\myconfiguration
}

\begin{document}

\pgfkeys{/prAtEnd/.cd, configuration options}

\sayhello

\stepcounter{mycounter}
\sayhello

\stepcounter{mycounter}
\sayhello
\end{document}

enter image description here

13
  • thanks for the answer, but it's not exactly what I want. What I would like to have is to replace all the The counter value is X with The value of my great counter is X (as configured in the \myconfiguration macro). Basically, you can imagine that you replace the \myconfiguration with text={The value of my great counter is \number\value{mycounter}} to get what I want. (see my edit)
    – tobiasBora
    Commented May 6, 2019 at 18:39
  • @tobiasBora I added a version that does that. However, I would like to argue that it would be much more elegant if the store the revised configuration options in a style, and not in a macro.
    – user121799
    Commented May 6, 2019 at 18:53
  • Thanks for the edit, I didn't know the .expanded trick. But there is still two issues: first I'd like to avoid to change the definition of \myconfiguration because I would like it to be consistent with the remaining configuration that does not require \noexpand. Second, this overwrite all the definitions of configuration options (this may be fixable by using a temporary style, but sounds strange).
    – tobiasBora
    Commented May 6, 2019 at 19:06
  • For your second comment, I'm not sure if I can, because I'm getting this macro from two ways: the first time I'm getting it from kvoptions (this is send to package configuration), and the second time I need to define a "global" style to go though environments, so I use \global\def..., and unfortunately pgfkeys seems to be only local.
    – tobiasBora
    Commented May 6, 2019 at 19:06
  • @tobiasBora Well, the main problem is that I do not understand your question. I thought I did to some extent, but then there are all the extra boundary conditions not listed in the question. You wrote "I would like to have at the end the same result as if \myconfiguration was replaced with it's definition", and this is what the above answer accomplishes.
    – user121799
    Commented May 6, 2019 at 20:00
1

After getting inspired by this, I came up with this solution (edit: .expand once is better):

\documentclass{article}

\usepackage{pgfkeys}

\newcounter{mycounter}
\def\myconfiguration{text={The value of my greatt counter is \number\value{mycounter}}}

\show\myconfiguration

\pgfkeys{
  /prAtEnd/.cd, 
  % Text
  text/.code={\def\sayhello{#1}},
  second text/.code={\def\saybye{#1}},
  configuration options/.style={
    text={The counter value is \number\value{mycounter}},
  },
}

\edef\mydefinepgfkeys{\noexpand\pgfkeys{%
    /prAtEnd/.cd,%
    configuration options/.append style={%
      \unexpanded\expandafter{\myconfiguration}%
    },%
  }}\mydefinepgfkeys

\begin{document}

\pgfkeys{/prAtEnd/.cd, configuration options}

\sayhello
% \saybye
\stepcounter{mycounter}
\sayhello

\stepcounter{mycounter}
\sayhello
\end{document}

You must log in to answer this question.

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