Here's the question: why does the following code reset the meaning of the \pgPtList
control sequence when the group ends?
\documentclass[12pt]{article}
\begin{document}
\begingroup
\gdef\pgPtList{}
\edef\pgPtList{8}
pgPtList is: \pgPtList
\edef\pgPtList{8, 5}
pgPtList is: \pgPtList
\endgroup
Empty environment has ended.
pgPtList is: \pgPtList
\end{document}
Here's the output it produces:
pgPtList is: 8
pgPtList is: 8, 5
Empty environment has ended.
pgPtList is:
My current understanding is that this has something to do with how groups work, so a likely follow-up question is this: how can I redefine the control sequence such that the change sticks once the group ends?
Here's the broader context: I'm trying to typeset an exam and track the number of points on each page, and I'm continually updating \pgPtList
to keep track of how many points are on each page. At the each of the exam, \pgPtList
acts as a default parameter to another command that creates a table of point values.
The points on each page are updated within an enumerate
environment, which is why the \begingroup
and \endgroup
are present in the reprex. Ideally, I'd like to be able to update the \pgPtList
variable within this environment, even though the point table command will be called outside of it.
My main point of confusion is around \edef
and \gdef
. My naive understanding is that these commands are meant to make the resulting control sequences accessible outside of the current group, but the output I get from the code above suggests that something else is happening.
\xdef
rather than\edef
.\edef
is local, just as\def
is.\xdef
is global, just as\gdef
is. Note that the\gdef
definition does survive the group - you don't get an error due to an undefined control sequence. But all your later (re)definitions are local.\addtocounter{mycounter}{2}
or whatever.