1

Latex does not break the line after a long url. However, this doens't appear if in the url are "-" or "/". As shown in the MWE I'm using bibulous and natbib. I looked at several solutions on this and other formus but they all didn't work.

enter image description here

\documentclass[11pt, a4papter] {scrartcl}
\usepackage{natbib}
 \usepackage{hyperref}

\usepackage{filecontents}
\begin{filecontents*}{svenz.bst}
TEMPLATES:
book =  [<au>.] [\textit{\enquote{<title>}}.] [<address> <year>.]
electronic = [<au>.] [\textit{<institution>.}] [<date>.] [\enquote{<title>}.] [<url>.] [<dateofchange>.] [<urldate>.]
oral = [<au>.] [<interviewer>,] [<date>,] [<address>.]
inbook = [<au>.] [\enquote{<title>}.] [In: <booktitle>,] hg. v. [<publisher>.] [<address>] [<place>.] [<pages>.]
article = [<au>.] [\enquote{<title>}.] [\textit{<journal>,] [<year>,] [<number>,}] [<pages>.]

SPECIAL-TEMPLATES:
citelabel = <authorlist.0.last>, <year>
sortkey = <authorlist.0.last><year>

OPTIONS:
namelist_format = last_name_first
use_firstname_initials = False
\end{filecontents*}
\begin{filecontents}{\jobname.bib}
@Electronic{gehalt,
  author      = {Rütti, Nicole},
  institution = {NZZ Online},
  year        = {2017},
  date        = {2017-07-27},
  title       = {Sind Frauen wirklich selber schuld am tieferen Gehalt?},
  url         = {https://www.nzz.ch/wirtschaft/frauenundlohnverhandlungenaufstoeckelschuhenrueckwaertsuebereinminenfeldlaufenlddfaedafeadfeadfeasfeiasnddskfjeidlkajfeiadkje.1307984},
  urldate     = {2018-10-14},
}


@Electronic{ackeret,
  author      = {Ackeret, Markus},
  institution = {NZZ Online},
  year        = {2017},
  date        = {2017-05-25},
  title       = {Im Übereifer des politischen Gefechts},
  url         = {https://www.nzz.ch/international/debatte-umasfdlkajsdfiojaweklfjasdiasdfkjasldfjiejklasdjfie-die-bundeswehr-im-uebereifer-des-politischen-gefechts-ld.1296154},
  urldate     = {2018-10-14},
}

@Electronic{Lenz1,
  author      = {Lenz, Christoph},
  institution = {Tages-Anzeiger Online},
  year        = {2017},
  date        = {2017-07-04},
  title       = {Rechenspiele der Armee haben politische Folgen},
  url         = {https://www.tagesanzeiger.ch/schweiz/standard/rechenspiele-dasdfkjasdifasdlkfjasiofjasdklfjieklkjadikeidjadlskfjei19945957},
  urldate     = {2018-10-16},
}
\end{filecontents}

\begin{document}
Lorem ipsum dolor sit amet, \citet{gehalt} consetetur sadipscing elitr, 
\cite{Lenz1}. Lorem ipsum dolor sit amet. \cite{ackeret}

\bibliographystyle{svenz}
\bibliography{\jobname}
\end{document}

How can I autmoatically break after a long url?

3

LaTeX has trouble breaking URLs because they do not look like normal text or words that LaTeX can break with its hyphenation algorithm and patterns. If the URL happens to have a - or / in a convenient place that can facilitate a line break, but this is not always guaranteed. When LaTeX reads frauenundlohnverhandlungenaufstoeckelschuhenrueckwaertsuebereinminenfeldlaufenlddfaedafeadfeadfeasfeiasnddskfjeidlkajfeiadkje it is lost and does not know where to hyphenate that 'word'.

If you want your URLs to be linked, you should be using the \url command for URLs. So in your .bst file you need \url{<url>} instead of a plain <url>.

But even then the issue remains that it is tricky to break the URL if it does not have enough possible break points. By default \url will only break a URL at assorted punctuation like ., /. If you URL has large stretches without such punctuation, hyphenation can be extremely tricky. (By default URLs are not broken at - to avoid confusion with actual hyphenation.) Forcing linebreaks in \url shows many possible solutions to get line breaks in URL.

For many URLs it is enough to allow line breaks at hyphens with

\usepackage[hyphens]{url}

before loading hyperref or url. But that is not always enough.

The most radical solution is loading the xurl package. The xurl package allows line breaks at any point in an URL. The disadvantage of this approach is that if words in your URL will not be broken according to hyphenation rules, so you may end up with

https://www.nzz.ch/wirtschaft/frauenundlohnverhandlunge
naufstoeckelschuhenrueckwaertsuebereinminenfeldlaufen

instead of

https://www.nzz.ch/wirtschaft/frauenundlohnverhandlungen
aufstoeckelschuhenrueckwaertsuebereinminenfeldlaufen

But I know of no solution for URL breaking that parses the actual words (if present) in the URL and chooses break points according to word hyphenation and word boundaries.

\documentclass[11pt, a4papter] {scrartcl}
\usepackage{natbib}
\usepackage{xurl}
%\usepackage[hyphens]{url}
\usepackage{hyperref}
\urlstyle{same}

\usepackage{filecontents}
\begin{filecontents*}{svenz.bst}
TEMPLATES:
book =  [<au>.] [\textit{\enquote{<title>}}.] [<address> <year>.]
electronic = [<au>.] [\textit{<institution>.}] [<date>.] [\enquote{<title>}.] [\url{<url>}.] [<dateofchange>.] [<urldate>.]
oral = [<au>.] [<interviewer>,] [<date>,] [<address>.]
inbook = [<au>.] [\enquote{<title>}.] [In: <booktitle>,] hg. v. [<publisher>.] [<address>] [<place>.] [<pages>.]
article = [<au>.] [\enquote{<title>}.] [\textit{<journal>,] [<year>,] [<number>,}] [<pages>.]

SPECIAL-TEMPLATES:
citelabel = <authorlist.0.last>, <year>
sortkey = <authorlist.0.last><year>

OPTIONS:
namelist_format = last_name_first
use_firstname_initials = False
\end{filecontents*}
\begin{filecontents}{\jobname.bib}
@Electronic{gehalt,
  author      = {Rütti, Nicole},
  institution = {NZZ Online},
  year        = {2017},
  date        = {2017-07-27},
  title       = {Sind Frauen wirklich selber schuld am tieferen Gehalt?},
  url         = {https://www.nzz.ch/wirtschaft/frauenundlohnverhandlungenaufstoeckelschuhenrueckwaertsuebereinminenfeldlaufenlddfaedafeadfeadfeasfeiasnddskfjeidlkajfeiadkje.1307984},
  urldate     = {2018-10-14},
}


@Electronic{ackeret,
  author      = {Ackeret, Markus},
  institution = {NZZ Online},
  year        = {2017},
  date        = {2017-05-25},
  title       = {Im Übereifer des politischen Gefechts},
  url         = {https://www.nzz.ch/international/debatte-umasfdlkajsdfiojaweklfjasdiasdfkjasldfjiejklasdjfie-die-bundeswehr-im-uebereifer-des-politischen-gefechts-ld.1296154},
  urldate     = {2018-10-14},
}

@Electronic{Lenz1,
  author      = {Lenz, Christoph},
  institution = {Tages-Anzeiger Online},
  year        = {2017},
  date        = {2017-07-04},
  title       = {Rechenspiele der Armee haben politische Folgen},
  url         = {https://www.tagesanzeiger.ch/schweiz/standard/rechenspiele-dasdfkjasdifasdlkfjasiofjasdklfjieklkjadikeidjadlskfjei19945957},
  urldate     = {2018-10-16},
}
\end{filecontents}

\begin{document}
Lorem ipsum dolor sit amet, \citet{gehalt} consetetur sadipscing elitr, 
\cite{Lenz1}. Lorem ipsum dolor sit amet. \cite{ackeret}

\bibliographystyle{svenz}
\bibliography{\jobname}
\end{document}

screenshot of the output, the URLs break and do not run into the margin

Especially if you plan to print your work I would urge you to try and make sure that the URL you are giving is the shortest usable URL possible. Often URLs may contain redundant or one-time information like session IDs or titles. Take this question as an example. The URL shown in my browser is

https://tex.stackexchange.com/questions/458049/forcing-linebreak-in-bibliography-with-natbib

but when I generate a 'share' link to the question I obtain

https://tex.stackexchange.com/q/458049/35864

the last bit is just my user ID for link tracking, so the actual minimal link is

https://tex.stackexchange.com/q/458049

That is much easier to type if you happen to only have a printed version of the document and is much better for line breaking since it is shorter.

The URL in your screenshot (not in the MWE) includes a session id in PHPSESSID that could probably be removed.

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.