7

MWE:

test.tex:

% !TeX program = xelatex
\documentclass[paper=A4,fontsize=12pt]{scrartcl}
\usepackage[left=3cm, right=3cm, bottom=3cm, top=3cm]{geometry}
\usepackage{fontspec}
\usepackage[backend=biber,style=authoryear,citestyle=authoryear]{biblatex}
\DeclareNameAlias{sortname}{last-first}
\DeclareNameAlias{default}{last-first}
\setcounter{biburllcpenalty}{7000}
\setcounter{biburlucpenalty}{9999}
\addbibresource{resources.bib}
\begin{document}
\cite{Test.2018}
\printbibliography
\end{document}

resources.bib:

@misc{Test.2018,
    author = {{testauthor test author testauthor}},
    year= {2018},
    title = {{testfile test file testfile}},
    url = {http://abcdefghijklmnopqrstuvwxy.com/phrrFK5dS1ztTjiaCmveRARXpGBtV01U%20B93FcrxkOVhj2CceIguNuUPpwDaUD4GjTTWh9cL30D6rLgWxapYlhegDXQGFlE0hao7B2YS227ca%20keoWHWNmQVKVcNOUFxfsCp0SwCElfTVtpMv6DKqiSwhWgO2zrHvstd4UNBo99UQBSCTkzL7lD2UyRLTRy6xXdJ}
}

Here the URL does not break properly due to the fontspec package changing the parindent. How can I fix this?

\begingroup
\sloppy
\printbibliography
\endgroup

This makes the URL look extremely ugly with the first line not even justified. It is just this one weird character extending into the border. I do not want to use any weird workarounds like using the url package and changing the bibliography when BibLaTeX supports URLs natively with Biber.

pomeloy
  • 303
  • 1
    Young Un Thank you for helping keep things into the QA structure in the other question. It definitely works better this way. If I may, I have another suggestion. How about you use in your question the actual URL that's giving you trouble? If you provide us with an arbitrarily long made up URL, the only general solution may be to use something of the kind of \sloppy. But usually real URL are less badly behaved (even if nasty enough!), so the real case might get you a better solution. – gusbrs Dec 16 '18 at 18:29
  • You could also use \setcounter{biburlnumpenalty}{9900} (or some other value), to also allow breakpoints after numbers. – Skillmon Dec 16 '18 at 18:33
  • Thanks for staying with me! Actually the real url is of a similar length and also composed of a similar length of random characters (or base64 strings) after the tld. This is what confuses me. The url is literally a string of random characters and latex decides not to break at one random point that is at the end of the regular line but instead after the next character. I tried setting biburlnumpenalty, it did not have an impact either. – pomeloy Dec 16 '18 at 18:36
  • Mmh, In this case, let's see what folks have to say. – gusbrs Dec 16 '18 at 18:39
  • You could at least tell us which character seems to be a viable breakpoint in your opinion but isn't considered. – Skillmon Dec 16 '18 at 18:44
  • 2
    As to your comment "I do not want to use any weird workarounds like using the url package and changing the bibliography when biblatex supports urls natively with biber." Actually biblatex loads the url package and it is url that provides biblatex's support for urls. So, you might as well use its options, if they do get things better. – gusbrs Dec 16 '18 at 18:47

3 Answers3

7

You could insert a bit stretchable space:

\documentclass[paper=A4,fontsize=12pt]{scrartcl}
\usepackage[left=3cm, right=3cm, bottom=3cm, top=3cm]{geometry}
\usepackage{fontspec}

\usepackage[backend=biber,style=authoryear,citestyle=authoryear]{biblatex}
\DeclareNameAlias{sortname}{last-first}
\DeclareNameAlias{default}{last-first}
\setcounter{biburllcpenalty}{100}
\setcounter{biburlucpenalty}{100}
\setcounter{biburlnumpenalty}{100}
\addbibresource{resource.bib}

\renewcommand*{\biburlsetup}{%
  \Urlmuskip=0mu plus 3mu\relax
  \mathchardef\UrlBigBreakPenalty=100\relax
  \mathchardef\UrlBreakPenalty=200\relax
  \def\UrlBigBreaks{\do\:\do\-}%
  \def\UrlBreaks{%
    \do\.\do\@\do\/\do\\\do\!\do\_\do\|\do\;\do\>\do\]\do\)\do\}%
    \do\,\do\?\do\'\do\+\do\=\do\#\do\$\do\&\do\*\do\^\do\"}%
  \ifnumgreater{\value{biburlnumpenalty}}{0}
    {\def\do##1{\appto\UrlSpecials{\do##1{\mathchar`##1 \mskip 0mu plus 1mu\penalty\value{biburlnumpenalty}}}}%
     \do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\0}
    {}%
  \ifnumgreater{\value{biburlucpenalty}}{0}
    {\def\do##1{\appto\UrlSpecials{\do##1{\mathchar`##1 \mskip 0mu plus 1mu\penalty\value{biburlucpenalty}}}}%
     \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}
    {}%
  \ifnumgreater{\value{biburllcpenalty}}{0}
    {\def\do##1{\appto\UrlSpecials{\do##1{\mathchar`##1 \mskip 0mu plus 1mu\penalty\value{biburllcpenalty}}}}%
     \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}
    {}%
  \let\do=\noexpand}

\begin{document}


\cite{Test.2018}
\printbibliography
\end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • +1 I played with this definition quite a lot, to no avail. But why does placing the glue inside \UrlSpecials differs from setting a larger glue in \Urlmuskip? – gusbrs Dec 16 '18 at 20:36
  • 1
    \Urlmuskip has an effect only at the dot and the slash. It doesn't affect the problematic line. – Ulrike Fischer Dec 16 '18 at 20:39
  • Ah! so that's why I had to go up to 10mu just to get a lousy result... – gusbrs Dec 16 '18 at 20:41
4

biblatex 3.13 implements a way to add stretchable space to the URL in the way demonstrated in Ulrike Fischer's answer, see https://github.com/plk/biblatex/issues/850.

The relevant lengths are \biburlnumskip, \biburlucskip and \biburllcskip.

\documentclass[paper=A4,fontsize=12pt]{scrartcl}
\usepackage[left=3cm, right=3cm, bottom=3cm, top=3cm]{geometry}
\usepackage{fontspec}

\usepackage[backend=biber,style=authoryear]{biblatex}

\DeclareNameAlias{default}{family-given}
\DeclareNameAlias{sortname}{default}

\setcounter{biburllcpenalty}{100}
\setcounter{biburlucpenalty}{100}
\setcounter{biburlnumpenalty}{100}

\biburlnumskip=0mu plus 1mu\relax
\biburlucskip=0mu plus 1mu\relax
\biburllcskip=0mu plus 1mu\relax

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@misc{Test.2018,
  author = {{testauthor test author testauthor}},
  year   = {2018},
  title  = {{testfile test file testfile}},
  url    = {http://abcdefghijklmnopqrstuvwxy.com/phrrFK5dS1ztTjiaCmveRARXpGBtV01U%20B93FcrxkOVhj2CceIguNuUPpwDaUD4GjTTWh9cL30D6rLgWxapYlhegDXQGFlE0hao7B2YS227ca%20keoWHWNmQVKVcNOUFxfsCp0SwCElfTVtpMv6DKqiSwhWgO2zrHvstd4UNBo99UQBSCTkzL7lD2UyRLTRy6xXdJ}
}
\end{filecontents}

\addbibresource{\jobname.bib}

\begin{document}
\cite{Test.2018}
\printbibliography
\end{document}

URL with nicely aligned line breaks

moewe
  • 175,683
3

try

\usepackage[style=authoryear,citestyle=authoryear]{biblatex}
\usepackage{xurl}

enter image description here

  • Tried, did not work at all. The url now overshoots the page by about 10 cm. – pomeloy Dec 16 '18 at 19:25
  • see edited answer –  Dec 16 '18 at 19:51
  • Is using a whole different mono font the only way to fix one single linebreak? – pomeloy Dec 16 '18 at 19:52
  • this font is only an example! –  Dec 16 '18 at 19:54
  • I am a LaTeX newbie. It is very hard for me to understand why I should play with the font settings if the problem lays solely in the linebreaking. – pomeloy Dec 16 '18 at 20:17
  • You should not play with the font setting. It was only an example which font I am using! And it has nothing to do with url breaking! –  Dec 16 '18 at 20:23
  • 1
    I'm sorry, I can not follow. Your example literally changes the font and my problem is about an url not breaking the way I want it to. – pomeloy Dec 16 '18 at 21:42
  • I deleted the font setting and used your example code! –  Dec 17 '18 at 06:18