Image Conversion

The image file formats listed in the previous section are the ones that may be embedded directly in a PDF. ConTeXt also supports a few other formats that are first converted to PDF using an external program. Of course, for such a conversion to work, the corresponding converter must be in the PATH.

Format

Extension

Converter

SVG

.svg, .svgz

inkscape

EPS

.eps, .ai

gs (or gswin32c on Windows) from Ghostscript

GIF

.gif

gm from GraphicsMagick

TIFF

.tiff

gm from GraphicsMagick

The conversion generates a PDF file with prefix m_k_i_v_ and a suffix .pdf added to the name of the original file. The result is cached, and the conversion is rerun if the timestamp of the original file is newer than that of the converted file.

Interaction

By default, the interactive elements of the included PDF file are discarded. To enable the interactive elements of the included PDF file, use

\externalfigure[filename.pdf][interaction=yes]

Image Directory

By default, ConTeXt searches an image in the current directory, the parent directory, and the grand-parent directory.

To search for images in other directories, for example a ./images subdirectory and /home/user/images, use:

The default search order is: the current directory, the parent directory, the grand-parent directory, and then the paths specified by the directory key. To restrict image search only to the paths specified by the directory key, use:

\setupexternalfigures[location=global]

To restore the default search behavior, use:

\setupexternalfigures[location={local,global}]

The ConTeXt distribution includes three sample images: cow.pdf, mill.png, and hacker.jpg, that are useful when creating minimum working examples to illustrate a bug on the mailing list. These images are locating in the TEXMF directory. To add the TEXMF directory to the image search path, use:

\setupexternalfigures[location={local,global,default}]

The above alternative adds the entireTEXMF directory to the search path, including thedoc/directory! Therefore, one needs to be extremely careful when using this option. In fact, I would advise not using location=default except for illustrative minimal working examples.

Remote Images

When a document containing a remote file is compiled for the first time, the remote file is downloaded from the server and stored in the LuaTeX cache directory. This cached file is used during subsequent runs.

Normally, the remote image is downloaded again if the image in the cache is older than 1~day. To change this threshold to, for example, 2minutes (120seconds), either add

\enabledirectives[schemes.threshold=120]

in the ConTeXt file, or compile the ConTeXt file using the command

context --directives=schemes.threshold=120 <em>filename</em>

The variable schemes.threshold is global, so changing its value affects all other macros like \input, \usemodule, \component, etc. that load remote files.

HTTP Proxy

To use an http proxy for fetching images, the http variable (LuaSocket) has to be set up as follows:

HTTPS

For self-signed certificates retrieved over HTTPS, the curl command requires a flag to retrieve insecure files, which is not enabled by default. Open tex/texmf-context/tex/context/base/mkiv/data-sch.lua to find:

Transformations

Image Scaling

To scale an image use the scale key: scale=1000 corresponds to the original dimensions of the image, scale=500 scales the image to 50% of the original size, scale=1500 scales the images to 150% of the original size, and so on. For example:

\externalfigure[logo.pdf][scale=500]

Use \setupexternalfigures to set the scale of all images. For example, to scale all images to be twice their original size, use:

\setupexternalfigures[scale=2000]

If either width or height is specified, then the scale key has no effect.

In addition, the xscale and yscale keys scale the image in only one dimension. For example:

Scaling changes the visible size of a picture, but not the data or file size. If you want to reduce your file size by decreasing image resolution, see Downsampling.

Image Dimension Restriction

ConTeXt can limit included images to particular dimensions. For example, to ensure that an included image is not more than 0.2\textwidth:

\externalfigure[logo.pdf][maxwidth=0.2\textwidth]

If maxwidth is specified and the width of the image is less than maxwidth, then the image is not scaled; if the width of the image is greater than maxwidth, then the width is restricted to maxwidth and the height is scaled appropriately to maintain the original aspect ratio.

The option maxheight is analogous to maxwidth, for checking the height of the image.

For example, to ensure that figures do not overflow the text~area, one may set:

\setupexternalfigures[maxwidth=\textwidth,
maxheight=0.8\textheight]

Image Rotation

Rotate included images by 90°, 180°, or 270° using the orientation key. For example:

\externalfigure[logo.pdf][orientation=90]

To rotate by an arbitrary angle, use the \rotate command. For example:

\rotate[rotation=45]{\externalfigure[logo.pdf]}

Image Mirroring

To mirror (flip) an image, use the generic \mirror command. For example, to mirror horizontally:

\mirror{\externalfigure[logo.pdf]}

To mirror vertically, first rotate the image by 180° and then mirror it:

\mirror{\externalfigure[logo.pdf][orientation=180]}

Image Clipping

Clip an image using the generic \clip command. For example, to clip the original image to a 1cm x 2cm rectangle at an offset of (3mm,5mm) from the top left corner:

As another example, this cuts the image into a 3x3 pieces and then outputs the (2,2) piece:

\clip[nx=3,ny=3,x=2,y=2]{\externalfigure[logo.pdf]}

Troubleshooting

This section describes various tips for discovering problems with embedded images.

Visualize Bounding Box

If, for instance, the image is taking more space than expected, it can be useful to visualize the bounding box of the image. To do this:

\externalfigure[logo.pdf][frame=on]

ConTeXt includes a Perl script pdftrimwhite that removes extra white space at the borders of a PDF file. To run this script:

mtxrun --script pdftrimwhite <em>[flags] input output</em>

The most important flag is --offset=dimen, which keeps some extra space around the trimmed image.

Similar functionality is provided by another Perl script, pdfcrop, that is included in most TeX distributions.

Diagnostic Tracking

To get diagnostic information about image inclusion, enable the tracker graphics.locating by editing the ConTeXt file and adding:

\enabletrackers[graphics.locating]

Alternatively, compile the ConTeXt file using:

context --trackers=graphics.locating filename

The tracker writes diagnostics to the console. Suppose we use \externalfigure[somefile.pdf] and ConTeXt finds the file in the current search path; then the following information is printed on the console:

Multiple Image Settings

Image Settings

Suppose your document contains many side-by-side images, and you want all of these images to be of the same size. In addition, you want to control the size of all images by changing only one setup. To do this, you can use the \defineexternalfigure macro, which defines a named collection of image settings. For example, to define a collection where the image width is 3cm, use:

\defineexternalfigure[logo-settings][width=3cm]

And then to use these settings in an image, use:

\externalfigure[group.pdf][logo-settings]

or, if you want to add or override settings, use:

\externalfigure[group.pdf][logo-settings][height=2cm]

Image Labels

Suppose your document contains an image at multiple locations; all of these images are to be of the same size, which is not necessarily the same as the natural size of the image. Furthermore, as before, you want to set the size of all the images by changing only one setup. Here, the macro to use is \useexternalfigure, which defines a symbolic label for inserting an image plus settings. For example:

\useexternalfigure[mylogo][logo.pdf][width=2cm]

defines an image label mylogo that maps to the image file logo.pdf and sets its width to 2cm. This image label may be used as a normal image filename:

Movies

preview=yes shows the first image as preview.
You find more about interactive Elements in mwidget-s.pdf

Unfortunately, people who are fond of Linux cannot embed movies because the linux release of acroread doesn't support that. An alternative solution consists in launching your prefered player (MPlayer) from acroread: