0

I am defining page margins with geometry, adding a footer and header. I want the body of the text (not the footer or header) to be enclosed in a box (every page).

This is a similar question to What is the easiest way to get borders around a page in LaTeX? and other that I found, but them all are planned for a full border around the whole page (for cropping needs, for instance).

Suggestions are welcome.

2 Answers 2

2

Not sure if this is the best solution, but the tikzpagenodes provides tikz anchors for each block (text, footer, header...) and background provides \AddEverypageHook to add text on every page. Combining the two gives:

enter image description here

\documentclass{article}
\usepackage{tikzpagenodes}
\usepackage[contents={}]{background}
\AddEverypageHook{%
  \tikz[remember picture,overlay]{
    \draw (current page text area.north east) -- (current page text area.north west)
          -- (current page text area.south west) -- (current page text area.south east) -- cycle;
  }
}

\begin{document}
Hello
\newpage
Hello
\end{document}

By adding options to the tikz drawing, you can easily customize it with dashed lines, colors…

1
  • Perfect. As you say, there might be a cleaner solution, but for me it works great. Thanks
    – Alberto
    Sep 21, 2021 at 14:00
5

It can also be done with no packages.

\documentclass{article}

\AddToHook{shipout/background}{\fboxsep=0pt
  \ifodd\value{page}
    \put({\dimexpr 1in+\oddsidemargin-\fboxrule},{\dimexpr -1in-\topmargin-\headheight-\headsep-\textheight-\fboxrule})%
      {\fbox{\rule{0pt}{\textheight}\rule{\textwidth}{0pt}}}
  \else
    \put({\dimexpr 1in+\evensidemargin-\fboxrule},{\dimexpr -1in-\topmargin-\headheight-\headsep-\textheight-\fboxrule})%
      {\fbox{\rule{0pt}{\textheight}\rule{\textwidth}{0pt}}}
  \fi}

\begin{document}
Hello

\noindent\verb|\fboxsep| = \the\fboxsep
\newpage
Hello
\end{document}
3
  • Nice suggestion....
    – MadyYuvi
    Sep 21, 2021 at 15:17
  • How to exclude the first page?
    – Stephen
    Jun 8 at 8:02
  • @Stephen - \ifnum\value{page}=1\else ... \fi Jun 8 at 12:41

You must log in to answer this question.

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