Gloss Alignment

In linguistic texts, foreign-language example sentences are often presented with glosses, that is, a word-for-word translation. The script aligns these glosses and the words in the foreign language example.

This script* can be used to align glosses and their sources in example sentences of the following form:

Linguistic glosses

The script spaces the example and its glosses by applying kerning at the insertion point before the word that should be moved:

Language glosses

The cursor is immediately before the word speelt.

To use the script, simply place the cursor anywhere in the first line and run the script. To line up more than two lines of items, select those lines:

Language glosses

It’s not necessary to select whole lines: anywhere in the first and last lines will do.

Sub-examples are handled as well:

Language glosses

The single requirement is that there shouldn’t be any space characters at the start of sentences or their glosses. So in fact, you can have examples and their glosses at the margin, with no numbers at all.**

Word and gloss separators

The script expects normal spaces as word and gloss separators. There mustn’t be any spaces at the start of lines and there mustn’t be any spurious spaces – line-final spaces and strings of two or more spaces. (The assumption is that by the time you align example sentences all these spurious spaces have been cleared out.)

If the number of spaces in the example and its gloss do not match, the script shows an alert and exits. Fix the problem and run the script again. When fixing a problem, take into account two special cases, compound words and blanks, which we’ll discuss now.

Compound words and compound glosses

Because spaces are considered word bounderies, we need some way to ignore spaces in compound words when the gloss is a single word and, conversely, when a single word’s gloss is a compound word:

Language glosses

In this example we made the compound nog steeds a single word by replacing the normal space with a non-breaking space.

This example could be fixed in a different way as well. Because the non-breaking space is in fact considered a word, we can use it as a word placeholder:

Language glosses

In this example the non-breaking space is a placeholder gloss for steeds.

Blanks

Similarly, in examples that contain material that should not be glossed, you can use non-breaking spaces as placeholders to create blanks:

Language glosses

Hyphens

Some authors and/or publishers require that glossed morphemes are aligned separately:

Language glosses

Here the gloss -3SG is aligned with the mopheme -t. To enable this, open the script file and in change this line:

var lineUpHyphens = false;

change false to true. Save the script as a plain-text file.

Asterisks, question marks, brackets, etc.

A sentence-initial asterisk (to indicate ungrammaticality) is unindented so that the first word’s gloss aligns properly:

Language glosses

The unindent is done by changing the tab stop in the first line.

Similarly, glosses to bracketed words are moved to the right a bit so that the gloss aligns properly:

Language glosses

These cases are handled differently. The bracket is copied to before the gloss ‘his’ and the document’s [None] swatch is applied to it.

Troubleshooting

In my experience the only problem is sometimes that the numbers of spaces (and/or hyphens) don’t match. To find the problem, show the hidden characters (Type > Show hidden characters) and zoom in so that you can see the space characters. Don’t forget to use non-breaking spaces in compound words and to handle blanks.

Notes

* This script started life in the 1990s as a WordPerfect macro. I converted it to JavaScript when InDesign first supported it, and I have used it extensively in my typesetting work, which consisted mostly of linguistic texts – a lot of typology books with many foreign-language examples, syntax and morphology books, and linguistics journals. I must have set tens of thousands of glossed examples. This script has been one of the most important time savers for me.

** You’ll have noticed that the script could be used to set matrices:

Language glosses -- matrix

To change the width of the gutters, open the script file, look for To increase the gutter, and follow the instruction there.


Version history

23 Oct. 2025: Posted.


Peter Kahrel Script Repository
Hosted as a community service, and with deep appreciation, by CreativePro.com


Scripts by Topic | Script Index
Basic Script Installation and Use
How to Modify or Configure These Scripts
Resources for Scripting and Publishing Automation
About This Repository


Repository Home Page


Contact Peter Kahrel


Note to users: You run scripts at your own risk: CreativePro and the author accept no liability for anything that may be caused by any of these scripts. Always take the necessary precautions. We encourage you to save backups and test scripts on copies of your documents.

Bookmark
Please login to bookmark Close

This article was last modified on August 22, 2026

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading comments...