TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create some templating infrastructure that heavily depends on pgfkeys' styles to easily customise the appearance of how some bits of text are typeset on my document. However, I've run into some problem when trying to append more key-value pairs to a style that, as its job, already defined more codes or styles.

The following example shows the problem I'm talking about:


  /my package/.cd,
  fancy/.style = {
    format/.code = \textbf{##1},
  format=hello, % typesets `hello' in boldface
  fancy/.append style = { },
  format=hello, % nothing is typeset
%  format/.show code, % uncomment to see the definition of format


Uncommenting the last line in the \pgfkeys command shows that, after trying to append to the fancy style, the code in the format key has been replaced by:

\textbf {\pgfkeysnovalue }

and thus nothing is typeset on the second format=hello.

I (sort of) understand why this happens. The same reason why I had to write ##1 when defining the code within the style. However, is there a fix or workaround to make the style “robust” enough so that it is possible to append to it without breaking it?

Edit 1

Thanks to the comment from Joseph Wright and attempted answer from percusse, I figured out that—at the lower level—the problem to solve is the following: given an already defined macro which takes one argument (this is now codes/styles in pgfkeys are internally defined), patch the replacement text of the macro by appending some more text.

Now, from etoolbox, there is a macro for doing just this: \apptocmd. The following is an example of how a solution using this macro should probably look like:


The only problem is that the second patching attempt doesn't work, and etoolbox shows the following as debug information:

[debug] tracing \apptocmd on input line 10
[debug] analyzing '\fancy'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] ++ control sequence is a macro with parameters
[debug] -- nested patching command and parameters in patch
[debug] -> the patching command seems to be nested in the
[debug]    argument to some other command
[debug] -> the patch text seems to contain # characters
[debug] -> either avoid nesting or use # characters with
[debug]    category code 12 in the patch text
[debug] -> simply doubling the # characters will not work

Would it be possible to have an \apptocmd that works even with # characters in the new replacement text?

Disclaimer: I do now have some workaround for my particular case, which only works because my fancy styles in fact do not need to take any arguments (format does, but fancy doesn't). So, instead of storing the code as a style, I store it as a regular plain value, and make .code expand to this value. Later, a simple /.append can be used to patch the “fake” style.

Still, I would very much like a solution to the original problem, so that one can also append to styles containing both arguments and (let's call them) sub-arguments. I don't know, however, if this is technically possible. So, this is my deal: If someone believes that a general solution is feasible, and lets me know this by just posting a comment, I'm willing to open a 300 bounty of my rep for a working solution to the general case. If, however, people believe that this is either just too difficult or technically impossible (and I suspect of this because etoolbox did not implement it) then I'll just be content with my sillier workaround and post a full answer expanding on it.

share|improve this question
I'd say this looks like a bug in pgfkeys: if you try fancy/.show code you get \pgfkeysalso { format/.code = \textbf {##1}, } to begin with but \pgfkeysalso { format/.code = \textbf {#1}}\pgfkeysalso { } later, with the loss of a #. – Joseph Wright Apr 11 '13 at 17:06

This looks like a bug in pgfkeys. If you do

  /my package/.cd,
  fancy/.style = {
    format/.code = \textbf{##1},
  fancy/.show code,
  fancy/.append style = { },
  fancy/.show code

you see

\pgfkeysalso { format/.code = \textbf {##1}, }

the first time but

\pgfkeysalso { format/.code = \textbf {#1}}\pgfkeysalso { }

the second time: note the loss of a #.

The code which underlies .append style is

\pgfkeys{/handlers/.add code/.code 2 args=%
  % Find out, whether with args or not.
  {% Yes, so add to body and reuse args
    % Redefine code
      % support for \pgfkeysndefargs:
      % support for \pgfkeysdefargs:
    % No, so single argument (simple \pgfkeysdef). Redefine accordingly.
      {\pgfkeys@temptoks\expandafter\expandafter\expandafter{\csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname##1\pgfeov}}%
      \xdef\pgfkeys@global@temp{\the\toks0 \the\pgfkeys@temptoks \the\toks1 }%

The issue is the line toward the end


As this simply expands \pgfkeys@global@temp, TeX will undouble any # it contains. Instead, an \edef plus toks should be used:

share|improve this answer
Reported as sourceforge.net/p/pgf/bugs/250 – Joseph Wright Jul 7 '13 at 6:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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