- LaTeX Cookbook
- Stefan Kottwitz
- 866字
- 2021-07-09 21:25:01
Absolute positioning of text
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:
\pagestyle{empty}
- Start the document:
\begin{document}
- Add the page number to all pages as follows:
\AddToShipoutPictureBG{% \setlength{\unitlength}{1cm}% \put(2.5,2){Test document}% \put(\paperwidth-2cm,2cm){\llap{\thepage}}% }
- 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}}% }
- Add a confidential sign at the top of the page:
\AddToShipoutPictureFG{% \AtPageCenter{\rotatebox{15}{\makebox[0pt]{% \Huge\bfseries\color{red}Confidential}}}% }
- 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'spicture
commands, which are put into a zero-lengthpicture
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. Theeso-pic
package actually builds on it. - The
everyshi
package does a similar job. Theatbegshi
package is a modern reimplementation of it. - The
textpos
package bases on theeveryshi
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 thecurrent page
node inoverlay
mode - The
PSTricks
bundle contains the packagepst-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.
- C語言程序設計案例教程
- 計算機網絡
- UML和模式應用(原書第3版)
- OpenShift開發指南(原書第2版)
- Mastering ServiceStack
- Clojure for Domain:specific Languages
- C語言最佳實踐
- 從學徒到高手:汽車電路識圖、故障檢測與維修技能全圖解
- JavaScript:Moving to ES2015
- C語言程序設計
- Julia 1.0 Programming Complete Reference Guide
- SQL Server 2008 R2數據庫技術及應用(第3版)
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- 安卓工程師教你玩轉Android
- C Primer Plus(第6版)中文版【最新修訂版】