# How to draw borders around page contents

During learning, while conducting different experiments, it's useful to see actual page borders. So I wrote the code which does this and would like to know whether it is the proper way to accomplish this kind of task.

The code is used like this:

\input page_borders
whatever content
\bye

This is 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}
• Package framed is for LaTeX. Is that acceptable? – Symbol 1 Aug 27 '15 at 1:12
• @PrzemysławScherwentke yes... I saw \bye and edit my comment in the last minute. By the way there are other packages (for LaTeX, of course) such as showframe. I guess they are just some tricks around boxes so one should be able to translate the code back to plain-TeX. – Symbol 1 Aug 27 '15 at 1:22

First \nointerlineskip is not necessary, because it follows \hrule; third and fourth \nointerlineskips are not necessary, because they follow a kern. Also, there is a slight catch here: we must add width \hsize to first and fourth \hrule, lest top and bottom rules extend out of page boundaries (because of \moveright \hsize ... + width 0.4 pt of right margin rule and because \hrule takes all available width by default).

So, the page_borders.tex becomes:

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

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