![在段落中使用连字符连接 URL,同时保留下划线链接](https://linux22.com/image/400685/%E5%9C%A8%E6%AE%B5%E8%90%BD%E4%B8%AD%E4%BD%BF%E7%94%A8%E8%BF%9E%E5%AD%97%E7%AC%A6%E8%BF%9E%E6%8E%A5%20URL%EF%BC%8C%E5%90%8C%E6%97%B6%E4%BF%9D%E7%95%99%E4%B8%8B%E5%88%92%E7%BA%BF%E9%93%BE%E6%8E%A5.png)
问题:
我希望断开或连字符段落中的链接,同时保留下划线链接。
最小工作示例(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}