354

This question led to a new package:
xurl

I wish to typeset some relatively long URLs in a piece of text, and when I use \url{..}, the resulting text does not respect the margin boundaries that govern the main text body, instead going all the way to the edge of the paper before wrapping around.

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\url{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}
\end{document}

I'm compiling using pdflatex, although I doubt this makes a difference.

Suresh
  • 16,511
  • 14
  • 55
  • 64
  • 18
    ... but URL shortening (a) makes the destination opaque and (b) adds another indirection layer vulnerable to linkrot. – Reid Sep 16 '13 at 20:09
  • I also suggest to consider not solving the problem but use short URLs instead. There are several services like http://goo.gl which will help you here. If the document is printed, then its even easier to transfer the URL to Browser. – math Nov 28 '13 at 08:32
  • 2
    The problem with all the answers, I think, provided to this question is that (when a certain URL is broken across multiple lines) every PDF viewer which I tried will only highlight that line of the URL which one is clicking on (and not the complete URL), which is confusing. Anybody knows how to fix it? – O0123 Apr 16 '16 at 22:12

17 Answers17

257

(converting a previous comment to an answer)

A quick google search (alas, too quick) reveals one solution. Use the [hyphens] option with the url package:

\PassOptionsToPackage{hyphens}{url}\usepackage{hyperref}

Some recommendations also include inserting \sloppy in case latex is trying too hard to align things.

Suresh
  • 16,511
  • 14
  • 55
  • 64
  • 3
    Do you mean that it should look like this: \usepackage{url}[hyphens] ? This does not work for me. – sixtyfootersdude Oct 30 '10 at 15:08
  • 1
    Did you use both hyperref and url packages? I got an error saying a package conflict. – Display Name Jun 14 '11 at 22:20
  • 1
    I did use both. has never had a problem for me. – Suresh Jun 19 '11 at 20:11
  • 75
    @xport: hyperref loads the url package internally. Use \PassOptionsToPackage{hyphens}{url}\usepackage{hyperref} to pass the option to the url package when it is loaded by hyperref. This avoids any package option clashes. – Martin Scharrer Jul 12 '11 at 11:26
  • 10
    @Suresh A minimal usage-example with some blind text would be nice. – Martin Thoma Sep 20 '12 at 04:51
  • 11
    @Suresh @Martin Scharrer

    Both \usepackage[hyphens]{url} and \PassOptionsToPackage{hyphens}{url}\usepackage{hyperref} do not seem to work properly. Sure, the url is broken, but this happens at the right boundary of the page instead of at where the right margin begins. How should I solve this?

    – Adriaan Jan 10 '14 at 09:51
  • 24
    If the \PassOptionsToPackage{hyphens}{url} approach does not work, maybe it's "because you're trying to load the url package with a specific option, but it's being loaded by one of your packages before that with a different set of options. Try loading the url package earlier than the package that requires it. If it's loaded by the document class, try using \RequirePackage[hyphens]{url} before the document class." http://latex-community.org/forum/viewtopic.php?f=5&t=22526#p76347 – Alessandro Cuttin Mar 23 '15 at 16:37
  • 9
    I have just experienced the issue too. On my side, \PassOptionsToPackage{hyphens}{url} was not working because of BibLaTeX, I had to write that command and load hyperref BEFORE BibLaTeX was loaded, which solved the problem. Gonna edit the answer. – wget Apr 09 '15 at 08:54
  • Actually I made a new question and self-answered it, as this wasn't completely related to this one. – wget Apr 09 '15 at 09:55
  • 3
    It looks like (as you might expect) this only breaks URLs on hyphens, and doesn't solve eg https://www.example.com/foo/bar/baz/qux/quux/corge – Alec May 10 '18 at 07:33
  • Just add e.g.: \def\UrlBreaks{\do/\do-} after you loaded hyperref, this will include dashes for a linebreak – AdamKalisz Oct 10 '18 at 00:18
  • 3
    See the newer answer about the xurl package, which automatically resolves the issue. – Zach Boyd Oct 17 '18 at 01:26
  • 1
    This does not solve my "too-long"-issue with \url{very-long-url} on the overleaf platform. @ZachBoyd A link to the resource just here would be helpfull. Thanks – MasterControlProgram Apr 25 '20 at 18:54
  • It seems like \PassOptionsToPackage{hyphens}{url} is on for default for me, can that be the case? At least I didn't see any difference when using it in front of \usepackage{hyperref}. What made a difference was the use of \sloppy before the \url macro, but now LaTeX complains about an underfull \hbox instead. Ah, what to do :) – StrawberryFieldsForever Nov 12 '21 at 09:40
  • I tried using this solution in the case of using an \href with a long \url but I couldn't get pdflatex to compile on overleaf. Any ideas on this? – Steven Jun 18 '23 at 09:34
  • it did not work for me but the simple addition of the xurl package worked – lehalle Sep 21 '23 at 13:05
