|
|
LaTeX-to-PDF How-To |
|
||||||||||||||||||||||||||||||||||
| About News Products Publications People / Contact us Links | ||||||||||||||||||||||||||||||||||||
| All JNI How-to LaTeX-PDF How-to Automating PPP From PGP to GnuPG |
The ``Linux LaTeX-PDF HOW-TO'' by Udo SchuermannCopyright © 2001,2002,2003 RingLord Technologies and Udo K. SchuermannAll rights reserved Revised: 2-Jul-2003 Revised: 24-Apr-2003 Revised: 17-Jan-2003 Revised: 9-Aug-2002 Revised: 8-Apr-2002 Revised: 24-Jul-2001 Revised: 18-Jun-2001 Revised: 17-April-2001 Revised: 16-April-2001 Revised: 15-April-2001 Click here for a printer-friendly copy of this document HTML is well-positioned for delivering information with contextual formatting to a wide variety of rendering agents (``browsers''). In most cases HTML is more than sufficient for this task but indisputably there arise some occasions that demand more control over formatting and the requirement for a document feature set that exceeds the ``one-page fits all'' model of HTML. In such cases LaTeX and the Portable Document Format (PDF) offer a perfect solution! Why not stay with TeX/LaTeX and the .dvi (device independent) format? Most people in the academic community are perfectly comfortable with .dvi files, knowing already about tools such as dvips(1), kdvi, xdvi, etc. that allow printing and viewing of .dvi files. In the Microsoft Windows world, however, technical expertise is often sorely lacking (let's face it: Microsoft sells to people who would mistake MS Word for a DTP program) so .dvi files are not an option when addressing the general public. And this brings us to the purpose of this document: how to create high-quality (e.g. fast-displaying) PDF documents from your LaTeX sources. These documents will offer the following benefits to those who read your work; all of that capability is available completely free of charge. I've done all the work for you with this comprehensive how-to document:
The combination of tools available allows you to generate documents whose visual quality and feature-completeness can rival anything you can get out of commercial tools on other platforms. What's more, few of the documents created with Adobe Distiller are as feature-rich as what you get with these tools. And with Linux it's all free. No surprise at all. Let's get started! What You NeedLinux is not required, but given the context of this document I make the assumption here that you have an operating system that is basically Unix-like. Solaris, BSD, Linux... they should all work the same for this purpose:
A Quickie TestTo see if you have all the right pieces in place, create yourself a working directory (perhaps called ``tex'') and then cut-and-paste the following text into a file in that directory named ``test.tex'':
pdflatex test twice in a row to ensure that the \pagerefS are resolved. If you are not familiar with the way TeX and LaTeX work, you should know at least this: TeX and LaTeX works as a compiler does. It parses the document specification and builds output from that. As it encounters \labelS (markers whose section and/or page number you can reference) it writes these to a file for future reference. Any forward references (say a reference on page 1 to an object somewhere else, maybe on page 50) are not yet known by the time that the reference on page 1 is processed. By running the program a second time it will be able to fill-in the reference on page 1 that it finally encountered on page 50 on the previous run. But back to our test.tex file: If you get errors then check again that you copied the entire test document correctly. You should get several dozen lines of output from the program, most or all of which you can ignore. You should get the shell prompt back; if not then there's an error and you should check that you copied and pasted the test document from Figure 1 correctly. If everything went as planned then you should now have a simple PDF file named ``test.pdf'' (yes, test.tex --> test.pdf, that is the expected pattern). You could now blindly trust that your PDF document is ready for distribution. Instead, it would be good to check it. Further below I'll also show you how to add thumbnail images to the PDF for some extra karma. Try the following commands in succession. As soon as one of them works you should have a way of verifying your documents. I'll leave it up to you to research the operation of these programs:
If you have Adobe Acrobat Reader (acroread) installed then you have access to a set of features of the PDF document that the other programs may not make fully accessible to you. Figure 2 shows you what this document looks like on my 1600x1200 sized display screen. Note the bookmark outline in the left column. And if you look closely you'll notice that there is some blue (web link) and green (external document link) text, too. There is a red page number, too, that links to a page in this document, but it's too small to read at the resolution of that image. You should be able to see that on your own copy, though! At this point you've managed to produce a fairly nice, if simple PDF document. In the next sections I'll show you how to add some cool features. PDF PlusPlusThis is a good time to examine the various parts of that ``test.tex'' (Figure 1) file to get an idea of what it is that got us this far. This will also let you customize the output. Here is that relevant section:
The hyperref macro package provides you with the ability to specify hyperlinks in your document. The package is configured with a series of comma-separated options in the (optional) [ ]-list:
Now, you are probably wondering what's with these ``rltred'', ``rltgreen'', and ``rltblue'' colours. These are defined after (and could be defined before) the hyperref package, but must be defined after the color package is included. Of note here is the fact that I'm using a somewhat darker green than usual because green on a white computer monitor background gets washed-out too easily.
Mixing PDF and LaTeX CommandsIf you tried to compile ``test.tex'' as a normal LaTeX document, you may have noticed (with some dismay) that it no longer compiled. That's because the various references to PDF constructs aren't understood by the LaTeX constructs. There's an easy way to fix that, however. It involves a bit of TeX code:
If that doesn't make a lot of sense to you, don't worry about it. The last five lines are the interesting stuff. It's basically an if-then-else statement that allows us to do some stuff when compiling the document for PDF, and something else for LaTeX. I'll have another example for you later (see Figure 8) that includes all the tricks. For now, just keep reading: Adding ThumbnailsThe THUMBPDF program takes as its input a PDF file. From that it can generate a series of PNG images (what, you're still using GIF?! Shame on you!) which it then merges into the PDF file. This works well enough, but THUMBPDF doesn't create terribly nice looking snapshots of the pages: there is no antialiasing. Does that mean THUMBPDF is useless? Not at all! The author of THUMBPDF is a smart person and has allowed us to skip the generation of PNG images, thereby accepting existing images, or images that we've generated with another program! Haha, flexible software. Gotta love it! So, what program should we use to generate high-quality scaled representations of our PDF document's pages? The combination that has worked very well for me is Ghostscript to take snapshots of the pages as JPEG images, and The GIMP to scale and post-process these images into PNG files that THUMBPDF can read. What, The GIMP is slow? It's true that The GIMP is a large, large program and takes about five seconds (on my system) to load. But there's a way to get it to process all of our pages without reloading the program so we pay this slow-loading penalty only once per document. That's not bad. Once loaded The GIMP isn't all that slow, so let's go with that solution. If you're adventurous you can use the PPM utilities, ImageMagick, or whatever tool you prefer. Using The GIMP gives me a chance to introduce non-interactive use of that program, which took me a while to figure out and get right. Why not teach you that, too? :-) Using Ghostscript to take Page SnapshotsThe following invocation of Ghostscript (gs) will take an inputfile.pdf and generate a 36 DPI snapshot of each of the file's pages, writing these to files named 1.jpeg, 2.jpeg, etc.:
But wait, I've already incorporated this in a nifty tool (see Figure 8). We'll now proceed to using The GIMP to perform automated processing of multiple image files from the command line. Yes, this little recipe is useful for just about any command-line usage of The GIMP: Processing Multiple Images With the GIMP From the Comand LineStore the following file as ``scale-file.scm'' in your GIMP scripts directory. If you have GIMP 1.2 installed then there should be a directory .gimp-1.2 in your home directory. Within that is a directory named scripts, so you'd store the following file as ``~/.gimp-1.2/scripts/scale-file.scm'':
As the comment in this file explains it will accept an input file and an output file, as well as the desired dimensions for the image to be written to the output file. The scaling will perform an ``unsharp mask'' operation to undo some of the blurring effect caused by the down-scale operation. This is especially valuable for small thumbnails: they will have a slightly heightened contrast, which improves their appearance. Note, the ``1'' as the first argument in the commands refers to the fact that this script is to run in non-interactive mode. I've commented out the (gimp-display-new img) command; if you ran this in interactive mode it would not just prompt you for various parameters but would also produce an image window to show you the result. But we definitely do not want to bother with visible stuff and interactive operations in this project. Onwards we go: A Totally Cool Unified PDF-Thumbnail ToolThe program in Figure 8 puts the previous two together into a unifying whole. The effect is that we give this program a PDF file and it spits out a bunch of scaled and nicely post-processed PNG images that are ready to use by the THUMBPDF program for insertion into our PDF document. There will be a slight delay while Ghostscript does its work. Then there is a slightly longer delay (probably five seconds or more) while The GIMP is loaded. After that it processes between 10 and 20 page images per second (depends on your processor's speed, of course). So you see, even with a 100 page document you're not likely to feel yourself growing old while waiting for this to finish :-) Ah, before I forget: There are some shell script variables at the very beginning of the program. You may want to look over them (and the comments) to verify that they are not blatantly wrong or in danger of conflicting with something of your own making. And finally, if as of July 24, 2001 the Totally Cool Unified PDF-Thumbnail Tool creates on its own all the Lispish code described above, so you don't need to store any of it in The GIMP directory. Just make sure that the script knows from where The GIMP loads its scripts. . . N.B. This script relies on ThumbPDF whose filename pattern has undergone changes from release to release. Please search for '@@@' in this script and apply what changes may be necessary to correct things, if necessary:
Adding the Thumbnails to the PDFFigure 10 shows two ways of generating and adding the thumbnails to the PDF. The ``Low-quality Thumbnails'' column does not use The GIMP; the thumbnails will have no antialiasing. The ``High-quality Thumbnails'' column uses The GIMP and the scripts in Figure 8 above (we named that script ``coolthumbs'') and then adds the thumbnails without having thumbpdf generate its own images.
That's basically it. You should have all the pieces to build your own high-quality PDF documents. If you do not want to spare the cycles to generate high-quality thumbnails then just use the sequence of commands from the left column of Figure 10, otherwise use the ones from the right column. The Makefile presented in Figure 12 also assumes that you want high-quality thumbnails. If not, change that file accordingly. A More Complete DocumentAnd now, as promised above, I present you in Figure 11 a document with some TeX conditionals that allows you to continue using your document as a normal TeX or LaTeX document; there is a reference to an image file there. Just create yourself a little image and store it as ``myLogo.jpg'' before compiling this document:
And while I'm at it, here is a convenient little Makefile that might be useful for you as a template. Just run ``make'' to build the test file. Run ``make final'' to add nice thumbnails to the document. Run ``make public'' to create a final version and then copy it to a public folder. Naturally you'd have to change the first two variables that name the file's basename and the public folder, but this little script encapsulates all the pieces presented in this article. All you need is to ensure that you have the supporting software installed, including the scripts from Figures 7 and 8.
ReferencesI am indebted to the numerous sources of fine (and free!) software, and the uncounted individuals on the internet who have shared their various knowledge with the rest of us. Without their sharing, I could not have gathered all this information here, built the meager tools you find here, and then shared it in turn with others.
Thanks go to Andreas Abraham for suggesting the -L / --landscape option. Thanks go to Jürgen Wenzel, who made me aware of the new filename pattern that later versions of THUMBPDF use, a change which caused the script in Figure 8 to break. Thanks go to Tom Heinrich as well as George Dowding, both of whom have pointed out independently to me the fact that the $THUMBPDF_V2 flag needed to be checked near the end of the script, too. Took me a while to get around to adding that to the script in Figure 8 above. If this article has helped you, or you have suggestions for improvements or corrections, why not drop us a line? Go to the contacts page. Use the help or status email addresses. Thanks! |
|||||||||||||||||||||||||||||||||||
|
The alteration of any part of this content by manual or automated means (adding, removing, or in any other way altering links, text, or images) constitutes misrepresentation of our content in violation of United States copyright law. For more details, please see our content ownership details page for elaboration. |
||||||||||||||||||||||||||||||||||||