# How to temporarily replace Latin characters with Greek

Often I am typing equations with many Greek characters in them, and it becomes cumbersome to type out the commands for them all. I would like to make a command or environment that replaces Latin characters with analogous Greek ones. For example, something like \greek{abge} or \begin{greek}abge\end{greek} would display the same as \alpha\beta\gamma\epsilon.

I've tried making letters active so that I can use something like \let a\alpha, but the problem with that approach is that then I can't use them as part of commands - I'd like to be able to have something like \greek{a\dot{b}ge} display as greek{\alpha\dot{\beta}\gamma\epsilon}.

I also tried using DeclareUnicodeCharacter to replace e.g. a with the alpha character, but I get an error because the unicode value of a is less than 00A0. Is there some other way I can make this work?

Fill in the table:

\documentclass{article}

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

\newcommand{\setuplatintogreek}{%
\mathcodea=\alpha
\mathcodeb=\beta
\mathcodeg=\gamma
\mathcoded=\delta
\mathcodee=\varepsilon
%...
}

\begin{document}

$abgde+\greek{abgde}$

\end{document}


• I've decided to accept this answer since it was simplest for me to set up and get working. The other answers are good too! – Carmeister Oct 3 '19 at 0:57

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:
{\processtoks{##1}}%            HOW TO TREAT GROUP CONTENTS
\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}


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}

• You don't need luatex to be able to use input like βγδεφη, pdftex could handle this too. – Ulrike Fischer Oct 2 '19 at 11:22
• Really? It has never worked for me. Are you sure you are talking about math mode? – Psychonaut Oct 2 '19 at 11:25
• α is a command in pdflatex, so you can always (re)define it to do whatever you want - including typing an \alpha. Try e.g. \DeclareUnicodeCharacter{03B1}{\ifmmode\alpha\else\textalpha\fi} – Ulrike Fischer Oct 2 '19 at 11:47
• Fine, but the whole point of my post is to provide a convenient method for entering Greek characters that does not require individually defining the behaviour of characters (at least, not at the *TeX level). So the fact that U+03B1 and friends can be individually redefined in pdftex is not really relevant to my solution. – Psychonaut Oct 2 '19 at 12:03
• It should work in PDFLaTeX if you load the LGR font encoding and also declare a substitutefont` for LGR. – Davislor Oct 2 '19 at 23:40