190

Herbert Voß created (December 2017) the package xurl.

This package allows urls to break everywhere, can be compiled simply with pdflatex, and has the same options as url (indeed, it loads url package).

Here an MWE:

% arara: pdflatex
\documentclass{article}
\parindent=0pt

\usepackage{xurl}

\begin{document}
\rule{\linewidth}{1pt}

An example of everywhere breaking url: 
\url{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}

Another example of everywhere breaking url: 
\url{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}

A third example: 
\url{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}

\end{document}

Here the output:

enter image description here

Update: since version 0.05 (December 2018) the package author have added support for biblatex which has its own url handling.

If you load xurl after biblatex this is done by default.

If you want to avoid it, load xurl before biblatex or use the option nobiblatex:

\usepackage[nobiblatex]{xurl}

For more info, see package documentation.

CarLaTeX
  • 62,716
  • This does not seem to work in a bib file. – J.-E. Pin Nov 23 '18 at 16:14
  • @carlatex There is already a question on line breaks of long URLs in biblatex bibliography. Would your answer also work for this question? – J.-E. Pin Nov 24 '18 at 12:01
  • @J.-E.Pin There was an update to the package, now biblatex is supported, see my renewed answer. – CarLaTeX Dec 11 '18 at 08:38
  • This and the other answers don't work for me! Screenshot: https://i.imgur.com/FwBWCLg.png. Source code: https://github.com/jamesray1/holochain-proto/commit/3233caadc075b387051cee1dc73d3d3469f8c8df#diff-9f7a258b295b34d531092c80da297b7fR28. I'll keep trying the others... – James Ray Mar 01 '19 at 05:14
  • @JamesRay Please post a new question with a minimal example of your code which reproduces your problem. – CarLaTeX Mar 01 '19 at 05:54
  • @CarLaTeX the PDF is actually failing to compile and refresh, so I can't tell if the changes have been effective yet. I had issues with installing LaTeX due to limited RAM, and rather than setting TMP, TMPDIR, or TEMP env variable to a directory that has enough free space available, I'm waiting for another computer to be built, tested and delivered, as my current one can lag, particularly with VS Code. – James Ray Mar 01 '19 at 06:03
  • @JamesRay No hurry! However, your comment here is not seen by many people, it is better to post an ordinary question, with a minimal example, explaining which is your problem. I'm sure you'll find someone who can help you. – CarLaTeX Mar 01 '19 at 06:24
  • It seems not working for xelatex. Probably my hyperref has conflict? – Alpha Huang Apr 22 '19 at 17:05
  • @XingangHuang It's better if you ask a question on the main site, I'm not an expert of XeLaTeX, I don't use it, sorry. – CarLaTeX Apr 22 '19 at 17:08
  • @Kebby When the answer was accepted, xurl didn't exist yet :) – CarLaTeX Oct 05 '21 at 19:25
  • @CarLaTeX Thank you for the answer! This package \xurl is great but is it its default behavior to use a different font for the URL than the text (as seen in your screenshot and also my own attempt)? Is it possible to use the same font as the text? – nanjun Mar 27 '23 at 01:50
  • @nanjun Try with \urlstyle{same}. It should use the same font as the text: https://tex.stackexchange.com/a/87093/101651 – CarLaTeX Mar 27 '23 at 04:56
  • I tried using this solution in the case of using an \href with a long \url but I couldn't get pdflatex to compile on overleaf. Any ideas on this? – Steven Jun 18 '23 at 09:34
  • @Steven Please ask a new question with a minimal example – CarLaTeX Jun 18 '23 at 12:05
  • @CarLaTeX Thanks for your reply. I have posted my MWE here: https://tex.stackexchange.com/questions/689423/using-xurl-package-with-href-and-a-long-url-within-the-longtable-package – Steven Jun 23 '23 at 23:25
  • 1
    @Steven Is David's answer OK for you? – CarLaTeX Jun 24 '23 at 08:24
  • @CarLaTeX Yes! Thank you! My issue is resolved. – Steven Jun 24 '23 at 08:46
94

The package url defines a command \def\UrlOrds{\do\*\do\-\do\~\do\'\do\"\do\-}% which can be added to the default url break characters at which a line can be broken. Below, the line \g@addto@macro appends the list of characters defined in \UrlOrds to the \UrlBreaks macro.

\documentclass{article}
\textwidth=8cm
\parindent=0pt

\usepackage{url}
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\makeatother
\begin{document}
\rule{\linewidth}{1pt}

foo bar baz \url{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-}

\end{document}

output example

The same effect could be obtained by using the more usual \renewcommand on \UrlBreaks, but this would remove the characters that were already defined in \UrlBreaks.

Edit:
If you load package etoolbox you can reduce these 3 lines:

\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\makeatother

to this oneliner:

\gappto{\UrlBreaks}{\UrlOrds}
Speravir
  • 19,491
  • 3
    woohoo! This is the only one that worked for me. The other similar answer by @xamde blew TeX's capacity. – Joe Corneli May 22 '13 at 13:28
  • Thanks! Works in combination with sloppypar wonderfully! – gilu Feb 05 '16 at 07:58
  • I wonder why xurl does not have that characters in its UrlBreaks definition. – koppor Jan 09 '18 at 07:49
  • 1
    @koppor Oh, it has, at least in version from 2018-12-23 (which is later than your comment).\UrlBreaks is just redefined from scratch, but includes the characters that are in \UrlOrds in url.sty. – Speravir Jan 04 '20 at 00:25
  • I tried using this solution in the case of using an \href with a long \url but I couldn't get pdflatex to compile on overleaf. Any ideas on this? – Steven Jun 18 '23 at 09:36
80

I carefully read the documentation of the url package (I'm using PDFLaTeX) and found the following to work quite nice to let LaTeX add line breaks after every normal alphabetic character:

\expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks%  save the current one
  \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
  \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
  \do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D%
  \do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N%
  \do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X%
  \do\Y\do\Z}

Update:
Herbert Voß (who provided the edited version above with all the \expandafters) created a package xurl which makes it much simpler (see also answer of CarLaTeX). This package takes also care of biblatex and does also include numbers and the characters that are defined as \UrlOrds in url, cf. this other answer (which is also from Herbert, though you can not recognize this anymore.)

Speravir
  • 19,491
  • 7
    None of the previous (higer-rated) suggestions worked for me with a troublesome URL I was dealing with. This option did work. Are there any potential problems with this approach? All I see is that it may not always break at a "visually appealing" part of a URL. – SSilk Jun 25 '12 at 13:19
  • I used the same solution, but with UrlBigBreaks. Does anybody know whether this has any adverse effects? – user1202136 Jan 20 '14 at 11:03
  • For the record, I was having the url problem while using \url{} inside enumerate environment. The other solutions were fine when the \url was inside the text. But this answer is the only one that fixed the url problem inside enumerate. Thank you! – Jorge V Dec 14 '15 at 03:59
  • 1
    This solution seems to work better than the others, but I experienced a problem in the limit case when the url ends close to line ending; LaTeX is unable to put the word following the url to a new line and puts such word (or at least the first syllable of it) in the same line, even if it protrudes from right justification, with really bad results. – mmj Nov 28 '16 at 11:27
  • 2
    I had to cite the following link in a bibliography. http://www.ons.gov.uk/economy/nationalaccounts/uksectoraccounts/datasets/grossandnetcapitalstocksfortotaleconomybyindustryandassetincurrentpricesandchainedvolumemeasures This answer was the only solution. – Hugh Dec 05 '16 at 12:35
  • It works for me, unlike the other proposed alternatives. – eightShirt Feb 15 '20 at 07:21
  • I tried using this solution in the case of using an \href with a long \url but I couldn't get pdflatex to compile on overleaf. Any ideas on this? – Steven Jun 18 '23 at 09:37
32

Using the sloppypar environment as in

\begin{sloppypar}
  Figure \ref{fig:example_instant_lumi}:
  Public result available from \url{https://twiki.cern.ch/twiki/bin/view/AtlasPublic/LuminosityPublicResults}, version of 16.12.2011.
\end{sloppypar}

helps in my case. Without sloppypar the AtlasPublic part runs over the right margin, with sloppypar LaTeX breaks after view/ and everything's fine. (Doesn't need the url package.)

  • I found that sloppypar and the url package worked well together. – Faheem Mitha Sep 22 '13 at 18:44
  • I have combined the suggestion by @Herbert and yours, and it looks like this method works for me. But I found one "full stop" notation below the reference entry. Is there any way to eliminate that? Thank you. – Vijay Sep 22 '14 at 05:47
  • 1
    This also works with biber. \begin{sloppypar} \printbibliography \end{sloppypar} – koppor Feb 02 '17 at 08:17
16
  1. \usepackage[hyphens]{url} should be ok. If it does not work, then go to (2).

  2. \RequirePackage[hyphens]{url} before \documentclass.

  3. \expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks% save the current one \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j% \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t% \do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D% \do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N% \do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X% \do\Y\do\Z}

    It does work, but it is not a good way because it breaks the url address at an arbitrary place, which looks ugly.

  4. \sloppy sometimes does work, sometimes does not. It is not a thorough solution.

