How to get certain Unicode characters appearing in LaTeX/PDF output?

1,514 views
Skip to first unread message

Vinay Sajip

unread,
Mar 27, 2020, 11:44:51 AM3/27/20
to sphinx-users
I have some examples in my documentation of Unicode text characters \u2603 and \U0001F602. These appear fine in the HTML documentation, but I'm having trouble getting them to appear in PDF output generated using the latexpdf builder. I've set the utf8extra key in latex_elements to define the characters (using DeclareUnicodeCharacter) but nothing appears in the final PDF output for the characters.

I'm using Sphinx 1.6.7 and the build completes with warnings but no errors (on Sphinx 2.x, the LateX build fails, but I haven't had time to shave that particular yak). The .tex file just contains the utf-8 encoded characters.

What do I need to do to get arbitrary Unicode characters (in this case, they happen to be emojis) to appear in LaTeX/PDF output?

Thanks and regards,

Vinay Sajip

Ruben Di Battista

unread,
Mar 27, 2020, 12:28:02 PM3/27/20
to sphinx...@googlegroups.com
Could you send us the latex_elements definition?

I think you should put \usepackage[utf8]{inputenc} in the LaTex preamble.


--
You received this message because you are subscribed to the Google Groups "sphinx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/687ee7f4-230c-40f4-af2e-1f674a27a6c9%40googlegroups.com.

jfbu

unread,
Mar 27, 2020, 7:08:36 PM3/27/20
to sphinx...@googlegroups.com
Hi, you need suitable font. If leaving latex_engine to its default "pdflatex", you typically will need your \DeclareUnicodeCharacter to do a suitable \selectfont and also select the correct slot among 255 in the font, and you probably need some font package loaded in the preamble which provides the suitable glyph.

If using "xelatex" as latex_engine, the document font (by default GNU FreeFont) perhaps already has a glyph for that Unicode slot and you have nothing to do. Perhaps try with DejaVu as other font. Else a similar procedure can be done with \usepackage{newunicodechar}, using some \newfontfamily (fontspec documentation), and then \newunicodechar here also with some \selectfont.

Notice that a recent LaTeX distribution will incorporate the noto-emoji font package.
However if I understand correctly xetex does not support color emojis (I am not sure about this, finding fonts from xelatex/lualatex with fontspec always has quirks, and maybe something is wrong in my locale set-up), but luahbtex does.

See https://stackoverflow.com/questions/190145/how-to-insert-emoticons-in-latex/57076064#57076064

With soon available TeXLive2020, luatex will be luahbtex, i.e. use the HarfBuzz library.

I tested this at home

\documentclass{article}
\usepackage{fontspec}
\newfontfamily\NotoEmoji{NotoColorEmoji.ttf}[RawFeature={mode=harf}]
\usepackage{newunicodechar}
\newunicodechar{☃}{{\NotoEmoji ☃}}
\begin{document}
This ☃ that
\end{document}
% Local Variables:
% TeX-engine: lualatex
% End:

and it works. (with pretest of TeXLive 2020).

The whole point of the \newunicodechar, is that as far as I know, that is the only more or less simple way to let a Unicode codepoint like ☃ trigger usage of appropriate font.

These are the additional elements to add to the latex preamble.
i.e.
\newfontfamily\NotoEmoji{NotoColorEmoji.ttf}[RawFeature={mode=harf}]
\usepackage{newunicodechar}
\newunicodechar{☃}{{\NotoEmoji ☃}}

This method is a bit lying ahead in future. If you use a LaTeX distribution not from future but from very recent past, then you need luahblatex or lualatex-dev.

See also

https://www.ctan.org/pkg/coloremoji

but this uses an image for each emoji, so they can't be copied-pasted as Unicode from PDF.

good luck
Jean-François


Vinay Sajip

unread,
Mar 29, 2020, 2:29:39 AM3/29/20
to sphinx-users
Dear Jean-François,

Thanks for the detailed and very helpful suggestions! I will have a go.

Regards,

Vinay
Reply all
Reply to author
Forward
0 new messages