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?

  • 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 9 '12 at 10:27
  • @percusse I would like both to point to the same macro color->color@cx and somecolor->color@cx. – Yiannis Lazarides Apr 9 '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 9 '12 at 10:51
6

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}}
  • 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. – Christian Feuersänger May 11 '12 at 20:30
  • @ChristianFeuersänger:Thanks for the info; I have edited this into the answer. – Daniel May 11 '12 at 21:10
  • I added a missing closing brace to Daniel's second solution. – Ahmed Musa May 11 '12 at 23:58
  • @Daniel I would appreciate your comments on the code I posted --still finding my way around pgf:) . – Yiannis Lazarides Jun 6 '12 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}
  • I don't know pgfkeys well, but what is the name/macro \pgfkeyscurrentpath/.@blank supposed to do? – Ahmed Musa Jun 6 '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 6 '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. – Yiannis Lazarides Jun 6 '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 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".

  • 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. – Yiannis Lazarides Jul 20 '18 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 :) – Andreas Storvik Strauman Jul 20 '18 at 13:55
  • 1
    True. PGFKeys is such an amazing package. – Yiannis Lazarides Jul 20 '18 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 Dec 15 '18 at 20:34

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.