# rule based replacement of s with ſ (long s)

Is there a way in any LaTeX (LuaLaTex, LuaHBTeX, pdfTeX …) to automatically typeset a document written with standard «s» with long s (ſ) instead where appropriate? The rules seem to be mostly language independent and can be found here.

This is basically the opposite of this question.

I am aware of the OpenType character variant method and produce long s in Latex but those do not answer this question.

### MWE

\documentclass{article}

\usepackage[german,english]{babel}

\begin{document}
\section{s}
In German, the s stands (please no automatic replacement)
\begin{description}
\item[At the end of a word]
\foreignlanguage{german}{Das Haus, des Landes}
\item[At the end of a syllable]
\foreignlanguage{german}{dasselbe, Eispalast, Häuschen}
\item[word gap s if the following word is self standing]
\foreignlanguage{german}{Donnerstag, lostreten, deswegen}
\item[a word gap s in case of a following suffix syllable which begins with a consonant]
\foreignlanguage{german}{Häuslein, Wachstum, nachweisbar, Weisheit}
\item[in loan word prefix syllables dis- and des-]
\foreignlanguage{german}{Distribution, Desinfektion}
\item[before d, k, m, n and w]
\foreignlanguage{german}{Dresden, brüsk, Osman, Mesner, Oswald}
\end{description}
\section {ſ}
In German, the ſ stands (in the following example words s should be replaced by ſ   )
\begin{description}
\item[at the beginning of a word]
\foreignlanguage{german}{so, sieben}
\item[at the beginning and inside of syllables]
\foreignlanguage{german}{saufen, Wunsch, erstaunen}
\item[at the end of a syllable except composita]
\foreignlanguage{german}{Gasse, Wasser, Bissen, fassen}
\item[before p, t and ch (sch trigraph)]
\foreignlanguage{german}{Knospe, fast, löschen}
\end{description}
\section{ſſ}
The double ſſ stands (double replacement desired):
\begin{description}
\item[if there is as short vowel before the first s and any vowel is following the second s]
\item[if a following e is omitted by an apostroph]
\foreignlanguage{german}{ich lass' es sein}
\item[in flexed forms of words that end with -as, -is, -nis and -us]
\foreignlanguage{german}{die Ananasse, Wallisser Aprikosen, des Zeugnisses, des Busses}
\end{description}
\section{ß}
The Esszet ligature ſs ß stands:
\begin{description}
\item[at the end of a word]
\foreignlanguage{german}{muss, nass, Biss}
\item[at the end of a syllable]
\foreignlanguage{german}{hässlich, vergesslich}
\item[before a consonant]
\foreignlanguage{german}{lasst, hasst, wisst, verpasst, verlässlich}
\item[after a long vowel]
\foreignlanguage{german}{grüssen, Strasse, fliessen}
\item[after a diphtong]
\foreignlanguage{german}{heissen, aussen, scheusslich}
\end{description}
joking: It's that easy!

Rules courtesy of \texttt{www.deutsch-kurrentschrift.de}
\end{document}

• Would it be possible to edit your document to post a sample document with a few hundred words that contain single-s and double-s instances? That would make easier to test the correctness of some string pattern matching routines.
– Mico
Jun 15, 2020 at 18:17
• @Mico Thanks to your comment I am now aware of package selnolig which is affected by this question. Writers in Germany will probably directly enter a Eszet but in Switzerland you would type ss and hope for correct replacement either by ſſ or ß. But selnolig is not doing this today. The MWE for testing is included now. Jun 15, 2020 at 20:00
• Yeah, as of now, the selnolig package only deals with ff, fi, fl, ffi, ffl, and tt ligatures in German-language documents.
– Mico
Jun 15, 2020 at 20:13
• Exception cases in German where s and ſ result in a different meaning. I was not aware of that at time of writing the question. Jun 17, 2020 at 18:40

I'm working on a new feature in babel for luatex meant for changes like this, although it's based on the language, so it has its limitations (after all, babel is about languages). It's based on lua patterns and here is an example:

\documentclass{article}

\usepackage[german]{babel}

\babelfont{rm}{FreeSerif}

\babelprehyphenation{german}{s[a-eg-z]}
{ string = ſ,
{}
}

\begin{document}

abcs abcsde abcsfe

\end{document}


It prints “abcs abcſde abcsfe”.

Edit. To be honest, I've found some rules a bit vague, and on other hand, some rules are clearly impossible to carry out in a more or less simple way (how to know if a word is a loan word?). So, the solution is partial, but I hope it can help for similar cases, and also as a starting point which can be used to make tests and feature requests (after all, and as I said, I'm working on it, so it's a good time!). Well, and even contribute 🙂. Some rules are defined with \babelprehyphenation and some others with \babelposthyphenation. (I think there also rules related to hyphenation, and with \babelposthyphenation the discretionaries can be modified.)

ſ before p or t

\babelprehyphenation{german}{s[pt]}
{ string = ſ,
{}
}


Before ch

\babelprehyphenation{german}{sch}
{ string = ſ,
{},
{}
}


At the beginning of words

Here a special rule is necessary, which marks explicitly the text to be caught with empty matches. They are added automatically in other patterns.

\babelposthyphenation{german}{^()s()}
{
string = ſ
}


Beginning of a syllable

Assuming a discretionary marks a syllable boundary. Special cases would require additional rules.

\babelposthyphenation{german}{|s}
{
{},
string = ſ
}

• I added a MWE to my question. This rule set seems to be too simple. At the time of writing my question I was not aware that the rules are language dependent. Jun 15, 2020 at 19:41
• Sure, it was simple and even simplistic, Now with the MWE I can write a more complete set of rules (but no time right now, in a few hours). Jun 16, 2020 at 5:36
• @tanGIS A question. Is it more or less safe to assume ‘end of a syllable’ is equivalent to ‘discretionary’ (a candidate to a line break)? Rules like ‘At the end of a syllable’ are somewhat vague, because we need another rule, namely, where a syllable ends. But if an hyphen marks it, we could use the hyphenated word to take decisions. Jun 16, 2020 at 15:00
• I am no linguist. This is more complicated than I thought. My translation of the rules may be incorrect. For what I know the assumption is partially safe. The main exception is a single vowel syllable at the end of a word. Those are no line break candidates. Then it's unclear what a syllable is (and/or morpheme). See this SE question. So Wachstube and Wachſtube both exist but have a completely different meaning (wax collapsible tube vs. guard room). Jun 17, 2020 at 18:28