如何在页面内容周围绘制边框

如何在页面内容周围绘制边框

在学习过程中,进行不同的实验时,查看实际的页面边框很有用。所以我编写了执行此操作的代码,想知道这是否是完成此类任务的正确方法。

代码使用如下:

\input page_borders
whatever content
\bye

这是 page_borders.tex

% Overload shipout (idea from quire.tex):
\newbox\mybox
\let\oldshipout\shipout
\def\shipout{\global\let\shipout\oldshipout\afterassignment\myboat\setbox\mybox=}
\def\myboat{\aftergroup\myship}
\def\myship{\setbox\mybox=\vbox{
  % This is the main part:
  \vskip-0.4pt \hrule
  \nointerlineskip
  \moveleft 0.4pt \vbox{\hrule height 0pt depth \vsize width 0.4pt}
  \nointerlineskip
  \kern-\vsize \nobreak
  \nointerlineskip
  \moveright \hsize \vbox{\hrule height 0pt depth \vsize width 0.4pt}
  \nointerlineskip
  \kern-\vsize \nobreak
  \unvbox\mybox
  \hrule
}\shipout\box\mybox}

答案1

第一个\nointerlineskip不是必需的,因为它遵循\hrule;第三个和第四个\nointerlineskip也不是必需的,因为它们遵循字距。此外,这里还有一个小问题:我们必须添加width \hsize第一个和第四个\hrule,以免顶部和底部规则超出页面边界(因为\moveright \hsize ...右边距规则的宽度为 + 0.4 pt,并且因为\hrule默认采用所有可用宽度)。

因此,page_borders.tex 变成:

\newbox\pagebox
\let\oldshipout\shipout
\def\shipout{\global\let\shipout\oldshipout\afterassignment\myboat\setbox\pagebox=}
\def\myboat{\aftergroup\myship}
\def\myship{\setbox\pagebox=\vbox{
  % This is the main part:
  \kern-0.4pt
  \hrule width \hsize
  \moveleft 0.4pt \vbox{\hrule height 0pt depth \vsize width 0.4pt}
  \nointerlineskip
  \kern-\vsize \nobreak
  \moveright \hsize \vbox{\hrule height 0pt depth \vsize width 0.4pt}
  \kern-\vsize \nobreak
  \unvbox\pagebox
  \hrule width \hsize
}\shipout\box\pagebox}

要绘制不包括页脚线的页面边框,请使用此 page_borders.tex:

\newbox\pagebox
\newdimen\pagedepth
\def\pagebody{\setbox\pagebox=\vbox to\vsize{\boxmaxdepth\maxdepth \pagecontents}%
  \vbox{
    \kern-0.4pt
    \hrule width \hsize
    \moveleft 0.4pt \vbox{\hrule height 0pt depth \vsize width 0.4pt}
    \nointerlineskip
    \kern-\vsize \nobreak
    \moveright \hsize \rlap{\vbox{\hrule height 0pt depth \vsize width 0.4pt}}
    \nointerlineskip
    \kern-\vsize \nobreak
    \box\pagebox
    % back up by current prevdepth before inserting bottom margin, and restore afterwards
    \pagedepth=\prevdepth
    \kern-\pagedepth
    \hrule width \hsize
    \kern-0.4pt
    \prevdepth\pagedepth
  }%
}

相关内容