9

I am trying to define new unicode chars for country flags. Unfortunately flags are encoded using two regional indicator symbols according to the ISO 3166-1 alpha-2 two-letter country codes. So 🇩🇪 for example is 🇩 🇪. (To make it visible here I just added a space between the two characters.)

The problem is, that both \DeclareUnicodeCharacter (pdfTeX) and \newunicodechar (XeTeX and LuaTeX) only accept one char. That's why

\documentclass{article}
\usepackage{newunicodechar}
\newunicodechar{🇩🇪}{\rule{1.3em}{1em}}
\begin{document}
🇩🇪
\end{document}

for example does not work. Can I trick TeX into thinking a combination of two chars is one char? Or are there any other ideas for a workaround?

  • you can't define a "Unicode char" (unless you are the Unicode consortium. Normally you would just use a TeX command \newcommand\DE{..} do you need to use a form with no backslash? (which is easy of you do not use capital letters for anything else, harder otherwise. – David Carlisle Feb 22 '16 at 7:38
  • @DavidCarlisle I'm working on a package to make Emoji One available in LaTeX. You can have a look at it here: github.com/benjamin-weiss/emojione-latex For every symbol I use \DeclareUnicodeCharacter or \newunicodechar to make it "directly" available, but I also define a macro. So the german flag for example is already accessible via \emojiflagde. But I think it would be confusing for the users if the flags are only available via a TeX command, but every other symbol works directly. – Benjamin Feb 22 '16 at 7:46
  • sorry I didn't spot that your DE were not just the ascii characters DE, If you enter 🇩🇪 directly then I suppose most natural would be for the font to ligature that to the flag, but if that is not the case you could make them active and define each one to look ahead. – David Carlisle Feb 22 '16 at 7:54
  • oh, it's not a font, rather a collection of images? – David Carlisle Feb 22 '16 at 8:01
  • @DavidCarlisle Yes it is a collection of images. But can I still define the regional indicator symbols as active and look ahead? What does look ahead exactly mean? – Benjamin Feb 22 '16 at 8:08
6

You can emulate what basically utf8 does:

\documentclass{article}
\usepackage{newunicodechar}
\usepackage{xparse}

\ExplSyntaxOn
\newunicodechar{🇩}{\flags_D:n}
\newunicodechar{🇺}{\flags_U:n}

\cs_new_protected:Nn \flags_D:n
 {
  \str_case:nnF { #1 }
   {
    {🇪}{Germany}
    {🇰}{Denmark}
   }
   {BAD}
 }
\cs_new_protected:Nn \flags_U:n
 {
  \str_case:nnF { #1 }
   {
    {🇰}{United~Kingdom}
    {🇸}{United~States}
   }
   {BAD}
 }
\ExplSyntaxOff

\begin{document}

Here is 🇩🇪

Here is 🇩🇰

Here is 🇺🇰

Here is 🇺🇸

\end{document}

enter image description here

A version that works also with pdflatex (weird errors are to be expected if regional indicator symbols not appear in pairs.

\documentclass{article}
\usepackage{ifxetex}
\ifxetex\else
  \usepackage[utf8]{inputenc}
\fi
\usepackage{newunicodechar}
\usepackage{xparse}

\ExplSyntaxOn
\newunicodechar{🇩}{ \flags_print:n {D} }
\newunicodechar{🇺}{ \flags_print:n {U} }

\bool_if:nTF { \sys_if_engine_luatex_p: || \sys_if_engine_xetex_p: }
 {
  \cs_new:Nn \flags_print:n
   {
    \flags_print_unicode:nn { #1 }
   }
 }
 {
  \cs_new:Nn \flags_print:n
   {
    \peek_charcode:NTF ^^f0
     {
      \flags_print_eightbit:nnnnn { #1 }
     }
     {
      BAD
     }
   }
 }

\cs_new_protected:Nn \flags_print_unicode:nn
 {
  \use:c { flags_#1:n } { #2 }
 }

\cs_new_protected:Nn \flags_print_eightbit:nnnnn
 {
  \use:c { flags_#1:n } { #2#3#4#5 }
 }

\cs_new_protected:Nn \flags_D:n
 {
  \str_case:nnF { #1 }
   {
    {🇪}{Germany}
    {🇰}{Denmark}
   }
   {BAD}
 }
\cs_new_protected:Nn \flags_U:n
 {
  \str_case:nnF {#1}
   {
    {🇰}{United~Kingdom}
    {🇸}{United~States}
   }
   {BAD}
 }
\ExplSyntaxOff

\begin{document}

Here is 🇩🇪

Here is 🇩🇰

Here is 🇺🇰

Here is 🇺🇸

\end{document}

It would be possible to add a check on the next character also for Unicode engines, but it seems more crucial for pdflatex.

  • Thank you very much! This seems to be exactly what I need. But why is there a \tl_to_str:n { #1 } in \flags_D:n but not in \flags_U:n? The "U-version" seems to be working just fine. – Benjamin Feb 22 '16 at 8:36
  • @Benjamin Because I forgot to fix it. ;-) – egreg Feb 22 '16 at 9:43
  • I thought I could easily adapt your solution for pdfTeX by replacing \newunicodechar{🇩}{\flags_D:n} by \DeclareUnicodeCharacter{127465}{\flags_D:n}. Unfortunately this isn't the case. I'm still looking into it, but if you already know why that is, maybe you can post also a solution for pdfTeX. – Benjamin Feb 22 '16 at 10:27
  • @Benjamin With pdftex it's more complicated. Added. – egreg Feb 22 '16 at 10:47
  • Still trying to understand all the code. I just started learning LaTeX3. But couldn't I just prevent the "weird errors" by replacing {BAD} with {#1}? – Benjamin Feb 22 '16 at 11:03

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.