44

I want my document to have borders in all pages. I imagine I can do that using TikZ, but is there an easier way to do it?

I also asked this question here, but as the main question was something else, this one problem got ignored (probably the use of the word "margin" rather than "border" did not help my case).

Can anyone help me?

2
  • 4
    A margin refers to the whitespace between the text and the edge of the paper. It sounds like you are referring to a box or a border.
    – Sharpie
    Aug 2, 2010 at 0:49
  • 1
    This question is ambiguous. It will be better if you attach a hand-made drawing to this question to avoid ambiguity! :-) Aug 5, 2011 at 18:04

7 Answers 7

15

Memoir class has option showtrims and commands \trimFrame, \settrimmedsize, \settrims. Example:

\documentclass[10pt,a4paper,showtrims]{memoir}
\trimFrame  
\settrimmedsize{210mm}{145mm}{*} 
\settrims{20mm}{34mm}
2
  • 2
    Looks like this doesn't work when the page size is defined using the geometry package.
    – kontextify
    Jan 4, 2018 at 18:36
  • 1
    The geometry package, by definition, blocks the memoir's page macros.
    – djnavas
    Apr 17, 2022 at 22:10
30

You're right that it can be done with TikZ, but it's not necessary to load in the whole of TikZ to get the bits required for this. The part that is needed is pgfpages. You can do a lot of page manipulation with this package, but here's some to get you started:

\documentclass{article}
\usepackage{pgf}
\usepackage{pgfpages}

\pgfpagesdeclarelayout{boxed}
{
  \edef\pgfpageoptionborder{0pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=1,%
  }
  \pgfpageslogicalpageoptions{1}
  {
    border code=\pgfsetlinewidth{2pt}\pgfstroke,%
    border shrink=\pgfpageoptionborder,%
    resized width=.95\pgfphysicalwidth,%
    resized height=.95\pgfphysicalheight,%
    center=\pgfpoint{.5\pgfphysicalwidth}{.5\pgfphysicalheight}%
  }%
}

\pgfpagesuselayout{boxed}
\begin{document}

Something simple

\end{document}

Of course, if you're going to use the same layout again and again, put it in a separate style file and \usepackage{mylayouts} in each document.

Note: the package pgf is only needed to provide the command \pgfstroke. As all this does is draw a line, it may be possible to remove this in favour of a \rule of some sort.

8
  • 2
    After trying various suggestions, this was the only one that worked well within beamer on the first shot. Thanks! Nov 15, 2013 at 22:06
  • How to change the color of the frame?
    – Dion
    Jul 26, 2014 at 15:59
  • 1
    I'm only getting borders along the left and bottom edges of the page with this approach. Also, page numbers are added.
    – kontextify
    Jan 4, 2018 at 18:28
  • 1
    @kontextify I've just checked and it still works for me (despite this answer being 7 years old!). There are page numbers because there are page numbers in a standard document and this doesn't remove them. To get rid of page numbers, use \pagestyle{empty} and \thispagestyle{empty}. Jan 4, 2018 at 21:38
  • 1
    @Alberto This is a very old answer on a very old question. If you want to ask about something related to it, then it is best to ask a new question. Sep 21, 2021 at 20:04
11

I think the easiest way would be to use the crop package. Tikz is not exactly the easiest way, but with tikzpagenodes you can do fancy things.

\documentclass{scrartcl}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usepackage{scrlayer-scrpage}
\usepackage{lipsum}

\pagestyle{scrheadings}


\newcommand{\jamanta}{\tikz[remember picture,overlay] 
    \draw [blue,line width=2mm]
    (current page.south west)
    rectangle
    (current page.north east)
    ;}

\newcommand{\greenborders}{\tikz[remember picture,overlay] \draw [green]
    (current page text area.south west)
    rectangle
    (current page text area.north east)
    ;}

\chead[\jamanta]{\jamanta} % for page borders

%\chead[\greenborders]{\greenborders} %for margin borders

\begin{document}


\section{A}
\lipsum[1-10]
\section{B}
\lipsum[1-10]

