TransWikia.com

Unicode emoticons with pdflatex

TeX - LaTeX Asked on January 5, 2022

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?

3 Answers

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!

Answered by Christoph Thiede on January 5, 2022

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}
newcommanddoEmoji[3]{%    character, macroname, page
  begin{my}#1end{my}}
include{do-emoji}
end{document}

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

newcommandincludeEmoji[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.28fontcharhtfont`A]{emoji-from-list}}}}}

{newcommanddoEmoji[3]{%   character, macroname, page
    expandafterxdefcsname #2endcsname{noexpandincludeEmoji{#3}}%
    edefnext{noexpandincludeEmoji{#3}}%         % would need more expansion in the next row otherwise
    expandafterexpandafterexpandafterGnewunicodecharexpandafterexpandafterexpandafter{expandafter#1expandafter}%
       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:

    defUNItoNUMz{%        Actually, do not need hex in what follows!
      edefOUT{thecount0}%
    }
    defUNItoNUMcont#1#2{%
      multiplycount0 64relax
      advancecount0 -"80relax
      advancecount0 `#2relax
      #1}
    
    defUNItoNUM#1{%       No attempt is made to detect out-of-range bytes
      count0=`#1%
      ifnum`#1<"C0relax   % In the range a0..bf would give false positives
        letnext=UNItoNUMz
      else
        ifnum`#1<"E0relax
          advancecount0 -"C0relax
          defnext{UNItoNUMcontUNItoNUMz}
        else
          ifnum`#1<"F0relax
            advancecount0 -"E0relax
            defnext{UNItoNUMcont{UNItoNUMcontUNItoNUMz}}
          else
            ifnum`#1<"F8relax
              advancecount0 -"F0relax
              defnext{UNItoNUMcont{UNItoNUMcont{UNItoNUMcontUNItoNUMz}}}
            fi
          fi
        fi
      fi
      next}
    
    newcommandGnewunicodechar[2]{{%   (Since we need global for foreach, localize changes anyway.)  Specific for utf8x
      defgdefUNI##1##2{%
        expandafterletcsname uc@temp@aendcsnameglobalcsname uni@declcharoptendcsname{##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.

Answered by Ilya Zakharevich on January 5, 2022

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=fontcharhtfont`A]{smilie}

Answered by egreg on January 5, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP