2

I've got longer phrases separated with hyphens that I'd like to typeset in typewriter font. I'd like latex to break the lines to avoid them from overflowing:

\documentclass{article}
\begin{document}

    Normal font breaks the line:

    a-very-long-text-in-typewriter-font-separated-by-hyphens-that-should-break-the-line.

    But typewriter font does not:

    \texttt{a-very-long-text-in-typewriter-font-separated-by-hyphens-that-should-break-the-line.}
\end{document}

Just to be clear, I don't want latex to add more hyphens, I just want it to break the line at the existing ones.

1
  • See this post for potential answers (in addition to the proposed one)
    – BambOo
    Sep 21, 2019 at 0:04

1 Answer 1

3

enter image description here

\documentclass{article}
\begin{document}

\raggedright
    Normal font breaks the line:

    a-very-long-text-in-typewriter-font-separated-by-hyphens-that-should-break-the-line.

    But typewriter font does not:

    \texttt{%
     \hyphenchar\font=`\- % enable hyphenation
     \hyphenpenalty=10000 % disable it again
     \exhyphenpenalty=-50 % enable (encourage) it at explicit hyphens
     a-very-long-text-in-typewriter-font-separated-by-hyphens-that-should-break-the-line.}
\end{document}
4
  • Nice, but it seems to also affect any \texttt{} that come afterwards. Is there a way to limit it to one \texttt{} ?
    – Stefan
    Sep 20, 2019 at 23:15
  • @Stefan oh sorry you need to set \hyphenchar\font=-1 at the end of the text (inside the group, just before the } (it's a global setting) Sep 20, 2019 at 23:17
  • That does the trick! Do I have to reset the other two as well?
    – Stefan
    Sep 20, 2019 at 23:18
  • @Stefan no they are local so will be reset at the } Sep 20, 2019 at 23:18

You must log in to answer this question.

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