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?

David Carlisle
  • 757,742
tralston
  • 321
  • 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
  • @DavidCarlisle You are a genius!!!! That totally worked. Now that the real problem is fixed, I'm still curious if the question I asked has an answer also :) – tralston May 28 '16 at 20:22
  • (I left a comment on github) – David Carlisle May 28 '16 at 20:22
  • The package is not very well written, as far as I can see. It doesn't declare itself as a package, it uses document commands rather than package ones. (And, of course, the spaces.) – cfr May 28 '16 at 20:22
  • @cfr Yes, will it's a work in progress, and open-source means helping others get it right. I'll fork it and update the code with these suggestions. I do thank the author of the package for getting the ball rolling with the code. – tralston May 28 '16 at 20:24
  • Off-topic: don't break lines with \\. – cfr May 28 '16 at 20:25
  • @tralston yes see http://tex.stackexchange.com/questions/88991/what-do-different-fontdimennum-mean/88993#88993 – David Carlisle May 28 '16 at 20:25
  • 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
  • Is there any reason for this to be billed as XeLaTeX-specific? It is just including pictures. It needs \RequirePackage{graphicx}, too. – cfr May 28 '16 at 20:26
  • @cfr it ought to be tagged as pdftex specific, really, not that suitable for xetex. – David Carlisle May 28 '16 at 20:27
  • @DavidCarlisle Indeed. Though it would work with Xe/LuaTeX, at least. But I did more mean: this seems an approach which is specifically not XeTeX-specific. As you say, it isn't an especially good approach there. (Though one might want to use it to ensure compatibility with multiple engines, of course.) – cfr May 28 '16 at 20:30
  • Feel free to edit the question. I was using xetex, so I included it, as most users on this site want to know what the setup is. – tralston May 28 '16 at 20:32
  • yes but the whole point of `(new)uniocdechar is to define tex code emulating unicode in pdftex that has no native unicode support, it's odd, verging on wrong, to use it in a native unicode system like xetex – David Carlisle May 28 '16 at 20:41
  • @DavidCarlisle Only a few fonts have real emojis; the Apple Emoji font actually uses images and this package seems to want to provide them. XeTeX is unable to use them otherwise. – egreg May 28 '16 at 20:48
  • @egreg when I looked at the package source in my browser I saw emoji in all the \newunicodechar so I have at least one font with them (Segoe UI I think) – David Carlisle May 28 '16 at 20:51
  • @DavidCarlisle Browsers do tricks with Emojis, which XeTeX is unable to. – egreg May 28 '16 at 20:53
  • @egreg yes colour in fonts generally isn't standardised yet I think, but the basic glyphs work OK. – David Carlisle May 28 '16 at 21:11

1 Answers1

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}
David Carlisle
  • 757,742
  • 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