4

I have seen Force hyphenation for a word beginning with a digit, but I do not think the solution there applies in my case.

So, I use pandoc to write Markdown, and to export to PDF via Latex. I have the following Markdown document, test.md:

---
author: John Doe
date: \today
---

# The mimsy borogoves when outgrabeing STM32CubeProgrammer

 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere mauris justo, vitae molestie tortor mollis ut. Donec nec dolor consectetur, porttitor augue et, feugiat odio. Aenean quis sem tortor. Sed finibus justo sit amet massa consectetur malesuada. Quisque enim lacus, commodo in varius a, congue id nisl. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque auctor est vitae neque finibus dapibus. In vitae nisl in arcu fringilla faucibus non ac urna. Duis ut semper velit. Fusce turpis diam, elementum in libero fringilla, posuere sodales dolor. Quisque pellentesque, ipsum ac cursus ullamcorper, nibh nisi dictum ligula, sed pretium purus nunc eget dolor. Maecenas nec magna ullamcorper, suscipit erat et, mattis risus. Sed vehicula purus eu dictum varius.

The .tex output that pandoc generates based on this Markdown is a bit more complicated, but can be reduced to this, let's call it test.tex:

\documentclass[]{article}
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering

\author{John Doe}
\date{\today}

\begin{document}

\hypertarget{the-mimsy-borogoves-when-outgrabeing-stm32cubeprogrammer}{%
\section{The mimsy borogoves when outgrabeing
STM32CubeProgrammer}\label{the-mimsy-borogoves-when-outgrabeing-stm32cubeprogrammer}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere
mauris justo, vitae molestie tortor mollis ut. Donec nec dolor
consectetur, porttitor augue et, feugiat odio. Aenean quis sem tortor.
Sed finibus justo sit amet massa consectetur malesuada. Quisque enim
lacus, commodo in varius a, congue id nisl. Orci varius natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Pellentesque habitant morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Pellentesque auctor est vitae neque finibus
dapibus. In vitae nisl in arcu fringilla faucibus non ac urna. Duis ut
semper velit. Fusce turpis diam, elementum in libero fringilla, posuere
sodales dolor. Quisque pellentesque, ipsum ac cursus ullamcorper, nibh
nisi dictum ligula, sed pretium purus nunc eget dolor. Maecenas nec
magna ullamcorper, suscipit erat et, mattis risus. Sed vehicula purus eu
dictum varius.

\end{document}

The result of doing pdflatex test.tex then looks like this:

pdf output

Clearly the "STM32CubeProgrammer" is too long, and breaks the margins; Latex could have hyphenated it, but it does not know it.

So I tried adding in the preamble:

\hyphenation{STM32-Cube-Pro-gram-mer}

... however, that fails with:

! Not a letter.
l.5 \hyphenation{STM3
                     2-Cube-Pro-gram-mer}
?

Yes, indeed, 3 is not a letter ... and apparently "\hyphenation doesn't take numbers (Define global hyphenation for words containing numbers)"

So the thing is:

  • Since this heading should also end up in TOC, and is in pandoc Markdown (and it might end up in other formats), I would not like to add (La)TeX specific commands like \discretionary{-) (as suggested in hyphenation next to a parenthesis) in the heading itself

Thus, the best solution for me, would be to define a hyphenation for the "STM32CubeProgrammer" globally, and the let LaTeX take care of it (hyphenate when appropriate, in heading, or header/footer, or TOC) -- but I cannot use \hyphenation since it does not parse numbers.

So, my question is - is there anything else I could do in the Latex preamble, so that Latex treats "STM32CubeProgrammer" as a hyphenate-able word? I thought, if digits "break" hyphenation search, maybe Latex sees the portion after the 32 as a new word input for hyphenation, so I tried \hyphenation{Cube-Pro-gram-mer}, but that had no effect.

Maybe there is something that can be done, by temporarily changing the catcodes of 3 and 2, just so \hyphenation passes? Unfortunately, I do not know enough about the internals of Latex and catcodes, to be able to solve this myself ...

1
  • 2
    I would probably not hyphenate a title it is usually best to set titles ragged right – David Carlisle Apr 16 at 19:57
5

Based on Add hyphenation pattern for word with special characters

\documentclass{article}
\lccode`\2`\2
\lccode`\3`\3

\hyphenation{STM-32-Cube-Pro-gram-mer}

\setlength\textwidth{1mm} % just for this example
\begin{document}

. STM32CubeProgrammer% start with some character b/c pdflatex doesn't hyphenate the first word of a paragraph

\end{document}

enter image description here

As to the unintended side effects, adapting from the cited answer:

"yes they make 2 and 3 letters for the hyphenation algorithm so foobar23 will be looked up as the 8 letter word foobar23 not the 6 letter word foobar, this can affect hyphenation all over. But if you restrict it to areas just containing technical constructs it's probably ok as natural language hyphenation patterns are not always appropriate there anyway."

1
  • 1
    Thanks - I'll accept this answer, since it demonstrates how hyphenation can be hacked for this specific case, which I was not sure if it was doable at all. – sdbbs Apr 16 at 20:19
4

You could add \-

enter image description here

But it is usually best to set section titles ragged right then hyphenation is not needed.

enter image description here

For the first, use

\section{The mimsy borogoves when outgrabeing
STM32\-CubeProgrammer}\label{the-mimsy-borogoves-when-outgrabeing-stm32cubeprogrammer}}

A variant of you want to produce thi soutput without havig a tex-syntax \- in the markdown would be ti use a Unicpde spft hyphen chanracter U+00AD LaTeX will treat this as \-

\section{The mimsy borogoves when outgrabeing
STM32­CubeProgrammer}\label{the-mimsy-borogoves-when-outgrabeing-stm32cubeprogrammer}}

You can not see it here but there is a soft hyphen between 2 and C

and for the second add

\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries\raggedright}}
\makeatother

To the preamble.

3
  • Thanks for the answer - the problem with \- is that I write in pandoc markdown; so if I decide to use the same source for say HTML, the markdown interpreter will treat that as verbatim characters, and the heading will contain those in the output as well. So, basically, I can only use "preamble hacks" here, so to speak. However, the ragged right suggestion is excellent, might end up using that, too. – sdbbs Apr 16 at 20:21
  • 2
    @sdbbs you could use the unicode soft hyphen U+00AD character in the markdown that will act as a hyphenation point in most systems the same way x­y there is a soft hyphen between x and y here. – David Carlisle Apr 16 at 20:25
  • 1
    @sdbbs see update with soft hyphen – David Carlisle Apr 16 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.