# Control URL breaking

Is there a way to control the url breaking with biblatex? I am using ACS Guide and it says:

Note: Different web browsers break the text in different places of a URL. In a printed work, if the URL needs to be broken at the end of a line, the break should be made after a colon or a double slash; before a single slash, a tilde, a period, a comma, a hyphen, an underline, a question mark, a number sign, or a percent symbol; or before or after an equals sign or an ampersand.

I am also using the url package to typeset the url with the same font as my text. I read the package documentation and I am a bit confused by the macros \UrlBreaks and \UrlBigBreaks, butfrom what I gather these can only be used to allow a break after the listed characters, but not before as some of the requirements of the style guide (and I personally would not like breakings before a period for sure!). Is there a way to do this?

• Maybe take a look at the xurl package. – Bernard Mar 5 at 23:58
• @Bernard I just read the documentation and unless it's the wrong file, it just says that it is a package that tells the url package to allow a break. I am far from an expert but the style file defines the \UrlBreaks for all the characters, which means it defines breaks after the character, not before. – ralk912 Mar 6 at 0:03
• The command you are after is \UrlSpecials. You can control breaks before characters with this. I find it's usage a bit complicated though… – David Purton Mar 6 at 4:53

I think this should do what you want. It defines a new style \urlstyle{acs} which redefines \UrlBreaks and \UrlSpecials according to your rules.

There's one minor issue. It will prefer to break between : and // rather than between the //, but if you have // somewhere else, there is potential to break between. I tried to fix this, but couldn't make it work. I don't think this will occur in a real example though.

# MWE

\documentclass{article}
\usepackage{url}

\makeatletter
\def\url@acsstyle{%
\def\UrlBreaks{\do\/}%
\def\UrlSpecials{%
\do\~{\penalty\UrlBreakPenalty\mathchar~}%
\do\.{\penalty\UrlBreakPenalty\mathchar.}%
\do\,{\penalty\UrlBreakPenalty\mathchar,}%
\do\-{\penalty\UrlBreakPenalty\mathchar-}%
\do\_{\penalty\UrlBreakPenalty\mathchar_}%
\do\?{\penalty\UrlBreakPenalty\mathchar?}%
\do\#{\penalty\UrlBreakPenalty\mathchar"23}%
\do\%{\Url@percent}%
\do\={\penalty\UrlBreakPenalty\mathchar=\penalty\UrlBreakPenalty}%
\do\&{\penalty\UrlBreakPenalty\mathchar&\penalty\UrlBreakPenalty}%
\do\ {\Url@space}\do\^^M{\Url@space}%
\Url@force@Tilde}%
}
\makeatother

\raggedright

\begin{document}

\hsize 5cm

\section*{Default Style}

\url{https://tex.stackexchange.com/questions/477945/control-url-breaking}

\section*{ACS Style}

\urlstyle{acs}

\url{https://tex.stackexchange.com/questions/477945/control-url-breaking}

\end{document}


• This definitely works in text. Any clue how to get bibtex to recognize these, though? – ralk912 Mar 6 at 17:01
• @ralk912 just put the code and the call to \urlstyle into your preamble after you load biblatex and it should just work. – David Purton Mar 6 at 20:56
• Ah! Ended up finding that for some reason I had to use the style after \begin{document} (I was using it in the preamble as \urlstyle{same} and it did its job just fine). I just added the line \def\UrlFont{} to your code to get the desired font. Thanks! – ralk912 Mar 7 at 17:46

You'll need three different packages, so include this in your code:

\usepackage{url}
\usepackage{breakurl}


Now, you'll just need to add \def\UrlBreaks{\do\/\do-} if you want to be able to break it after a / or a -.

@misc{GartnerNFC,
author = "Clark, Sarah",
howpublished = "nfcworld.com",
month = aug,
note = "Cited July 3rd 2012",
title = "{Gartner’s Hype Cycle places NFC at ‘Peak of Inflated Expectations’}",
url = "http://www.nfcworld.com/2011/08/11/39008/gartner-hype-cycle-places-nfc-at-peak-of-inflated-expectations",
year = "2011"
}


It will look like this:

For other options, look into the url package documentation.

• Maybe my question is not clear enough. I get how to indicate breaks after a character, but the style guide also indicates allowed breaks before a character, which unless I'm missing something, this won't do. – ralk912 Mar 6 at 0:06