\end{document}
3
  • 1
    Tried the other answers. This seems to the most reliable and customisable way!
    – kontextify
    Jan 4, 2018 at 19:30
  • 1
    This also seemed the most reliable to me, thanks!
    – Richard
    Dec 9, 2018 at 5:14
  • This doesn't add a border around the page. It draws a box in the top right corner, which isn't what was requested.
    – richard
    May 5, 2022 at 20:00
7

The answer provided by Stefan Kottwitz helped me. I wanted to add a working example for easy reference based on his suggestion of using the crop package. My example may need improvement.

\documentclass{article}
\usepackage{geometry} 
\usepackage{setspace}
\usepackage[none]{hyphenat}
\geometry{paperwidth=2in, paperheight=4in, margin=2em, bottom=1.5em, nohead}
\usepackage[center,dvips,frame]{crop}
\begin{document}
\pagestyle{empty}
\doublespacing \noindent Hello, world, this is my Pinterest message.
% Add a blank line

\vspace{0.2in}
\noindent Choose your words with care because you don't get a second chance to make a first impression.
% Add a blank line

\vspace{0.3in}
\noindent \footnotesize Made with \normalsize \LaTeX

\end{document}

LaTeX Output Image PNG

1
  • This works, but as far as I can tell from the crop docs, there is no way to customise the border.
    – kontextify
    Jan 4, 2018 at 19:12
5

A box around your document: it sounds like a frame around your logical page on a bigger paper size. This can be done using the crop package.

1
  • 1
    An example would improve this answer.
    – Richard
    Dec 9, 2018 at 5:11
3

Actually though you suggest you want something easier then TikZ, doing it with that is not so hard. Here is the most important part. I am really only posting this because this was the first Google result I found, but I was not happy with the results. This is what I am using now.

\usepackage[all]{background}
\usepackage{tikz}
\usetikzlibrary{calc} 
\usepackage{ifoddpage}
\usepackage{changepage}
\strictpagecheck

\newcommand*{\bordersp}{0.125in}

\newcommand{\MyTikzBoarder}{%
\begin{tikzpicture}[remember picture,overlay,draw=red]
\checkoddpage
\ifoddpage
    \draw (0.75in-\bordersp,-0.5in+\bordersp) -- (\textwidth+0.75in+\bordersp,-0.5in+\bordersp) -- (\textwidth+0.75in+\bordersp,-\textheight-0.5in-\headheight-\headsep-\bordersp) -- (0.75in-\bordersp,-\textheight-0.5in-\headheight-\headsep-\bordersp) -- cycle;
\else
    \draw (0.5in-\bordersp,-0.5in+\bordersp) -- (\textwidth+0.5in+\bordersp,-0.5in+\bordersp) -- (\textwidth+0.5in+\bordersp,-\textheight-0.5in-\headheight-\headsep-\bordersp) -- (0.5in-\bordersp,-\textheight-0.5in-\headheight-\headsep-\bordersp) -- cycle;
\fi
 \end{tikzpicture}
}

\SetBgContents{\MyTikzBoarder}% Set tikz picture

\SetBgPosition{current page.north west}% Select location
\SetBgOpacity{1.0}
\SetBgAngle{0.0}
\SetBgScale{1.0}
1

For those who are not using the memoirs document class, and also need to use table of contents. I would suggest the use of the background package along with the calc library in tikz. The inclusion of tikz allows you to have a lot more options including rounded corners (which I personally love).

I have come to this solution after trying out multiple options, including pgfpages which actually squeezes your page inside instead of adding borders. This leads to displacement of toc links.

\usepackage{background}
\usetikzlibrary{calc}

\SetBgScale{1}
\SetBgAngle{0}
\SetBgColor{black}
\SetBgContents{
\begin{tikzpicture}[overlay,remember picture]
    \draw [line width=1pt,rounded corners=4pt,]
        ($ (current page.north west) + (2cm,-2cm) $)
        rectangle
        ($ (current page.south east) + (-2cm,2cm) $);
\end{tikzpicture}}

I did struggle a lot with this and this seems like the easiest way to have margins, hope it helps :)

The following is the output image for a page from my document page with margins

You must log in to answer this question.

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