How to temporarily replace Latin characters with Greek

Fill in the table:

\documentclass{article}

\newcommand{\greek}[1]{\begingroup\setuplatintogreek#1\endgroup}

\newcommand{\setuplatintogreek}{%
  \mathcode`a=\alpha
  \mathcode`b=\beta
  \mathcode`g=\gamma
  \mathcode`d=\delta
  \mathcode`e=\varepsilon
  %...
}

\begin{document}

$abgde+\greek{abgde}$

\end{document}

enter image description here


This is just the kind of problem for which the tokcycle package was made.

I've only implemented a handfull of the Greek, but you just need a \tcmapto z\zeta type statement for each new mapping. In the MWE, I also use |...| to escape characters back to the original Latin. Macros are automatically intercepted and preserved with the Latin interpretation of the name. All grouping considerations of the \greek environment are preserved in the transformed output.

\documentclass{article}
\usepackage{tokcycle}
\tokcycleenvironment\greek%     NEW TOKCYCLE ENVIRONMENT:
{\addcytoks[4]{\tcremap{##1}}}% HOW TO TREAT CHARACTERS
{\processtoks{##1}}%            HOW TO TREAT GROUP CONTENTS
{\addcytoks{##1}}%              HOW TO TREAT MACROS
{\addcytoks{##1}}%              HOW TO TREAT SPACES
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}}
\newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname
  \csname tcmapto#1\expandafter\endcsname\else\expandafter#1\fi}
\tcmapto a\alpha \tcmapto b\beta \tcmapto e\epsilon 
\tcmapto g\gamma \tcmapto p\pi
\begin{document}
\[
y = \greek a^2b\frac{2e}{g + p^|x|}\endgreek + x
\]
\end{document}

enter image description here

I edited the answer to NOT make use of \expanded since not everyone has an updated TeX installation. So, \addcytoks[4]{...} indicates to perform 4 expansions on the argument before storing the result, which is sufficient here to achieve the goal. The alternate syntax of \addcytoks[x]{...} invokes \expanded on the argument before storing the result. With the [x] option, the \tcremap macro can also be simplified, by eliminating the \expandafters as such: \newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname\csname tcmapto#1\endcsname\else#1\fi}

Note: MikTeX doesn't quite load the package properly. You will have to manually download tokcycle.tex to your localtexmf at \tex\generic\tokcycle\tokcycle.tex. See https://www.ctan.org/pkg/tokcycle


Rather than using LaTeX to manually define a mapping between typed letters and Greek characters, you may find it more convenient to simply type the Greek characters as-is into your document. For this you will need to use XeLaTeX or LuaLaTeX, the unicode-math package, an OpenType math font, and a keyboard layout that permits direct entry of Greek letters. The first three of these are probably bundled with your TeX distribution.

The keyboard layout is controlled by your operating system. You basically have two options here:

  1. You can configure your operating system's keyboard settings such that a standard Greek layout is available in addition to whatever layout you normally use, and such that you can easily switch between the two using a keyboard shortcut. (Ctrl+Alt+k is a common suggestion, but I prefer remapping the CapsLock key for this purpose.) The advantage to this approach is that it requires minimal setup and is likely to work on pretty much any modern operating system. Fortunately, the standard Greek keyboard layout generally puts the Greek letters in the same positions as their Latin QWERTY equivalents (a gives α, s gives σ, etc.) so you won't spend much time learning what keys to type.

  2. You can switch to a keyboard layout that is similar to your usual one, but which also provides access to Greek letters, possibly through the use of AltGr (i.e., the right Alt key) key combinations or a "dead Greek" key. The X.Org Server used by most GNU/Linux distributions, for example, includes a few such keyboard layouts (for French, German, and Bulgarian, but not, it seems, for English).

    If a suitable keyboard layout is not distributed with your operating system, you may be able to create one yourself (following the example of the existing ones, which on GNU/Linux usually live in /usr/share/X11/xkb/symbols). Or, again on GNU/Linux, you can simply invoke the following command as a quick kludge:

    xmodmap -e 'keycode 108 = dead_greek Meta_R Alt_R Meta_R'
    

    This command remaps your AltGr key to be the "dead Greek" key, which you can use to compose Greek characters in much the same way as described in the first option above. This means, for example, that you can get σ by pressing AltGr+s, Σ by pressing AltGr+Shift+s, etc. Note that this command is effective only for your current login session; to make the change permanent, you'll need to add it to your shell's startup script (e.g., $HOME/.bashrc for bash).

    Similar solutions exist for other operating systems, though some of them may require you to install extra software. If you are using Microsoft Windows, you may want to look into the Microsoft Keyboard Layout Creator.

Once you've got a keyboard layout set up, you can use it to directly type in documents like the following, which must be compiled with XeLaTeX or LuaLaTeX:

\documentclass{article}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}
\begin{document}
$αβγδεφη$
\end{document}

Tags:

Macros

Greek