TransWikia.com

Apply lefthyphenmin to parts of a word spelled with hyphens

TeX - LaTeX Asked by corvus_192 on August 28, 2021

In German, there are some words that have a single letter separated with a hyphen, for example "E-Ink-Display", "E-Auto" (electric car), "E-Mail", "x-Achse" (x-axis) and many more. When hyphenating these, the single letter must not be split up.

Is there a way (maybe with lefthyphenmin) to apply this rule globally without having to use nobreakdash or similar everytime such a word is used?

MWE:

documentclass{article}
usepackage{showframe}
usepackage[american,ngerman,shorthands=off]{babel}
usepackage{amsmath}
begin{document}
    xxxxx xxxxx xxxxxxxxx xxxx xxxx xxx xxxxx xxxxxxxx xxxxxx xxxxx E-Auto
    
    should look like this:
    
    xxxxx xxxxx xxxxxxxxx xxxx xxxx xxx xxxxx xxxxxxxx xxxxxx xxxxx Enobreakdash-Auto
end{document}

enter image description here

3 Answers

As you are using babel with luatex, you can use babelposthyphenation:

usepackage[american,ngerman,shorthands=off]{babel}

babelposthyphenation{ngerman}{ ^().=() }
  { {},
    { pre=-, no=-, penalty=10000 }
  }

See Non–standard hyphenation with luatex for further info. Here I used the trick to limit the capture, because the ^ (word start) must be outside. It works with more or less straight texts (eg, no whatsits in the middle of a word). . is any char and = is an explicit hyphen. Perhaps the pattern can be fine tuned and ‘any char’ is not the best option, but it can be replaced by a list of chars.

EDIT. Here is a better pattern:

babelposthyphenation{ngerman}{ ^{A}*(){a}=() }

With {A}* we consider the possibility of leading characters like ( or , because {A} means ‘non-letter’ (it's the same as %A in lua). This part is placed before that to be processed, which is enclosed between () ().

Correct answer by Javier Bezos on August 28, 2021

Use U+2011 NON-BREAKING HYPHEN

documentclass{article}
usepackage{showframe}
usepackage[american,ngerman,shorthands=off]{babel}
usepackage{amsmath}
begin{document}

xxxxx xxxxx xxxxxxxxx xxxx xxxx xxx xxxxx xxxxxxxx xxxxxx xxxxx E^^^^2011Auto

xxxxx xxxxx xxxxxxxxx xxxx xxxx xxx xxxxx xxxxxxxx xxxxxx xxxxx E‑Auto

should look like this:

xxxxx xxxxx xxxxxxxxx xxxx xxxx xxx xxxxx xxxxxxxx xxxxxx xxxxx Enobreakdash-Auto

end{document}

enter image description here

Answered by egreg on August 28, 2021

Since you use LuaLaTeX, you can (a) set up a Lua function that replace all instances of <single alphabetical character>-<letter character(s)> with <single alphabetical character>nobreakdash-<letter character(s)> and (b) assign the function to LuaTeX's process_input_buffer callback. That way, it'll act like a preprocessor on the input code, before TeX starts its usual processing.

enter image description here

(The framelines in the preceding screenshot are drawn by the showframe package.)

documentclass{article}
usepackage{showframe}
usepackage[american,ngerman,shorthands=off]{babel}
usepackage{amsmath} % for "nobreakdash" macro

usepackage{luacode} % for 'luacode' environment
begin{luacode}
function nbdash ( s ) 
  return ( s:gsub ( "(%A%a)%-(%a+)" , "%1nobreakdash-%2" ) ) 
end
luatexbase.add_to_callback ( "process_input_buffer" , nbdash , "nbdash" )
end{luacode}

begin{document}
xxxxx xxxxx xxxxxxxxx xxxx xxxx xxx xxxxx xxxxxxxx xxxxxx xxxxx E-Auto
    
xxxxx xxxxx xxxxxxxxx xxxx xxxx xxx xxxxx xxxxxxxx xxxxxx xxxxx x-Achse
end{document}

Answered by Mico on August 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP