0

I'm trying to make a margin par that will show the value of some macro defined after the placement point of the marginpar (think of it as something like \footnotemark vs \footnotetext).

However, the \marginpar command seems to fully expand its argument and I cannot get the desired behavior with any combination of \noexpand and similar.

Here is a little example:

\documentclass{article}

\begin{document}

\def\mymacro{Content}
Some text. \marginpar{\mymacro}

\def\mymacro{Other content}

Some other text.

\end{document}

Here I would like the marginpar to display the text "Other content". If I put \noexpand before \mymacro in the \marginpar argument, I get no marginpar at all, which is strange.

Of course there has to be a limit on how far the definition has to be, because I cannot delay the expansion further than the execution of the page shipout code (although I'm only vaguely familiar with how the mechanism works). But in the case of this example I'd expect it to be possible.

Is it possible to get \marginpar to not expand immediately its argument?

  • 1
    How should that be possible? \marginpar inserts something right when it is called, and does not wait till the end of the document, say. – user121799 Sep 9 '18 at 10:13
  • That's why I put the expansion tag on the question. I suppose in some way it should be possible to delay the expansion of the macro until it gets actually typeset. Unless \marginpar is typesetting the content right away and saving it as a box. Is this the case? – gigabytes Sep 9 '18 at 10:15
  • Well, you can try to do something with atbegshi or so. What I wanted to say is that a plain \marginpar won't do it. – user121799 Sep 9 '18 at 10:17
  • I don't understand why. I see it simply as a matter of when the argument of \marginpar is expanded. – gigabytes Sep 9 '18 at 10:30
  • 1
    marginpars like floats and footnotes are fully typeset at the point they are entered, like \savebox they are placed on the page like \usebox which just copies a typeset formatted list, no expansion happens at that time. – David Carlisle Sep 9 '18 at 11:05
4

marginpars like floats and footnotes are fully typeset at the point they are entered, like \savebox they are placed on the page like \usebox which just copies a typeset formatted list, no expansion happens at that time

if you put \noexpand before your macro then you have

\marginpar{\noexpand\mymacro}

which is exactly the same as

\marginpar{\relax}

so you get an empty marginpar. (But it is actually there, LaTeX still goes to the trouble of placing it in the margin, but it has no content)

As always, if you want text to depend on a forward reference depending on which page it is finally typeset, then you can use or abuse \label/\ref

enter image description here

\documentclass{article}

\begin{document}



Some text. \marginpar{\ref{foo}}

{\makeatletter\def\@currentlabel{Other content}\label{foo}}

Some other text.

\end{document}
  • Thanks, that explains a lot of things! So abusing \label is just a way of using the .aux file, right? – gigabytes Sep 9 '18 at 11:24
  • @gigabytes yes. – David Carlisle Sep 9 '18 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.