LaTeX takes care of full justification, text height balancing, and placing floating objects such as figures and tables. It does a great job, but sometimes one may need to tell LaTeX to place text or an image exactly at a certain position on a page.
Most positioning commands work in relation to the current position in the document. Now we would like to output text at an absolute position.
How to do it...
We will use the eso-pic package for positioning. We will print text at the edge of the page, in the middle, and at specific positions. We will break down the code in to fine steps. However, you can copy the code as a whole from the book's website at https://www.packtpub.com or from http://latex-cookbook.net.
Follow these steps:
Start with any document class. We chose the article package with A5 paper here:
\documentclass[a5paper]{article}
Load the lipsum package so that you can generate dummy text:
\usepackage{lipsum}
Load the graphicx package; we will later use its rotating feature:
\usepackage{graphicx}
Load the showframe package for visualizing page dimensions, just to help us in the draft stadium:
\usepackage{showframe}
Load the eso-pic package, which does the positioning for us:
\usepackage{eso-pic}
Load the classic picture package to place with coordinates:
\usepackage{picture}
We will print the page numbers by ourselves, so disable the original page numbering:
Add some text to a single page as follows. Note the star (*):
\AddToShipoutPictureBG*{%
\AtPageLowerLeft{Page bottom left}%
\AtPageUpperLeft{\raisebox{-\height}{Page top left}}%
\AtTextUpperLeft{\raisebox{-\height}{%
\color{red}Text area top left}}%
}
Print some dummy text. It starts at the beginning of the page, as usual. Then, end the document:
\lipsum
\end{document}
Compile the document and take a look at the first page:
How it works...
We loaded several packages, as follows:
The lipsum package gives us dummy text, which we get by the command \lipsum.
The graphicx package is for including images, but here we loaded it because we use its command \rotatebox to rotate text in step 11.
The showframe package prints lines around text and margin areas, as you saw in a recipe before.
The eso-pic package does the main job here. It provides commands for printing text or graphics independent of the current position on the page. It can do this in the background, which means behind text, but also in the foreground, overwriting the normal text.
The picture package is a helper package. Some commands, such as \put, expect arguments with simple numbers, which are interpreted as factors of \unitlength. This package allows the use of different lengths with arbitrary units.
We set an empty page style; that is, one without header and footer text. In the document, we called the eso-pic commands:
The \AddToShipoutPictureBG command takes LaTeX's picture commands, which are put into a zero-length picture environment in the lower-left corner of the page. This is printed onto the page background or behind the normal text.
Here, we first defined the base unit length to be 1 centimeter. In the following \put command, we used multiples of the base length to print "Test document" near the lower-left corner. Finally, we printed the current page number near the lower-right corner. We calculated with paper width and centimeter values. This syntax is brought by the picture package. The \llap command puts its argument right-aligned into a zero-length box.
This will be repeated for the following pages.
The \AddToShipoutPictureBG* command works like the \AddToShipoutPictureBG command, but only on the current page. With the \AtPageLowerLeft command, we placed text in the lower-left corner of the page. The \AtPageUpperLeft command works similarly for the upper left. We just lowered the text by its own height. The \AtTextUpperLeft command is the analogous command for the text area.
The \AddToShipoutPictureFG page works like the \AddToShipoutPictureBG command, but for the page foreground. We placed the word "Confidential" in huge red lettering, rotated above the text, in the middle of the page.
The \AddToShipoutPictureFG* command is the analogous command for the foreground, which affects only the current page.
There's more...
There are alternative ways to achieve what we just did. These are packages that do a similar job:
The atbegshi package can be used directly. The eso-pic package actually builds on it.
The everyshi package does a similar job. The atbegshi package is a modern reimplementation of it.
The textpos package bases on the everyshi package. It provides a convenient user interface.
Capable graphics packages provide their own means in addition.
The package TikZ can be used to place text in relation to the current page node in overlay mode
The PSTricks bundle contains the package pst-abspos for putting an object at an arbitrary position on the page
If any of it fits better to your work tools, you may take a closer look. As you saw in other recipes, you can access each package's documentation by calling the texdoc command at the command prompt, followed by the package name and return key. For online access, just type the package name into the search field at http://texdoc.net.