69

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?

  • 1
    I have the same problem. Apparently using the geometry package or the fullpage package causes \texttt{} command to fail in cutting lines... Hope it will help – user6818 Jul 21 '11 at 15:40
56

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 7 '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 7 '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 5:16
42

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{minimal}

\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.

  • 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 '11 at 12:23
  • Why does this change the default interword space for the used font? – Blaisorblade Aug 25 '17 at 20:59
26

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.

  • 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
  • 5
    Oh, I forgot to mention :-) hyphenation is disabled inside tt text. – Will Robertson Jul 31 '10 at 0:43
  • 4
    is there any way to enable it then? – Frederick Nord May 5 '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 tex.stackexchange.com/a/10378/13552) – Jonathan Komar Feb 11 '15 at 17:48
7

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

Example:

\texttt{
Super\\ Late\\ Reply
}
  • 1
    this simple enough for me to use! +1 – samsamara Jan 28 '18 at 8:50
2

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.

  • 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 4:35

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.