Difference between revisions of "Using Graphics"

From Wiki
Jump to navigation Jump to search
m (changed pre to texcode and tt to cmd)
m (update for the latest beta (pdfe supports png streams as well now))
(48 intermediate revisions by 20 users not shown)
Line 1: Line 1:
< [[Graphics]] | [[Combinations]] >
+
< [[Graphics]] | [[Combinations]] | [[File Formats|Supported Media File Formats]] | [[Including pages from PDF documents]] | [[Animation]] | [[Downsampling]] >
  
'''Direct use of <tt>mypic.pdf</tt>'''
+
= Usage =
 +
 
 +
The simplest way to insert an image is to use:
 +
 
 +
<texcode>\externalfigure[logo.pdf]</texcode>
 +
 
 +
This command places the PDF image <tt>logo.pdf</tt> in a {{cmd|vbox}}; the width and height of the image are equal to the natural dimensions of the image.
 +
 
 +
To set the width of the image to a specific size, say <tt>1cm</tt>, use:
 +
 
 +
<texcode>\externalfigure[logo.pdf][width=1cm]</texcode>
 +
 
 +
Similarly, to set the height of the image to a specific size, say <tt>2cm</tt>, use:
 +
 
 +
<texcode>\externalfigure[logo.pdf][height=2cm]</texcode>
 +
 
 +
If only the <tt>width</tt> or <tt>height</tt> of the image is specified, the other dimension is scaled appropriately to keep the aspect ratio.
 +
 
 +
To include a specific page, say page 5, of a multi-page PDF file, use:
 +
 
 +
<texcode>\externalfigure[logo.pdf][page=5]</texcode>
 +
 
 +
These four variations cover the most common use cases.
 +
 
 +
== File Formats ==
 +
 
 +
ConTeXt supports the image formats enumerated below. The image format is determined from the file extension (case insensitive).
 +
 
 +
* '''PDF''': File extension <tt>.pdf</tt>
 +
* '''MPS''' (MetaPost output): File extension <tt>.mps</tt> or <tt>.&lt;digits&gt;</tt>
 +
* '''JPEG''': File extension <tt>.jpg</tt> or <tt>.jpeg</tt>
 +
* '''PNG''': File extension <tt>.png</tt>
 +
* '''JPEG 2000''': File extension <tt>.jp2</tt>
 +
* '''JBIG''' or '''JBIG2''': File extension <tt>.jbig</tt>, <tt>.jbig2</tt>, or <tt>.jb2</tt>
 +
 
 +
== 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 <tt>PATH</tt>.
 +
 
 +
<table>
 +
<tr class="odd">
 +
<td align="left">Format</td>
 +
<td align="left">Extension</td>
 +
<td align="left">Converter</td>
 +
</tr>
 +
<tr class="even">
 +
<td align="left"><strong>SVG</strong></td>
 +
<td align="left"><code>.svg</code>, <code>.svgz</code></td>
 +
<td align="left"><code>inkscape</code></td>
 +
</tr>
 +
<tr class="odd">
 +
<td align="left"><strong>EPS</strong></td>
 +
<td align="left"><code>.eps</code>, <code>.ai</code></td>
 +
<td align="left"><code>gs</code> (or <code>gswin32c</code> on Windows) from Ghostscript</td>
 +
</tr>
 +
<tr class="even">
 +
<td align="left"><strong>GIF</strong></td>
 +
<td align="left"><code>.gif</code></td>
 +
<td align="left"><code>gm</code> from GraphicsMagick</td>
 +
</tr>
 +
<tr class="odd">
 +
<td align="left"><strong>TIFF</strong></td>
 +
<td align="left"><code>.tiff</code></td>
 +
<td align="left"><code>gm</code> from GraphicsMagick</td>
 +
</tr>
 +
</table>
 +
 
 +
The conversion generates a PDF file with prefix <tt>m_k_i_v_</tt> and a suffix <tt>.pdf</tt> 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
 +
<texcode>\externalfigure[filename.pdf][interaction=yes]</texcode>
 +
 
 +
== 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 <tt>./images</tt> subdirectory and <tt>/home/user/images</tt>, use:
 +
 
 +
<texcode>\setupexternalfigures[directory={images, /home/user/images}]</texcode>
 +
 
 +
