3

My question evolves around greek script, but it could be any other script I suppose.

If I have a font set with \setmainfont{#latinfont} which does not contain Greek glyphs and thus set another font with \newfontfamily\greekfont[script=Greek]{#greekfont}, I don't get Greek output from xelatex in my PDF. In order to get Greek script, I have to enclose the respective text in \textgreek{#text}.

My MWE is as follows

\documentclass{article}

\usepackage{polyglossia}

\setmainfont{Cantarell}
\setsansfont{Cantarell}
\newfontfamily\greekfont[Script=Greek]{Liberation Serif}
\newfontfamily\greekfontsf[Script=Greek]{Liberation Serif}
\setdefaultlanguage{english}
\setotherlanguage{greek}

\begin{document}
\tableofcontents{}

\section{Αυτό είναι ελληνικά.}
Welcome to Greek ``serif'': Αυτό είναι ελληνικά

Welcome to Greek ``sans'': \textsf{Αυτό είναι ελληνικά}

Another try to get Greek: \textgreek{Αυτό είναι ελληνικά}
\end{document}

This compiles to

my MWE compiles, but its not how I would like it to look

Unfortunately, always using \textgreek is not only cumbersome, but also not possible in some circumstances. For example in bibliographies, I organize my library in zotero and export from there to biblatex in UTF-8 encoding. Thus, Greek script in, e.g., article titles is contained in the bibliography, often times its just one character (like in γ-Hydroxybutyric acid).

Thus my question: Is it possible to get xelatex/xetex/polyglossia/fontspec to select the correct font depending upon which script is present in the source file? If yes, how can I achieve this? If not, are there alternatives or workarounds?

1 Answer 1

4

You can use ucharclasses, but a proper markup is better.

\documentclass{article}

\usepackage{polyglossia}
\usepackage{ucharclasses}

\setmainfont{XCharter} % no Cantarell on my machine
\setsansfont{Futura} % a different one
\newfontfamily{\greekfont}{Liberation Serif}[
  Script=Greek,
  NFSSFamily=liberationserif,
]
\newfontfamily{\greekfontsf}{Liberation Sans}[
  Script=Greek,
  NFSSFamily=liberationsans,
]

\setdefaultlanguage{english}
\setotherlanguage{greek}

\makeatletter
\newcommand{\switchtogreek}{%
  \ifnum\strcmp{\f@family}{\sfdefault}=\z@
    \usefont{\encodingdefault}{liberationsans}{\f@series}{\f@shape}
  \else
    \usefont{\encodingdefault}{liberationserif}{\f@series}{\f@shape}
  \fi
}
\makeatother
\setTransitionsForGreek{\begingroup\switchtogreek}{\endgroup}

\begin{document}
\tableofcontents

\section{Αυτό είναι ελληνικά.}
Welcome to Greek ``serif'': Αυτό είναι ελληνικά

\textsf{Welcome to Greek ``sans'': Αυτό είναι ελληνικά}

Another try to get Greek: \textgreek{Αυτό είναι ελληνικά}
\end{document}

enter image description here

2
  • Thank you for your exact and concise answer. I would be more than happy to hear any suggestion about not using ucharclasses but rather proper markup. I would like to again raise the case of bibliographic items containing singular greek letters. Should I rather look for some sort of parser, which takes the bib-file as an input and outputs every greek letter enclosed in \textgreek{}? What do you suggest?
    – hintze
    Jan 17, 2017 at 12:58
  • 1
    @ChristianHintze It mostly depend on your specific use cases. Single letters or words can be dealt with using ucharclasses (choose better compatible fonts, of course; I don't have Cantarell, so I can't judge). Passages of text in Greek ought to be marked up.
    – egreg
    Jan 17, 2017 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .