12

There are several ways to get long, uncooperative URLs to line-break in LaTeX. If you're using pdfLaTeX, then the hyperref package can break them across lines for you, roughly after any /. If you give it the [hyphens] option, it will also allow line breaks after any - characters. If you're not using pdfLaTeX, the breakurl package gives the same behavior. But the documentation for it says that it will never break URLs at dashes, because the dashes could be mistaken for hyphens that aren't part of the URL. Similarly the documentation for the hyphens option of hyperref warns that breaking after hyphens may lead to typographic confusion. Specifically, if LaTeX had to process a url like http://foo.com/is-this-read-able-or-readable, and happened to break after the third dash, then viewing the printed result:

   http://foo.com/is-this-read-
able-or-readable

is ambiguous: unless you as a reader already know that breakurl or [hyphens]{hyperref} never introduce new - characters, it is possible to read that url as is-this-readable-or-readable, which is incorrect.

So let's say you follow the warnings, and decide not to break after dashes. Then, naturally, you may find yourself with badly under- or over-full boxes. The recommended advice, that I normally follow, is to use FlushLeft, but even then you may have links that are particularly nasty to break into lines. So my scenario is:

  • Even FLushLeft thinks the line is underfull
  • There aren't any good characters to break the URL, after the last /
  • In the online case (i.e. viewing on a computer, not in printout), I need to support hyperref linking the URL properly -- so weird characters shouldn't show up in the text or hyperref could get confused.
  • I'm using pdflatex, if that impacts the solution at all.

