Is there any kind of straightforward method of telling tex to somehow run a command on certain page-numbers?
Before I go on, I should state that I'm using the memoir package to format my document.
In particular, what I want to achieve is to have an on-the-fly list generated in the margin that is populated with small mathematical equations or short snippets of text (these equations or text-snippets are defined somewhere in the main body of the document via a command that is elucidated below). Specifically, I want the first appearance of the list to be in the margin of the same page that the first list-item is created on. I then want the same list to be generated on every second page (i.e., every even or every odd page, depending on where the first instance of the list popped up) until a page is reached on which a new list-item has been created, at which point I want all subsequent lists to be populated with both items. This process should then be repeated, picking up new list-items as it goes, until the end of the current section.
To highlight with a specific example, let's say I create list entries on pages 1, 2, 7, and 8. Then I should have the following:
- Page 1 margin-list: one item.
- Page 3 margin-list: two items.
- Page 5 margin-list: two items.
- Page 7 margin-list: three items.
- Page 9 margin-list: four items.
So far I've managed to cobble together (thanks in large to questions on this very site) a few commands that take care of most of what I need. I have a command-building command that can be used – in conjunction with a counter – to create indexed commands which contain my list-entries:
\newcounter{CountItems}[section] % This creates a counter that counts how many list-items have appeared in a given section.
\newcommand\addindex[4][g]{\csname #1def\expandafter\endcsname\csname #2\roman{#3}\endcsname{#4}} % This is a command-creating command that uses concatenation to create indexed commands using a counter. #2 is the root-name of the function, #3 uses a counter to create an index to be appended to the root-name, #4 is the content stored by the command, and #1 is used to modify the behaviour of the "def" command.
\newcommand{\DeriveCond}[3]{\stepcounter{CountItems}{\addindex{conditem}{CountItems}{#1}}} % This command takes as input #1, which is the actual text to appear in the list. Using the above command (\addindex), this command creates indexed commands "conditemi", "conditemii", "conditemiii", etc, that contain the content for the list.
I can then construct a list on any page in my document using a for-loop in an itemize environment. I mentioned at the start that I'm using the memoir class, so I can wrap the itemize environment with \sidebar{} to produce a command – marginlist – that generates an up-to-date margin-list on whichever page it is called on:
\newcounter{itemloopcounter}[section] % This creates a counter to be used in list-generation. This is a dummy-counter created for use in the loop in the next command. It keeps track of the value of the loop variable \cond so that each of the "contitem" commands can be called easily.
\newcommand{\marginlist}[1]{
\sidebar{
\begin{itemize}{\usecounter{itemloopcounter}}
\foreach \cond in {1,...,\value{CountItems}}
{
\stepcounter{itemloopcounter}
\item[\roman{itemloopcounter})] \csname conditem\roman{itemloopcounter}\endcsname
}
\end{itemize}
\setcounter{itemloopcounter}{0}
}
}
However, at this point, I must manually run this command on every page that I want the list to appear on; this can be annoying if I subsequently include new content/remove old content in-between two instances of \marginlist as I might then have to find somewhere new to execute the latter instance of the command so that it's on the correct page (either left or right). Not a big deal, but something that could go overlooked in the editing process. I'm somewhat lost on where to look next in order to achieve the type of automation that I want.
I'm not too familiar with the inner-workings of tex (this is probably the deepest I've ever delved into it), so I took a stab in the dark and assumed that maybe the command \sidebar{} uses \thepage in order to figure out where to build the environment; I locally (and temporarily) changed the value of \thepage within my \marginlist command in the hopes that I could fool tex into thinking that the command had been executed on a different page, but alas, tex appears to be smarter than this.
Any pointers on how this can be achieved would be appreciated.
\documentclass
command, include any necessary packages and be as small as possible to demonstrate your problem.