# Modifying a command in a footer not working correctly

I'm making a document with LaTeX and I wish to add a lateral index to each page with the first letters of the first and last words.

To do that, I need to reset a command at the end of every page

I put a snippet of code in footer (of the fancyhdr package) with:

\fancyfoot[C]{
\def\TwoFirstLettersBegin{Indef} % Resetting the command
\TwoFirstLettersBegin            % Showing the new state of the command
}


but when I build my document, something strange happens:

My command seams to have been correctly resetted and the right content (Indef) when it is shown in the footer. But in the next page, when I print the command, nothings seems to have been modified.

In my pages content, I regularly modify other commands and every thing is nice. I don't understand what I made wrong.

Do you have any idea why my new command definition work in the footer and is cancelled in the next page?

• Most likely the footer is grouped somehow, hence the definition also remains local according to that group. Maybe try using \gdef... – Ruben Jan 16 '17 at 22:28
• In general I would recommend to use the everyshi package for such things, resp. its \EveryShipout command. – Ruben Jan 16 '17 at 22:31
• Technically your centre footer should show Indef from the point onward where you call it that way. – Werner Jan 16 '17 at 23:01

I think using fancyhdr for this is kind of a misuse. The everyshi package was actually designed for the exact purpose of performing actions right before the page gets shipped out.

\documentclass{article}
\usepackage{lipsum}
\usepackage{fancyhdr}
\fancyfoot[C]{\TwoFirstLettersBegin}
\usepackage{everyshi}
\EveryShipout{\gdef\TwoFirstLettersBegin{Indef}}

\def\TwoFirstLettersBegin{}
\newcommand\setfirstletters[1]{\gdef\TwoFirstLettersBegin{#1}}

\pagestyle{fancy}

\begin{document}
\setfirstletters{Indef}
\verb|\TwoFirstLettersBegin|: \TwoFirstLettersBegin

\setfirstletters{Foo}
\verb|\TwoFirstLettersBegin|: \TwoFirstLettersBegin

\setfirstletters{Bar}
\verb|\TwoFirstLettersBegin|: \TwoFirstLettersBegin

\clearpage

On the new page it's resetted -- \verb|\TwoFirstLettersBegin|: \TwoFirstLettersBegin
\end{document}