89

I often format path names with \texttt which can be very long. Usually Tex will not break this text. The line will just create a hbox overflow. It looks like this:

................
...C:\documents and settings
................
................
................
................
................
................
................

How do I get Tex to create a line break?

András Salamon
  • 2,145
  • 3
  • 25
  • 33
Eduard Wirch
  • 993
  • 1
  • 6
  • 6

6 Answers6

83

If you are using the default LaTeX fonts (or a small number of others), you can enable hyphenation within \texttt using the hyphenat package:

\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage[htt]{hyphenat}
\begin{document}
\texttt{\lipsum[1]}
\end{document}

Note that this still won't do a great job of formatting arbitrary strings because it will be relying on the hyphenation patterns in use.

If your problem is about typesetting paths specifically, another option (arguably better) is to load the url package and use its \path{...} command:

\documentclass[twocolumn]{article}
\usepackage{url}
\begin{document}
Here is a long path: \path{/usr/local/texlive/2010/texmf-dist/tex/latex/biblatex/biblatex.sty}
\end{document}

This can have limitations with non-ASCII characters, however. If you get really stuck, you can force a line to end using \newline (aka \\) or \linebreak. These differ in important ways: \newline will cause the paragraph to end the line abruptly at that point, whereas \linebreak will still attempt to fill the line completely for justification purposes. This is illustrated in the following example:

\documentclass[twocolumn]{article}
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
Curabitur dictum grav$\bullet$\newline ida mauris. 
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
\bigskip

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
Curabitur dictum grav$\bullet$\linebreak ida mauris. 
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
\end{document}

enter image description here

  • 1
    Just a note: \path apparently breaks on forward slash /, but not on 'minus' (i.e. suggested/soft hyphens '\-' will be output verbatim, with the backslash included, and it will break on the backslash) - try for instance \path{A\-VERYLONGWORDENTRYISBEINGWRITTENHERE}. – sdaau Oct 07 '11 at 12:15
  • What if I don't want to actually make hyperlink to file pointed by \url{/home/user/something_that_doesnt_exist}? BTW: \url doesn't work with $, eg.: \url{$(HOME)/something}. – patryk.beza Apr 07 '16 at 23:50
  • 3
    It doesn't work if the path contains non-ANSI characters. – kakyo May 12 '16 at 19:14
  • This should really not be the accepted answer, it's too specific as @kayko suggests. – einpoklum May 21 '18 at 16:37
  • @einpoklum Well it's a bad question. Do you go by the title, or the example, or actual question in the text? IMO I answered correctly according to the problem that the OP was trying to solve. If \path breaks with UTF8 input, that's a different question. – Will Robertson May 22 '18 at 05:16
  • @einpoklum I've now edited the answer to be a bit more general. Anything else you'd like to see there? – Will Robertson May 22 '18 at 05:22
49

Hyphenation and full justification is possible with typewriter text as well. Here's a command \justify for this purpose, shown with the example above:

\documentclass{article}

\usepackage{lipsum}

