14

Following this answer, I tried to use Unicode emoticons with pdflatex like so:

{\fontfamily{DejaVuSans-TLF}\selectfont 😎}

The font is selected but the emoticon does not show.

There are some TTF fonts which support the characters, such as Symbola, but apparently getting TTF fonts to work with pdflatex is a pain in the butt.

Is there a way out?

4
  • 1
    Fonts in pdflatex have only up to 256 characters.
    – egreg
    Nov 14, 2012 at 22:04
  • 1
    @egreg Is that a "no way, you have to use (XeLa|Lua)Tex"? I guess using actual images or drawing them with TikZ (access via newunicodechar) would be a way out, if a tedious one.
    – Raphael
    Nov 14, 2012 at 22:09
  • 1
    You could use otftotfm to create a font with the desired glyphs from DejaVu, but it would be very time consuming. Probably getting them as pictures (maybe PDF files built with standalone via XeLaTeX) and then using \newunicodechar to access them.
    – egreg
    Nov 14, 2012 at 22:13
  • You can use the coloremoji package, which will insert the images from Apple’s fonts.
    – Davislor
    Jul 25, 2020 at 1:43

3 Answers 3

18

Prepare the following file

% smilie.tex
\documentclass[preview]{standalone}
\usepackage{fontspec}
\setmainfont{DejaVu Sans}
\begin{document}
😎
\end{document}

and compile it with XeLaTeX. Then you can use the glyph via the so built PDF file:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{newunicodechar}
\usepackage{graphicx}
\newunicodechar{😎}{\includegraphics{smilie}}

\begin{document}
Here is a 😎.
\end{document}

enter image description here

Probably some tweaking with the borders in the standalone file is necessary.

You can get the height of an uppercase letter in the current font by saying

\includegraphics[height=\fontcharht\font`A]{smilie}
5
  • 4
    Sweet. Feels like playing Jenga with a sledgehammer, though. ;)
    – Raphael
    Nov 14, 2012 at 22:40
  • On second thought, this approach is mechanical enough to make a script of it. Take list of symbols, create a pdf for each of them and then create an sty defining all the newunicodechar mappings. One minor problem remains: included PDFs don't scale with the font size. Can this be added?
    – Raphael
    Nov 16, 2012 at 17:24
  • 1
    @Raphael I've added a way to scale the symbol
    – egreg
    Nov 16, 2012 at 18:16
  • Can you have \includegraphics adjust color and background-color of the symbol? Jan 18 at 23:55
  • 1
    @UlrichDiez I don't think so.
    – egreg
    Jan 18 at 23:56
10

Here is my solution which is partially based on another post from tex.sx I am afraid I cannot find again:

In a nutshell: Download SVG from a database like twemoji (used by Twitter), convert it and scale it appropriately. Pros: Looks exactly like on Twitter or any other platform you desire. Does not require XeLaTeX. Con: Does not work completely automated.

We make an example of the proposed process by inserting the 🦄 emoji into a TeX document.

  1. Find the unicode of your emoji

    Using Google Fu, we find out that the code point for our emoji is U+1F984.

  2. Download an SVG file of the emoji

    Be sure to use one consistent data source for all of your tweets. We are referring to twemoji for doing this.

    Download the SVG file from twemoji by adjusting this link: https://raw.githubusercontent.com/twitter/twemoji/master/assets/svg/1f984.svg

  3. Convert the SVG file into an EPS file

    There are certain packages for doing so such as includesvg, but I don't want to install inkscape on my machine, so I use an online converter:

    Go to https://convertio.co/de/svg-eps/ and insert the URL from step 2. Download the converted file into your LaTeX figures folder.

  4. Typeset the emoji in LaTeX

    This is how I could typeset emojis the best way in LaTeX:

    \usepackage{textcomp}  % Required for encoding \textbigcircle
    \usepackage{scalerel}  % Required for emoji \scalerel
    
    \def\🦄{\scalerel*{\includegraphics{figures/1f984.eps}}{\textrm{\textbigcircle}}}
    

    To use it, just write something like:

    I like \🦄s, and what's about you?
    

And here is MWE for it: https://overleaf.com/read/tjngsyvrfcmy

PS: I always like to hear your simpler ideas!

1
  • 3
    Using eps files causes trouble for me, but this also works with converting the svg to a pdf, e.g. with inkscape 1f680.svg --export-pdf=1f680.pdf.
    – kuropan
    Mar 23, 2021 at 14:53
2

I use a setup with 3 files: the list of macros/symbols in UTF8 do-emoji.tex:

% Names below are arbitrary; a macro with this name will be defined.
% After editing the list in the next row, (re)run:    xelatex emoji-from-list
\foreach [count=\P] \M/\C in {smilie/😎,ghost/👻,pumpkin/🎃}
   {\doEmoji{\C}{\M}{\P}}

(I think one should avoid whitespace in the list in the first row.) Then the rendering file emoji-from-list.tex:

\documentclass[multi=my,crop]{standalone}
\usepackage{pgffor}
\usepackage{fontspec}
\setmainfont{Symbola}
\begin{document}
\newcommand\doEmoji[3]{%    character, macroname, page
  \begin{my}#1\end{my}}
\include{do-emoji}
\end{document}

(process with xelatex emoji-from-list). Finally, the actual LaTeX file includes:

\newcommand\includeEmoji[1]{%   With CM fonts and 1.28, it ascends to the top of the capitals, and descends to the bottom of comma.
  \ensuremath{\vcenter{\hbox{\includegraphics[page=#1,height=1.28\fontcharht\font`A]{emoji-from-list}}}}}

