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. –  cgnieder Mar 3 '13 at 8:32
How about \newcommand\mypagenote[1]{\expandafter\pagenote\expandafter{#1}}? –  cgnieder 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. –  cgnieder 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
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8876864314079285, "perplexity": 2719.827648995032}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-23/segments/1406510271862.24/warc/CC-MAIN-20140728011751-00476-ip-10-146-231-18.ec2.internal.warc.gz"}