LaTeX graphics | \includegraphics command
Actually, there are two aspects of LaTeX graphics that are worth mentioning:
- vector graphics described directly by means of LaTeX/TeX commands and,
- inclusion of external graphics described by means of external formats, like EPS, PDF or raster PNG and PDF.
The former has limited support in the original LaTeX and its packages. It is implemented with help of TeX vertical and horizontal rules. That’s because the original LaTeX is based on the original TeX engine, which produces main output in DVI format, which is not suitable for describing either lines and curves or binary raster image data. But engines that are able to output to graphics-supporting formats (like pdfTeX to PDF) can also deal with graphics using format-dependent packages or package features. The latter graphics are not supported in basic LaTeX for the same reason. We will discuss graphics inclusion features below.
The LaTeX graphics
package and \includegraphics
command
When talking about LaTeX graphics inclusion, the first thing that comes to mind is the \includegraphics
command. It is defined in the graphics
package, which you should mention in the preamble to enable its features.
1\usepackage{graphics}
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
file:
1\usepackage[pdftex]{graphics}
A driver
file 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.
Now, the simplest way to include, say, a PNG image is to type:
1\includegraphics{sample-image.png}
where sample-image.png
is the name of the file you want to include. You may even ommit the extension. The graphics
package contains the list of definitions of supported formats. When looking for a file, it traverses that list and includes the first matching file found.
You can also specify the full file name using an absolute or relative path:
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}}
Scaling the graphics
LaTeX graphics
package provides commands for manipulating the content. So you can scale the included graphics (in fact, anything described by TeX/LaTeX code) as follows:
1\scalebox{.5}{\includegraphics{sample-image.png}} % scales both width and height by 0.5
2\scalebox{.5}[1.5]{\includegraphics{sample-image.png}} % scales the width and height by 0.5 and 1.5 respectively
Resizing the graphics
It’s similar to scaling, but you specify the required size instead of a scaling factor(s):
1\resizebox{10mm}{!}{\includegraphics{sample-image.png}} % changes the width to 10mm preserving the proprtions
2\resizebox{20mm}{10mm}{\includegraphics{sample-image.png}} % changes both width and height independently
Rotating the graphics
1\rotatebox{25}{\includegraphics{sample-image.png}} % rotates the image by 25 degrees counterclockwise
The LaTeX graphicx
package
The LaTeX graphicx
package provides the key=value
interface to content transformations. To enable its features, you should mention it in the preamble:
1\usepackage{graphicx}
2\usepackage[pdftex]{graphicx} % with the driver option
You can combine any of the following options, but keep in mind that the order is important.
Graphics viewport
1\includegraphics[viewport=10 10 280 220]{sample-image.png} % sets the viewport with the lower left corner
2 % at the point (10, 10) (coinsides with (0, 0)
3 % of the box) and dimensions 280x220pt
4\includegraphics[viewport=10 10 250 220,clip]{sample-image.png} % the same, but the image is clipped by the viewport
Scaling graphics
1\includegraphics[scale=.5]{sample-image.png} % scales both width and height by 0.5
Resizing graphics
1\includegraphics[width=15mm]{sample-image.png} % changes the width to 15mm preserving the proprtions
2\includegraphics[height=15mm,width=25mm]{sample-image.png} % changes both width and height independently
Rotating graphics
1\includegraphics[angle=10]{sample-image.png} % rotates the image by 10 degrees counterclockwise
To learn more about the graphics
package bundle features, see the documentation
here and
here.
You may also check out the free conversion web app built based on Aspose.TeX for .NET API.