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?

3
  • 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
    Commented Apr 9, 2012 at 10:27
  • @percusse I would like both to point to the same macro color->color@cx and somecolor->color@cx.
    – yannisl
    Commented Apr 9, 2012 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
    Commented Apr 9, 2012 at 10:51

3 Answers 3

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}}
4
  • Note that this is 100% equivalent to other path/somecolor/.style={path/color=#1}. This is because /.style is defined by means of \pgfkeysalso (just as you did). An "alias" in this sense is a style. Commented May 11, 2012 at 20:30
  • @ChristianFeuersänger:Thanks for the info; I have edited this into the answer.
    – Daniel
    Commented May 11, 2012 at 21:10
  • I added a missing closing brace to Daniel's second solution.
    – Ahmed Musa
    Commented May 11, 2012 at 23:58
  • @Daniel I would appreciate your comments on the code I posted --still finding my way around pgf:) .
    – yannisl
    Commented Jun 6, 2012 at 13:48
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}
4
  • I don't know pgfkeys well, but what is the name/macro \pgfkeyscurrentpath/.@blank supposed to do?
    – Ahmed Musa
    Commented Jun 6, 2012 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
    Commented Jun 6, 2012 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
    Commented Jun 6, 2012 at 23:06
  • That would be a handy addition to pgfkeys, unless some similar already exists. You could propose it. Commented Jul 14, 2019 at 8: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".

4
  • Thanks. There is a slight difference, with .foward to its own code is executed as well before forwarding. See the example on the page you provided.
    – yannisl
    Commented Jul 20, 2018 at 13:54
  • @YiannisLazarides True! However, that is only if a key with that name exists. So you could use it as an alias if you have control over your keys :) Commented Jul 20, 2018 at 13:55
  • 1
    True. PGFKeys is such an amazing package.
    – yannisl
    Commented Jul 20, 2018 at 14:19
  • The /.forward to key is on page 663 of version 2.10 of the TikZ manual, so it has been around for a long time.
    – AndréC
    Commented Dec 15, 2018 at 20:34

You must log in to answer this question.

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