LaTeX includegraphics | Aspose.TeX for Java

Introduction

While working on your academic and scientific documents, such as research papers, conference proceedings, and academic presentations, you may occasionally want to include graphic figures.

On the one hand, those can be drawings, plots, histograms, or something else that is generally called vector graphics. Modern LaTeX systems (such as pdfTeX) provide plenty of tools for creating such graphics in a document. For example, pict2e, PGF(TikZ) and PGFplots packages. In most cases, figures themselves are generated by a TeX engine and included in the output file as sequences of vector graphics instructions appropriate for a specific output format.

On the other hand, you may have raster images that you also want to include in your publication. LaTeX systems are currently also capable of doing this, but you need to specify the path to your image file in your LaTeX file as a parameter to some LaTeX macro.

This article covers including raster images in LaTeX files using the \includegraphics macro from the graphics (and the accompanying graphicx) package.

Which package to choose, graphics or graphicx?

Both packages, graphics and graphicx, are very similar. The main difference is in the syntax. While the graphics package uses a set of separate macros to apply transformations to an image, the graphicx package uses the key=value notation for the parameters of the \includegraphics macro to specify these transformations. We will give examples of each use case for both packages at once to make the difference more clear.

Including the chosen package

This is done with the \usepackage command the same way as for any other LaTeX package:

1\usepackage{graphics} % to use the graphics package
2\usepackage{graphicx} % to use the graphicx package

Specifying a driver

If your typesetting system is not configured to use, say, pdfTeX by default, you should specify the appropriate option that will force the package to use the required driver:

1\usepackage[pdftex]{graphics} % graphics
2\usepackage[pdftex]{graphicx} % graphicx

A driver is a part of a package that implements the interface between package commands and format-dependent low-level TeX extension primitives. Aspose.TeX’s LaTeX graphics support is configured to use its own driver, so you don’t need to specify the driver option.

LaTeX \includegraphics macro

Now let’s proceed with the simplest example. This is of course a case of including an image without any transformation at all. Expectedly, the LaTeX code using both packages is the same:

1\includegraphics{sample-image.png}

This example assumes that your image file is located in the same directory as the LaTeX file. But there are several ways to specify a different location. The LaTeX code will again be the same for both packages:

1\includegraphics{d:/sample-image.png} % absolute path
2\includegraphics{./sample-image.png} % relative to the current directory
3\includegraphics{../img/sample-image.png} % relative to the directory containing the current one

Another way to specify the graphics files location(s) is to define a list of alternative paths using \graphicspath command before calling \includegraphics:

1\graphicspath{{d:/img}{c:/img}{d:/work/img}}

LaTeX graphics transformations

LaTeX graphics and graphicx packages provide a number of ways for manipulating the content.

Scale figure LaTeX

So you can scale the included graphics (in fact, anything described by TeX/LaTeX code) as follows:

1% graphics examples
2\scalebox{.5}{\includegraphics{sample-image.png}} % scales both width and height by 0.5
3\scalebox{.5}[1.5]{\includegraphics{sample-image.png}} % scales the width and height by 0.5 and 1.5 respectively
4
5% graphicx examples
6\includegraphics[scale=.5]{sample-image.png} % scales both width and height by 0.5
7% there's no analogue for the graphicx package to scale both dimensions independently

Resize figure LaTeX

It’s similar to scaling, but you specify the required size instead of a scaling factor(s):

1% graphics examples
2\resizebox{10mm}{!}{\includegraphics{sample-image.png}} % changes the width to 10mm preserving the proprtions
3\resizebox{20mm}{10mm}{\includegraphics{sample-image.png}} % changes both width and height independently
4
5% graphicx examples
6\includegraphics[width=15mm]{sample-image.png} % changes the width to 15mm preserving the proprtions
7\includegraphics[height=15mm,width=25mm]{sample-image.png} % changes both width and height independently

Rotate figure LaTeX

1% graphics example
2\rotatebox{25}{\includegraphics{sample-image.png}} % rotates the image by 25 degrees counterclockwise
3
4% graphicx example
5\includegraphics[angle=-10]{sample-image.png} % rotates the image by 10 degrees clockwise

Setting a viewport and clipping

1% This time there's no analogue for the graphics package
2
3% graphicx example
4\includegraphics[viewport=10 10 280 220]{sample-image.png} % sets the viewport with the lower left corner
5                                                           % at the point (10, 10) (coinsides with (0, 0)
6                                                           % of the box) and dimensions 280x220pt
7\includegraphics[viewport=10 10 250 220,clip]{sample-image.png} % the same, but the image is clipped by the viewport

As for the graphicx package, transformations can be combined, but the order is important.

To learn more about the graphics package bundle features, refer to the package page on CTAN and the package documentation.

LaTeX Include Graphics free web app

If you do not want to work with includegraphics latex package programmatically you may also check out the free LaTeX Include Graphics web app built based on Aspose.TeX for .NET API.

The app allows you to select multiple images and visually adjust transformations for them. It also generates LaTeX source code using the graphicx package and renders it displaying the result on the page.

LaTeX Figure Rendering feature in the Aspose.TeX library

The Aspose.TeX library provides a way to create raster and SVG figures that you can use in your publications outside of the TeX/LaTeX context. For example, on a web page. This is called the LaTeX Figure Rendering. Briefly, this part of the API allows you to render a fragment of LaTeX source code regardless of the page layout. For more details, refer to this article.

The article on including external LaTeX packages can also give you a clue on how to create figures out of LaTeX plots.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.