如何定义在页面主体或文本列的开始和结束时执行的宏?

如何定义在页面主体或文本列的开始和结束时执行的宏?

我需要一种方法来在页面主体的开始和结束处插入\special{...}或命令,但在将页眉和页脚插入到要发出的框之前。\pdfliteral{...}

\special{...}我想要插入的或命令\pdfliteral{...}不占用空间,因此既不会改变页面的外观也不会改变分页符。它们只是应该可靠地出现在放入页面主体的第一个可见元素之前和最后一个可见元素之后。理想情况下,这应该适用于单列、双列或多列环境以及任何文档类。

我尝试覆盖寄存器\output和使用包\AtBeginShipout...的宏atbegshi,但不太成功。

答案1

例如这样:

\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}}

答案2

\@outputpage例程和\@outputbox似乎是正确的钩子,以便在不影响页眉和页脚的情况下在页面主体的开始和结束时实现操作。

举个例子,我们通过在原来的文本周围\@outputpage插入两个颜色来覆盖,从而在每个新页面中以循环的方式在红色、绿色和蓝色之间改变文本颜色。\pdfliteral\@outputbox

这已针对许多流行的文档类别和不同数量的文本列进行了测试。

在此处输入图片描述

\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}

相关内容