5

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 '15 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 '15 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 '15 at 14:50
  • @erreka : The question is not {pdftex}-specific. – AlexG Nov 12 '15 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. – erreka Nov 12 '15 at 17:57
5

For example like this:

\let\oriship=\shipout
\def\shipout{\afterassignment\shipA\setbox0=}
\def\shipA{\aftergroup\shipB}
\def\shipB{\oriship\vbox{\preship\box0\postship}}
\def\preship{\pdfliteral{q 1 0 0 rg}}
\def\postship{\pdfliteral{Q}}
  • The colour of headers and footers changes as well :-( . I guess, box \@cclv needs to be modified in the output routine. – AlexG Nov 12 '15 at 9:27
4

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

\documentclass[a5paper,12pt]{memoir}
%\documentclass[twoside]{article}
%\documentclass{scrbook}

\pagestyle{myheadings}
\markboth{Left head}{Right head}

\usepackage{multicol}

\usepackage{lipsum}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define actions for begin & end of page body
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{expl3}\ExplSyntaxOn
\seq_gset_from_clist:Nn\g_colour_stack_seq{1,0,0}

%cycle through red, green, blue text colours
\cs_new_nopar:Npn\doAtBegin{
  \pdfliteral~direct~{q~\seq_use:Nn\g_colour_stack_seq{~}~rg}
  \seq_gpop_right:NN\g_colour_stack_seq\l_tempa_tl
  \seq_gput_left:NV\g_colour_stack_seq\l_tempa_tl
}

%reset to graphics state before colour change
\cs_new_nopar:Npn\doAtEnd{\pdfliteral~direct~{Q}}
\ExplSyntaxOff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% overwrite output routine `\@outputpage'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\let\outputpageOrig\@outputpage
\def\@outputpage{
  \setbox\@outputbox\vbox{%
    \doAtBegin%
    \box\@outputbox%
    \doAtEnd%
  }%
  \outputpageOrig%
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
  \lipsum[1-3]
  \begin{multicols}{3}
  \lipsum[4-31]
  \end{multicols}
  \lipsum[32-35]
\end{document}

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.