Image formats & inclusion

To put a photo, a plot, or a diagram into a paper or a book you pull an external image file into the document. The doorway is the \includegraphics command from the graphicx package. This page sorts out the basic syntax, the options for scaling, rotating, and cropping, how to set the image search path, and — crucially — which formats each TeX engine can read. Two ideas anchor it all: the split between vector and raster images, and the fact that the accepted formats depend on the engine you compile with.

graphicx and \includegraphics

Image inclusion is handled by the standard graphicx package. Load it in the preamble with \usepackage{graphicx}, then in the body write \includegraphics[options]{filename}. graphicx extends the older graphics package; its distinguishing feature is that you give options in a key=value form. Both ship in LaTeX’s standard latex-graphics bundle, so there is nothing extra to install.

document.tex
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=0.6\textwidth]{plot}
\end{document}

The thing to remember here is to omit the extension from the filename. In the example above, if plot.pdf or plot.png exists, LaTeX picks the right format automatically according to the engine. Writing the extension makes it harder to swap formats later and can misfire on names that contain a period, such as plot.tar.gz. For the same reason it is safest to avoid spaces and dots in image filenames.

When images live in another folder, add to the search path with \graphicspath. **Wrap each directory name in its own braces (do this even for a single directory) and end each with a forward slash / (a forward slash even on Windows).** LaTeX looks in the current directory first, then in the listed directories in order. A path containing spaces is wrapped in quotes, as in {"my figures/"}.

latex
\usepackage{graphicx}
\graphicspath{ {figures/} {../shared-figures/} }

Sizing, rotation & cropping options

Inside the brackets of \includegraphics you list options as key=value pairs. Specifying the size is all but mandatory in practice. width= and height= set the rendered width or height directly; writing them relative to \textwidth (the text width) or \linewidth, as in width=0.8\textwidth, lets the image scale to the page. scale= is a multiplier on the image’s natural size.

Giving both width= and height= distorts the image by breaking its aspect ratio. keepaspectratio prevents that: with it set, the image is scaled to fit within the given width and height while preserving its proportions. To rotate, give angle= an angle in degrees measured counter-clockwise — angle=90, for instance, turns the image a quarter turn.

To crop the edges of an image, combine trim= with clip. Give trim four values in the order left, bottom, right, top (note that this is not the intuitive top-bottom-left-right). The default unit is bp (big points). And **always add clip**: without it, the part you meant to remove is not discarded but spills over the surrounding text. Negative values do the opposite, adding margin. There is also viewport=, which specifies the crop window in absolute coordinates.

latex
% 幅を本文幅の半分に
\includegraphics[width=0.5\textwidth]{photo}

% 縦横比を保って枠に収める
\includegraphics[width=8cm,height=5cm,keepaspectratio]{photo}

% 反時計回りに 90 度回転
\includegraphics[angle=90,width=6cm]{diagram}

% 左20 下20 右30 上10(bp) を切り落として表示
\includegraphics[trim=20 20 30 10,clip,width=6cm]{scan}

The main options can be laid out as a table. In practice you usually combine several of them.

OptionWhat it does
width=Rendered width; relative values like 0.8\textwidth are handy
height=Rendered height
scale=Multiplier on natural size (scale=0.5 is half)
angle=Counter-clockwise rotation angle (degrees)
keepaspectratioKeep proportions when both width and height are given
trim= … clipCrop left, bottom, right, top; needs clip, default unit bp
page=Select which page of a multi-page PDF to include

Vector vs raster

Images fall into two broad families, and the choice between them shapes the result. Vector images describe points, lines, curves, and text as mathematical coordinates — PDF, EPS, and SVG are the prime examples. Because the outlines are recomputed from the math, they stay crisp at any magnification, and text stays sharp. Raster images (bitmaps) are a grid of colored pixels — PNG and JPEG being the main ones. Magnify them and each pixel grows, so edges turn jagged or blurry.

The rule of thumb: anything made of lines and text — plots, line drawings, circuit diagrams, figures with equations — should be vector (PDF/EPS/SVG). It is independent of print resolution and always looks its best on paper or screen. Continuous-tone images — photographs, screenshots — should be raster (PNG/JPEG). Turning a photo into a vector gains nothing and bloats the file. When you do use raster, supply source data at a print-worthy resolution (generally 300 dpi or more). If you generate figures within LaTeX (TikZ, pgfplots, and the like), the output is naturally vector.

A word on choosing between PNG and JPEG. JPEG is lossy compression aimed at photographs; files are small, but compression noise appears around the edges of lines and text. PNG is lossless and suits screenshots, flat-color graphics, and anything needing transparency. When unsure: JPEG for photos, PNG for everything else raster.

Which formats each engine reads

