[Tex/LaTex] Insert hyphenation point in BibTeX using PDFLaTeX and hyperref

bibtexhyperrefhyphenationline-breakingpdftex

How to tell PDFLaTeX to make hyphenation in place where I want it to be?

I have my document file:

\documentclass[10pt,a4paper]{article}

\PassOptionsToPackage{hyphens}{url}\usepackage{hyperref}
\usepackage[margin=2.5cm]{geometry}

\begin{document}

This is text with \cite{reference}.

\bibliographystyle{unsrt}
\bibliography{bib_file}

\end{document}

and bib_file.bib file:

@misc{reference,
    title = {{Some dummy title in bib.}},
    howpublished = {\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html\#concurrency}},
    note = {Last access: 12.03.2013},
}

After building that file, the new line is places just after '.':

1st line: Some dummy title in bib. http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.
2nd line: html#concurrency. Last access: 12.03.2013.

But I wanted to insert hyphenation point on other place, like:

1st line: Some dummy title in bib. http://docs.oracle.com/javase/1.5.0/docs/
2nd line: relnotes/features.html#concurrency. Last access: 12.03.2013.

or even:

1st line: Some dummy title in bib. http://docs.oracle.com/javase/1.5.0/docs/ <-/
2nd line: relnotes/features.html#concurrency. Last access: 12.03.2013.

<-/ – is the enter arrow.

I tried to insert {\-} and \- into the url, but it adds in that place \discretionary{-
}{}{}
text

Best Answer

The solution in this answer already shows how to fine tune the line breaking of URL's by specifying \UrlBreaks, \UrlBigBreaks, and \UrlNoBreaks.

To get a carriage return symbol at breaking points, we specify the breaking points as \UrlSpecials, that include the symbol when a line break occurs there.

\usepackage{dingbat}  % for \carriagereturn symbol
\newcommand{\urllb}{\discretionary{\carriagereturn}{}{}}
\renewcommand{\UrlBreaks}{}    % no standard breaking points
\renewcommand{\UrlBigBreaks}{}
\renewcommand{\UrlSpecials}{\do\.{\mathchar`\.\urllb}%
                            \do\/{\mathchar`\/\urllb}%
                            \do\@{\mathchar`\@\urllb}%
                            \do\\{\mathchar`\\\urllb}%
                            \do\-{\mathchar`\-\urllb}%
                            \do\#{\mathchar`\#\urllb}}% and so on

To specify breaking points by hand we use a symbol that hopefully won't occur in any URL like a star *, and make it a break URL's like the above symbols, but vanishing from the string:

\renewcommand{\UrlSpecials}{\do\*{\urllb}}

In order to have correct links with this solution the star needs to be filtered out from the link, too. We use the gorgeous solution of Qrrbrbirlbel:

\makeatletter
\def\strip@star#1*#2\@strip@star{%
    \expandafter\def\expandafter\strip@star@result\expandafter{\strip@star@result#1}%
    \if\relax\detokenize{#2}\relax\else
        \def\strip@star@temp{#2}%
        \expandafter\expandafter\expandafter\strip@star\expandafter\strip@star@temp
        \expandafter\@strip@star
    \fi
}
\patchcmd{\hyper@linkurl}{\Hy@pstringdef\Hy@pstringURI{#2}}{%
    \def\strip@star@result{}%
    \expandafter\strip@star#2*\@strip@star
    \Hy@pstringdef\Hy@pstringURI{\strip@star@result}%
}{}{}
\makeatother

Of course, both solutions can be mixed, i.e., we can specify the normal breaking points as \UrlSpecials, as well as the star for additional hand made breaking points.

Complete code:

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@misc{reference,
    title = {{Some dummy title in bib.}},
    howpublished = {\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#*concur*rency}},
    note = {Last access: 12.03.2013},
}
\end{filecontents*}
\documentclass[10pt,a4paper]{article}

\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}
\usepackage[margin=2.5cm]{geometry}

\usepackage{etoolbox}
\usepackage{dingbat}  % for \carriagereturn symbol
\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}%

\makeatletter
\g@addto@macro{\UrlNoBreaks}{\do\.} % do not break after dot '.'
\makeatother

\renewcommand{\UrlBreaks}{}    % no standard breaking points
\renewcommand{\UrlBigBreaks}{}
\renewcommand{\UrlSpecials}{\do\_{\mathchar`\_\urllb}%
                            \do\/{\mathchar`\/\urllb}%
                            \do\@{\mathchar`\@\urllb}%
                            \do\\{\mathchar`\\\urllb}%
                            \do\-{\mathchar`\-\urllb}%
                            \do\#{\mathchar`\#\urllb}}% and so on
% \renewcommand{\UrlSpecials}{\do\*{\urllb}}

%% solution of Qrrbrbirlbel, see https://tex.stackexchange.com/q/103870/21591
\makeatletter
\def\strip@star#1*#2\@strip@star{%
    \expandafter\def\expandafter\strip@star@result\expandafter{\strip@star@result#1}%
    \if\relax\detokenize{#2}\relax\else
        \def\strip@star@temp{#2}%
        \expandafter\expandafter\expandafter\strip@star\expandafter\strip@star@temp
        \expandafter\@strip@star
    \fi
}
\patchcmd{\hyper@linkurl}{\Hy@pstringdef\Hy@pstringURI{#2}}{%
    \def\strip@star@result{}%
    \expandafter\strip@star#2*\@strip@star
    \Hy@pstringdef\Hy@pstringURI{\strip@star@result}%
}{}{}
\makeatother

\parindent0pt
\begin{document}
This is text with \cite{reference}.

Automatic breaking with carriage return symbol:

Text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

\bigskip

\renewcommand{\UrlSpecials}{\do\*{\urllb}}
Hand tuned URL breaking:

Text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

\bibliographystyle{unsrt}
\bibliography{faramir2}
\end{document}

yields

sample output

All hail to Qrrbrbirlbel for the hard part.