Insert hyphenation point in BibTeX using PDFLaTeX and hyperref
If you load package hyperref
or package url
LaTeX is able to find proper breaking points for an unmissleading hyphenation. As a result of this it often happen, that you get big white spaces in an justified text. It is not a good idea to insert in an url an additional -
sign to mark a hyphenation. The reader does not know whether the -
belongs to the url or is only a hyphenation sign. Urls are best hyphenated after a /
, .
etc.
I suggest to use package ragged2e
and to use the command \raggedright
to get a left justified bibliography. All the bibliographys I did this way looked very well!
Edit: I updated the MWE with a peace of code copied from this question and got the linebreak in the url in that way you want. But keep in mind: with this solution LaTeX decides were to break the url, not you with a special character.
Try this MWE:
\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/relnotes/features.html\#concurrency}},
note = {Last access: 12.03.2013},
}
\end{filecontents*}
\documentclass[10pt,a4paper]{article}
\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}
%https://tex.stackexchange.com/questions/3033/forcing-linebreaks-in-url?rq=1
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\g@addto@macro{\UrlNoBreaks}{\do\.} % do not break after dot '.'
\makeatother
\usepackage[%
newcommands % \RaggedRight=\raggedright etc.
,newparameters % use default settings of ragged2e
]{ragged2e}
\usepackage[margin=2.5cm]{geometry}
\begin{document}
This is text with \cite{reference}.
\bibliographystyle{unsrt}
{\raggedright % group to end left justification after bib
\bibliography{\jobname}
} % ends group for left justified bibliography
\end{document}
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
All hail to Qrrbrbirlbel for the hard part.