1

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.

  • See this post for potential answers (in addition to the proposed one) – BambOo Sep 21 '19 at 0:04
2

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}
  • 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 '19 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) – David Carlisle Sep 20 '19 at 23:17
  • That does the trick! Do I have to reset the other two as well? – Stefan Sep 20 '19 at 23:18
  • @Stefan no they are local so will be reset at the } – David Carlisle Sep 20 '19 at 23:18

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.