How does one restore the previous value of a PGF/TikZ key?

For example, I would like to set the inner sep of a matrix to 0em but set the inner sep of nodes within the matrix to be the value that existed just before the matrix was drawn. I thought that the following would work.




\begin{tikzpicture}[every node/.style={draw}]
  \pgfkeysgetvalue{/tikz/inner sep}{\previnnersep}
  \matrix [matrix of nodes, inner sep=0em,
           cells={inner sep=\previnnersep}] {
    X & Y \\ };


However, it produces output as if inner sep=0em were never included; this is certainly unexpected:

matrix with inner sep not set to 0em, surprisingly

Even more startlingly, when I change the cells option to be cells={inner sep=1em} but leave the \pgfkeysgetvalue line included, the output is exactly as before --- the same as if inner sep=0.3333em were used everywhere!

This odd behavior seems to originate from the \pgfkeysgetvalue line because everything is as expected when I comment it out.

Why is this happening, and how can I achieve the desired behavior?

  • Inner sep is not a value-storing key. It is an alias that further sets inner xsep and inner ysep. So you'd need to grab one of them. – Loop Space Jun 21 '12 at 22:08
  • 1
    @AndrewStacey You are right. Be sure to use the /pgf prefix: /pgf/inner xsep and /pgf/inner ysep. – Paul Gaborit Jun 21 '12 at 22:28
  • @PolGab Indeed, if I try the /tikz prefix instead of /pgf, I get the error "Undefined control sequence. \pgfmath@dimen@ ...men@@ #1=0.0pt\relax \pgfmath@" However, since page 176 of the 2.10 manual says that /tikz/inner xsep is an alias for /pgf/inner xsep, I'm curious why it is necessary to use /pgf for this to work. – Henry DeYoung Jun 22 '12 at 0:59
  • @AndrewStacey In that case, why does \pgfkeysgetvalue{/tikz/inner sep}{\previnnersep} not give some sort of error instead of the very odd behavior? Trying to use \pgfkeysgetvalue{/pgf/inner sep}{\previnnersep} does give an error, though. – Henry DeYoung Jun 22 '12 at 1:06
  • 1
    Because /tikz/pgf is not actually an alias, it isn't defined. When you call it, pgfkeys passes unknown tikz keys on to pgf so it is effectively an alias. But getting its value uses a different mechanism to calling so it doesn't notice the alias. – Loop Space Jun 22 '12 at 6:43

Based on the comments by Andrew Stacey and PolGrab, you could create a style that achieves the effect you describe:

\tikzset{cells preserve inner sep/.style={
        inner xsep=\pgfkeysvalueof{/pgf/inner xsep},
        inner ysep=\pgfkeysvalueof{/pgf/inner ysep}

Then use it like this (the order is important):

\begin{tikzpicture}[every node/.style={draw}]
    \matrix [matrix of nodes, cells preserve inner sep, inner sep=0em] {
        X & Y \\


cells preserve inner sep


A re-arranged approach is:

\tikzset{matrix style/.style={
    inner xsep=\pgfkeysvalueof{/pgf/inner xsep},
    inner ysep=\pgfkeysvalueof{/pgf/inner ysep}
  matrix of nodes, inner sep=0em

\begin{tikzpicture}[every node/.style={draw}]
    \matrix [matrix style] {X & Y \\};

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.