官术网_书友最值得收藏!

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:

  1. Start with any document class. We chose the article package with A5 paper here:
    \documentclass[a5paper]{article}
  2. Load the lipsum package so that you can generate dummy text:
    \usepackage{lipsum}
  3. Load the graphicx package; we will later use its rotating feature:
    \usepackage{graphicx}
  4. Load the showframe package for visualizing page dimensions, just to help us in the draft stadium:
    \usepackage{showframe}
  5. Load the eso-pic package, which does the positioning for us:
    \usepackage{eso-pic}
  6. Load the classic picture package to place with coordinates:
    \usepackage{picture}
  7. We will print the page numbers by ourselves, so disable the original page numbering:
    \pagestyle{empty}
  8. Start the document:
    \begin{document}
  9. 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}}%
    }
  10. 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}}%
    }
  11. Add a confidential sign at the top of the page:
    \AddToShipoutPictureFG{%
      \AtPageCenter{\rotatebox{15}{\makebox[0pt]{%
        \Huge\bfseries\color{red}Confidential}}}%
    }
  12. Print some dummy text. It starts at the beginning of the page, as usual. Then, end the document:
    \lipsum
    \end{document}
  13. Compile the document and take a look at the first page:
    How to do it...

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.

主站蜘蛛池模板: 鸡西市| 仁怀市| 互助| 应用必备| 雷州市| 上犹县| 开平市| 绥棱县| 肥城市| 泰安市| 蓬安县| 临沂市| 澎湖县| 柏乡县| 彩票| 乌恰县| 乐至县| 郎溪县| 贵阳市| 泸水县| 镇康县| 米泉市| 承德县| 扬州市| 上饶市| 垫江县| 若尔盖县| 康定县| 贡嘎县| 临潭县| 德清县| 云霄县| 建德市| 呼伦贝尔市| 郯城县| 克拉玛依市| 徐州市| 灵宝市| 上犹县| 客服| 德钦县|