\documentclass[a4paper]{article}
\usepackage{parskip}
\usepackage{booktabs}

\ifdefined \UnicodeEncodingName % set by LaTeX for Unicode-aware engines
  % Setup for Unicode fonts (Xe-/LuaTeX)
  \usepackage{fontspec}
  \setmainfont{Linux Libertine O}
  \setsansfont{Linux Biolinum O}
  \newcommand*{\greekfontencoding}{TU}
\else
  % Setup for 8-bit fonts (pdfTeX/LuaTeX)
  % (XeTeX in compatibility mode would require inputenc hacks and is not
  % reliable.)
  \usepackage{lmodern}
  \usepackage[LGR,T1]{fontenc}
  \newcommand*{\greekfontencoding}{LGR}
  \newcommand*{\latinencoding}{T1}
\fi

\usepackage[pdfencoding=auto,colorlinks=true,linkcolor=blue]{hyperref}
% \usepackage{bookmark}
\makeatletter
\providecommand*{\href}{\@secondoftwo}
\providecommand*{\url}{\texttt}
\makeatother

\usepackage[normalize-symbols, % comment option out to test error reporting
            keep-semicolon%
           ]{textalpha}

% compatibility defs:
\ProvideTextCommandDefault{\textvarstigma}{$\oslash$%
  \message{Character textvarstigma not defined.}}
\ProvideTextCommand{\textvarstigma}{PU}{⌀}
\providecommand*{\accinvertedbrevebelow}{$\oslash$%
  \message{Character accinvertedbrevebelow not defined.}}
\providecommand*{\accbrevebelow}{$\oslash$%
  \message{Character accbrevebelow not defined.}}



\begin{document}

\title{The \emph{textalpha} package}
\author{Günter Milde}
\date{2020/10/30}

\maketitle