\newcommand*\justify{% \fontdimen2\font=0.4em% interword space \fontdimen3\font=0.2em% interword stretch \fontdimen4\font=0.1em% interword shrink \fontdimen7\font=0.1em% extra space \hyphenchar\font=`-% allowing hyphenation }

\begin{document} \texttt{\justify\lipsum[1]} \end{document}

alt text

Further the everysel package might be useful, as shown in Full justification with typewriter font.

egreg
  • 1,121,712
Stefan Kottwitz
  • 231,401
  • 1
    Thanks, @Stefan Kottwitz - Just wanted to note that \justify also seems to work (break) \texttt{\justify with only a few words} in a paragraph of otherwise different font. – sdaau Oct 07 '11 at 12:23
  • Why does this change the default interword space for the used font? – Blaisorblade Aug 25 '17 at 20:59
  • I asked a follow-up question at https://tex.stackexchange.com/questions/624295/how-can-i-save-and-restore-fontdimen-parameters , since the effect of \justify in this answer is global (it applies to all \texttt after its first invocation) – Clément Nov 28 '21 at 20:25
33

Actually, \texttt does break the text flow, as evidenced from the following test:

\documentclass{minimal}

\usepackage{lipsum}

\begin{document}
  \lipsum[1]
  \texttt{\lipsum[1]}
\end{document}

The problem is that \texttt doesn’t break the text correctly, because apparently it computes the line width incorrectly.

The easiest solution is to introduce soft hyphens at appropriate places:

\texttt{C:\doc\-uments and set\-tings}

This will use the hyphens as hints for possible word separators, and hyphenate the words accordingly. If that doesn’t help, a manual line break helps.

Though quite unsatisfactory, another advice is actually to reformulate the offending sentence so that it flows more nicely.

Konrad Rudolph
  • 39,394
  • 22
  • 107
  • 160
  • 2
    "because apparently it computes the line width incorrectly" — not true: TeX just doesn't have built-in rules for hyphenating URLs and paths (it's used to words). I suspect the example in the question is a slightly poor way to describe the problem since there a linebreak would have been found where the space is. – Will Robertson Jul 30 '10 at 16:40
  • @Will: try setting my MWE … you won’t get a properly formatted paragraph for the tt-ed text, even though it just contains normal text, so this isn’t due to missing hyphenation rules for paths. – Konrad Rudolph Jul 30 '10 at 17:09
  • 6
    Oh, I forgot to mention :-) hyphenation is disabled inside tt text. – Will Robertson Jul 31 '10 at 00:43
  • 5
    is there any way to enable it then? – Frederick Nord May 05 '14 at 12:40
  • 1
    @WillRobertson I would REALLY like to have an answer to Frederick Nord's question. (However, I think I just found it here http://tex.stackexchange.com/a/10378/13552) – Jonathan Komar Feb 11 '15 at 17:48
  • Stefan's answer above works in general, or the hyphenat package works if you're using the fonts it supports. I should add something to fontspec to make this a bit easier, too, at some point. – Will Robertson Feb 14 '15 at 06:12
9

Had this question myself and the answer turned out to be: \\

Example:

\texttt{
Super\\ Late\\ Reply
}
Luc
  • 214
4

I have an answer that uses a borrowed replace function from here: https://tex.stackexchange.com/a/213952/78981

I had a document where all the \texttt blocks were paths - at least the long ones. I used the following:

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\replace}{mmm}
 {
  \marian_replace:nnn {#1} {#2} {#3}
 }

\tl_new:N \l_marian_input_text_tl

\cs_new_protected:Npn \marian_replace:nnn #1 #2 #3
 {
  \tl_set:Nn \l_marian_input_text_tl { #1 }
  \tl_replace_all:Nnn \l_marian_input_text_tl { #2 } { #3 }
  \tl_use:N \l_marian_input_text_tl
 }
\ExplSyntaxOff

\usepackage{url}
\let\OldTexttt\texttt
\renewcommand{\texttt}[1]{\OldTexttt{\replace{#1}{/}{/\allowbreak}}}

The code makes all the \texttt commands call the snippet above which replaces every / with /\allowbreak, which allows the text to be wrapped into the next line. This is very similar to the \url command, but I went this route because it works better with Pandoc.

M. Gruber
  • 103
  • 3
  • How would you do the same for multiple characters? One could nest multiple \replace, but that gets unwieldy quickly. – Axel Rauschmayer Dec 20 '17 at 04:35
  • In Overleaf, this seems to do nothing. Do you need to use a particular interpreter? – pretzlstyle Jun 02 '22 at 15:05
  • Unfornately, I haven't used LaTeX much after this, and it's been too long to remember clearly. There's a good chance I was doing this with Pandoc, or just using standand Linux (Ubuntu) or OSX (Homebrew) packages – Charles L. Jun 03 '22 at 16:31
1

I had a similar requirement, but I wanted to split at a period rather than a forward slash, so I used code from the answer: https://tex.stackexchange.com/a/213951

\usepackage{xstring}
\let\oldtexttt\texttt
\renewcommand{\texttt}[1]{\oldtexttt{\StrSubstitute[0]{#1}{.}{.\allowbreak}}}
Tahir Hassan
  • 111
  • 3