I recommend Solution (1) or (2).

STUDY
  • 161
  • 1
    \soppy should be used iin a group only. – Johannes_B Oct 08 '15 at 09:10
  • 4
    I feel this is more or less a mash-up of other answer already given, can you explain what your approach adds to the answers present. – moewe Oct 08 '15 at 09:30
  • 1
    @moewe I have surveyed and summarized the answers above. – STUDY Oct 08 '15 at 10:31
  • Mhhh, yes, I thought that as well. But normally I think this is left to the reader and we prefer not to add answers that don't introduce new approaches. – moewe Oct 08 '15 at 10:33
  • @moewe I agree with you in general. But I think it is also important and necessary to tell others which approaches are formal and which are unsuitable. After all, people's time is precious. At the same time, I have troubled with this problem for so long a time that I want to tell the correct answer to you once I find it. – STUDY Oct 08 '15 at 10:48
  • 4
    I think you can show your approval or disapproval of certain solutions by voting and commenting, so those summary answers are of no real use. But then again, some people may like those summary answers. I just wanted to point out that these answers are really unusual here. – moewe Oct 08 '15 at 15:20
  • 1
    @moewe Yes, you are right. But the only problem is that I have no enough reputations to comment others' answers. – STUDY Oct 09 '15 at 06:17
  • Ah well, that is the reputation system for you ... – moewe Oct 09 '15 at 06:35
  • 1
    Upvoting the best answers ensures they are on top and seen first. This is our way of making a summary ;-) – Johannes_B Oct 12 '15 at 08:53
  • Your number 2. solution worked for me since I am using the url package and the Tufte Book Class. I was having the issue of the URL's extending beyond the margin of the page in the margin notes. – Steven Sep 04 '22 at 04:17
16

If it's not necessary for the url to appear verbatim, you could use \href{url}{text}. That way you could give the url a human-readable form. If your document is used electronically you just use it as an hyperlink and if the document is printed you could (or should) question the relevance of long (and probably cryptic) url's.

Pieter
  • 5,411
  • 8
    URLs often appear in bibliographies. For an online resource, they should not be omitted. – TH. Sep 13 '10 at 10:02
  • I could have reformatted the text as a bibliography, but in my particular setting this would have been too clunky. – Suresh Sep 14 '10 at 07:17
  • 2
    For some ungainly long URLs one can set up URL-shortened links via tinyurl.com, bit.ly, or other similar sites. Then the printed link can be the readable short link but the underlying link in the PDF can go to the original URL. – András Salamon Sep 26 '10 at 15:59
  • 6
    You can also use \href with the text set to the url, using \texttt, and putting the line breaks in explicitly. You gain flexibility at the price of a little more work. – Tom Zych Sep 15 '12 at 00:00
  • 1
    Instead of \texttt with manual line breaks, you can use the \path command from the url package, as mentioned in this question. This is the only way I was able to get sensible display of URLs in a bibliography on the arXiv. – Mike Shulman Jul 23 '13 at 21:14
  • I tried using this solution in the case of using an \href with a long \url but I couldn't get pdflatex to compile on overleaf. Any ideas on this? Here is my MWE using \xurl package: https://tex.stackexchange.com/questions/689423/using-xurl-package-with-href-and-a-long-url-within-the-longtable-package – Steven Jun 24 '23 at 00:58