My question (which I hope isn't duplicated by the many related questions on this site!) is, is there a way to configure \url so that I can break at any character, insert a carriage-return symbol (something visually similar to \hookleftarrow, I think) as the hyphen, and still get hyperref to link properly (i.e., not think the carriage-return is part of the text)? According to this question, it's tricky, and it doesn't look like it'll play nicely with hyperref. And other suggestions on other questions mostly boil down to "use ragged-right text". Obviously, I'd rather break at a breakable point in the URL rather than resort to inserting symbols, but this URL is particularly uncooperative...

Edited: I revised the description of the problem to (hopefully) clarify what I'm aiming for. I'm aware of solutions involving breaking at dashes; I'm trying to avoid any confusion about dashes versus hyphens in the first place :)

2
  • 2
    Are you compiling with LaTex or pfdLaTeX? pfdLaTeX is not suported by breakurl? Also, are you including breakurl after hyperref? It would be helpful to post a MWE. Sep 18, 2011 at 20:44
  • @PeterGrill, this was more of a thought experiment, I guess. I mostly use pdflatex, so yes, breakurl is ireelevant; I included to link to it as "related work". The point about hyphens still stands; for a long, dashed section, breaking at hyphens is ambiguous, unless and until you realize that it only breaks at existing dashes...
    – Ben Lerner
    Sep 19, 2011 at 13:09

3 Answers 3

11

Do you need to use breakurl? If not, \usepackage[hyphens]{url} mostly works (the box around the hypenated URL is not quite right).

\documentclass{article}
\usepackage[hyphens]{url}
\usepackage{hyperref}

\begin{document}
\url{https://tex.stackexchange.com/questions/28835/option-to-break-urls-with-carriage-return-symbol}

\bigskip% An alternative, if this is meant for online use:
\href{https://tex.stackexchange.com/questions/28835/option-to-break-urls-with-carriage-return-symbol}{Question about to break in URLs}
\end{document}

The [hyphens] option also seems to solve the problem with the bibliography (adapted form Url references in bibliography):

\documentclass{article}
\usepackage[hyphens]{url}
\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@misc{A01,
  author = {Lerner, Ben},
  year = {2011},
  title = {Break URLs},
  url = {http://short.domain.com/very-long-dashed-section},
}
\end{filecontents}

\begin{document}
\nocite{*}
\bibliographystyle{alphaurl}
\bibliography{\jobname}
\end{document}

enter image description here

UPDATE: 2011-09-20:

Borrowing from Replace hyphenation character by a backwards arrow, this uses a \hookleftarrow to be hyphen separator and seems to work. Here I have allowed every character to be breakable, but if that is not desired, you could selectively insert the \BreakableChar only at the characters you want to be breakable:

enter image description here

\documentclass{article}
\usepackage{hyphenat}
\usepackage{url}
\usepackage{xstring}
\usepackage{forloop}
\usepackage{xcolor}
%\usepackage{filecontents}

\newsavebox\MyArrowBox%
\sbox\MyArrowBox{$\hookleftarrow$}%
\makeatletter%
\newcommand*{\BreakableChar}{%
  \leavevmode%
  \prw@zbreak%
  \discretionary{\usebox\MyArrowBox}{}{}%
  \prw@zbreak%
}%

\newcounter{index}%
\newcommand{\AddBreakableChars}[1]{%
  \StrLen{#1 }[\stringLength]%
  \forloop[1]{index}{1}{\value{index}<\stringLength}{%
    \StrChar{#1}{\value{index}}[\currentLetter]%
    {\currentLetter\BreakableChar}%
  }%
}%

\newcommand*{\MyUrl}{https://tex.stackexchange.com/questions/28835/option-to-break-urls-with-carriage-return-symbol}%

\begin{document}
\parbox{4cm}{\textcolor{blue}{\AddBreakableChars{\MyUrl}}}

\bigskip
\parbox{7cm}{\textcolor{blue}{\AddBreakableChars{\MyUrl}}}
\end{document}
7
  • 1
    I'm aware of the [hyphens] option for hyperref, but that option can be misconstrued when reading the link text as hyphenating a word: breaking after the third dash in "is-this-link-read-able-to-break" is ambiguous between "read-able" and "readable". I'm asking more for a solution that involves a character that is visibly not part of the url, to avoid the hyphenation ambiguity entirely.
    – Ben Lerner
    Sep 19, 2011 at 13:15
  • I have been unable to get this to produce "read-able". ie, it does not seem to add any extra hyphens. Sep 19, 2011 at 18:26
  • Correct, the [hyphens] option won't introduce any new hyphens, but rather allow LaTeX to break after any existing ones. My point, which I guess I'm not articulating well, is that when reading a printout of this link (i.e., not the online case where you can click on the link), the reader can be confused whether the dash is part of the url or not, unless it's known that the typesetter (hyperref, in this case) would not introduce new dashes. My goal is to avoid the issue entirely, and use some other (invalid for URLs) character to indicate the line break. I'll clarify the original question.
    – Ben Lerner
    Sep 19, 2011 at 21:12
  • thanks! One quibble/irony -- though you've made the text blue, and indeed can break at any character (which is what I was after), you've eliminated calls to \url, which is the remaining part of the question. I've tried using \renewcommand{\url}[1]{\href{#1}{\texttt{\AddBreakableChars{#1}}}}, and it works in your MWE but not in my actual document (errors about missing numbers for \unhboxes and missing $ inserted...) What does \prw@zbreak do in your code, so I can try to figure out how it works?
    – Ben Lerner
    Sep 20, 2011 at 14:19
  • ok, figured out why I'm getting errors. Your code above is over-zealous in inserting breaks: if a url contains \_ or \~ or any other macro, then the inserted break after the ` will cause errors. So the forloop` needs to grab things one token at a time, rather than one character...
    – Ben Lerner
    Sep 20, 2011 at 15:01
3

The url and by extension, the hyperref package let you define break points in urls by specifying \UrlBreaks, \UrlBigBreaks and \UrlNoBreaks. The hyphens option does nothing else than adding the hyphen do the \UrlBigBreaks list. But there is another tuning point: the \UrlSpecials. Here you con define characters that shall do funny things. So we can make the breaking characters insert the carriage return symbol when line breaking occurs at them. Note that url's are actually set in math mode, so the breaking characters have to be defined as to map to math characters, and the breaking point.

There is one little drawback to this solution. In the original setup, with / being a \UrlBreak character, no line break can happen between two consecutive slashes, as in http://... When making the slash special, such a break can happen.

\documentclass[10pt,a4paper]{article}

\PassOptionsToPackage{hyphens}{url}
\usepackage[colorlinks]{hyperref}
\usepackage{dingbat} % for \carriagereturn
\newcommand{\urllb}{\discretionary{\carriagereturn}{}{}}

%% original breaking points of url package
% \def\UrlBreaks{\do\.\do\@\do\\\do\/\do\!\do\_\do\|\do\;\do\>\do\]%
%  \do\)\do\,\do\?\do\'\do+\do\=\do\#}%
% \def\UrlBigBreaks{\do\:\do@url@hyp}%

\renewcommand{\UrlSpecials}{\do\.{\mathchar`\.\urllb}%
                            \do\/{\mathchar`\/\urllb}%
                            \do\@{\mathchar`\@\urllb}%
                            \do\\{\mathchar`\\\urllb}%
                            \do\-{\mathchar`\-\urllb}%
                            \do\#{\mathchar`\#\urllb}}% 
                            % and so on; I'm too lazy to type all breaking characters 
                            % here, you get the idea
\begin{document}    
\urldef{\myurl}\url{http://tex.stackexchange.com/questions/28835/option-to-break-urls-with-carriage-return-symbol}
\parbox{3cm}{\myurl}

\bigskip

\parbox{4cm}{\myurl}

\bigskip

\parbox{5cm}{\myurl}
\end{document}

yields

sample output

2

As you are using hyperref, it's possible to break URLs manually. That gives you the freedom to put any character you care to in between the two halves, and more generally to fine tune the output.

The thing you need to be careful about is that the broken halves point to the right thing -- you can achieve that by using \href and \nolinkurl in place of \url.

\documentclass{article}
\usepackage{hyperref}
\usepackage{parskip}

\begin{document}
\href{http://tex.stackexchange.com/questions/28835/option-to-break-urls-with-carriage-return-symbol}{\nolinkurl{http://tex.stackexchange.com/questions/}}%
\(\hookleftarrow\)\\
\qquad\href{http://tex.stackexchange.com/questions/28835/option-to-break-urls-with-carriage-return-symbol}{\nolinkurl{28835/option-to-break-urls-with-carriage-return-symbol}}
\end{document} 
1
  • 1
    True, but these sorts of tricky situations mostly come up in bibliographic citations, and I don't want to be editing my .bib files since their references will be typeset at many different line lengths.
    – Ben Lerner
    Oct 19, 2012 at 20:08

You must log in to answer this question.

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