# Unicode multi-alphabet dictionary

I am trying to generate multilingual, multi-alphabet text and I am not happy with my current version...

What is my dream:

\documentclass{book}
\usepackage[russian,greek,portuguese]{babel}
\usepackage[utf8]{inputenc}
\usepackage[UNICODE]{fontenc}  %%% FIXME!
\begin{document}
abelha -- μελισσα -- пчела
\begin{verbatim}
abelha -- μελισσα -- пчела ∈ {animals}
\end{verbatim}
\end{document}


The order to make this work, I am adding lots of painful \foreignlanguage{greek}{...} I would like to avoid, and that I can't use in verbatins and similar. What is right move?

If you have a font that covers the script you are using you can use lualatex. Be aware that while it shows all the glyphs it doesn't change hyphenation patterns and similar. If this is needed you still will have to mark up the language. The fallback option used in the example needs a very recent luaotfload and it is rather experimental. You can leave it out but then you will have to use e.g. a macro for the ∈ or make it active.

\documentclass{book}
\usepackage[russian,greek,portuguese]{babel}
\usepackage{fontspec}

% fallback for the ∈
\directlua
("myfallback",
{
"DejaVuSansMono:mode=node;",
}
)
}
\setmainfont{cmuserif}[RawFeature={fallback=myfallback}]
\setmonofont{cmutypewritertext}[RawFeature={fallback=myfallback}]
\begin{document}
abelha -- μελισσα -- пчела ∈
\begin{verbatim}
abelha -- μελισσα -- пчела ∈ {animals}
\end{verbatim}

\end{document}


It can be done with a recent babel and luatex. Here is an example showing its capabilites.

\documentclass{book}

\usepackage[russian,greek,portuguese]{babel}

% Tell babel to select the hyphenation rules and the font based on
% the chars script.
\babelprovide[onchar=ids fonts]{greek}
\babelprovide[onchar=ids fonts]{russian}

% If you want serveral fonts:
\babelfont{rm}{DejaVu Serif}
\babelfont[greek]{rm}{FreeSerif}
\babelfont[russian]{rm}{FreeSans}

% If a single font is enough:
\babelfont{tt}{DejaVu Sans Mono}

\begin{document}
abelha -- μελισσα -- пчела ∈
\begin{verbatim}
abelha -- μελισσα -- пчела ∈ {animals}
\end{verbatim}
\end{document}


onchar=ids fonts sets the font, and also the line breaking rules. See What's new in babel 3.38.

I'm not sure of the purpose of ∈, but you can assign it to either greek or russian, if you like, with something like:

\babelcharproperty{\∈}{locale}{russian}


For XeLaTeX you can do something similar to Ulrikes answer for LuaLaTeX. Also in this case you need to find a font that covers different scripts, such as one of the DejaVu variants, or CMU Typewriter Text for serif monospace. Then the input can just be given as in the question, without any font or language switching commands - however the hyphenation will not work properly, as Ulrike mentioned in her answer.

The difference with LuaLaTeX is handling characters that are not in the font. In XeLaTeX you cannot specify a fallback font, but with the ucharclasses package you can specify a font for a range of characters. For example, if you want CMU Typewriter Text as mono font but DejaVu Sans Mono for mathematical symbols:

\documentclass{article}
\usepackage[russian,greek,portuguese]{babel}
\usepackage{fontspec}
\usepackage{ucharclasses}

\setmainfont{DejaVu Serif}
\setmonofont{CMU Typewriter Text}
\newfontfamily\mathmonofont{DejaVu Sans Mono}

\begin{document}
abelha -- μελισσα -- пчела ∈

\setTransitionsForMathematics{\begingroup\mathmonofont}{\endgroup}
abelha -- μελισσα -- пчела ∈
\begin{verbatim}
abelha -- μελισσα -- пчела ∈ {animals}
\end{verbatim}
\end{document}


Result:

There are many different predefined classes that can be used to switch fonts, for scripts/blocks such as Greek, Arabic, Cyrillic, Chinese, Cherokee, Punctuation, to name a few (see page 5 of the manual for all predefined classes). This is useful for mathematics, but also when you have one font that covers Latin and another for Cyrillic, for example. You can also use this to automatically insert \selectlanguage commands to use correct hyphenation patterns.

Note however that the \setTransitionsForMathematics command (and similar for other classes) works on all fonts, also on fonts that do have the character. In the example above it is visible that on the first line in the output the ∈ from DejaVu Serif is used, but after the transisitons command the ∈ from DejaVu Sans Mono is used. This may or may not be desired - for consistency it is not so bad, but it might harm the readibility of a paragraph if the transition font is very different from the normal font. You can unset the transition specification when you no longer need it with \setTransitionsForMathematics{}{}`, however this of course breaks the requirement to just type any text without entering font/language related commands in between.