9

You may also want to look at the breakurl package.

adl
  • 1,258
7

According to the documenation, BigBreaks is the way to go otherwise it breaks at the first character.

\def\UrlBigBreaks{\do\/\do-\do:}

See: http://texdoc.net/texmf-dist/doc/latex/url/url.pdf I had the problem that a line break was inserted always after http:, even when using \UrlBreaks, the documentation clarified that this is the intended behavior.

Mensch
  • 65,388
Julian
  • 71
  • I tried using this solution in the case of using an \href with a long \url but I couldn't get pdflatex to compile on overleaf. Any ideas on this? – Steven Jun 18 '23 at 09:41
4

Not using \url, but \href, the URL is not visible anyway, so the suggestion in regard to employing tinyurl is relevant as it does avoid the ugliness of the URL in the text.

Much better, I find, to make an appropriate part of the text active, as in \href{URL}{active in context text}.

This way, the URL is not visible in the print out either.

Torbjørn T.
  • 206,688
3

I troubles I had with url breaking in an itemize environment could be solved by adding \Urlmuskip = 0mu plus 1mu in the preamble.

Jonas
  • 31
3

The top-voted answer did not completely work for me and I really won't use the most ridicule way of requesting a line break for each letter of the alphabet(!) as some answers suggest here. (I'd wonder what happens if you then have a lot of emojis in your URLs or so, which is – theoretically at least – a thing in 2019.)

Instead though, I found this nice solutions, which really seems to force breaking URLs at any position:

% always break URLs anywhere!
\setcounter{biburllcpenalty}{9000} % lower letters
\setcounter{biburlucpenalty}{9000} % upper letters

Source is this forum post, which actually refers to this German forum post.

In my testing it worked, and, okay, I admit, I did not test it with emojis.

rugk
  • 1,090
  • 1
  • 10
  • 27
3

I have a similar problem with itemize environment. When a word (or an url) is too long to respect the margin it does not starts a newline. Using \sloppy works but is very ugly.

My LaTeX code is generated by rst2latex.py and I use pdflatex on it.

I found that much better solution on the latex-community.org forum :

\tolerance 1414
\hbadness 1414
\emergencystretch 1.5em
\hfuzz 0.3pt
\widowpenalty=10000
\vfuzz \hfuzz
\raggedbottom

So I add this snippet in my docutils.tex and everything is just fine !

Stan
  • 131
  • 1
    I think it is ok to post this here, but please have a look, if this works for the original problem too. – bodo Sep 14 '12 at 11:59
  • I have checked with urls and it works. I can't remember why I was talking about the abstract context, maybe my brain was still in another post. – Stan Sep 14 '12 at 15:39
1

I used the .bib file with these packages.

\usepackage{breakurl}
\usepackage{url}

And that made the work.

Stefan Pinnow
  • 29,535
1

I would like to link to the following question here on TeX-SE: https://tex.stackexchange.com/a/207712/128658

