2

Based on Avoiding inelegant splits on URLs, I have this MWE:

\documentclass{article}
\usepackage[backend=biber]{biblatex}
\usepackage{filecontents}
\usepackage{trace}
\usepackage{hyperref}
\begin{filecontents}{\jobname.bib}
@misc{A01,
  author = {Author, A. and Buthor, B.  },
  year = {2001},
  title = {Alpha aaa},
  url = {http://www.thewebsiteurl.com/ab/XC/PYR12337.pdf},
}
@misc{A02,
  author = {Authoq, A. and Buthoq, B.  },
  year = {2002},
  title = {Betaa bbb},
  url = {http://www.trythesesites.com/cx/AB/KXY0012C.pdf},
}
\end{filecontents}
\addbibresource{\jobname.bib}

\def\mybiburlval{0}% 0 is otherwise the default
% \def\mybiburlval{1}% uncomment this to set to 1

% hyphenation does not work in URLs:
%\hyphenation{the-website-url}
% so use as per:
% https://tex.stackexchange.com/questions/30857/avoiding-inelegant-splits-on-urls

% Set low penalties for breaks at numbers, uppercase letters and lowercase letters
\setcounter{biburlnumpenalty}{\mybiburlval}
\setcounter{biburlucpenalty}{\mybiburlval}
\setcounter{biburllcpenalty}{\mybiburlval}

% Define a category to selectively print entries with a higher lowercase penalty
\DeclareBibliographyCategory{badbreaks}
\addtocategory{badbreaks}{A01} % only A01 here

\AtEveryBibitem{%
  \ifcategory{badbreaks}
    {\defcounter{biburllcpenalty}{9000}}
    {}}

\begin{document}

\noindent
A: \fullcite{A01}

B: \fullcite{A02}

\printbibliography

\end{document}

Compiled this with pdflatex test.tex && biber test && pdflatex test.tex && pdflatex test.tex, using Texlive 2014 frozen. Here is a screenshot of how evince renders this output:

test1.png

So the links mostly "leak"/"spill over" the margin, except the second one, which is line-broken at a period/dot.

Now uncomment the \def\mybiburlval{1} line, and recompile again; the output now is:

test2.png

Notice how all links are line-broken here at lowercase characters - even if I've only added the first (A01) citation to the badbreaks category; and even if I've set the lower case breaking penalty to the smallest possible value (1)!

So my question is - how can I selectively choose to have a URL break only for the citations which are in the badbreaks category (in this case, only A01); and otherwise typeset links as in the usual case (even if they spill over the margin)?

Maybe of note would be to recall:

$ texdef -t latex -p biblatex -f setcounter
\setcounter is defined by (La)TeX.

\setcounter:
macro:#1#2->\@ifundefined {c@#1}{\@nocounterr {#1}}{\global \csname c@#1\endcsname #2\relax }

$ texdef -t latex -p biblatex -f defcounter
\defcounter first defined in "etoolbox.sty".

\defcounter:
\protected macro:#1#2->\ifcsundef {c@#1} {\etb@noglobal \@nocounterr {#1}}{\csname c@#1\endcsname \numexpr #2\relax }

So, apparently, \setcounter is global, and \defcounter is not (is local) - but I get the same output (i.e. breaking applied nonselectively everywhere), even if I use \setcounter instead of \defcounter in the switch - and even if I specifically set a 0 in the "else" part, as in:

\AtEveryBibitem{%
  \ifcategory{badbreaks}
    {\setcounter{biburllcpenalty}{9000}}
    {\setcounter{biburllcpenalty}{0}   }}

So how can I apply url breaking at lowercase characters selectively - only to some citations?

6

The penalties are not used in a bibitem, but only in \biburlsetup which is done at the begin of the bibliography. If you want to change the settings for one bibitem, you should call the url setup again:

\AtEveryBibitem{%
  \ifcategory{badbreaks}
    {\defcounter{biburllcpenalty}{9000}\biburlsetup}
    {}}

Output with OP MWE (\def\mybiburlval{0}):

test3.png

  • Many thanks for that, @UlrikeFischer - seems to work, added image to the answer; however, I'm slightly puzzled: \ifcategory by definition should have the true clause be written first; yet in the output, the A01 url (which is added to category) is not broken at lowercase; while only the A02 url (which is not in category, so should not be broken) is in fact line-broken. Any ideas why? – sdaau May 29 '15 at 14:59
  • 1
    A01 is the entry with the author "Author" and the title alpha. So the output is correct. It would naturally be easier to see, if you hadn't chosen so similar names ... – Ulrike Fischer May 29 '15 at 15:26
  • Thanks again, @UlrikeFischer - that was indeed it; while trying to write a separate question, I could confirm that if I specify \printbibliography[sorting=none] (and then, if I want also the citation numbers to be in order, I also have to do \usepackage[backend=biber,defernumbers=true]{biblatex} along with \printbibliography[sorting=none,resetnumbers=true]). Cheers! – sdaau May 29 '15 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.