4

I defined a pgf key with a default value. In the following code I can overwrite its value in the default argument I created but I can't change the value using the pgfsetkeyvalue macro.

\documentclass{article}
\usepackage{fontspec}

\usepackage{tikz}
\pgfkeys{
   /titleblock/.is family, /titleblock,
   titlesize/.default = 48,
   titlesize/.store in = \titlesize,
   titlesize
}

\newcommand{\titleblock}[1][]{%
   \pgfkeys{/titleblock/.cd, #1}%
    \node[align=left, inner sep=0mm, outer sep=0mm,
    font={\fontsize{\titlesize}{2\titlesize}\selectfont}]
    (title) 
    at (0,0)
    {This is my test title};
}


\begin{document}

\begin{tikzpicture}
  \titleblock
\end{tikzpicture}


 \begin{tikzpicture}
   \titleblock[titlesize=25pt]
 \end{tikzpicture}

\pgfkeyssetvalue{/titleblock/titlesize}{10}

\begin{tikzpicture}
  \titleblock
\end{tikzpicture}

\end{document}

Is there anyway that I can change this value without using the optional argument of \titleblock?

3

Key /titleblock/titlesize does not store the value directly, but defines a handler to store it in \titlesize. Therefore, \pgfkeys and friends can be used for setting as done in \titleblock:

\pgfkeys{/titleblock/titlesize=10}

or with a setup command:

\newcommand*{\titleblocksetup}{\pgfqkeys{/titleblock}}
...
\titleblocksetup{titlesize=10}

Full example:

\documentclass{article}
\usepackage{fontspec}

\usepackage{tikz}
\pgfkeys{
   /titleblock/.is family, /titleblock,
   titlesize/.default = 48,
   titlesize/.store in = \titlesize,
   titlesize
}
\newcommand*{\titleblocksetup}{\pgfqkeys{/titleblock}}

\newcommand{\titleblock}[1][]{%
   \pgfkeys{/titleblock/.cd, #1}%
    \node[align=left, inner sep=0mm, outer sep=0mm,
    font={\fontsize{\titlesize}{2\titlesize}\selectfont}]
    (title)
    at (0,0)
    {This is my test title};
}

\begin{document}

\begin{tikzpicture}
  \titleblock
\end{tikzpicture}


 \begin{tikzpicture}
   \titleblock[titlesize=25pt]
 \end{tikzpicture}

\titleblocksetup{titlesize=10}

\begin{tikzpicture}
  \titleblock
\end{tikzpicture}

\end{document}

Result

2

As Heiko says, the problem is that \pgfkeyssetvalue does not invoke the handler so \titlesize is not set by \pgfkeyssetvalue{/titleblock/titlesize}{10}.

When I use pgfkeys I prefer to store the values in the keys rather than have the keys define macros because this seems inefficient to me. This is how I would code your example:

\documentclass{article}

\usepackage{tikz}
\pgfkeys{/titleblock/.is family, /titleblock,
   titlesize/.initial = 25,% initial value of key
}

\newcommand{\titleblock}[1][]{%
   \pgfkeys{/titleblock, #1}%
   \pgfkeysgetvalue{/titleblock/titlesize}{\titlesize}% key val -> \titlesize
   \node[align=left, inner sep=0mm, outer sep=0mm,
         font={\fontsize{\titlesize}{2\titlesize}\selectfont}]
        (title) at (0,0) {This is my test title};
}

\begin{document}

    \begin{tikzpicture}
      \titleblock
    \end{tikzpicture}

    \begin{tikzpicture}
      \titleblock[titlesize=15]
    \end{tikzpicture}

    \pgfkeyssetvalue{/titleblock/titlesize}{10}% set key value

    \begin{tikzpicture}
      \titleblock
    \end{tikzpicture}

\end{document}

The output is much the same as what Heiko has, but not as pretty:

enter image description here

In the \titleblock macro I have extracted the current key value into the macro \titlesize. This isn't really necessary but I didn't want to write

font={\fontsize{\pgfkeysvalueof{/titleblock/titlesize}}%
               {2\pgfkeysvalueof{/titleblock/titlesize}}\selectfont}

Finally, I have actually used font sizes of 25, 15 and 10, respectively. This is only because I took out the \usepackage{fontspec} line and used pdflatex.

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.