8

Can I tell LaTeX that it may hyphen words that contain an underscore _ always before the underscore?

I have a lot of code examples in text, like is_error_condition_enum and I would like to tell LaTeX it may hyphen this as:

Lorem ipsum is_error_-
condition_enum lorem.

always.

If that is not possible globally, how do I make an entry for some underscore-words like:

\hyphenation{is_-error_-condition_-enum}

because that entry is rejected, obviously.

In sourcecode I write words like these as \gpmono{is\gpus{}error\gpus{}condition\gpus{}enum}.

doncherry
  • 54,637
towi
  • 2,500
  • 2
  • 22
  • 29

3 Answers3

11

The underscore package provides a way to do this automatically: It modifies the defintion of the \_ command in order to allow hyphenation after the underscore. Furthermore, it allows you to simply write _ instead of \_ in text mode to produce an underscore. Everything you have to do is loading the package with

\usepackage{underscore}
diabonas
  • 25,784
  • Did you actually try this with the \hypenation{} (TeX!-)command? Because for me it's not working at all. Getting the same error: Improper \hyphenation will be flushed. ... – 0xC0000022L Jul 04 '19 at 09:49
9
\newcommand{\gpus}{\_\discretionary{-}{}{}}

If \gpus is used in other places where hyphenation is not wanted, you can modify its definition only in the argument of \gpmono:

\newcommand{\gpmono}[1]{\begingroup
   \renewcommand{\gpus}{\_\discretionary{-}{}{}}%
   \ttfamily#1\endgroup}

Of course, without knowing your original definitions of \gpmono and \gpus, I tried to guess.

egreg
  • 1,121,712
  • Ok, I looked up the definition of \gpus (comes from the style I use) and inserted \renewcommand{\gpus}{\symbol{"5F}\discretionary{-}{}{}} into my document. It works ok now. But a question: Does this now "inject" \discretionary at all my usings of \gpus? Is that necessary? Would it not be enough to define/declare it at one specific place once? I have no idea how \discretionary works (obviously). – towi Sep 12 '11 at 10:44
  • 2
    Yes, it will use \discretionary{-}{}{} in all places where \gpus is found. In alignments it will have no effects, unless the cell is a "paragraph cell", where you of course want again the hyphenation. – egreg Sep 12 '11 at 10:55
3

The package hyphenat enables hyphenation in words that contain underscores.

For example, the following TeX:

\documentclass{article}
\usepackage{hyphenat}
\begin{document}
Lorem\_ipsum\_dolor\_sit\_amet,\_consetetur\_sadipscing\_elitr,\_sed\_diam\_nonumy\_eirmod\_tempor\_invidunt\_ut\_labore\_et\_dolore\_magna\_aliquyam\_erat,\_sed\_diam\_voluptua.\_At\_vero\_eos\_et\_accusam\_et\_justo\_duo\_dolores\_et\_ea\_rebum.\_Stet\_clita\_kasd\_gubergren,\_no\_sea\_takimata\_sanctus\_est\_Lorem\_ipsum\_dolor\_sit\_amet.\_Lorem\_ipsum\_dolor\_sit\_amet,\_consetetur\_sadipscing\_elitr,\_sed\_diam\_nonumy\_eirmod\_tempor\_invidunt\_ut\_labore\_et\_dolore\_magna\_aliquyam\_erat,\_sed\_diam\_voluptua.\_At\_vero\_eos\_et\_accusam\_et\_justo\_duo\_dolores\_et\_ea\_rebum.\_Stet\_clita\_kasd\_gubergren,\_no\_sea\_takimata\_sanctus\_est\_Lorem\_ipsum\_dolor\_sit\_amet.
\end{document}

produces this output: enter image description here

As you can see, it does both hyphenation after underscores and within words.