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:
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:Full example:
A version with minimized number of packages in the TeX file. As variation, the euro symbol is generated by package
eurosym
: