13

Using biblatex with alphabetic style, how could I insert a line break (\\ or \linebreak) before the URL output (with the aim to have less linebreaks within URL)? (I use url and hyperref)

schtandard
  • 14,892

4 Answers4

8
\documentclass{scrartcl}
\usepackage{xcolor}
\usepackage[style=authoryear,backend=biber,]{biblatex}
\usepackage[colorlinks=true,urlcolor=red!50!black]{hyperref}
\addbibresource{biblatex-examples.bib}
\DeclareFieldFormat{formaturl}{\newline #1}
\newbibmacro*{url+urldate}{%
\printtext[formaturl]{%
  \printfield{url}}%
  \iffieldundef{urlyear}
    {}
    {\setunit*{\addspace}%
     \printtext[urldate]{\printurldate}}}

\begin{document}
\cite{ctan}
\printbibliography
\end{document}

With DeclaredFieldFormat you can format the whole string of url and urldate. In this case I insert a \newline in front of the ouput.

EDIT:

added printtext

enter image description here

Marco Daniel
  • 95,681
2

I found the following package useful for line breaking an URL. At the preamble, just put \usepackage{breakurl} somewhere after \usepackage{hyperref}. The \burl command is defined and, by default, the package also turns the \url command into a synonym of \burl.

Werner
  • 603,163
0

I think the cleanest solution is to just redefine the corresponding field format. I would replace the \space after "URL" with a \nobreakspace instead of explicitly adding a line break. This allows TeX to decide if a line break is the best solution. (I recommend setting the bibliography \raggedright in order to give TeX some more wiggle room here.)

\DeclareFieldFormat{url}{\mkbibacro{URL}\addcolon\nobreakspace\url{#1}}

If you really want the explicit \newline, you can of course do that as well. I would still keep the \nobreakspace in order to avoid "URL" being on a line by itself.

\DeclareFieldFormat{url}{\newline\mkbibacro{URL}\addcolon\nobreakspace\url{#1}}
schtandard
  • 14,892
0

This was posted by OP as an edit to the question, but it's clearly an answer.

Slightly enhancing @Marco's original answer:

If it should be totally like in the original but with a line break, use

\DeclareFieldFormat{url}{\newline\mkbibacro{URL}\addcolon\space\url{#1}}
\newbibmacro*{url+urldate}{%
 \printfield[url]{url}%   
 \iffieldundef{urlyear}     
 {}     
 {\setunit*{\addspace}%      
 \printtext[urldate]{\printurldate}}}
schtandard
  • 14,892