Generalpgfkeys | Setting Keys within List Environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Addle
Posts: 4
Joined: Sun Jul 08, 2012 7:13 pm

pgfkeys | Setting Keys within List Environment

Post by Addle »

When pgfkeys or another key/value system is used to set a key value inside a list like enumerate, the value persists until the end of the list, but the value reverts to its old value after the list. Here's a complete example:

Code: Select all

\documentclass{article} 
\usepackage{pgfkeys} 

\begin{document} 

\pgfkeyssetvalue{mykey}{one} 

Before list, mykey is: \pgfkeysvalueof{mykey}. 

\begin{enumerate} 
\item Start of list, mykey is: \pgfkeysvalueof{mykey}. 
\item Setting value to two. \pgfkeyssetvalue{mykey}{two} 
\item End of list, mykey is: \pgfkeysvalueof{mykey}. 
\end{enumerate} 

After list, mykey is: \pgfkeysvalueof{mykey}. 

\end{document}
Is there a way to set the value of mykey inside the list in a way that sticks, so it remains in effect after the list ends?
Last edited by localghost on Sat Oct 06, 2012 10:23 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

pgfkeys | Setting Keys within List Environment

Post by cgnieder »

The setting of a key is local. Try

Code: Select all

{\pgfkeyssetvalue{mykey}{three}}\pgfkeysvalueof{mykey}
and you'll see that the value outside the group hasn't changed. And since each LaTeX environment is a implicit group the same thing happens with the enumerate environment. It seems there is no global equivalent of \pgfkeyssetvalue but you can define it yourself:

Code: Select all

\documentclass{article}
\usepackage{pgfkeys}

\makeatletter
% set key value globally:
\long\def\pgfkeysgsetvalue#1#2{%
  \pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}%
}
\makeatother

\begin{document}

\pgfkeyssetvalue{mykey}{one}

Before list, mykey is: \pgfkeysvalueof{mykey}.

\begin{enumerate}
\item Start of list, mykey is: \pgfkeysvalueof{mykey}.
\item Setting value to two. \pgfkeysgsetvalue{mykey}{two}
\item End of list, mykey is: \pgfkeysvalueof{mykey}.
\end{enumerate}

After list, mykey is: \pgfkeysvalueof{mykey}.
\end{document}
Regards
site moderator & package author
Post Reply