# Page numbers in the side margin

• Tags :

In books, you mostly see the page numbering printed in the side margin of the page. LaTex prints them in the bottom by default. A few ways to get the page numbering in the side margin of your LaTeX documents have been studied and I picked out the easiest one. In this post, we are going to use the background package to create a background for all pages (containing the current page number).

## Document setup

The first lines of your preamble should look like this:

\documentclass[twoside]{article} \usepackage{lipsum}

In short, we’re generating a two-sided article on which dummy text can be printed. The reason why I’ve chosen to add the twoside option is because we can now distinguish even and odd pages.

## Packages

As was told before, the background package will be used for this trick. One more package that we need to call is the ifthen package. Again, this is needed for the code to print the page number either on the left (odd page) or on the right (even page). Add the following to your preamble:

\usepackage[some]{background} \usepackage{ifthen}

## Background package setup

In this section, the setup of the background package will be handled. On forehand, it is advised to look at the package documentation here.

Print \thepage (current page numnber) on the separate PDF layer:

\SetBgContents{\thepage}

Opacity of the text on the layer (0 = full transparency, 1 = no transparency)

\SetBgOpacity{1}

Scale of the text on the layer

\SetBgScale{1}

Angle of the text on the layer (0=horizontally)

\SetBgAngle{0}

Textcolor of the text on the layer

\SetBgColor{black}

The above is all quite straight forward. The next thing to do is to write an ‘if-loop’ (if page is even/odd) to determine wheter to print the page number left or right

\makeatletter \AddEverypageHook{% \ifthenelse{\isodd{\thepage}}% {\SetBgPosition{.8\paperwidth,-.5\paperheight}% \pagestyle{empty}% }% {\SetBgPosition{0,-.5\paperheight}% \pagestyle{empty}% }% \bg@material} \makeatother

Note that I’ve also added the command \pagestyle{empty}. This is done to suppress the ‘normal’ page numbering.

## Compiling the document

This last step is very easy. Recall that we are using the lipsum package. Therefore, we can generate a dummy document with the \lipsum command:

\begin{document} \thispagestyle{empty} \lipsum[1-20] \end{document}