1

I want to make a key by \pgfkeys whose value is a macro. But the following try fails. Please see my question in the code.

\documentclass[a4paper]{article}
\usepackage{xcolor,pgf}
\usepackage{geometry}
\geometry{showframe}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
\begin{document}
\pgfkeys{
  halign/.code=#1,
  halign/.default=\centering,
  width/.initial,
  width/.default=4in
}
\newcommand\test[1][]{
  \pgfkeys{width,#1}
    \fbox{\parbox{\pgfkeysvalueof{/width}}{\pgfkeys{/halign} some text}}
}
\test\\
\test[halign=AAA]\\% Why is "AAA" out of the box?

\end{document}

Why is the following version not able to get what I want?

\documentclass[a4paper]{article}
\usepackage{xcolor,pgf}
\usepackage{geometry}
\geometry{showframe}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
\begin{document}
\pgfkeys{
  halign/.initial={}, halign/.default={},
  width/.initial=4in,
}
\newcommand\test[1][]{{%
  \pgfkeys{halign,#1}%
  \fbox{\parbox{\pgfkeysvalueof{/width}}{\pgfkeys{/halign} some text}}%
}}
\test[width=2in,halign=AAA]\\
\test[halign=\raggedleft]\\
\test
\end{document}

1 Answer 1

2

To use some pgfkeys (a standalone package which is auto loaded by pgf) key to hold some value, generally one can use

  • key/.initial=<init value> to initialize a key,
  • key=<new value> to update the stored value, and finally
  • \pgfkeysvalueof{<full path>} to retrieve the stored value.

So in OP's example,

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

\begin{document}
% init keys
\pgfkeys{
  halign/.initial=\centering,
  width/.initial=4in,
}
\newcommand\test[1][]{{% use an extra group of braces to restrict the scope
  % locally modify keys
  \pgfkeys{#1}%
  % use keys
  \fbox{\parbox{\pgfkeysvalueof{/width}}{\pgfkeys{/halign} some text}}%
}}
\test \\
\test[halign=AAA] \\
\end{document}

enter image description here

When halign is defined by halign/.code=#1, \pgfkeys{halign=AAA} will output AAA, which results in AAA out of the box.

15
  • Many thanks @muzimuzhi Z for your help. I still do not quite understand why halign/.code=#1 results in AAA out of box? And what's the meaning of use an extra group of braces to restrict the scope
    – lyl
    Feb 4, 2022 at 0:24
  • @lyl 1) With \test[halign=AAA], halign=AAA is the first argument of \test. Then in \test \pgfkeys{halign=AAA} is called, which will typeset AAA. Note this AAA is typeset before \fbox. 2) With \newcommand\test[1][]{\pgfkeys{#1}...}, the change taken by \pgfkeys{#1} will influence following use of \test. With \newcommand\test[1][]{{\pgfkeys{#1}...}} (note the {...} added), the change is local. Feb 4, 2022 at 0:37
  • Thank you so much!! And which is the better for group, {...} or \begingroup...\endgroup?
    – lyl
    Feb 4, 2022 at 0:40
  • @lyl No difference and either works. If the enclosed content is rather long, then the longer hence more obvious \begingroup ... \endgroup might be preferable. Feb 4, 2022 at 0:42
  • 1
    @lyl With halign/.initial=<val> only, \pgfkeys{halign} will use <val>. But when halign/.default=<val2> is additionally used, \pgfkeys{halign} is equivalent to \pgfkeys{halign=<val2>}. Note the pgfmanual is wrong about the relation of /.initial and /.default, see issue pgf#1133. Feb 4, 2022 at 2:22

You must log in to answer this question.

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