5

I'm typsetting emojis in xetex and I found this great library called xelatexemoji (on github). But when I use different fonts, the spacing around the included graphic changes. As I've seen in various responses, spacing changes with the font. I understand that. Here's a MWE:

\documentclass[12pt]{article}

\usepackage{graphicx}
\usepackage{fontspec}
\usepackage{xltxtra}
\usepackage{xelatexemoji}

\begin{document}
    \setlength{\parindent}{0cm}
    \textrm{Hello world😜😜😜😜}\\
    \setmainfont{Times New Roman}
    \textrm{Hello world😜😜😜😜}\\
\end{document}

Output

Bad-Spacing1

Notice how for the first font (default, Latin Modern I think), the spacing is fine, but when the font is changed, the spacing is wrong. (The spacing for the letters doesn't even look that different, but the included graphics spacing is!)

If you look at xelatexemoji.sty, they use \newunicodechar, which works fine, but the spacing around the inserted graphic needs to be adjusted. Snippet:

\usepackage{newunicodechar}
\usepackage{amsmath}

\providecommand{\xelatexemojipath}[1]{images/#1.pdf}

\newcommand{\xelatexemoji}[1]{
    \hspace{-1em}
    \raisebox{-0.15em}{
      \includegraphics[height=1em]{\xelatexemojipath{#1}}
    }
    \hspace{-1em}
}

...

\newunicodechar{😜}{\xelatexemoji{1f61c}}

I tried adjusting the \hspace in the command to account for this in the second font, but look what happens when I recompile:

Bad-spacing2

Is there a way to determine:

  1. What the currently assigned font is
  2. Query the properties of that font to find the relevant spacing value so that I can dynamically adjust for it by doing a bit of math in the command

I know that I could hardcode the \hspace values for each individual font I plan on using, but I'd like to make this robust. Any ideas?

  • 1
    The xelatexemoji package doesn't seem to be on the CTAN, and hence it isn't distributed with TeXLive. Is the package available online somewhere? – Mico May 28 '16 at 18:58
  • @Mico Updated question with link to github repo. – tralston May 28 '16 at 19:30
  • 2
    with xelatex why use images for the emoji rather than use a font with them in? – David Carlisle May 28 '16 at 20:15
  • 3
    you are missing % from the ends of all the lines of your definition you are inserting 6 inter word spaces for each command. – David Carlisle May 28 '16 at 20:17
  • 1
    @tralston but for xelatex you would be better to use a font, the image inclusion would work with pdftex. – David Carlisle May 28 '16 at 20:26
4

The spaces used for word space are available in the font domen parameters in classic tfm metrics and emulated as the same values in xetex when using system fonts, the

What do different \fontdimen<num> mean

so you want \fontdimen2\font however the only reason there is font specific spacing here is that the macro adds 6 interword spaces for each symbol annd guesses a correction of -2em to compensate removing the spurious spacing would fix the issue

\newcommand{\xelatexemoji}[1]{%%
   %%\hspace{-1em}%%
    \raisebox{-0.15em}{%%
      \includegraphics[height=1em]{\xelatexemojipath{#1}}%%
    }%%
    %%\hspace{-1em}%%
}

note however

\newunicodechar{😜}{\xelatexemoji{1f61c}}

is mainly intended to provide tex code such as image inclusion for pdftex which can not natively handle Unicode. With xetex it would be far more natural to use a font containing the emoji directly as text.

Here are the emoji as text (black and white though)

enter image description here

\documentclass[12pt]{article}


\usepackage{fontspec}


\begin{document}
    \setlength{\parindent}{0cm}
    \textrm{Hello world\fontspec{Segoe UI Emoji}😜😜😜😜}\\
    \setmainfont{Times New Roman}
    \textrm{Hello world\fontspec{Segoe UI Emoji}😜😜😜😜}\\
\end{document}
  • So you'd have to switch to the emoji font everytime you wanted to include an emoji? And then switch back to the normal font? That doesn't feel natural. – tralston May 28 '16 at 21:11
  • @tralston it's more natural (and efficient) than loading an image for each one, but as egreg comments above, if you want coloured emoji as text you may have to wait for implementations to catch up (I think I saw some trials in luatex) so the image version is still there if you need it:-) – David Carlisle May 28 '16 at 21:14

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.