Note: always use forward slashes (`/`) in path names, regardless of operating system.
 +
 
 +
The default search order is: the current directory, the parent directory, the grand-parent directory, and then the paths specified by the <tt>directory</tt> key. To restrict image search only to the paths specified by the <tt>directory</tt> key, use:
 +
 
 +
<texcode>\setupexternalfigures[location=global]</texcode>
 +
 
 +
To restore the default search behavior, use:
 +
 
 +
<texcode>\setupexternalfigures[location={local,global}]</texcode>
 +
 
 +
The ConTeXt distribution includes three sample images: <tt>cow.pdf</tt>, <tt>mill.png</tt>, and <tt>hacker.jpg</tt>, that are useful when creating minimum working examples to illustrate a bug on the mailing list. These images are locating in the <tt>TEXMF</tt> directory. To add the <tt>TEXMF</tt> directory to the image search path, use:
 +
 
 +
<texcode>\setupexternalfigures[location={local,global,default}]</texcode>
 +
 
 +
The above alternative adds the ''entire'' <tt>TEXMF</tt> directory to the search path, ''including the'' <tt>doc/</tt> ''directory!'' Therefore, one needs to be extremely careful when using this option. In fact, I would advise not using <tt>location=default</tt> except for illustrative minimal working examples.
 +
 
 +
== Remote Images ==
 +
 
 +
The {{cmd|externalfigure}} command supports reading files from web servers, for example:
 +
 
 +
