[Tex/LaTex] pdfLaTeX: Convert € (euro symbol) to command

macrospdftexsymbols

I would like to use € (\€ is also acceptable) in a way which €{9x10,00} (or €[]) produces \SI{9x10,00}{€} (so a particular case of converting € to command). Preferably, if typed alone () it should produce \si{€}, but that's probably extra detail.

The € symbol comes up a lot in some of my documents, and typing \SI{#1}{€} becomes annoying for someone (not me) that doesn't have much latex/programming experience and is used to word, hence the goal of such a simple final command.

I've tried:

\catcode`€=11 %also \catcode8364=11 which is 0x20AC, unicode for €

\newcommand{\€}[1]{\SI{#1}{€}}

but I get an error on catcode because € is utf-8. Also I'd be required to input €{} and would become an error. This is close to what I want, but it relies on catcode.

Best Answer

It's trivial with Unicode TeX engines like XeTeX or LuaTeX:

\catcode€=\active
\newcommand*€[1]{\SI{...}{...}}

The same applies to the other TeX engines with 8-bit input encodings (for example, latin9).

But it is trickier with UTF-8 bytes as input, because the Euro symbol consists of three UTF-8 bytes. Three bytes cannot be one active byte, also the category codes are usually not "letter" (11), but "other" (12). Thus these UTF-8 bytes cannot be used inside command names.

The following example uses LaTeX's inputenc machinery to assign the UTF-8 byte sequence to a macro:

  • If the macro is immediately followed by the left curly brace (without spaces in between), then the macro takes the number as parameter and passes it to \SI.

  • If the macro is immediately followed by the left square bracket, then an optional argument together with the number is passed to \SI.

  • Otherwise the normal euro sign is printed without using a parameter.

  • \SI{9}{€} works.

  • € is robust for \section and the table of contents.

  • For hyperref's bookmarks, \texteuro can be used or an alternative for the bookmarks can be provided via \texorpdfstring. The euro symbol without arguments can be enabled by:

    \pdfstringdefDisableCommands{\let\EuroMacro\texteuro}
    

Full example:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{newunicodechar}

\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{lmodern}

\usepackage{ltxcmds}

\usepackage{siunitx}
\sisetup{output-decimal-marker={,}}
\DeclareSIUnit{\euro}{\texteuro}

\newcommand*{\sieuro}[2][]{\SI[{mode=text,#1}]{#2}{\euro}}

\makeatletter
\newcommand*{\EuroMacro}{}
\protected\def\EuroMacro{%
  \ltx@ifnextchar@nospace\bgroup\sieuro{%
    \ltx@ifnextchar[\sieuro\texteuro
  }%
}
\newunicodechar{€}{\EuroMacro}
\makeatother

\begin{document}
  €{12,34} and the currency is €.

  €[add-integer-zero]{.42}

  \sisetup{mode=text}
  \SI{9}{€}
\end{document}

Result

A version with minimized number of packages in the TeX file. As variation, the euro symbol is generated by package eurosym:

\documentclass{article}

\usepackage[utf8]{inputenc}% UTF-8 input for non-Unicode TeX engines
\usepackage{eurosym}% euro sign
\usepackage{siunitx}

\sisetup{output-decimal-marker={,}}
\DeclareSIUnit{\Euro}{\euro}
\newcommand*{\sieuro}[2][]{\SI[{mode=text,#1}]{#2}{\Euro}}

\makeatletter
\newcommand*{\EuroMacro}{%
  \ifx\@let@token\bgroup
    \expandafter\sieuro
  \else
    \ifx\@let@token[%
      \expandafter\expandafter\expandafter\sieuro
    \else
      \expandafter\expandafter\expandafter\euro
    \fi
  \fi
}
\protected\def\EuroMacroAux{%
  \futurelet\@let@token\EuroMacro
}
\AtBeginDocument{%
  \DeclareUnicodeCharacter{20AC}{\EuroMacroAux}%
}
\makeatother

\begin{document}
  €{12,34} and the currency is €.

  €[add-integer-zero]{.42}

  \sisetup{mode=text}
  \SI{9}{€}
\end{document}

Result