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?

2

6 Answers 6

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

6
  • 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 7, 2011 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}. Apr 7, 2016 at 23:50
  • 3
    It doesn't work if the path contains non-ANSI characters.
    – kakyo
    May 12, 2016 at 19:14
  • This should really not be the accepted answer, it's too specific as @kayko suggests.
    – einpoklum
    May 21, 2018 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. May 22, 2018 at 5:16
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.

3
  • 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 7, 2011 at 12:23
  • Why does this change the default interword space for the used font? Aug 25, 2017 at 20:59
  • I asked a follow-up question at tex.stackexchange.com/questions/624295/… , since the effect of \justify in this answer is global (it applies to all \texttt after its first invocation)
    – Clément
    Nov 28, 2021 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.

6
  • 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. Jul 30, 2010 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. Jul 30, 2010 at 17:09
  • 6
    Oh, I forgot to mention :-) hyphenation is disabled inside tt text. Jul 31, 2010 at 0:43
  • 5
    is there any way to enable it then? May 5, 2014 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 tex.stackexchange.com/a/10378/13552) Feb 11, 2015 at 17:48
9

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

Example:

\texttt{
Super\\ Late\\ Reply
}
2
  • 1
    this simple enough for me to use! +1
    – samsamara
    Jan 28, 2018 at 8:50
  • 7
    This solution works provided you know where you want the line breaks. But it doesn't let \texttt calculate the breaks itself.
    – ben
    Apr 30, 2020 at 19:38
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.

3
  • How would you do the same for multiple characters? One could nest multiple \replace, but that gets unwieldy quickly. Dec 20, 2017 at 4:35
  • In Overleaf, this seems to do nothing. Do you need to use a particular interpreter? Jun 2, 2022 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 3, 2022 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}}}

You must log in to answer this question.

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