0

In Polish and Czech typography short words should never be typeset at the end of a line, hyphenating a longer word is preferred instead. Sadly typing a NB space after every short word adds a lot of work (they are quite common). A simple general rule to apply would be automatically converting every space after a word 3 or less letters long to a NB space. (Not to mention the fact such a solution simply looks more elegant in any Latin script language).

In fact, for biological or chemical texts the rule should also extend to hyphens - a hyphen immediately after a 3 or less letter long word should be non-breaking, so e.g. an element symbol before a name of a chemical compound or a Greek letter in front of a protein name is not separated from what follows.

All existing solutions to the problem involve either a dictionary of words to insert a NB space after or cover only single letter words. This however does not work for many technical texts, because the rule should also take numbers into account (in a properly typeset text unit symbol is never separated from number it belongs to for example). So far I found no way to extend the code to cover any group of three characters.

(no MWE because IMO the matter is too general to make any use of one)

EDIT: upon some consideration I think this question could be divided in two parts. First would be setting a list of non-breaking strings, which should include explicit hyphen (-), dash (–), pause (—), colon (:) and colon surrounded by spaces ( : ) as all these character can appear as parts of a word in a chemical context. Second would be detecting words made of less than 4 letter-like characters (must include letters outside basic Latin script, e.g. Greek, and digits) and converting spaces behind them to non-breaking spaces, what I think could be done with a RegEx as a last resort.

9
  • 1
    That is LuaLaTeX only and covers just hyphens. Besides it's also just 1-char what's the main issue with all solutions I tried. Oct 6, 2020 at 16:31
  • you can't reasonably do that in TeX but should be a trivial edit in the text editor you use to write the source so I'd recommend that. Oct 6, 2020 at 17:55
  • Yeah, I was afraid of someone suggesting a RegEx parsing of the input... The problem is a RegEx like that must avoid tinkering with TeX functions, it should parse just the content text and leave all code parts alone. If anyone else tries RegEx'ing the input: don't forget your .bib file! Oct 6, 2020 at 18:02
  • Welcome to TeX.SE. Does your earlier comment imply that you're not interested in a LuaLaTeX-based solution? Please clarify.
    – Mico
    Oct 6, 2020 at 18:26

1 Answer 1

1

So I ended up moving the project to LuaLaTeX and writing pre-linebreak filters. File is a bit lengthy because it requires creating a Unicode/UTF-8 transition database (default Lua has no libraries to get category of a Unicode glyph which is necessary to provide cross-language support). https://gitlab.com/PawelMalecki/pawelualatex/-/blob/master/linebreak_lib.lua

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .