2

I am trying to use \href to display www.site.com but (without an http://) but include http:// in the actual link the output directs to when clicked. Long descriptions are running past the end of the line, and all of the solutions that I've found online seem to affect the behaviour of \url but not \href. I found a similar question at Line breaks in \href link description, but the solution is 5 years old and did not work for me.

How do I make \href break the description (ideally at all punctuation marks, including commas)?

MWE:

\documentclass{article}

\usepackage{breakurl}
\usepackage{xurl}
\usepackage[breaklinks=true]{hyperref}

\begin{document}
    \title{Title}
    \maketitle
    
    Here is href with a very long description: 
    \href{https://google.com}{www.link.with.many.pieces/sublink/with?comma,delimited,section,for,extra,fun}.
    I want the href to break the description.
\end{document}

Output

enter image description here

2
  • I recommend to use \nolinkurl to format the typeset (second) argument of \href: \href{https://google.com}{\nolinkurl{www.link.with.many.pieces/sublink/with?comma,delimited,section,for,extra,fun}}. Then the link is formatted and broken as usual (as if you had used \url).
    – moewe
    Commented Aug 17, 2021 at 19:26
  • Thanks, that's just what I was looking for! If you post it as an answer then I will accept it Commented Aug 17, 2021 at 21:16

1 Answer 1

4

If the printed part of the hyperlink is of a URL-ish nature, I recommend you look into using \nolinkurl. \nolinkurl formats its argument pretty much like \url, but without the link, so it breaks just like \url.

\documentclass{article}

\usepackage{xurl}
\usepackage[breaklinks=true]{hyperref}

\begin{document}
  Here is href with a very long description: 
  \href{https://google.com}{\nolinkurl{www.link.with.many.pieces/sublink/with?comma,delimited,section,for,extra,fun}}.
  I want the href to break the description.
\end{document}

Here is href with a very long description: www.link.with.many.pieces/
sublink/with?comma,delimited,section,for,extra,fun. I want the href to
break the description.

I dropped the breakurl package since it only makes sense for the LaTeX -> DVI -> PS -> PDF route that most people no longer use.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .