I need a way to insert \special{...} or \pdfliteral{...} commands at the start of the page body and at its end, but before headers and footers are inserted into the box to be shipped out.

The \special{...} or \pdfliteral{...} commands I seek to insert don't take up space and thus neither change the look of the page nor the page breaks. They just should reliably occur before the very first visible element put into the page body and after the last one. Ideally, this should work in single, two or multicolumn environments and for any document class.

I experimented with overwriting the \output register and with the \AtBeginShipout... macros of the atbegshi package, but was not very successful.

  • If you tried using atbegshi, then it means that you're willing to try something that executes after the entire page has been constructed (headers/footers included). However, this contradicts what you mention at the start, where you want to insert content before headers/footers are inserted. Could you explain a little more?
    – Werner
    Nov 11, 2015 at 14:18
  • @Werner : I am trying to improve the ocg environment in my ocgx2 package, in order to work across page breaks. Since the ocg environment can be nested, I maintain a stack of open OCGs. At the end of the page, any open OCGs should be closed automatically (just calling \pdfliteral~direct~{EMC} per open OCG) to insert the required number of EMC tags into the page content stream. Immediately at start of the next page, pending OCGs should be reopened by inserting the corresponding BDC tags.
    – AlexG
    Nov 11, 2015 at 14:43
  • @Werner : I've already discarded everyshi because, as you stated, headers/footers would become part of open OCGs which is, of course, not what I want.
    – AlexG
    Nov 11, 2015 at 14:50
  • @erreka : The question is not {pdftex}-specific.
    – AlexG
    Nov 12, 2015 at 7:47
  • @AlexG I know. I added the tag to draw attention to pdftex enthusiasts, since you mention either \specials or \pdfliterals, and the latter are implemented only in pdfTeX and LuaTeX, not XeTeX or TeX.
    – jarnosc
    Nov 12, 2015 at 17:57

2 Answers 2


For example like this:

\def\preship{\pdfliteral{q 1 0 0 rg}}
  • The colour of headers and footers changes as well :-( . I guess, box \@cclv needs to be modified in the output routine.
    – AlexG
    Nov 12, 2015 at 9:27

The \@outputpage routine and the \@outputbox seem to be the right hooks in order to implement actions at start and end of the page body without affecting headers and footers.

As an example we overwrite \@outputpage to change the text colour in a cyclic way between red, green and blue for every new page by inserting two \pdfliteral around the original \@outputbox.

This was tested for a number of popular document classes and with a varying number of text columns.

enter image description here


\markboth{Left head}{Right head}



% define actions for begin & end of page body

%cycle through red, green, blue text colours

%reset to graphics state before colour change

%% overwrite output routine `\@outputpage'


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .