# Using sprite sheets with LaTeX

Sprite sheet is the name of a big image containing several smaller images or icons. It’s a technique usually employed by webdesigners to reduce the number of requests the browser makes to the server – reducing the number of HTTP requests can make a Web page load much faster. It’s also used a lot in animation engines.

I was wondering if I could use a sprite sheet in a LaTeX document, so I got a sample image from the Tango Desktop Project and mapped it:

All icons are placed in a big image, like a matrix, and the size is fixed at 24px × 24px per icon. How can I select one of the icons from a LaTeX document? Thanks to our friend and moderator Martin Scharrer, including sprite sheets is a breeze with adjustbox!

This amazing package, amongst other features, allows us to adjust images with \adjustimage. We can also use \includegraphics with the clip option which we can use for mapping our sprite sheet. For instance, let’s include the warning sign in our document. We need to collect some data first:

We have all the parameters for including the warning sign. It’s now a simple call to \includegraphics[clip,trim=48px 96px 120px 48px]{sampletango.png} or \adjustimage{Clip=48px 96px 120px 48px}{sampletango.png} and the correct icon is included! The parameters to the trim/Clip option are:

• 2 icons from left to right: 2 × 24px = 48px
• 4 icons from bottom to top: 4 × 24 = 96px
• 5 icons from right to left: 5 × 24 = 120px
• 2 icons from top to bottom: 2 × 24px = 48px

The full code is as follows:

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\begin{figure}
\centering
% using includegraphics
% \includegraphics[clip,trim=48px 96px 120px 48px]{sampletango.png}
\caption{A warning sign.}
\end{figure}

\end{document}
The output:

There we go, sprite sheets with LaTeX. :)

Filed under LaTeX

• Ilmari Karonen says:

I suppose the obvious question is, does this actually accomplish anything in the LaTeX context? That is, does it actually result in smaller (or otherwise better) output files than just using a bunch of small images?

I suppose it’s possible, if every step in the rendering pipeline is smart enough to store only one copy of the sheet and to display it with different clipping shapes. On the other hand, if any step is dumb enough to store a separate copy of the whole sheet for each use of it, that could seriously bloat the output.

Alas, I just tested it, and at least the combination of \includegraphics and pdflatex seems to be just that dumb — the PDF output contains several copies of the whole sprite sheet. :(

• Paulo Cereda says:

Yes, you are right. And it’s indeed sad to find out that the output contains several copies of the whole sprite sheet. :( The big image is there for every call, even if only a small bit of it is being displayed.

I was quite curious about sprite sheets and their use in LaTeX. Sadly, there seems not to have an advantage on using such approach. But I must confess, it was fun to write the code. :)

Update: Uh-oh, I’m terribly wrong. :) Martin found out that the image is reused. See his comment below.

• Martin says:

Sorry, but that’s not correct. pdflatex only includes the image once and references it. This can be also seen in the log output:

 <sampletango.png, id=1, 192.72pt x 168.63pt> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> <use sampletango.png> 

Here it doesn’t matter if you use \includegraphics or \adjustimage. The latter uses the internal macros of \includegraphics anyway.

• Alex says:

I found a use for this! (Though I didn’t find this blog post until after I rediscovered the technique.) The online latex service Overleaf has a limit on the number of files you can upload. I had a document with 8 figure pages of 6 graphs each (48 image files). Combined with the rest of the project, this put me at 76 source files, over the limit of 60. So I made my own sprite sheets, one per figure page. This reduces 48 images to 8, plus one shell script to generate the sprite sheets with Graphicsmagick, and I am way under the limit at 37 files in the project!