Followed by \Urlmuskip=0mu plus 1mu\relax this does result in URLs that are properly broken over multiple lines without causing any overfull boxes. Plus this does also work for URLs in bibliographys (opposed to @xamde's answer to this question).

If you are doing this sorcery inside a package instead of your actual document you have to encapsulate the counter-changes in a \AtBeginDocument as they aren't defined otherwise. It could therefore look something like this:

% set special behaviour for hyperlinks in pdfs
\RequirePackage[breaklinks=true, pdfencoding=auto]{hyperref}
\g@addto@macro\UrlBreaks
{%
    \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
    \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
    \do\u\do\v\do\w\do\x\do\y\do\z\do\&\do\1\do\2\do\3%
    \do\4\do\5\do\6\do\7\do\8\do\9\do\0\do\/\do\.%
}
\g@addto@macro\UrlSpecials
{%
    \do\/{\mbox{\UrlFont/}\hskip 0pt plus 10pt}%
}

\Urlmuskip=0mu plus 1mu\relax

\AtBeginDocument{%
    \setcounter{biburlucpenalty}{1}  %break URL after uppercase character
    \setcounter{biburlnumpenalty}{1} %break URL after number
    \setcounter{biburllcpenalty}{1}  %break URL after lowercase character
} 
Raven
  • 3,023
1

The solution that works perfect for me both for bibtex and biblatex is the combination of sloppypar that @fuenfundachtzig mentioned with the \usepackage[hyphens]{url} that proposed by @Suresh.

The next code is used to be able to use both ways (bibtex and biblatex) to compile your file and works for me in cases of natbibs agsm style and bibers authoryear. Actually you don't have to use both ways and the method will work (I think) anyway if you just keep the code of each case (bibtex or biblatex) [except of the trick of @moewe with the url that solved the problem I had in my post here that will be possibly need to be adjusted according to the bibtex style we will use].

\documentclass{article}
\usepackage{filecontents}
% \UseBibLaTex is a variable that if is less than 1 the document 
% will have to compiled with `bibTeX`... else with biber
\def\UseBibLaTeX{1}
% ``url'' package is used in order to make LaTeX to break the urls 
% in places like the bibliography combined with the next (upcoming) 
% command
%\usepackage{breakurl}
\usepackage[hyphens]{url}
\Urlmuskip=0mu plus 1mu minus 5mu


\ifnum\UseBibLaTeX>0
\usepackage[backend=biber,style=authoryear]{biblatex}
\addbibresource{myBib.bib}
\else
\usepackage{natbib}
\def\harvardurl#1{{\bfseries URL:}\space\url{#1}}
\fi


\begin{filecontents*}{myBib.bib}
@Article{cite1,
title={One Article with a url that has to break both in bibtex and biblatex},
author={Someone NotMe and Someother Me},
year={2018},
month={10},
url={http://A-long-URL-that-has-to-break-through-lines-and-biber-has-to-ignore-the-url-command},
doi={http://dx.doi.org/10.1038/nphys1170}
}
@Article{cite2,
title={One Article with another url that has to break both in bibtex and biblatex},
author={Someone NotMe and Myself Me},
year={2017},
month={10},
url={http://AlongUrlThatHasToBreak.ThroughlinesAndBoth.BiberAndBiblatex.HasToAcceptTheCitation.com},
doi={http://dx.doi.org/10.1038/nphys1170}
}
\end{filecontents*}

\title{Double Functionality about Bibliography}
\author{Konstantinos Leledakis}
\date{October 2018}

\begin{document}

\maketitle

\nocite{*}
\ifnum\UseBibLaTeX>0
\begin{sloppypar}
\printbibliography
\end{sloppypar}
\else
\bibliographystyle{agsm}
\begin{sloppypar}
  \bibliography{myBib}
\end{sloppypar}
\fi

\end{document}

Output with bibtex:

enter image description here

Output with biblatex (biber):

enter image description here

koleygr
  • 20,105
1

There is another possibility to force a linebreak by using a user-defined character inside the url. This is documented in the url package under 5.2.1 Arbitrary character actions:

You can do arbitrarily complex things with characters by specifying their definition(s) in \UrlSpecials. This makes them ‘active’ in math mode (mathcode "8000). The format for setting each special character c is: \do\c{hdefinitioni}, but other definitions not following this style can also be included. Here is an example to make “!” inside \url force a line break instead of being treated verbatim (it uses LATEX’s \g@addto@macro):

\makeatletter
\g@addto@macro\UrlSpecials{\do\!{\newline}}

I think there are some downsides like using only ascii characters as the user-defined character (maybe, I'm not sure), and to have the user-defined character in the original url text, of course. I successfully tried the character | which I haven't found in any of my urls.

So you can write:

\url{https://what-an-example.org/subfolder/view.!php?id=12345}

which will give you:

https://what-an-example.org/subfolder/view.
php?id=12345
colidyre
  • 246