# Hooking into the header/footer mechanism

I have a switch I'd like to turn off for the headers and footers but keep on for the main body text. What commands actually tell TeX to write the headers and footers after the body text has been set and how would I add a switch before and after these commands (with etoolbox or similar?).

What I'm looking for is something like \AtBeginPrintHeader. In analogy to \AtBeginDocument, or the hooks from everyhook which allow you to do something at the beginning of each paragraph, mathmode etc. I know there's bophook for doing stuff at the beginning of the page, but I need something that does something between the main body text being written and the headers and footers being written.

Patching \ohead with \apptocmd from etoolbox obviously doesn't work because it's not \ohead that is called when TeX tries to set a header. \ohead sets the relevant internal macros to be what they should so that the header you want is set. So I guess what I want to know is what actually happens when the header is being set: what command should I \appto in order to have something happen right before the header is set?

Is there such a mechanism? Is it compatible with scrpage2 and fancyhdr?

Here's the example. I've used everyhook to add input line numbers to each paragraph. Awkwardly, this includes paragraphs in the header and footer.

\documentclass{article}
\usepackage{everyhook,marginnote}
\newif\ifnotmarginhook
\notmarginhooktrue
\PushPostHook{par}{%
\ifnotmarginhook%
\notmarginhookfalse%
{\marginnote{\small\ttfamily\the\inputlineno}}%
\notmarginhooktrue%
\fi%
}
\usepackage{scrpage2}
\newcounter{firstline}
\newcommand\nolineno[1]{\notmarginhookfalse #1\notmarginhooktrue}
\ohead{Problem $\to$}
\begin{document}
Some text
\end{document}


So I want to turn \notmarginhookfalse for the headers, and then turning it back on before I set more body text... Using \ohead{\notmarginhookfalse Text} doesn't seem to work.

So is there either a way to hook into the headers or some internal switch so I could do something like \ifheaders and modify things that way

-

If you load the everyhook package with the [excludeor] option, your solution should work as you expect. (I don't understand the numbering system in your example, but that's not relevant to the solution.)

\documentclass{article}
\usepackage[excludeor]{everyhook}
\usepackage{marginnote}
\newif\ifnotmarginhook
\notmarginhooktrue
\PushPostHook{par}{%
\ifnotmarginhook%
\notmarginhookfalse%
{\marginnote{\small\ttfamily\the\inputlineno}}%
\notmarginhooktrue%
\fi%
}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newcounter{firstline}
\newcommand\nolineno[1]{\notmarginhookfalse #1\notmarginhooktrue}
\rhead{\nolineno{Problem $\to$}}
\begin{document}
Some text

Some text
\end{document}


-
Seamus may be referring to this related comment on linking your source code with your PDF output. –  Werner Aug 9 '11 at 19:21
I'm actually referring to this question but yes. –  Seamus Aug 9 '11 at 21:18
I like this solution to my actual problem, but I guess I'd still like a general solution to hooking into the header/footer mechanism. –  Seamus Aug 9 '11 at 21:20
But I think that the general solution is the same; the problem with your example was in how the everyhook package works, not with the possibility of setting conditional elements within the header and footer. –  Alan Munn Aug 9 '11 at 21:28