<texcode>\externalfigure[http://tug.org/images/logobw.jpg]</texcode>
 +
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, 2<sub>minutes (120</sub>seconds), either add
 +
 
 +
<texcode>\enabledirectives[schemes.threshold=120]</texcode>
 +
 
 +
in the ConTeXt file, or compile the ConTeXt file using the command
 +
 
 +
<pre>context --directives=schemes.threshold=120 <em>filename</em></pre>
 +
The variable <tt>schemes.threshold</tt> is global, so changing its value affects all other macros like <tt>\input</tt>, <tt>\usemodule</tt>, <tt>\component</tt>, etc. that load remote files.
 +
 
 +
=== HTTP Proxy ===
 +
 
 +
To use an http proxy for fetching images, the http variable ([http://w3.impa.br/~diego/software/luasocket/http.html LuaSocket]) has to be set up as follows:
  
 
<texcode>
 
<texcode>
\externalfigure[mypic]
+
\ctxlua{http = require("socket.http"); http.PROXY = "http://proxy.example.com:3128"}
 
</texcode>
 
</texcode>
  
Additional parameters:
+
Replace `http://proxy.example.com:3128` with the proxy URL.
 +
 
 +
To disable the proxy again:
 +
 
 
<texcode>
 
<texcode>
\externalfigure[file or reference name][key=value, key=value, ...]
+
\ctxlua{http = require("socket.http"); http.PROXY = nil}
 
</texcode>
 
</texcode>
  
* scale            = ''scaling value''
+
=== HTTPS ===
* factor          = max, fit, broad
+
 
* wfactor, hfactor = max, fit, broad, ''value''
+
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:
* width, height    = ''dimension''
+
 
* frame            = on, off
+
<pre>
* preset, preview, repeat, object  = yes, no
+
local function runcurl(name,cachename) -- we use sockets instead or the curl library when possible
* display          = ''file name''
+
    local command = "curl --silent --create-dirs --output " .. cachename .. " " .. name
* type, method     = eps, mps, pdf, tif, png, jpg, mov, tex
+
     os.spawn(command)
 +
end
 +
</pre>
 +
 
 +
Insert the `-k` or `--insecure` option:
  
If only one dimension is given, scaling is proportional.
+
<pre>
 +
    local command = "curl --insecure --silent --create-dirs --output " .. cachename .. " " .. name
 +
</pre>
  
'''Where ConTeXt finds your picture files'''
+
== Inline Images ==
  
ConTeXt looks in the actual and parent directory plus those given in <tt>texmf.cnf</tt>.
+
Embedding inline images via base64 encoding using the memstream function (supports pdf and png streams):
  
You can define your own image directory with:
 
 
<texcode>
 
<texcode>
\setupexternalfigures[directory={../pictures}]
+
\startluacode
 +
local cow = mime.unb64("JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nJWayY40SW6E7/UUcRaQIafvfp6bAAkY9SM0oHXmMBAgvb7sM0ZmVf86CYNB/+HpC500Ghevv13ljqvwv+e/v//16+//eV3/+l9f8y4nRlz/8xVx99XW9Y9fcf2D/v8f+v+/a9q5/vSb/vnbn/7p629a+oq79L639jpjRKzcmC1fsUe927xaO3fR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DUxNTEyMjA1OSkKL01vZERhdGUoRDoyMDA2MDUxNTEyMjA1OSkKL0NyZWF0b3IoQ29yZWxEUkFXISkKL1RpdGxlKENPVy5FUFMpPj5lbmRvYmoKeHJlZgowIDEyCjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwNDYzNiAwMDAwMCBuIAowMDAwMDA0OTg2IDAwMDAwIG4gCjAwMDAwMDQ1NzcgMDAwMDAgbiAKMDAwMDAwNDQyNiAwMDAwMCBuIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDQ0MDYgMDAwMDAgbiAKMDAwMDAwNDY4NCAwMDAwMCBuIAowMDAwMDA0ODM4IDAwMDAwIG4gCjAwMDAwMDQ3MjUgMDAwMDAgbiAKMDAwMDAwNDc3OCAwMDAwMCBuIAowMDAwMDA0ODA4IDAwMDAwIG4gCnRyYWlsZXIKPDwgL1NpemUgMTIgL1Jvb3QgMSAwIFIgL0luZm8gMiAwIFIKL0lEIFs8ODE0NjQ5NTc3Qzg3MTE1QzRDQzBDQjg0QkU3RTQ1MEY+PDgxNDY0OTU3N0M4NzExNUM0Q0MwQ0I4NEJFN0U0NTBGPl0KPj4Kc3RhcnR4cmVmCjUxMzMKJSVFT0YK")
 +
figures.setmemstream("inline",cow)
 +
context.externalfigure({"memstream:///inline"})
 +
\stopluacode
 
</texcode>
 
</texcode>
You can define the path relative or absolute, but use always forward slashes!
 
  
'''Referenced Pictures'''
+
= Transformations =
 +
 
 +
== Image Scaling ==
 +
 
 +
To scale an image use the <tt>scale</tt> key: <tt>scale=1000</tt> corresponds to the original dimensions of the image, <tt>scale=500</tt> scales the image to 50% of the original size, <tt>scale=1500</tt> scales the images to 150% of the original size, and so on. For example:
 +
 
 +
<texcode>\externalfigure[logo.pdf][scale=500]</texcode>
 +
 
 +
Use <tt>\setupexternalfigures</tt> to set the scale of all images. For example, to scale all images to be twice their original size, use:
 +
 
 +
<texcode>\setupexternalfigures[scale=2000]</texcode>
 +
 
 +
If either <tt>width</tt> or <tt>height</tt> is specified, then the <tt>scale</tt> key has no effect.
 +
 
 +
In addition, the <tt>xscale</tt> and <tt>yscale</tt> keys scale the image in only one dimension. For example:
 +
 
 +
<texcode>\externalfigure[logo.pdf][xscale=500]
 +
\externalfigure[logo.pdf][yscale=500]</texcode>
 +
 
 +
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 <tt>0.2\textwidth</tt>:
 +
 
 +
<texcode>\externalfigure[logo.pdf][maxwidth=0.2\textwidth]</texcode>
 +
 
 +
If <tt>maxwidth</tt> is specified and the width of the image is less than <tt>maxwidth</tt>, then the image is not scaled; if the width of the image is greater than <tt>maxwidth</tt>, then the width is restricted to <tt>maxwidth</tt> and the height is scaled appropriately to maintain the original aspect ratio.
 +
 
 +
The option <tt>maxheight</tt> is analogous to <tt>maxwidth</tt>, for checking the height of the image.
 +
 
 +
For example, to ensure that figures do not overflow the text~area, one may set:
 +
 
 +
<texcode>\setupexternalfigures
 +
    [maxwidth=\textwidth,
 +
    maxheight=0.8\textheight]</texcode>
 +
 
 +
== Image Rotation ==
  
Declaration of all used pictures in your environment, project or header file is better than direct use of file names in your code.
+
Rotate included images by 90°, 180°, or 270° using the <tt>orientation</tt> key. For example:
  
<texcode>
+
<texcode>\externalfigure[logo.pdf][orientation=90]</texcode>
\useexternalfigure[reference name][file name][options]
+
 
</texcode>
+
To rotate by an arbitrary angle, use the {{cmd|rotate}} command. For example:
 +
 
 +
<texcode>\rotate[rotation=45]{\externalfigure[logo.pdf]}</texcode>
 +
 
 +
== Image Mirroring ==
 +
 
 +
To mirror (flip) an image, use the generic {{cmd|mirror}} command. For example, to mirror horizontally:
 +
 
 +
<texcode>\mirror{\externalfigure[logo.pdf]}</texcode>
 +
 
 +
To mirror vertically, first rotate the image by 180° and then mirror it:
 +
 
 +
<texcode>\mirror{\externalfigure[logo.pdf][orientation=180]}</texcode>
 +
 
 +
== Image Clipping ==
 +
 
 +
Clip an image using the generic {{cmd|clip}} command. For example, to clip the original image to a <tt>1cm x 2cm</tt> rectangle at an offset of <tt>(3mm,5mm)</tt> from the top left corner:
 +
 
 +
<texcode>\clip[width=1cm, height=2cm, hoffset=3mm, voffset=5mm]
 +
    {\externalfigure[logo.pdf]}</texcode>
 +
 
 +
As another example, this cuts the image into a <tt>3x3</tt> pieces and then outputs the <tt>(2,2)</tt> piece:
 +
 
 +
<texcode>\clip[nx=3,ny=3,x=2,y=2]
 +
    {\externalfigure[logo.pdf]}</texcode>
 +
 
 +
= 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:
 +
 
 +
<texcode>\externalfigure[logo.pdf][frame=on]</texcode>
 +
 
 +
ConTeXt includes a Perl script <tt>pdftrimwhite</tt> that removes extra white space at the borders of a PDF file. To run this script:
 +
 
 +
<pre>mtxrun --script pdftrimwhite <em>[flags] input output</em></pre>
 +
 
 +
The most important flag is <tt>--offset=dimen</tt>, which keeps some extra space around the trimmed image.
 +
 
 +
Similar functionality is provided by another Perl script, <tt>pdfcrop</tt>, that is included in most TeX distributions.
 +
 
 +
== Diagnostic Tracking ==
 +
 
 +
To get diagnostic information about image inclusion, enable the tracker <tt>graphics.locating</tt> by editing the ConTeXt file and adding:
 +
 
 +
<texcode>\enabletrackers[graphics.locating]</texcode>
 +
 
 +
Alternatively, compile the ConTeXt file using:
 +
 
 +
<texcode>context --trackers=graphics.locating filename</texcode>
 +
 
 +
The tracker writes diagnostics to the console. Suppose we use <tt>\externalfigure[somefile.pdf]</tt> and ConTeXt finds the file in the current search path; then the following information is printed on the console:
 +
 
 +
<pre>graphics &gt; inclusion &gt; locations: local,global
 +
graphics &gt; inclusion &gt; path list: . .. ../..
 +
graphics &gt; inclusion &gt; strategy: forced format pdf
 +
graphics &gt; inclusion &gt; found: somefile.pdf -&gt; somefile.pdf
 +
graphics &gt; inclusion &gt; format natively supported by backend: pdf</pre>
 +
If the file <tt>somefile.pdf</tt> is not found in the current search path, then the following information is printed on the console (even if the <tt>graphics.locating</tt> tracker is not set):
 +
 
 +
<pre>graphics &gt; inclusion &gt; strategy: forced format pdf
 +
graphics &gt; inclusion &gt; not found: somefile.pdf
 +
graphics &gt; inclusion &gt; not found: ./somefile.pdf
 +
graphics &gt; inclusion &gt; not found: ../somefile.pdf
 +
graphics &gt; inclusion &gt; not found: ../../somefile.pdf
 +
graphics &gt; inclusion &gt; not found: images/somefile.pdf
 +
graphics &gt; inclusion &gt; not found: /home/user/images/somefile.pdf
 +
graphics &gt; inclusion &gt; format not supported: </pre>
 +
and a placeholder gray box is put in the output:
 +
 
 +
<context> </context>
 +
 
 +
Sometimes, one would rather use a placeholder image for an image that is yet to be made. In such cases, load the MP library <tt>dum</tt> via:
 +
 
 +
<texcode>\useMPlibrary[dum]</texcode>
 +
 
 +
Then, whenever an image file is not found in the current search path, a random MetaPost image is shown in the output.
 +
 
 +
<context> </context>
 +
 
 +
== Leading Paragraph ==
 +
 
 +
Using {{cmd|externalfigure}}<tt>[...]</tt> at the beginning of a paragraph results in a line break after the image. This is because {{cmd|externalfigure}} is a {{cmd|vbox}} and when a {{cmd|vbox}} is encountered at (what appears to be) the beginning of a paragraph, it remains in vertical mode. To prevent this, add {{cmd|dontleavehmode}} before {{cmd|externalfigure}}, like this:
 +
 
 +
<texcode>\dontleavehmode
 +
\externalfigure[...] ... first line ...</texcode>
 +
 
 +
= 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 {{cmd|defineexternalfigure}} macro, which defines a named collection of image settings. For example, to define a collection where the image width is <tt>3cm</tt>, use:
 +
 
 +
<texcode>\defineexternalfigure[logo-settings]
 +
                    [width=3cm]</texcode>
 +
 
 +
And then to use these settings in an image, use:
 +
 
 +
<texcode>\externalfigure[group.pdf][logo-settings]</texcode>
 +
 
 +
or, if you want to add or override settings, use:
 +
 
 +
<texcode>\externalfigure[group.pdf][logo-settings]
 +
              [height=2cm]</texcode>
 +
 
 +
== 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 {{cmd|useexternalfigure}}, which defines a symbolic label for inserting an image plus settings. For example:
 +
 
 +
<texcode>\useexternalfigure[mylogo]
 +
                  [logo.pdf][width=2cm]</texcode>
  
Same options as above. You can even inherit them like this:
+
defines an image label <tt>mylogo</tt> that maps to the image file <tt>logo.pdf</tt> and sets its width to <tt>2cm</tt>. This image label may be used as a normal image filename:
  
<texcode>
+
<texcode>\externalfigure[mylogo]</texcode>
\useexternalfigure[dummy][nofile][width=\textwidth]
 
\useexternalfigure[myone][my_pic_one][dummy]
 
\useexternalfigure[mytwo][my_pic_two][dummy]
 
  
Somewhere in your text there's \externalfigure[myone].
 
</texcode>
 
  
'''Pictures as [[Floating Objects]]'''
+
= [[Floating Objects]] =
  
Floats are numbered and placed by ConTeXt, and they can get a caption.
+
Floats are numbered and placed by ConTeXt, with a configurable (and optional) caption.
  
 
<texcode>
 
<texcode>
\placeexternalfigure[place]{My Caption}{\externalfigure[myone]}
+
\placefigure[place]{My Caption}{\externalfigure[myone]}
 
</texcode>
 
</texcode>
 
<tt>place</tt> is one of:
 
<tt>place</tt> is one of:
Line 62: Line 334:
  
 
You get ''no'' caption with <tt>none</tt>.
 
You get ''no'' caption with <tt>none</tt>.
You get a table of figures with <cmd>showexternalfigures[alternative=a]</cmd>. Alternatives (styles) a, b and c are predefined.
+
You get a table of figures with {{cmd|showexternalfigures}}<code>[alternative=a]</code>. Alternatives (styles) a, b and c are predefined.
  
The second brackets of <cmd>placeexternalfigure</cmd> can keep any command, e.g. <cmd>getbuffer</cmd>.
+
The second brackets of {{cmd|placefigure}} can contain any command, e.g. {{cmd|getbuffer}}.
  
 
You find more about floats ([[Floating Objects]]) in the manual.
 
You find more about floats ([[Floating Objects]]) in the manual.
 +
= Movies =
  
'''Movies'''
+
Movies aren't recognized automatically yet; they must be delcared verbosely:
  
Movies aren't recognized automatically yet, you need a more verbose declaration:
 
 
<texcode>
 
<texcode>
 
\externalfigure[demo.mov][label=demo,width=4cm,height=4cm,preview=yes]
 
\externalfigure[demo.mov][label=demo,width=4cm,height=4cm,preview=yes]
 
</texcode>
 
</texcode>
 +
 
<tt>preview=yes</tt> shows the first image as preview.
 
<tt>preview=yes</tt> shows the first image as preview.
 
You find more about interactive Elements in [http://www.pragma-ade.com/general/manuals/mwidget-s.pdf mwidget-s.pdf]
 
You find more about interactive Elements in [http://www.pragma-ade.com/general/manuals/mwidget-s.pdf 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:
 +
 +
<texcode>
 +
\defineprogram[dummy.mpeg][dummy.mpeg.sh]
 +
 +
\starttext
 +
\goto{\externalfigure[dummy-preview][width=0.48\textwidth]}[program(dummy.mpeg{})]
 +
\stoptext
 +
</texcode>
 +
 +
The script dummy.mpeg.sh should contain:
 +
<pre><nowiki>
 +
FILE=$(basename "$0"); mplayer -fs -zoom ${FILE/.sh/}
 +
</nowiki></pre>
 +
 +
= See Also =
 +
 +
* [[Animation]]
 +
* [[Example photo page layout]]
 +
* [[Graphical_text_manipulation|scaling, rotating, mirroring, clipping]]
 +
 +
[[Category:Graphics]]
 +
 +
 +
{{Getting started navbox}}

Revision as of 00:35, 25 June 2018

< Graphics | Combinations | Supported Media File Formats | Including pages from PDF documents | Animation | Downsampling >

Usage

The simplest way to insert an image is to use:

\externalfigure[logo.pdf]

This command places the PDF image logo.pdf in a \vbox; the width and height of the image are equal to the natural dimensions of the image.

To set the width of the image to a specific size, say 1cm, use:

\externalfigure[logo.pdf][width=1cm]

Similarly, to set the height of the image to a specific size, say 2cm, use:

\externalfigure[logo.pdf][height=2cm]

If only the width or height of the image is specified, the other dimension is scaled appropriately to keep the aspect ratio.

To include a specific page, say page 5, of a multi-page PDF file, use:

\externalfigure[logo.pdf][page=5]

These four variations cover the most common use cases.

File Formats

ConTeXt supports the image formats enumerated below. The image format is determined from the file extension (case insensitive).

  • PDF: File extension .pdf
  • MPS (MetaPost output): File extension .mps or .<digits>
  • JPEG: File extension .jpg or .jpeg
  • PNG: File extension .png
  • JPEG 2000: File extension .jp2
  • JBIG or JBIG2: File extension .jbig, .jbig2, or .jb2

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:

\setupexternalfigures[directory={images, /home/user/images}]

Note: always use forward slashes (/) in path names, regardless of operating system.

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 entire TEXMF directory to the search path, including the doc/ 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

The \externalfigure command supports reading files from web servers, for example:

\externalfigure[http://tug.org/images/logobw.jpg]

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:

\ctxlua{http = require("socket.http"); http.PROXY = "http://proxy.example.com:3128"}

Replace http://proxy.example.com:3128 with the proxy URL.

To disable the proxy again:

\ctxlua{http = require("socket.http"); http.PROXY = nil}

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:

local function runcurl(name,cachename) -- we use sockets instead or the curl library when possible
    local command = "curl --silent --create-dirs --output " .. cachename .. " " .. name
    os.spawn(command)
end

Insert the -k or --insecure option:

    local command = "curl --insecure --silent --create-dirs --output " .. cachename .. " " .. name

Inline Images

Embedding inline images via base64 encoding using the memstream function (supports pdf and png streams):

\startluacode
local cow = mime.unb64("JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nJWayY40SW6E7/UUcRaQIafvfp6bAAkY9SM0oHXmMBAgvb7sM0ZmVf86CYNB/+HpC500Ghevv13ljqvwv+e/v//16+//eV3/+l9f8y4nRlz/8xVx99XW9Y9fcf2D/v8f+v+/a9q5/vSb/vnbn/7p629a+oq79L639jpjRKzcmC1fsUe927xaO3fRL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UxNTEyMjA1OSkKL01vZERhdGUoRDoyMDA2MDUxNTEyMjA1OSkKL0NyZWF0b3IoQ29yZWxEUkFXISkKL1RpdGxlKENPVy5FUFMpPj5lbmRvYmoKeHJlZgowIDEyCjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwNDYzNiAwMDAwMCBuIAowMDAwMDA0OTg2IDAwMDAwIG4gCjAwMDAwMDQ1NzcgMDAwMDAgbiAKMDAwMDAwNDQyNiAwMDAwMCBuIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDQ0MDYgMDAwMDAgbiAKMDAwMDAwNDY4NCAwMDAwMCBuIAowMDAwMDA0ODM4IDAwMDAwIG4gCjAwMDAwMDQ3MjUgMDAwMDAgbiAKMDAwMDAwNDc3OCAwMDAwMCBuIAowMDAwMDA0ODA4IDAwMDAwIG4gCnRyYWlsZXIKPDwgL1NpemUgMTIgL1Jvb3QgMSAwIFIgL0luZm8gMiAwIFIKL0lEIFs8ODE0NjQ5NTc3Qzg3MTE1QzRDQzBDQjg0QkU3RTQ1MEY+PDgxNDY0OTU3N0M4NzExNUM0Q0MwQ0I4NEJFN0U0NTBGPl0KPj4Kc3RhcnR4cmVmCjUxMzMKJSVFT0YK")
figures.setmemstream("inline",cow)
context.externalfigure({"memstream:///inline"})
\stopluacode

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:

\externalfigure[logo.pdf][xscale=500]
\externalfigure[logo.pdf][yscale=500]

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:

\clip[width=1cm, height=2cm, hoffset=3mm, voffset=5mm]
     {\externalfigure[logo.pdf]}

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:

graphics > inclusion > locations: local,global
graphics > inclusion > path list: . .. ../..
graphics > inclusion > strategy: forced format pdf
graphics > inclusion > found: somefile.pdf -> somefile.pdf
graphics > inclusion > format natively supported by backend: pdf

If the file somefile.pdf is not found in the current search path, then the following information is printed on the console (even if the graphics.locating tracker is not set):

graphics > inclusion > strategy: forced format pdf
graphics > inclusion > not found: somefile.pdf
graphics > inclusion > not found: ./somefile.pdf
graphics > inclusion > not found: ../somefile.pdf
graphics > inclusion > not found: ../../somefile.pdf
graphics > inclusion > not found: images/somefile.pdf
graphics > inclusion > not found: /home/user/images/somefile.pdf
graphics > inclusion > format not supported: 

and a placeholder gray box is put in the output:

Sometimes, one would rather use a placeholder image for an image that is yet to be made. In such cases, load the MP library dum via:

\useMPlibrary[dum]

Then, whenever an image file is not found in the current search path, a random MetaPost image is shown in the output.

Leading Paragraph

Using \externalfigure[...] at the beginning of a paragraph results in a line break after the image. This is because \externalfigure is a \vbox and when a \vbox is encountered at (what appears to be) the beginning of a paragraph, it remains in vertical mode. To prevent this, add \dontleavehmode before \externalfigure, like this:

\dontleavehmode
\externalfigure[...] ... first line ...

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:

\externalfigure[mylogo]


Floating Objects

Floats are numbered and placed by ConTeXt, with a configurable (and optional) caption.

\placefigure[place]{My Caption}{\externalfigure[myone]}

place is one of: left, right, here, top, bottom, inleft, inright, inmargin, margin, page, opposite, always, force, tall

You get no caption with none. You get a table of figures with \showexternalfigures[alternative=a]. Alternatives (styles) a, b and c are predefined.

The second brackets of \placefigure can contain any command, e.g. \getbuffer.

You find more about floats (Floating Objects) in the manual.

Movies

Movies aren't recognized automatically yet; they must be delcared verbosely:

\externalfigure[demo.mov][label=demo,width=4cm,height=4cm,preview=yes]

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:

\defineprogram[dummy.mpeg][dummy.mpeg.sh]

\starttext
\goto{\externalfigure[dummy-preview][width=0.48\textwidth]}[program(dummy.mpeg{})]
\stoptext

The script dummy.mpeg.sh should contain:

FILE=$(basename "$0"); mplayer -fs -zoom ${FILE/.sh/}

See Also


Template:Getting started navbox