Latex/Importing Graphics
Total Page:16
File Type:pdf, Size:1020Kb
LaTeX/Importing Graphics There are two possibilities to include graphics in your document. Either create them with some special code, a topic which will be discussed in the Creating Graphics part, (see Introducing Procedural Graphics) or import productions from third party tools, which is what we will be discussing here. Strictly speaking, LaTeX cannot manage pictures directly: in order to introduce graphics within documents, LaTeX just creates a box with the same size as the image you want to include and embeds the picture, without any other processing. This means you will have to take care that the images you want to include are in the right format to be included. This is not such a hard task because LaTeX supports the most common picture formats around. Contents 1 Raster graphics vs. vector graphics 2 The graphicx package 3 Document Options 4 Supported image formats 4.1 Compiling with latex 4.2 Compiling with pdflatex 5 Including graphics 5.1 Examples 5.2 Spaces in names 5.3 Borders 6 Graphics storage 7 Images as figures 8 Text wrapping around pictures 9 Seamless text integration 10 Including full PDF pages 11 Converting graphics 11.1 PNG alpha channel 11.2 Converting a color EPS to grayscale 12 Third-party graphics tools 12.1 Vector graphics 12.2 Raster graphics 12.3 Plots and Charts 12.4 Editing EPS graphics 13 Notes and References Raster graphics vs. vector graphics Raster graphics will highly contrast with the quality of the document if they are not in a high resolution, which is the case with most graphics. The result may be even worse once printed. Most drawing tools (e.g. for diagrams) can export in vector format. So you should always prefer PDF or EPS to PNG or JPG. The graphicx package As stated before, LaTeX can't manage pictures directly, so we will need some extra help: we have to load the graphicx package[1] (http://ctan.org/pkg/graphicx/) in the preamble of our document: \usepackage{graphicx} This package accepts as an argument the external driver to be used to manage pictures; however, the latest version of this package takes care of everything by itself, changing the driver according to the compiler you are using, so you don't have to worry about this. Still, just in case you want to understand better how it works, here are the possible options you can pass to the package: dvips (default if compiling with latex), if you are compiling with latex to get a DVI and you want to see your document with a DVI or PS viewer. dvipdfm, if you are compiling with latex to get a DVI that you want to convert to PDF using dvipdfm, to see your document with any PDF viewer. pdftex (default if compiling with pdflatex), if you are compiling with pdftex to get a PDF that you will see with any PDF viewer. But, again, you don't need to pass any option to the package because the default settings are fine in most of the cases. In many respects, importing your images into your document using LaTeX is fairly simple... once you have your images in the right format that is! Therefore, I fear for many people the biggest effort will be the process of converting their graphics files. Now we will see which formats we can include and then we will see how to do it. Document Options The graphics and graphicx packages recognize the draft and final options given in the \documentclass[...]{...} command at the start of the file. (See Document Classes.) Using draft as the option will suppress the inclusion of the image in the output file and will replace the contents with the name of the image file that would have been seen. Using final will result in the image being placed in the output file. The default is final. Supported image formats As explained before, the image formats you can use depend on the driver that graphicx is using but, since the driver is automatically chosen according to the compiler, then the allowed image formats will depend on the compiler you are using. Consider the following situation: you have added some pictures to your document in JPG and you have successfully compiled it in PDF. Now you want to compile it in DVI, you run latex and you get a lot of errors... because you forgot to provide the EPS versions of the pictures you want to insert. At the beginning of this book, we had stated that the same LaTeX source can be compiled in both DVI and PDF without any change. This is true, as long as you don't use particular packages, and graphicx is one of those. In any case, you can still use both compilers with documents with pictures as well, as long as you always remember to provide the pictures in two formats (EPS and one of JPG, PNG or PDF). Compiling with latex The only format you can include while compiling with latex is Encapsulated PostScript (EPS). The EPS format was defined by Adobe Systems for making it easy for applications to import postscript-based graphics into documents. Because an EPS file declares the size of the image, it makes it easy for systems like LaTeX to arrange the text and the graphics in the best way. EPS is a vector format— this means that it can have very high quality if it is created properly, with programs that are able to manage vector graphics. It is also possible to store bit-map pictures within EPS, but they will need a lot of disk space. Compiling with pdflatex If you are compiling with pdflatex to produce a PDF, you have a wider choice. You can insert: JPG, widely used on Internet, digital cameras, etc. They are the best choice if you want to insert photos. PNG, a very common format (even if not as much as JPG); it's a lossless format and it's the best choice for diagrams (if you were not able to generate a vector version) and screenshots. PDF, it is widely used for documents but can be used to store images as well. It supports both vector and bit-map images, but it's not recommended for the latter, as JPG or PNG will provide the same result using less disk space. EPS can be used with the help of the epstopdf package. Depending on your installation, you may just need to have it installed, there is no need to load it in your document; if it does not work, you need to load it just after the graphicx package. Additionally, since epstopdf will need to convert the EPS file into a PDF file and store it, you need to give "writing permissions" to your compiler. This is done by adding an option to the compiling command, e.g. pdflatex -shell-escape file.tex (if you use a LaTeX editor, they usually allow to modify the command in the configuration options). Check the epstopdf documentation for other compilers. Including graphics Now that we have seen which formats we can include and how we could manage those formats, it's time to learn how to include them in our document. After you have loaded the graphicx package in your preamble, you can include images with \includegraphics, whose syntax is the following: \includegraphics[attr1=val1, attr2=val2, ..., attrn=valn]{imagename} As usual, arguments in square brackets are optional, whereas arguments in curly braces are compulsory. The argument in the curly braces is the name of the image. Write it without the extension. This way the LaTeX compiler will look for any supported image format in that directory and will take the best one (EPS if the output is DVI; JPEG, PNG or PDF if the output is PDF). Images can be saved in multiple formats for different purposes. For example, a directory can have "diagram.pdf" for high-resolution printing, while "diagram.png" can be used for previewing on the monitor. You can specify which image file is to be used by pdflatex through the preamble command: \DeclareGraphicsExtensions{.pdf,.png,.jpg} which specifies the files to include in the document (in order of preference), if files with the same basename exist, but with different extensions. The variety of possible attributes that can be set is fairly large, so only the most common are covered below: Specify the preferred width of the NB. Only specifying width=xx imported image to xx. either width or height will scale the image Specify the preferred height of the height=xx while maintaining the imported image to xx. aspect ratio. This can be set to either true or false. When true, it will scale the image according to both height and width, but will keepaspectratio not distort the image, so that neither width nor height are exceeded. Scales the image by the desired scale factor. e.g, 0.5 to scale=xx reduce by half, or 2 to double. This option can rotate the image by xx degrees (counter- angle=xx clockwise) This option will crop the imported image by l from the left, b trim=l b r t from the bottom, r from the right, and t from the top. Where l, b, r and t are lengths. clip For the trim option to work, you must set clip=true. If the image file is a pdf file with multiple pages, this page=x parameter allows you to use a different page than the first. resolution=x Specify image resolution in dpi In order to use more than one option at a time, simply separate each with a comma.