24

I’m using XeLaTeX within MiKTeX and also the biblatex package for the literature.

I’ve noticed that my bibliography set using \printbibliography is not set with the \sloppy option by default, which is quite good in my opinion.

What I would like to be able to do is to make \sloppy or \sloppypar only those references in the bibliography that actually need it, that means those records which get overfull \hbox. I don’t want it to be done automatically but I haven’t found a way to do it even manually. Is this even possible?

lockstep
  • 250,273
josefec
  • 1,772
  • 3
    You could try to use the execute field in the bib-entry to insert the code (untested as you didn't provide a small example to play around with). – Ulrike Fischer May 18 '11 at 09:12

3 Answers3

23

I'd set \emergencystretch before \printbibliography:

\begingroup
\setlength{\emergencystretch}{8em}
\printbibliography
\endgroup

TeX uses \emergencystretch only when a paragraph doesn't fit with the usual parameters, so this may solve the problem. Experiment with the value of \emergencystretch.

egreg
  • 1,121,712
6

As Ulrike Fischer has hinted: Use the execute special field to add \sloppy for the respective reference entries.

\documentclass{article}

\usepackage{biblatex}

\newcommand*{\displayemergencystretch}{%
  Emergency stretch: \the\emergencystretch
}

\renewbibmacro*{finentry}{%
  \finentry
  \addspace% ADDED
  \printtext[brackets]{\displayemergencystretch}% ADDED
}

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@misc{A01,
  author = {Author, A.},
  year = {2001},
  title = {Alpha},
}
@misc{B02,
  execute = {\sloppy},
  author = {Buthor, B.},
  year = {2002},
  title = {Bravo},
}
@misc{C03,
  author = {Cuthor, C.},
  year = {2003},
  title = {Charlie},
}
\end{filecontents}

\addbibresource{\jobname.bib}

\nocite{*}

\begin{document}

\sloppy

A \verb|\sloppy| paragraph~-- \displayemergencystretch

\fussy

A \verb|\fussy| paragraph~-- \displayemergencystretch

\printbibliography

\end{document}

enter image description here

lockstep
  • 250,273
  • 3
    I tried this but it actually did not work. No idea why not. – josefec Jul 14 '12 at 06:57
  • @josefec +1, I got the same emergencystretch value (which seems good) but the paragraph is not sloppy. I tried with title = {a very long line with a ve\-rylongwordthatLaTeXwillnotbeabletohyphenate} instead of Bravo and only a \sloppy\printbibliography did the job correctly, unfortunately. I guess this is slightly more complicated than just adding execute = {\sloppy}... – hdl Mar 07 '18 at 13:19
-1

This isn't 100% a direct answer, but as I just fought through a similar problem in my bibliography, here's my approach. It uses the ragged2e package, which defines a \RaggedRight command that still permits hyphenation -- and crucially for me at least, that includes hyphens inside urls (when using the url package). I decided to enable \RaggedRight only for @electronic items (whose main contents are a short title and a wicked-to-linebreak url), and defined my own .bst file. It's a copy/paste/modify from abbrvnat.bst, and here's the relevant excerpt:

FUNCTION {electronic}
{ output.bibitem
  url empty$ 'skip$ { "\begin{FlushLeft}" write$ newline$ } if$
  format.authors output
  new.block
  format.title "title" output.check
  year empty$ month empty$ day and and
  'skip$
  {
    new.block
    format.date output 
  }
  if$
  new.block 
  format.url output
  note empty$ 'skip$
  {
    new.block
    note output
  }
  if$
  fin.entry
  url empty$ 'skip$ { "\end{FlushLeft}" write$ newline$ } if$
}

.bst files are tricky beasts, but the upshot of this code is, if there is a url, then I surround the contents of the entry with a FlushLeft environment, which enables \RaggedRight on this paragraph -- and only this paragraph.

To use the execute field, as @UlrikeFischer suggested, you could redefine output.bibitem and fin.entry to check for the execute field and output it:

% in output.bibitem
execute empty$ 'skip$ "\begingroup" execute * write$
% in fin.entry
execute empty$ 'skip$ "\endgroup" write$

Then your execute field could contain \RaggedRight and you'd get the same effect as my code above, but for all .bib items that you annotated. It's not quite the automatic "overfull hbox" solution, but it might be useful anyway.

Ben Lerner
  • 7,082
  • This is a regular natbib solution, not biblatex which is what @josefec is using. – Alan Munn Jun 30 '11 at 03:32
  • 1
    @AlanMunn, touche, I missed that key detail...my bad! I'm not a biblatex user (yet), let alone informed-enough expert, so I can't help there, then. But maybe the ideas above are helpful for people anyway. – Ben Lerner Jun 30 '11 at 20:51
  • 3
    I consider it wrong to use hyphens in URLs, for the simple reason that - can be part of a valid URL, and therefore adding a hyphen can change the URL. – celtschk Jan 09 '12 at 07:58
  • Have a +1 for your comment and a -1 for your answer (hopefully that cancles each other out ^^) – Sebastian Schmitz Jul 21 '14 at 16:06
  • FWIW, @SebastianSchmitz, they don't cancel each other, actually -- upvotes on comments don't add reputation points. It's fine; I don't mind the loss of 2 points ;-) – Ben Lerner Jul 23 '14 at 17:03