pdfLaTeX: Convert € (euro symbol) to command
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}
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}
You can't set the category code of €
in pdflatex
, because it's three byte long. However, there are other methods.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage[right]{eurosym} % I can't stand textcomp euro symbol
\usepackage{newunicodechar}
\makeatletter
\newunicodechar{€}{\olivetree@euro}
\newcommand{\olivetree@euro}{\@ifnextchar\bgroup\EUR{\euro}}
\makeatother
\begin{document}
You owe me €{100.23}; please, pay in €.
\end{document}
If you want to use siunitx
features for printing the number,
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage[right]{eurosym} % I can't stand textcomp euro symbol
\usepackage{newunicodechar}
\usepackage{siunitx}
\makeatletter
\newunicodechar{€}{\olivetree@euro}
\newcommand{\olivetree@euro}{\@ifnextchar\bgroup\olivetree@euro@arg\euro}
\newcommand{\olivetree@euro@arg}[1]{\num{#1}\,\euro}
\makeatother
\begin{document}
You owe me €{100.23}; please, pay in €.
\end{document}
Some changes are needed for XeLaTeX or LuaLaTeX:
\documentclass{article}
\usepackage{fontspec}
\usepackage{newunicodechar}
\usepackage{siunitx}
\makeatletter
\newcommand{\olivetree@euro}{\@ifnextchar\bgroup\olivetree@euro@arg{€}}
\newcommand{\olivetree@euro@arg}[1]{\num{#1}\,€}
\newunicodechar{€}{\olivetree@euro}
\makeatother
\begin{document}
You owe me €{100.23}; please, pay in €.
\end{document}
\€
is also acceptable
Then, a simple approach:
\documentclass{article}
\usepackage{eurosym,siunitx}
\def\€#1{\SI{#1}{\mbox{\euro}}}
\begin{document}
\€{9x10,00}
\end{document}