You are on page 1of 5

8/13/12

PDFs - Getting to grips with LaTeX - Andrew Roberts

Andrew Roberts Home Coding Writing Photography


Tw eet 1 1

Getting to Grips with LaTeX


PDFs
by Andrew Roberts I find myself increasingly wishing to have PDF versions of my documents. If I wish to distribute my document electronically, then PDF has become the de-facto choice due to the ubiquity of PDF viewers and the greatly reduced filesizes that the PDF standard permits (compared to PS). This tutorial aims to ensure you know how to create PDFs from your LaTeX source, and how to utilise other features like hyperlinking.

Generating PDFs with p d f l a t e x


There are many ways to create PDFs (I described one approach in the first tutorial) but I will be focussing only on the p d f l a t e xapproach. p d f l a t e x , is a tool which ships with most LaTeX distributions that will read your LaTeX source and generate a PDF directly. There is no intermediate DVI or PS files. It's therefore extremely quick and easy to create a PDF, which is the reason I choose to only look at this tool. To use, you just simply substitute 'pdflatex' where you would have normally used 'latex'. A typical pass for a document with figures, cross-references and a bibliography would be:
%p d f l a t e xm y d o c %b i b t e xm y d o c %p d f l a t e xm y d o c %p d f l a t e xm y d o c

You will notice that there is a new PDF file in the working directory called mydoc.pdf. Simple :)

Dealing with images


Generally speaking, p d f l a t e xand l a t e xcan be used interchangably with the only difference being that the former generates PDF files and the latter generates DVI files. However, there is some additional issues regarding the importing of images that causes incompatability problems. As you may recall from the Importing Graphics tutorial, l a t e xexpects images to be in EPS (or PS) format exclusively. p d f l a t e xon the other hand is a little more flexible, as it can import JPG, PNG and PDF formats. latex pdflatex JPEG EPS PNG PS
www.andy-roberts.net/writing/latex/pdfs 1/5

8/13/12

PDFs - Getting to grips with LaTeX - Andrew Roberts

PDF Notice, however, that the two sets are mutally exclusive. So, if you have a latex document with EPS images and you wish to process with p d f l a t e xthen you must convert the EPS files to one of the supported formats. There are tools like e p s t o p d fthat come with the Ghostscipt package for Windows and Linux, or utilities like c o n v e r tthat is part of the ImageMagick package (again, availble for Windows/UNIX-based systems). There are of course a myriad of freeware and commercial applications that can achieve the same results, but I won't go it this any further. Creating latex/pdflatex compatible documents Providing you followed the instructions in the Importing Graphics tutorial then there's little to do. To recap, here's how to import an image:
\ i n c l u d e g r a p h i c s { c h i c k }

Now, with l a t e xit would have expected an image file to exist called 'chick.eps' - the file extension is inferred by the LaTeX processor, so you don't need to write 'c h i c k . e p s ' as the argument to the i n c l u d e g r a p h i c s { } command. The same is also true for p d f l a t e x , in that if you have the above command, it will search for an image file called 'chick.pdf', 'chick.jpg' or 'chick.png' (I'm not sure of the exact order). Therefore, in this instance, there is no code to actually change, it's merely about existance of the appropriate image files. So, if I have a LaTeX document called, i m a g e _ e x a m p l e . t e xthat loads a image with the basename 'chick', here are the necessary files:
i m a g e _ e x a m p l e . t e x c h i c k . e p s( f o rp r o c e s s i n gw i t h' l a t e x ' ) c h i c k . p n g( f o rp r o c e s s i n gw i t h' p d f l a t e x '-i tc o u l dh a v eb e e ni nj p go fp d ff o r m a tt o o )

h y p e r r e f-

Hyperlinks and bookmarks

Just generating a PDF version of your LaTeX document will be adequate for most users. However, it's worth noting that the PDF standard allows you add additional features, such as providing document meta-data, or the ability to place links within a PDF. Most of these enhancements are for the benefit of those reading the document electronically using a PDF viewer. The h y p e r r e fpackage is designed to enhance documents that are to be output as HTML and PDF, although it's especially useful for the latter. It provides all the facilities you require to take advantage of all the additional features the PDF standard has to offer. This package does in fact offer a bewildering number of options - too many to cover here. I will therefore give you a snapshot of the most useful/common settings to customise. The full list is discussed in the package manual.
h y p e r r e fis included

by default with most mainstream TeX distributions, and to use it you simply do as you would with any other package:
\ u s e p a c k a g e { h y p e r r e f }

Although it's beneficial if you specify a driver argument that tells hyperref which method is being used to generate the final output.
\ u s e p a c k a g e [ d r i v e r ] { h y p e r r e f }
www.andy-roberts.net/writing/latex/pdfs 2/5

8/13/12

PDFs - Getting to grips with LaTeX - Andrew Roberts \ u s e p a c k a g e [ d r i v e r ] { h y p e r r e f }

