3

I have seen in Tikz that multiple properties can be assigned and retrieved from a key, unless I am misinterpreting it. The pgfkeys documentation always mentions a pair of key=value to set or to get but never mentions multiple values for the same key. Assuming I define keys for "State" and want to assign "state_name", "capital", and population as properties. Then I want to retrieve either the name of the state or population using the key (for example AZ). Here is sample code to start:

\documentclass[12pt,twoside]{article}
\RequirePackage{filecontents}% write the data file
\usepackage{pgfkeys}

\begin{filecontents}{states.csv}
    AZ = { Arizona,Phoenix, 6931071}
    CA = { California,Sacramento, 39250017}
    FL = { Florida,Tallahassee, 20612439} 
    GA = { Georgia,Atlanta, 10310371}
    MS = { Massachusetts,Boston, 6811779}
    NY = { New York,Albany, 19745289}
    TX = { Texas,Austin, 27862596}
\end{filecontents}

% BEGIN Read US States Definitions
\pgfkeys{/state/.is family, state,
% allow arbitrary unknown keys and set with \pgfkeyssetvalue
.unknown/.code={\pgfkeyssetvalue{\pgfkeyscurrentpath/\pgfkeyscurrentname}{#1}},
}
\newcommand\printstate[1]{% print the key if it is defined and ???Otherwise
\pgfkeysifdefined{/state/#1}{\pgfkeysvalueof{/state/#1}}{???}%
}

\newcommand\AddState[1]{\expandafter\pgfkeys\expandafter{/state, #1}}
\newread\statefile% file handler
\def\apar{\par}% \ifx\par won't work but \ifx\apar will
\newcommand\ReadStates[1]{% read file into [\pgfkeys{/state}
\openin\statefile=#1% open file for reading
\loop\unless\ifeof\statefile% loop until end of file
\read\statefile to \stateline% read line from file
\ifx\stateline\apar% test for \par
\else%
    \ifx\stateline\empty\relax% skip over empty lines/comments
\else\expandafter\AddState\expandafter{\stateline}%
    \fi%
\fi%
\repeat% end of file reading loop
\closein\statefile% close input file
}
\ReadStates{states.csv}% read the file

% END  Read US States Definitions

\begin{document}
   Hello:
    \printstate{TX}
    \\
    \\ How to print The capital of Texas is "Austin" and 
    \\ total population is "27862596"
    \\ Using "properties" of the key AZ?
    \\ Prefer to use pgfkeys with out using packages to split the content of the value of the key

\end{document}
2

You can specify an arbitrary argument pattern to the key codes with the /.code args handler. For instance, define the .unknown key handler like this:

.unknown/.code args={#1,#2,#3}{ % Format is <name>,<capital>,<population>
    \pgfkeyssetvalue{\pgfkeyscurrentpath/\pgfkeyscurrentname/name}{#1}
    \pgfkeyssetvalue{\pgfkeyscurrentpath/\pgfkeyscurrentname/capital}{#2}
    \pgfkeyssetvalue{\pgfkeyscurrentpath/\pgfkeyscurrentname/population}{#3}
}

Then you can use the defined /state/<NAME>/<field> keys like this:

The capital of \pgfkeys{/state/TX/name} is \pgfkeys{/state/TX/capital}
and total population is \pgfkeys{/state/TX/population}. 
  • IF the value of the state is in a macro variable (for \stateparam) what is the best way to call pgfkeys? \pgfkeys{/state/\stateparam/population}? Not sure if I should use expandafter or noexpand – hpcolos Feb 17 '17 at 6:00
  • @hpcolos The code you give in your comment should be working, you should not have to use expandafter or noexpand with pgfkeys, have your tried it? If you have a problem with expansion in pgf, please ask a separate question. – TonioElGringo Feb 20 '17 at 8:54

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.