Expand macro immediately in \pagenote

I'm trying to use the contents of a macro in a \pagenote, using the memoir class:

\documentclass{memoir}
\makepagenote

\def\myvar{Good morning.}

\begin{document}

In the morning\pagenote{\myvar}

\def\myvar{Good night.}

At night\pagenote{\myvar}

\printpagenotes
\end{document}


However, \myvar is written into the .ent file verbatim and unexpanded. As a result, the page notes show up in the final PDF as

1. Good night.
2. Good night.

Instead of the expected

1. Good morning.
2. Good night.

Is there anyway I can expand macros in \pagenote?

-
If \myvar is the only or at the least the first content of \pagenote then \expandafter\pagenote\expandafter{\myvar} should work. This could be wrapped up in a new macro if you need it more often. –  clemens Mar 3 '13 at 8:32
How about \newcommand\mypagenote[1]{\expandafter\pagenote\expandafter{#1}}? –  clemens Mar 3 '13 at 8:56
@cgnieder Thanks, that worked! (I had a typo in my previous attempt.) I seem to have run into further problems though, when trying to use \mypagenote in the definition of another macro. Perhaps it's better if I consider some approach other than pagenotes. –  LianTze Lim Mar 3 '13 at 14:29
Maybe you should explain what exactly are trying to do. I'm sure the issues can be resolved then. –  clemens Mar 3 '13 at 14:38
@cgnieder Sorry, my bad! I've just realised that everything (including your solution for when \myvar is the first content) works in a MWE, but fails in my main project code. I'll try to isolate what and where I messed up, before asking another question. –  LianTze Lim Mar 3 '13 at 14:42

There are several issues to solve. The \pagenote command is built precisely in order to avoid expansion of its argument. Thus a “naive” definition such as

\newcommand{\xpagenote}[1]{%
\begingroup\protected@edef\x{\endgroup\noexpand\pagenote{#1}}\x}


that should expand everything expandable in the argument to \xpagenote, passing the token list thus obtained, which is the standard way to cope with this case, doesn't work, because in the endnote file every space is changed into a new line character and there are two of them in case of an input such as

\xpagenote{\textbf{\myvar}}


A slightly more complicated approach seems to work:

\documentclass{memoir}

\makepagenote

\makeatletter
\newcommand{\xpagenote}[1]{%
\unskip\begingroup\protected@edef\x{\endgroup\noexpand\pagenote{#1}}%
\scantokens\expandafter{\x\relax}}
\makeatother

\def\myvar{Good morning.}

\begin{document}

In the morning\xpagenote{Something with \myvar''}

\def\myvar{Good night.}

At night\xpagenote{\textbf{\myvar}''}

\printpagenotes

\end{document}


The pass through \scantokens reduces again multiple spaces to single space tokens; the final \relax is used to suppress the final space added by \scantokens.

If your \pagenote command contains only the variable, a simpler approach works:

\newcommand{\sxpagenote}[1]{\expandafter\pagenote\expandafter{#1}}


and \sxpagenote{\myvar} will do the right thing.

-
This works, thanks! –  LianTze Lim Mar 3 '13 at 14:37