1

In \pgfkeys, I want the value of a key(b in my following example) changes with another key(a in my example) wihout introducing a new macro. How to get it?

Example:

\documentclass[a4paper]{article}
\usepackage{pgf}
\usepackage{geometry}
\geometry{showframe}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}

\begin{document}
\parindent=0pt
\pgfkeys{
  a/.initial=1in,
  b/.initial=a    % I know this is wrong.
}
\newcommand\test[1][]{
  \begingroup
  \pgfkeys{#1}
  \fbox{\parbox{\pgfkeysvalueof{/a}}{\pgfkeysvalueof{/a}}}\\
  \rule{\pgfkeysvalueof{/b}}{3pt}
  \endgroup
}
\test\\
\test[a=1cm]\\
\test[b=6cm]\\
\test
\end{document}

1 Answer 1

1

Here is a solution:

\documentclass[a4paper]{article}
\usepackage{pgf}
\usepackage{geometry}
\geometry{showframe}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}

\begin{document}
\parindent=0pt
\pgfkeys{
  a/.code={%
    \pgfkeyssetvalue{/a}{#1}%
    \pgfkeyssetvalue{/b}{#1}%
  },
  a=1in,
}
\newcommand\test[1][]{
  \begingroup
  \pgfkeys{#1}
  \fbox{\parbox{\pgfkeysvalueof{/a}}{a:\pgfkeysvalueof{/a}, b:\pgfkeysvalueof{/b}}}\\
  \rule{\pgfkeysvalueof{/b}}{3pt}
  \endgroup
}
\test\\
\test[a=1cm]\\
\test[b=6cm]\\
\test[a=2cm,b=6cm]\\
\test[b=6cm,a=2cm]
\end{document}

The last two examples show a potential problem: the order of the keys matters.

You must log in to answer this question.

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