10

I would like to be able to alias a PGF key to another name, is this possible and if it is, how do I do this?

For example if I have defined a key as:

 \pgfkeys{path/
    color/.store in=\color@cx
  }

I would like have the ability to let "color" to "somecolor" would it also be possible to alias keys that have been defined using .style?

percusse
  • 157,807
yannisl
  • 117,160
  • In this particular example, whenever you refer to it with color=somecolor it would be stored in \color@cx. Which one do you want to access later with alias? There is /.link key but I don't know if that's what you are after. – percusse Apr 09 '12 at 10:27
  • @percusse I would like both to point to the same macro color->color@cx and somecolor->color@cx. – yannisl Apr 09 '12 at 10:42
  • 1
    They can both be appointed to execute the same /.code. For example, \pgfkeys{setmycolor/.code={\color@cx=#1}}\/pgfkeys{color/.style={setmycolor=blue}}\pgfkeys{somecolor/.style={setmycolor=red}}. – percusse Apr 09 '12 at 10:51

3 Answers3

7

The approach I usually employ is to use a .code handler that just executes \pgfkeysalso on the alias target:

\pgfkeys{other path/somecolor/.code={\pgfkeysalso{path/color=#1}}

However, as I have learned from Christian Feuersänger, this is exactly how the .style handler is defined in pgfkeys. So an even cleaner solution is:

\pgfkeys{/other path/somecolor/.style={path/color=#1}}
Ahmed Musa
  • 11,742
Daniel
  • 37,517
4

I have expanded on Daniel's idea and used handlers to define a handler called .alias. The code is shown below:

\documentclass{article}
\usepackage{pgfkeys}
\begin{document}
\makeatletter
% alias key
\pgfkeys{%
   /handlers/.alias/.code=
      \pgfkeysedef\pgfkeyscurrentpath{%
                  \noexpand\pgfkeysalso{\pgfkeysdefaultpath#1={##1}}},%
      /handlers/.alias/.value required,%
      /handlers/.blank/.code=\pgfkeyssetvalue{\pgfkeyscurrentpath/.@blank}{#1},%
      /handlers/.blank/.default=\pgfkeysnovalue,%
}

\pgfkeys{test/.store in=\storethis,
         tes/.alias=test,}

\pgfkeys{tes=123}
\storethis 

\pgfkeys{test=this is a test}
\storethis
\end{document}
yannisl
  • 117,160
  • I don't know pgfkeys well, but what is the name/macro \pgfkeyscurrentpath/.@blank supposed to do? – Ahmed Musa Jun 06 '12 at 16:03
  • Good idea, way more PGFish :-) I probably would reverse the syntax and make .alias a handler that is executed on the key to link to (test/.alias=tes) instead of the alias, as this seems more natural to me (reflects the order of parameters to ln -s on UNIX) – Daniel Jun 06 '12 at 20:47
  • @AhmedMusa It creates a handler to provide a value for the empty case i.e, test/.default=default, test/.blank=this is blank, will print for test default and for test= will give this is blank. Not related to the question, but thought could be useful to leave it in. – yannisl Jun 06 '12 at 23:06
  • That would be a handy addition to pgfkeys, unless some similar already exists. You could propose it. – Faheem Mitha Jul 14 '19 at 08:34
3

Came across the .forward to handler in the pgf manual on page 891.

\documentclass{article}
\usepackage{pgfkeys}
\begin{document}
    \makeatletter
    \pgfkeys{
        path/color/.store in=\color@cx,
        path/somecolor/.forward to=/path/color
    }
    \pgfkeys{path/somecolor=red}
    \color@cx
\end{document}

Which produces "red".