3

Problem:

I wish to break or hyphenate links in paragraphs while maintaining an underline link.

Minimal Working Example (MWE):

\documentclass{article}
\usepackage{lipsum}
\usepackage[utf8]{inputenc}
\usepackage[OT6,T1]{fontenc}
\usepackage{xcolor}
\usepackage[normalem]{ulem}
\usepackage{hyperref}
\hypersetup{colorlinks,urlcolor=black}

\makeatletter
\DeclareUrlCommand\ULurl@@{%
  \def\UrlFont{\color{black}}%
  \def\UrlLeft{\uline\bgroup}%
  \def\UrlRight{\egroup}}
\def\ULurl@#1{\hyper@linkurl{\ULurl@@{#1}}{#1}}
\DeclareRobustCommand*\ULurl{\hyper@normalise\ULurl@}
\makeatother

\begin{document}

\noindent Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elit massa, volutpat a mattis vel, dictum dapibus sapien. Ut mollis neque quis sapien convallis accumsan. Proin vel odio sit amet neque euismod ultrices. Sed ut dolor quis nunc convallis eleifend vel eget mauris. Nulla eget viverra felis, sit amet vehicula lacus. Praesent at ex id justo rutrum egestas. \ULurl{http://creativecommons.org/licenses/by-sa/3.0/}. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elit massa, volutpat a mattis vel, dictum dapibus sapien from \ULurl{https://creativecommons.org/about/downloads/}. 
\end{document}

Outcome:

enter image description here

Desired outcome:

To break/hypenate links so that they may not extend beyond the width of text.

  • Off-topic: if you place your examples code between \begin{sloppypar} and \end{sloppypar} seems nice to me... But of course no hyphenation happens on the underlined text. On-topic: you should add in the title that the hyperlink's text should be underlined. Makes much difference. – koleygr Aug 23 at 16:25
  • 1
    @koleygr Adjusted, thanks for input – kexxcream Aug 23 at 16:29
2

Just load the xurl package immediately before hyperref and use the basic \url macro.

enter image description here

\documentclass{article}
\usepackage{lipsum}
\usepackage[utf8]{inputenc}
\usepackage[OT6,T1]{fontenc}
\usepackage{xcolor}
\usepackage[normalem]{ulem}
\usepackage{xurl}  % <-- new
\usepackage{hyperref}
\hypersetup{colorlinks,urlcolor=black}


\begin{document}
\noindent 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elit massa, volutpat a mattis vel, dictum dapibus sapien. Ut mollis neque quis sapien convallis accumsan. Proin vel odio sit amet neque euismod ultrices. Sed ut dolor quis nunc convallis eleifend vel eget mauris. Nulla eget viverra felis, sit amet vehicula lacus. Praesent at ex id justo rutrum egestas. \url{http://creativecommons.org/licenses/by-sa/3.0/}. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elit massa, volutpat a mattis vel, dictum dapibus sapien from \url{https://creativecommons.org/about/downloads/}. 
\end{document}
  • But this doesn't give un underlined link. – Ulrike Fischer Aug 23 at 16:58
  • @UlrikeFischer - I was assuming that the OP had inserted the instruction \hypersetup{colorlinks,urlcolor=black} for a reason. Maybe I assumed too much. – Mico Aug 23 at 17:34
  • well the OP used \uline in the command and asks about an "underline link". – Ulrike Fischer Aug 23 at 17:36
2

You could set the pdfborderstyle to underline:

\documentclass{article}
\usepackage{lipsum}
\usepackage[utf8]{inputenc}
\usepackage[OT6,T1]{fontenc}
\usepackage{xcolor}
\usepackage{hyperref}
\hypersetup{
 urlbordercolor=black,
 pdfborderstyle={/S/U/W 1}
 }

\begin{document}

\noindent Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elit massa, volutpat a mattis vel, dictum dapibus sapien. Ut mollis neque quis sapien convallis accumsan. Proin vel odio sit amet neque euismod ultrices. Sed ut dolor quis nunc convallis eleifend vel eget mauris. Nulla eget viverra felis, sit amet vehicula lacus. Praesent at ex id justo rutrum egestas. \url{http://creativecommons.org/licenses/by-sa/3.0/}. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elit massa, volutpat a mattis vel, dictum dapibus sapien from \url{https://creativecommons.org/about/downloads/}.
\end{document}

enter image description here

  • I have adopted your approach but it does not produce any underline in Texpad. – kexxcream Aug 23 at 17:22
  • +1 for producing a hyperlink with an underline. :-) – Mico Aug 23 at 17:36
  • how such a border is shown depends on the pdfviewer. – Ulrike Fischer Aug 23 at 17:41

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.