This is the easiest place to stumble. The formats \includegraphics accepts depend on which TeX engine processes the file and which driver ultimately produces the output. The modern mainstream — pdfLaTeX, LuaLaTeX, and XeLaTeX (the route that emits PDF directly) — reads PDF, PNG, and JPEG natively. But these engines cannot read EPS directly. The classic **latex + dvips route (which goes through DVI), conversely, handles EPS natively** but is not suited to PDF or PNG.

For Japanese, the common pLaTeX / upLaTeX emit DVI and convert to PDF with dvipdfmx. This route handles PDF, PNG, JPEG, and EPS. For PNG, JPEG, and PDF it may need a sidecar file (.xbb) recording the image’s dimensions (its bounding box), generated with the extractbb tool. And when using graphicx with pLaTeX/upLaTeX it is safest to state the output driver explicitly, as in \usepackage[dvipdfmx]{graphicx} (pdfLaTeX and friends detect it automatically).

Processing routeNative formatsEPS?
pdfLaTeXPDF, PNG, JPEGNo; convert via epstopdf
LuaLaTeXPDF, PNG, JPEGNo; convert via epstopdf
XeLaTeXPDF, PNG, JPEGNo; convert via epstopdf
pLaTeX/upLaTeX + dvipdfmxPDF, PNG, JPEG, EPSYes (needs a bounding box)
latex + dvipsEPS (PostScript family)This is its native format

The practical upshot is simple: if you are starting fresh, supply figures as PDF, PNG, or JPEG and they pass straight through pdfLaTeX, LuaLaTeX, XeLaTeX, or dvipdfmx alike. The conversions in the next two sections are only needed when all you have is EPS or SVG. For the routes themselves, see the “How processing works” page.

Bringing in EPS and SVG

EPS (Encapsulated PostScript) is an old vector format, still required by some journals’ legacy submission rules. To use EPS with pdfLaTeX and the like you must convert it to PDF — but if you load the **epstopdf package, an EPS file is converted to PDF automatically at compile time and pulled straight in. The mechanism hooks into graphicx’s processing, converts with Ghostscript, and skips the work if a PDF of the same name already exists. Because this automatic conversion runs an external program, compile with --shell-escape (or -shell-escape). When handling raw EPS, its BoundingBox** — the line at the top stating the image’s extent — must be present and correct.

latex
\usepackage{graphicx}
\usepackage{epstopdf}  % EPS をコンパイル時に PDF へ自動変換
% コンパイル例: pdflatex --shell-escape main.tex

SVG is the standard vector format on the web, but no TeX engine reads it directly. To include it, use the **svg package**. It provides \includesvg{file} and internally calls Inkscape’s command line to convert the SVG to PDF (or EPS when producing DVI output). It also exports the text inside the SVG to a separate file that LaTeX typesets, so the labels come out cleanly in the document’s own font. Because it invokes Inkscape, it too **requires --shell-escape** and a working Inkscape install. Failing that, converting the SVG to PDF up front with Inkscape or another tool is the reliable path.

For the reverse — producing SVG from TeX output — there is **dvisvgm**. It converts DVI (and EPS, and PDF) to SVG, supports pTeX’s vertical-writing DVI and XeTeX’s XDV, and embeds the glyph outlines of the required fonts into the SVG. It is handy when you want an equation or figure as a standalone SVG for the web.

The figure float, and common snags

\includegraphics merely drops the image in place; it adds no number and no caption. In a paper you normally put the image inside a **figure environment so it becomes a numbered “Figure.” LaTeX then floats** it to a suitable spot given the page layout, \caption{…} gives it a numbered description, and \label/\ref let you cite it from the text as “see Figure 3.”

latex
\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.7\textwidth]{plot}
  \caption{測定値と理論曲線の比較。}
  \label{fig:plot}
\end{figure}

図~\ref{fig:plot} に示すように……

Float placement, what [htbp] means, and putting figures side by side are covered on the “Floats & placement” page; caption styling and subfigures on the “Captions & subfigures” page. Here, a list of snags you will commonly hit when bringing images in.

  • Wrong format for the engine: handing EPS to pdfLaTeX halts with “Cannot determine size of graphic” and the like. Make the figure PDF/PNG/JPEG, or convert with epstopdf.
  • EPS BoundingBox: a missing or wrong BoundingBox line means bad placement. Fix it with ps2eps or epstool.
  • Multi-page PDF: only the first page is included by default. Pick a page with page= (for a whole document, the pdfpages package).
  • Whitespace in a PDF: wide margins in a figure PDF make it look small. Trim them first with pdfcrop (on CTAN).
  • Spaces or dots in filenames: spaces or dots in the path or name often break inclusion. Rename, or quote a spaced path.
  • Hard-coding the extension: naming the extension blocks swapping formats. As a rule, write the name without it.