{\newcommand\doEmoji[3]{%   character, macroname, page
    \expandafter\xdef\csname #2\endcsname{\noexpand\includeEmoji{#3}}%
    \edef\next{\noexpand\includeEmoji{#3}}%         % would need more expansion in the next row otherwise
    \expandafter\expandafter\expandafter\Gnewunicodechar\expandafter\expandafter\expandafter{\expandafter#1\expandafter}%
       \expandafter{\next}% % (#1=\C)
 }%  For a preamble common for several docs: do nothing if file is not present:
 \IfFileExists{do-emoji.tex}{\input{do-emoji}}{}}

After this, either use a macro (named in do-emoji.tex), or just use the UTF-8 character. (If you do not need macro names, give the same fake name to all of the characters in the list.) It is easy to auto-generate the file do-emoji.tex.

NOTES:

  1. I optimize for usage in math; so I use larger emoji than in other answers, and they are \vcenter⸣ed.

  2. Above, a macro \Gnewunicodechar is used. If you do not plan to use these Unicode chars in your document, just remove the last two lines inside the definition of \doEmoji above.

  3. I do not know about utf8, but for utf8x, \Gnewunicodechar may be defined as this:

    \def\UNItoNUMz{%        Actually, do not need hex in what follows!
      \edef\OUT{\the\count0}%
    }
    \def\UNItoNUMcont#1#2{%
      \multiply\count0 64\relax
      \advance\count0 -"80\relax
      \advance\count0 `#2\relax
      #1}
    
    \def\UNItoNUM#1{%       No attempt is made to detect out-of-range bytes
      \count0=`#1%
      \ifnum`#1<"C0\relax   % In the range a0..bf would give false positives
        \let\next=\UNItoNUMz
      \else
        \ifnum`#1<"E0\relax
          \advance\count0 -"C0\relax
          \def\next{\UNItoNUMcont\UNItoNUMz}
        \else
          \ifnum`#1<"F0\relax
            \advance\count0 -"E0\relax
            \def\next{\UNItoNUMcont{\UNItoNUMcont\UNItoNUMz}}
          \else
            \ifnum`#1<"F8\relax
              \advance\count0 -"F0\relax
              \def\next{\UNItoNUMcont{\UNItoNUMcont{\UNItoNUMcont\UNItoNUMz}}}
            \fi
          \fi
        \fi
      \fi
      \next}
    
    \newcommand\Gnewunicodechar[2]{{%   (Since we need global for \foreach, localize changes anyway.)  Specific for utf8x
      \def\gdefUNI##1##2{%
        \expandafter\let\csname uc@temp@a\endcsname\global\csname uni@declcharopt\endcsname{##1}{document}{##2}}%
      \UNItoNUM#1%                  % (#1=\C; sets \OUT); This is for utf8x; in utf8, may need something else
      \gdefUNI{\OUT}{#2}%       % finish definition
    }}
    
  4. Update: I changed \include to (the most robust combination of) \input and \IfFileExists. The reason: \include may create a spurious pagebreak. One should also consider the variant in the first comment below.

1
  • With amsmath, one can change the last line of definition of \includeEmoji to \text{$\mathsurround0pt\vcenter{\hbox{\includegraphics[page=#1,height=1.28\fontcharht\font``A]{emoji-from-list}}}$}} (replace double-backtick to single one). Then Unicode chars work fine in sub/superscripts too. Nov 4, 2017 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.