\abstract{\noindent
The \emph{textalpha} package enables the use of Greek characters
in text independent of font encoding or TeX engine.%
\footnote{
  This document was compiled using
  \ifdefined \UTFencname % defined by fontspec
     Unicode fonts (font encoding \latinencoding).
     For a version using 8-bit fonts, see
     \href{textalpha-doc.pdf}{textalpha-doc.pdf}.
  \else
     8-bit fonts (font encoding \latinencoding).
     For a version using Unicode fonts, see
     \href{textalpha-tu.pdf}{textalpha-tu.pdf}.
  \fi
}
Input is possible via text commands (\verb|\textalpha| \ldots
\verb|\textOmega|) or Unicode literals\footnote{%
Requires \emph{\href{https://ctan.org/pkg/greek-inputenc}{greek-inputenc}}
or XeTeX/LuaTeX.}.
}

\tableofcontents


\section{Usage}

Load this package in the preamble of your document with
\begin{verbatim}
  \usepackage[<options>]{textalpha}
\end{verbatim}
See the source of this document \url{textalpha-doc.tex} for a setup and
usage example, the literate source of the package
\href{textalpha.sty.html}{textalpha.sty} for the implementation, and
\href{test-tuenc-greek.pdf}{test-tuenc-greek.pdf} for Greek with
XeTeX/LuaTeX.


\subsection{Options}
\subsubsection{\texttt{normalize-symbols}}

Mathematical notation uses variant shapes of some Greek letters as
additional symbols.  There are separate code points for the symbol variants
in Unicode. TeX supports some of the variant shape symbols in mathematical
mode ($\theta|\vartheta, \phi|\varphi, \pi|\varpi, \rho|\varrho,
\epsilon|\varepsilon$) but not in the LGR font encoding used for Greek text
in 8-bit TeX.
The variations have no syntactic meaning in Greek text and text fonts may
use the variant shapes in place of the “regular” ones as a stylistic choice.

The \texttt{normalize-symbols} option merges letters and symbols to Greek
letters.
This way, text copied from external sources can be compiled without
errors even if it contains a GREEK SYMBOL … in place of a GREEK LETTER …
\begin{quote}
  The source of this paragraph uses both variants for beta (β|ϐ),
  theta (θ|ϑ), phi (φ|ϕ), pi (π|ϖ), kappa (κ|ϰ), rho (ρ|ϱ), Theta (Θ|ϴ),
  and epsilon (ε|ϵ).
\end{quote}
%
This option is ignored with Unicode fonts.

\begin{description}
  \item [Attention:] Do not use this option in cases where the distinction
between the symbol variants may be important (e.g. in a mathematical or
scientific context). Try the \href{alphabeta-doc.pdf}{alphabeta} package
with the respective characters in mathematical mode or use XeTeX/LuaTeX with
Unicode fonts in these cases.
\end{description}

\subsubsection{\texttt{keep-semicolon}}

LGR is no \href{https://mirrors.ctan.org/macros/latex/base/encguide.pdf}%
{standard text font encoding}.
Latin characters and some other ASCII symbols are mapped to Greek
``equivalents'' if LGR is the active font encoding. (See
\href{https://mirrors.ctan.org/language/babel/contrib/greek/babel-greek-doc.html#lgr-latin-transliteration}%
{babel-greek} for a description of this Latin-Greek transliteration.)

Special care is required with the question mark characters: The LGR font
encoding uses the Latin question mark as input for the \emph{erotimatiko}
and maps the semicolon to a middle dot (\emph{ano teleia}).
As a result, Unicode-encoded texts that use the semicolon as
\emph{erotimatiko} end up with an \emph{ano teleia} in its place!
Without special care, only the deprecated character 037E GREEK QUESTION MARK%
\footnote{The Unicode standard provides the code point 037E GREEK QUESTION MARK
        but says character 003B SEMICOLON and not 037E is the preferred
        character for a `Greek question mark' (erotimatiko).}
works with both, Xe/LuaTeX and 8-bit TeX.

The \verb|\textsemicolon| command inserts an \emph{erotimatiko} in LGR and a
semicolon else (i.e. always a character that looks like a semicolon):
\begin{quote}
  Latin (\latinencoding) a\textsemicolon{} b,
  Greek (\greekfontencoding) \ensuregreek{a\textsemicolon{} b}
\end{quote}

With the \texttt{keep-semicolon} option, character 003B SEMICOLON can be used
for the \emph{erotimatiko} also with LGR encoded fonts:

\begin{center}
\begin{tabular}{ccl}
  Latin (\latinencoding) & Greek (\greekfontencoding) & question mark character \\
  \midrule
  ; & \ensuregreek{;} & 037E GREEK QUESTION MARK \\
  ; & \ensuregreek{;} & 003B SEMICOLON \\
  ? & \ensuregreek{?} & 003F QUESTION MARK \\
\end{tabular}
\end{center}

This option is ignored with Unicode fonts (where the SEMICOLON literal
always prints a semicolon character).

Test whether this works as expected in math mode:
\ensuregreek{$a b; a\;b, (\mathrm{a;}\textrm{a;}2)$}.


\subsection{Shortcuts for Breathings}

\emph{textalpha} defines the shortcut macros \verb|\<| and \verb|\>| for the
\href{https://en.wikipedia.org/wiki/Rough_breathing}{dasia} (rough breathing)
and \href{https://en.wikipedia.org/wiki/Smooth_breathing}{psili} (smooth
breathing) diacritics.


\section{Limitations \label{sec:limitations}}

If compiling with 8-bit TeX, the internal font encoding switches interfere
with other work behind the scenes. Kerning, diacritics and up/down-casing
show problems if Greek letters are used without explicit change of the font
encoding.

These problems can be avoided by use of \emph{babel} and the correct
language setting (greek), an explicit font encoding switch (e.g.
wrapping the Greek text in \verb|\ensuregreek|%
\footnote{The \texttt{\textbackslash ensuregreek} macro ensures the argument
  is set in a font encoding supporting Greek.
  % This can be used to fix these
  % problems without adverse side-effects if the active font encoding is
  % already LGR or TU.
  },
or XeTeX/LuaTeX with Unicode fonts.

\subsection{Kerning}

With pdfTeX and 8-bit fonts, no kerning occurs between Greek characters in
non-Greek text due to the internal font encoding switch: compare
\ensuregreek{\textAlpha\textUpsilon\textAlpha} (\greekfontencoding) to
\textAlpha\textUpsilon\textAlpha{} (\latinencoding).

Compiling with LuaTeX provides kerning also on font encoding boundaries.

\subsection{Diacritics}

Accent macros do not work with Unicode literals as base character
under 8-bit TeX engines. Use the Latin transliteration or LICR commands.

Composition of diacritics (like \verb|\accdasia\acctonos| or \verb|\<\'|)
fails in other font encodings. Long names (like \verb|\accdasiaoxia|) work.

With LGR and TU, pre-composed glyphs are chosen if available. In other font
encodings, accent macros do not select pre-composed characters.
The difference is a sub-optimal placement of the accent and becomes obvious
if you drag-and-drop text from the PDF version of this document.:
\begin{quote}
  \ensuregreek{\<'\textalpha
               \'<\textalpha
               \accdasia\acctonos\textalpha
               \acctonos\accdasia\textalpha
               \accdasiaoxia\textalpha{}
               \<'\textAlpha
               \'<\textAlpha
               \accdasia\acctonos\textAlpha
               \acctonos\accdasia\textAlpha
               \accdasiaoxia\textAlpha
              } (\greekfontencoding)
  vs. \accdasiaoxia\textalpha{} \accdasiaoxia\textAlpha{} (\latinencoding).
\end{quote}

Greek diacritics (except the dialytika and sub-iota) are placed before
capital letters in Titlecase (Ἀρχιμήδης) and and dropped in uppercase
(ΑΡΧΙΜΗΔΗΣ). With the \verb|\MakeUppercase| implementation introduced
2022/06, Greek upcasing rules are only applied to literal characters
if the text language is set to Greek with Babel.

With the old \verb|\MakeUppercase| implementation, the above rules are fully
applied if the active font encoding is LGR or TU. However, accents input as
macros are misplaced if the active font encoding does not support Greek.
Pre-composed literal Unicode characters are handled correctly:
\begin{quote}
  \begin{tabular}{ccc}
                     & LICR                       & literal input   \\ \hline
  \greekfontencoding & \ensuregreek{\'\textAlpha} & \ensuregreek{Ά} \\
  \latinencoding     & \'\textAlpha               & Ά
  \end{tabular}
\end{quote}

The dialytika marks a \emph{hiatus} (break-up of a diphthong). It must be
present in UPPERCASE even where it is redundant in lowercase (the hiatus can
also be marked by an accent or breathing on the first of two consecutive
vowels). The auto-hiatus feature works in LGR and TU font encodings only:
\begin{quote}
  \acctonos\textalpha\textupsilon{}, \acctonos\textepsilon\textiota{} $\mapsto$
  \MakeUppercase{\ensuregreek{
    \acctonos\textalpha\textupsilon{}, \acctonos\textepsilon\textiota{}
  }} (\greekfontencoding) vs.
  \MakeUppercase{
    \acctonos\textalpha\textupsilon{}, \acctonos\textepsilon\textiota{}%
  } (\latinencoding)
\end{quote}

With the old implementation of \verb|\MakeUppercase|, the auto-hiatus
feature works with LICR macros but not Unicode literals.
The new implementation works with Unicode literals, too, but only if the text
language is Greek (i.e. not in this document that does not load Babel).
\begin{quote}
\ensuregreek{%
\accpsili\textalpha\textupsilon\textpi\textnu\acctonos\textiota\textalpha}
$\mapsto$ \ensuregreek{\MakeUppercase{%
\accpsili\textalpha\textupsilon\textpi\textnu\acctonos\textiota\textalpha}}
(LICR macros: OK with LGR or TU)

\ensuregreek{ἀυπνία} $\mapsto$
\ensuregreek{\MakeUppercase{ἀυπνία}} (literal characters: fails without Babel)
\end{quote}


\section{Test and Examples}

\subsection{Greek alphabet}

Greek letters via Latin transliteration (works only in LGR font encoding):

\begin{quote}
  \ensuregreek{a b g d e z h j i k l m n x o p r sv c t u f q y w}

  \ensuregreek{A B G D E Z H J I K L M N X O P R S T U F Q Y W}
\end{quote}
%
Greek letters via default macros in the font for Latin script
(here \latinencoding):

\newcommand*{\greekAlphabetsample}{
  \textAlpha{} \textBeta{} \textGamma{} \textDelta{} \textEpsilon{}
  \textZeta{} \textEta{} \textTheta{} \textIota{} \textKappa{}
  \textLambda{} \textMu{} \textNu{} \textXi{} \textOmicron{} \textPi{}
  \textRho{} \textSigma{} \textTau{} \textUpsilon{} \textPhi{}
  \textChi{} \textPsi{} \textOmega{}
  }
\newcommand*{\greekalphabetsample}{
  \textalpha{} \textbeta{} \textgamma{} \textdelta{} \textepsilon{}
  \textzeta{} \texteta{} \texttheta{} \textiota{} \textkappa{}
  \textlambda{} \textmu{} \textnu{} \textxi{} \textomicron{} \textpi{}
  \textrho{} \textsigma{} \textvarsigma{} \texttau{} \textupsilon{}
  \textphi{} \textchi{} \textpsi{} \textomega{}
}
\begin{quote}
  \greekalphabetsample

  \greekAlphabetsample
\end{quote}
%
Archaic Greek letters and Greek punctuation

\newcommand*{\archaicgreeksample}{
  \textdigamma \textDigamma{}
  \textkoppa   \textKoppa{}
  \textqoppa   \textQoppa{}
  \textsampi   \textSampi{}
  \textstigma  \textvarstigma \textStigma{}
  \textanoteleia{}
  \texterotimatiko{}
  \textdexiakeraia{}
  \textaristerikeraia{}
}

\begin{quote}
  \archaicgreeksample
\end{quote}
%
Diacritics

\begin{quote}
  Short macros:%
  \footnote{Composite diacritics require wrapping in
  \texttt{\textbackslash ensuregreek}.}
  \"{} \'{} \`{} \~{} \<{} \>{} \u{} \={}
  \ensuregreek{\"~{} \"'{} \"`{} \<~{} \<`{} \<'{} \>~{} \>'{} \>`{}}

  Named macros:
  \accdialytika{}
  \acctonos{}
  \accvaria{}
  \accperispomeni{}
  \accdasia{}
  \accpsili{}
  \ypogegrammeni{}
  \prosgegrammeni{}
  %
  \accdialytikaperispomeni{}
  \accdialytikatonos{}
  \accdialytikavaria{}
  \accdasiaperispomeni{}
  \accdasiavaria{}
  \accdasiaoxia{}
  \accpsiliperispomeni{}
  \accpsilioxia{}
  \accpsilivaria{}
  %
  Only in LGR:
  \accinvertedbrevebelow{} % == \textsubarch{}
  \accbrevebelow{}

\end{quote}

\medskip\noindent
Accent macros can start with \verb|\a| instead of \verb|\| when the
short form is redefined, e.\,g. inside a \emph{tabbing} environment.
This also works for the new-defined Dasia and Psili shortcuts:
\begin{quote}
  \begin{tabbing}
       COL1\quad \= COL2\quad \= COL3\quad \= COL4\quad \\
       Viele    \> Gr\a"u\ss e
       \> \greekscript \a<\textalpha{}
       \> \greekscript \a>\textomega
  \end{tabbing}
\end{quote}

\subsubsection{Sigma}

The lower Sigma comes in two variants: \verb|\textsigma| \textsigma{} is
used inside a word and \verb|\textfinalsigma| \textfinalsigma{} (or
\verb|\textvarsigma| \textvarsigma{}) at the end of words.

In LGR, the Latin letter \verb|s| and the command \verb|\textautosigma|
print the ``normal'' sigma if followed by another letter and the final sigma
if followed by space or punctuation. This is implemented via the font
ligature mechanism in LGR:
\ensuregreek{\textautosigma\textautosigma} (\greekfontencoding) vs.
\textautosigma{}\textautosigma{} (\latinencoding).%
\footnote{TODO: Fix \texttt{\textbackslash textautosigma} with Unicode fonts.}


The upper case of both sigma variants is \verb|\textSigma|, the lower case
of \verb|\textSigma| is \verb|\textautosigma|.

\smallskip\noindent
Test: Unicode literal and \verb|\text...| commands

\newcommand{\sample}{σ\textsigma{}
                     ς\textvarsigma \textfinalsigma \textautosigma{}
                     ΣΣ \textSigma\textSigma{}}

no change:     \sample

MakeUppercase: \MakeUppercase{\sample}

MakeLowercase (\latinencoding): \MakeLowercase{\sample}

MakeLowercase (\greekfontencoding): \ensuregreek{\MakeLowercase{\sample}}


\subsection{Greek literal characters in non-Greek text}

With the \emph{textalpha} package,
\href{https://ctan.org/pkg/greek-inputenc}{greek-inputenc} and input
encoding \texttt{utf8}, Greek Unicode characters can be used in text with
any font encoding. See Tables \ref{tab:greek-and-coptic} and
\ref{tab:greek-extended}.

Kerning is preserved if the active font encoding supports Greek. This can be
secured by wrapping the Greek text part in \verb|\ensuregreek| or setting the
text language with Babel: \ensuregreek{AΫA}

\begin{table}[tbp]
\setlength{\tabcolsep}{0.45em}
\centerline{
\begin{tabular}{rrrrrrrrrrrrrrrrr}
\toprule
    & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & A & B & C & D & E & F\\
\midrule
370 & ◦ & ◦ & ◦ & ◦ & ʹ & ͵ & ◦ & ◦ &   &   & ͺ & ◦ & ◦ & ◦ & ; &  \\
380 &   &   &   &   & ΄ & ΅ & Ά & · & Έ & Ή & Ί &   & Ό &   & Ύ & Ώ\\
390 & ΐ & Α & Β & Γ & Δ & Ε & Ζ & Η & Θ & Ι & Κ & Λ & Μ & Ν & Ξ & Ο\\
3A0 & Π & Ρ &   & Σ & Τ & Υ & Φ & Χ & Ψ & Ω & Ϊ & Ϋ & ά & έ & ή & ί\\
3B0 & ΰ & α & β & γ & δ & ε & ζ & η & θ & ι & κ & λ & μ & ν & ξ & ο\\
3C0 & π & ρ & ς & σ & τ & υ & φ & χ & ψ & ω & ϊ & ϋ & ό & ύ & ώ &  \\
3D0 & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & Ϙ & ϙ & Ϛ & ϛ & Ϝ & ϝ & Ϟ & ϟ\\
3E0 & Ϡ & ϡ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦\\
3F0 & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦ & ◦\\
\bottomrule
\end{tabular}
} % end centerline
\caption{Greek and Coptic Unicode Block, input as literal Unicode
   characters in \latinencoding{} font encoding
   (legend: ◦ glyph missing in LGR).}
\label{tab:greek-and-coptic}
\end{table}


\begin{table}[tbp]
\setlength{\tabcolsep}{0.45em}
\centerline{
\begin{tabular}{rrrrrrrrrrrrrrrrr}
\toprule
     & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & A & B & C & D & E & F\\
\midrule
1F00 & ἀ & ἁ & ἂ & ἃ & ἄ & ἅ & ἆ & ἇ & Ἀ & Ἁ & Ἂ & Ἃ & Ἄ & Ἅ & Ἆ & Ἇ\\
1F10 & ἐ & ἑ & ἒ & ἓ & ἔ & ἕ &   &   & Ἐ & Ἑ & Ἒ & Ἓ & Ἔ & Ἕ &   &  \\
1F20 & ἠ & ἡ & ἢ & ἣ & ἤ & ἥ & ἦ & ἧ & Ἠ & Ἡ & Ἢ & Ἣ & Ἤ & Ἥ & Ἦ & Ἧ\\
1F30 & ἰ & ἱ & ἲ & ἳ & ἴ & ἵ & ἶ & ἷ & Ἰ & Ἱ & Ἲ & Ἳ & Ἴ & Ἵ & Ἶ & Ἷ\\
1F40 & ὀ & ὁ & ὂ & ὃ & ὄ & ὅ &   &   & Ὀ & Ὁ & Ὂ & Ὃ & Ὄ & Ὅ &   &  \\
1F50 & ὐ & ὑ & ὒ & ὓ & ὔ & ὕ & ὖ & ὗ &   & Ὑ &   & Ὓ &   & Ὕ &   & Ὗ\\
1F60 & ὠ & ὡ & ὢ & ὣ & ὤ & ὥ & ὦ & ὧ & Ὠ & Ὡ & Ὢ & Ὣ & Ὤ & Ὥ & Ὦ & Ὧ\\
1F70 & ὰ & ά & ὲ & έ & ὴ & ή & ὶ & ί & ὸ & ό & ὺ & ύ & ὼ & ώ &   &  \\
1F80 & ᾀ & ᾁ & ᾂ & ᾃ & ᾄ & ᾅ & ᾆ & ᾇ & ᾈ & ᾉ & ᾊ & ᾋ & ᾌ & ᾍ & ᾎ & ᾏ\\
1F90 & ᾐ & ᾑ & ᾒ & ᾓ & ᾔ & ᾕ & ᾖ & ᾗ & ᾘ & ᾙ & ᾚ & ᾛ & ᾜ & ᾝ & ᾞ & ᾟ\\
1FA0 & ᾠ & ᾡ & ᾢ & ᾣ & ᾤ & ᾥ & ᾦ & ᾧ & ᾨ & ᾩ & ᾪ & ᾫ & ᾬ & ᾭ & ᾮ & ᾯ\\
1FB0 & ᾰ & ᾱ & ᾲ & ᾳ & ᾴ &   & ᾶ & ᾷ & Ᾰ & Ᾱ & Ὰ & Ά & ᾼ & ᾽ & ι & ᾿\\
1FC0 & ῀ & ῁ & ῂ & ῃ & ῄ &   & ῆ & ῇ & Ὲ & Έ & Ὴ & Ή & ῌ & ῍ & ῎ & ῏\\
1FD0 & ῐ & ῑ & ῒ & ΐ &   &   & ῖ & ῗ & Ῐ & Ῑ & Ὶ & Ί &   & ῝ & ῞ & ῟\\
1FE0 & ῠ & ῡ & ῢ & ΰ & ῤ & ῥ & ῦ & ῧ & Ῠ & Ῡ & Ὺ & Ύ & Ῥ & ῭ & ΅ & `\\
1FF0 &   &   & ῲ & ῳ & ῴ &   & ῶ & ῷ & Ὸ & Ό & Ὼ & Ώ & ῼ & ´ & ῾ &  \\
\bottomrule
\end{tabular}
} % end centerline
\caption{Greek Extended Unicode Block, input as literal Unicode
characters in \latinencoding{} font encoding.}
\label{tab:greek-extended}
\end{table}

Combined Diacritics work for pre-composed characters: ᾅ.

Diacritics (except diaeresis) are dropped with
MakeUppercase with LaTeX versions older than 2022/06
For other versions, set the language of to-be-upcased Greek text with Babel.

μαΐστρος, δύο  $\mapsto$ \MakeUppercase{μαΐστρος, δύο}.


\subsection{PDF strings}

With \emph{textalpha} and
\emph{\href{https://ctan.org/pkg/greek-inputenc}{greek-inputenc}}, there
are two options to get Greek letters in PDF strings: LICR macros and literal
Unicode input.

\subsubsection{\textlambda\textomicron\textgamma\textomicron\textvarsigma{},
         λογος and \ensuregreek{logos}}

The subsection title above uses: LICR macros, Unicode input and the LGR
transliteration for the Greek word \ensuregreek{logos}.

LICR macros and Unicode literals work fine everywhere, the
Latin transliteration remains Latin in the PDF metadata
(sidebar table of contents in the PDF viewer) and with Xe/LuaTeX.

\subsubsection{\greekalphabetsample}
\subsubsection{\greekAlphabetsample}
\subsubsection{\archaicgreeksample}

\end{document}