If you are using the pdflatex command discussed earlier, the driver you use is p d f t e x . The possible drivers (although most are unlikely to be relevant to the vast majority of users) are: hypertex - default driver if one is not specified. dvips dvipsone ps2pdf text4ht latex2html pdftex dvipdf dvipdfm dviwindo vtex vtexpdfmark textures As there are so many routes to get to a PDF or HTML output, h y p e r r e fneeds to know your expected route because the tools used during the conversion work differently and thus need to be catered for individually. It's an unfortunate consequence of the sheer flexibility of LaTeX. I recommend reading the introduction of the manual for more detail on this topic. Once enabled, h y p e r r e fwill load up its default configuration that specify all the properties of the forthcoming PDF. Most of the defaults are pretty sensible, and typically the main changes people make are to the document meta-data, and customising the link colours. Before introducing the available options, it's worth addressing how you actually specify your preferred settings. By far the most common is by passing arguments to the \ u s e p a c k a g e { h y p e r r e f }command:
\ u s e p a c k a g e [ p d f t e x , p d f a u t h o r = { A n d r e wR o b e r t s } , p d f t i t l e = { H y p e r r e fd e m o n s t r a t i o n } , c o l o r l i n k s ] { h y p e r r e f }

Alternatively, there is the \ h y p e r s e t u p { }command that is not dissimilar:


\ u s e p a c k a g e [ p d f t e x ] { h y p e r r e f } \ h y p e r s e t u p { p d f a u t h o r = { A n d r e wR o b e r t s } , p d f t i t l e = { H y p e r r e fd e m o n s t r a t i o n } , c o l o r l i n k s }

If you opt for the \ h y p e r s e t u p { }command, then you should use it within the document preamble. PDF document information Option Data type Default Description
3/5

www.andy-roberts.net/writing/latex/pdfs

8/13/12

PDFs - Getting to grips with LaTeX - Andrew Roberts

pdftitle

text

Sets the title field. Sets the author field. Sets the keywords field. Sets the subject field. Sets the creator field. Sets the producer field.

pdfauthor text pdfkeywords text pdfsubject pdfcreator text text

pdfproducer text Link options Option colorlinks linkcolor citecolor filecolor urlcolor Data type Default boolean colour colour colour colour false red black

Description Links will be printed in colour. Colour of standard internal links. Colour of anchor text.

anchorcolor colour

green Colour of citation links. magenta Colour of links to local files. red cyan false Colour of links to other pages. Colour of external URL links. Use small caps rather than colours for links.

pagecolor colour frenchlinks boolean Display options Option bookmarks Data type

Default

Description Specifies whether to generate Acrobat bookmarks. Displays the bookmarks fully expanded. Specifies the page view for when the PDF is opened within the viewer, e.g, fit to page-width/page-height, etc.. (See below for possible values)

boolean false

bookmarksopen boolean false pdfstartview pdfpagelayout pdfpagemode text text text FitB empty

The page layout of the displayed PDF, e.g., single-column, double-coloumn, etc. See below for all possible values. Gives direction to the viewer about how the PDF should be displayed when UseOutlines opened. Possible values are: N o n e ,U s e T h u m b s(show thumbnails), U s e O u t l i n e s(show bookmarks), and F u l l S c r e e n .

p d f s t a r t v i e wparameters F i t-

Fits the page to the window. Fits the width of the page to the window. F i t V- Fits the height of the page to the window. F i t R- Fits the rectangle specified by the four coordinates to the window. F i t B- Fits the page bounding box to the window. This basically reduces the amount of whitespace (margins) that is displayed and thus focussing more on the text content.
F i t Hwww.andy-roberts.net/writing/latex/pdfs 4/5

8/13/12

PDFs - Getting to grips with LaTeX - Andrew Roberts

F i t B H-

Fits the width of the page bounding box to the window. F i t B V- Fits the height of the page bounding box to the window.
p d f p a g e l a y o u tparameters S i n g l e P a g e-

Displays a single page; advancing flips the page. O n e C o l u m n- Displays the document in one column; continuous scrolling. T w o C o l u m n L e f t- Displays the document in two columns, odd-numbered pages to the left. T w o C o l u m n R i g h t- Displays the document in two columns, odd-numbered pages to the right.

Summary
From this tutorial I hope you can see that it's not difficult to take LaTeX to the next step and produce proper PDF files. Additionally, the hyperref package also allows you to exploit features within the PDF standard that can make the user experience a little bit better - mainly through enabling links and bookmarks. Of course, the hyperref package offers even more flexibility than discussed in this tutorial. I just covered the most useful functions, and once you get familiar with them, you can then look at the hyperref manual to learn more about the finer points of the package. Beyond this, there isn't really a great deal more to know about PDF and LaTeX. Useful resources: Hyperref manual Last updated: April 3, 2012 Back to LaTeX tutorials.
Tw eet 1 1

Andrew Roberts 2012

www.andy-roberts.net/writing/latex/pdfs

5/5

You might also like