在段落中使用连字符连接 URL,同时保留下划线链接

在段落中使用连字符连接 URL,同时保留下划线链接

问题:

我希望断开或连字符段落中的链接,同时保留下划线链接。

最小工作示例(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}

结果:

在此处输入图片描述

期望的结果:

断开/连字符链接,使它们不会超出文本的宽度。

答案1

xurl只需立即加载包hyperref并使用基本\url宏。

在此处输入图片描述

\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}

答案2

您可以将 pdfborderstyle 设置为下划线:

\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}

在此处输入图片描述

相关内容