Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

BibTeX does linebreaking on the .bbl files it write outs. How do I turn that off, or adjust it? It is breaking lines inside long URLs with spaces in the bibliography, causing them to stop working.

Here’s is a MWE.

\documentclass{article}
\usepackage[spaces,obeyspaces]{url}
\usepackage{hyperref}
\usepackage{filecontents}
\begin{document}

\begin{filecontents}{foo.bst}

ENTRY {
    url
  }
  {}
  {}

FUNCTION {misc}
{
    "\url{" url "}" * * write$ newline$
    "\par" write$ newline$
}

READ

ITERATE {call.type$}

\end{filecontents}

\begin{filecontents}{foo.bib}
@misc{key1,
    url = "http://example.com"
}

@misc{key2,
    url = "http://example.com/this is a long url that contains more spaces than BibTeX can handle"
}
\end{filecontents}

\nocite{*}

\section{Bibliography}

\bibliographystyle{foo}
\bibliography{foo}

\end{document}

Clicking the first link opens a browser, but the second does not.

Here is a line-numbered version of the .bbl file it spits out:

1 \url{http://example.com}
2 \par
3 \url{http://example.com/this is a long url that contains more spaces than
4   BibTeX can handle}
5 \par

btxhak.pdf claims that “write$ does reasonable line breaking” …

share|improve this question
6  
Not sure about bibtex but spaces are not actually legal in a URI, they should be encoded as %20 –  David Carlisle Aug 11 '12 at 22:12
add comment

1 Answer

BibTeX is hard-coded to wrap lines after 80 characters where a space occurs (older versions would wrap even without a space, which caused issues with URLs). As is mentioned in a comment, spaces are invalid inside URLs in any case, so they should be converted to %20. The best you can do is write a BibTeX function to deliberately line break at a point of your choosing, but that will depend on what you feel is acceptable.


One possible approach here would be to use a search-and-replace function to insert `%20`

\documentclass{article}
\usepackage[spaces,obeyspaces]{url}
\usepackage{hyperref}
\usepackage{filecontents}
\begin{document}

\begin{filecontents}{foo.bst}

ENTRY {
    url
  }
  {}
  {}

FUNCTION { not } 
  { 
      {#0}
      {#1} 
    if$
  }

INTEGERS { l }
FUNCTION { string.length }
  {
    #1 'l :=
      {
        duplicate$
         duplicate$
        #1 l substring$ = not
      }
      { l #1 + 'l := }
    while$
    pop$
    l
  }

STRINGS { replace find text }

INTEGERS { find.length }

FUNCTION { find.replace }
  { 
    'replace :=
    'find :=
    'text :=
    find string.length 'find.length :=
    ""
      { text empty$ not }
      {
        text #1 find.length substring$ find =
        {
          replace *
          text #1 find.length + global.max$ substring$ 'text :=
        }
        { text #1 #1 substring$ *
          text #2 global.max$ substring$ 'text :=
        }
      if$
    }
  while$
}

FUNCTION {misc}
{
    "\url{" url " " "%20" find.replace "}" * * write$ newline$
    "\par" write$ newline$
}

READ

ITERATE {call.type$}

\end{filecontents}

\begin{filecontents}{foo.bib}
@misc{key1,
    url = "http://example.com"
}

@misc{key2,
    url = "http://example.com/this is a long url that contains more spaces than BibTeX can handle"
}
\end{filecontents}

\nocite{*}

\section{Bibliography}

\bibliographystyle{foo}
\bibliography{foo}

\end{document}

using code from Tame the BeaST.

share|improve this answer
    
What do you mean by “write a BibTeX function to deliberately line break at a point of your choosing”? –  andrew Aug 15 '12 at 7:17
    
BibTeX can be programmed to do various things, although it's not so easy. You could write a custom .bst file which counts up the number of characters to be written to the .bbl file and forces a line break based on something you define. That would I think be a separate question, which would need to include what the criteria are for line breaking. –  Joseph Wright Aug 15 '12 at 7:48
    
I’m not sure how to go character-by-character. There is already a custom .bst file in the MWE. Could you show me what you mean? –  andrew Aug 15 '12 at 10:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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