Good keyboard layouts for typing (La)TeX

Long, long ago in a galaxy far, far away, I attempted starting a blog. It didn't last, but one of the things I posted was about this very subject. When I deleted the blog, I kept the articles. So here's that one. I apologise for the length.


A few years back I started to get what I think was RSI in my hands. I never got it officially diagnosed so I can't be sure, but all the symptoms seemed clear. It was worse when I was typing, and worst in my little fingers.

Think about typing. How much work do the little fingers do compared to the others? As well as having their own letters, they also work many of the punctuation characters and the shift (and control) keys. I found I was often having to stretch my hand to type characters and this was putting a lot of strain on my little fingers.

It's even worse when typing LaTeX documents. A quick scan through a thirty-page paper reveals that the five most typed characters are:

  • space 15525
  • e 7266
  • \ 6834
  • o 5476
  • t 5470

There then follow a few more lowercase letters, in 14th and 15th place are the parentheses (worryingly not the same number of each - must have some half-open intervals in there). 22nd is the underscore, 24th and 25th are the curly braces (quick check: the same number this time) with just over a thousand occurrences. The first number, 1, is way down the list with only 362 appearances.

By the way, if you want to generate this list, there are probably more elegant ways but here's my two-minute hack:

~% cat paper.tex| \
       perl -lne 'while ($_) {
            $_ =~ s/(.)//;
            $count{$1}++};
            END {
               @chars = sort {$count{$b} <=> $count{$a}} keys %count;
               while (@chars) {
                  $c = shift @chars;
                  print "$c $count{$c}";
               }
           }'

The backslash key is often hard to stretch to, the curly braces usually require shift (or Alt-Gr on some international keyboards). That's a lot of work for what is, as far as catching mammoths is concerned, something pretty useless.

My solution was to modify the keyboard. No, not with a hammer. With a nifty little program called xmodmap. This is a UNIX program which allows you to modify what the keys on the keyboard actually do. I use it to put the backslash where the semi-colon is (after all, who uses a semi-colon these days?), swap the curly braces and square brackets, and swap the numbers with their symbols (so pressing '3' produces '#' and 'Shift+3' produces '3').

Unfortunately, this method isn't very portable and I have to set it up for each machine. The problem is that it is a translation table from what the keyboard currently does to what you want it to do, so you first have to know what it currently does. However, it's fairly simple to explain how to set it up.

Suppose you want to put the backslash where the semi-colon is. First you need to find the keycode for the semi-colon. There are two ways to do this. Firstly, from a terminal run a program called xev. When you press a key in its window, it tells you lots of information about it in the terminal - including the keycode. The other way to do it is to run xmodmap -pke. This produces a list of all the current assignments from which you can read off the keycode for the semi-colon.

~% xmodmap -pke | grep semicolon
keycode   47 = semicolon colon oslash Oslash

(Yeah, I'm on a Norwegian keyboard.) Now you just need to remap that:

~% xmodmap -e 'keycode 47 = backslash colon oslash Ooslash'

Lo and behold! Keyboard modified.

Three things to note. Save the initial output of xmodmap -pke since if everything goes wrong typing:

~% xmodmap original_list

will reset it (though that might be difficult if you've reset all the keys! In that case, log out and log back in again). Secondly, to save you typing in all those commands every time, you can put them all in a file called, say, .xmodmap and put a line xmodmap ~/.xmodmap in your startup file. Gnome actually goes looking for these files and asks if you want to load them so you don't need to put them in your startup file. The final point is that you might want to consider having different keyboards for different tasks. I have a keyboard for writing LaTeX documents and a "normal" one for everything else; switching between them is easy using "hot keys".

Several years after figuring this out, I discovered that I was alone neither in the problem nor in the solution. Greg Kuperberg has also written about this, though his solution uses XKB rather than xmodmap. Your mileage may vary.


Here's the actual layout:

` 1 2 3 4 5 6 7 8 9 0 _ +
~ ! @ # $ % ^ & * ( ) - =

TAB Q W E R T Y U I O P [ ]
    q w e r t y u i o p { }

Ctrl A S D F G H J K L : " |
     a s d f g h j k l \ ' /

Shift > Z X C V B N M < > ? Shift
      < z x c v b n m , . ;

In addition, the "weird" keys along the bottom are mapped to various modifiers which give me access to other characters (most usually, øæå, as I'm in Norway). This is actually done with a two-stage xmodmaprc: the first stage changes my Scandinavian keyboard into something I'm a little more used to; the second stage does the TeX-related changes. I have one hotkey that starts Emacs and changes the keyboard all in one go!


Since you're asking for german umlauts: There is the Neo layout which is for german language. The wiki sounds rather interesting! There are even pages for TeX. (I have not used it myself, though).

http://wiki.neo-layout.org/wiki/

and

http://wiki.neo-layout.org/wiki/Neo%20f%C3%BCr%20Latex


Quick advice

Get a good keyboard, get a good text editor, and practice.

Detailed suggestions

A good programming keyboard will make your fingers happy with no re-mapping. Personally, I use the Happy Hacking Keyboard Professional II. I find that I can still type around 40 WPM, even stumbling over LaTeX syntax, when using this keyboard.

A good text editor with syntax highlighting and auto-complete will make writing LaTeX documents much, much easier. I got the keyboard above when I was using GNU Emacs, since it replaces the Caps-Lock key with a control key (you can accomplish the same with software remapping). Since then, I've used MacVIM, which uses a nice LaTeX Bundle. Currently, I use TextMate. If you're on Windows, you can use the similar E-Text Editor. I switched to TextMate because working with tables is much, much easier with the included bundle. TextMate also auto closes the (),$$,[], and {} sequences, plus others. It also handles quoting automatically (typing two backticks closes with straight quotes). I find the shortcuts for automatically closing environments is helpful, as well.

The one thing I picked up from Andrew Stacey's post is that your most common characters are going to be alpha characters. Typing math is going to be equally painful on most layouts. I find with enough practice, my fingers find special characters easily enough. If you're interested is gaining speed from typing alpha characters, I hear Dvorak is all the nerd-rage, but its efficacy is dubious.

Special characters

For printing special characters... that's why I use LaTeX. To me, typing \alpha is a lot easier than remembering an alt-code or option-key press, and faster than hunting and pecking on a character map. Umlauts, graves (or diacrticals), and special math characters are best expressed using the LaTeX macros, in my opinion. It avoids any problems that might arise when sharing files with others (no worries about UTF compatibilities). For instance, it's easy to type ü on my Mac, but it's just as simple to type \"u in LaTeX.

Tags:

Keyboard