Using Emoji in (pdf)LaTex

How to use Emoji fonts in LaTeX without resorting to XeLaTeX or LuaLaTeX

I recently wanted to use Emoji in a paper, but I did not want to force my contributors into using LuaLaTeX or XeLaTeX (in which you can just use fancy fonts). So as a work around, I set up a small helper document that helps me set up emoji as (vector) images instead, which I can then include using \includegraphics{}.

To set this up, I create a folder called emoji and create the a file called generator.tex with the following contents:

\documentclass[crop]{standalone}

\usepackage{emoji}
\usepackage{fontspec}

\directlua{luaotfload.add_fallback
   ("emojifallback",
    {
        "Apple Color Emoji:mode=harf;",
        "Noto Color Emoji:mode=harf;",
    })}

\setmainfont{Latin Modern Roman}[
  RawFeature={fallback=emojifallback}
]

\begin{document}
\emoji{\theemoji}
\end{document}
Note that this file intentionally won’t compile: we’ll use a trick to define variable \theemoji later.

We also create a Makefile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
all: mag.pdf fog.pdf prohibited.pdf unicorn.pdf detective.pdf

%.pdf: generator.tex
	lualatex -jobname='$(subst .pdf,,$@)' \
		'\def\theemoji{$(subst .pdf,,$@)}\input{generator.tex}'


.PHONY: clean
clean:
	$(RM) *.aux *-blx.bib *.bbl *.blg *.fdb_latexmk *.fls *.log *.upa *.run.xml

.PHONY: dist-clean
dist-clean: clean
	$(RM) *.pdf

Now, if we run make in this folder, you should see that the files mag.pdf (🔍), fog.pdf (🌫️), prohibited.pdf (🚫), unicorn.pdf (🦄), and detective.pdf (🕵️) get created. This is set by the all target on line 1 above: you can add emoji shortcodes to this list and they will automatically be created. Refer to the docs for the emoji package for a full list of supported shortcodes.

The magic happens in lines 3-5 of the above listing: we define the variable \theemoji based on the target file name, and use LuaLaTeX’s ability to pass source code directly to the engine to define this variable before including generator.tex.

File generator.tex itself is simply a standalone class file so that it automatically crops the document to the size of its contents. We set up the emoji font using fontspec’s more advanced features.

Note that this script still requires LuaLaTeX though! We avoid making our fellow contributors angry by just running the script on their behalf, and we commit the generated emoji PDF files to our git repository.

Note that you can exempt files from being ignored from git by using exemption rules in .gitignore files as follows:

!*.pdf

You can just create a .gitignore file in the emoji folder to only apply to that folder.

Please note that I have not tested this on either Linux or Windows machines yet; it likely works on Linux, but Windows might require a different font name in generator.tex.

Your results will also look different based on what fonts you set.

Thom Wiggers
Thom Wiggers
Senior Cryptography Researcher

My research interests include (post-quantum) cryptography and protocols