包含中文的 URL 可点击但不起作用

包含中文的 URL 可点击但不起作用

你可以通过将其粘贴到地址栏来确认,URL

https://baike.baidu.com/item/三转一响

没问题。下面是包含可点击 URL 的有效 tex 代码。urlfont 用于正确显示字体中的中文字符。

\documentclass{article}

\usepackage{hyperref}
\usepackage{fontspec}

\begin{document}
\newfontfamily\urlfontfamily{FandolSong-Regular.otf}
\def\UrlFont{\urlfontfamily}

\url{https://baike.baidu.com/item/三转一响}

\href{https://baike.baidu.com/item/三转一响}{https://baike.baidu.com/item/三转一响}

\end{document}

这是输出: 在此处输入图片描述

这些 URL 是可以点击的,但是会重定向到 baike.baidu 的错误页面,提示中文部分没有传递给浏览器。\href 甚至无法正常显示,而 \url 可以。

答案1

使用 lualatex 时,您必须输入编码后的链接百分比(您可以在线获取)。hyperref(内部 escapestring 命令)的当前实现会丢失非 ASCII 输入。请注意,斜线是逐字的,而不是编码的。

\documentclass{article}

\usepackage{hyperref}
\usepackage{fontspec}

\begin{document}
\newfontfamily\urlfontfamily{FandolSong-Regular.otf}
\def\UrlFont{\urlfontfamily}



\href{https://baike.baidu.com/item/%E4%B8%89%E8%BD%AC%E4%B8%80%E5%93%8D}{\nolinkurl{https://baike.baidu.com/item/ 三转一响}}

\end{document}

该链接在当前的 PDF 查看器中看起来不错:

在此处输入图片描述

答案2

我使用 XeLaTeX 时没有遇到任何问题如果我(a)fontspec在之前加载hyperref,(b)url在之前hyperref也加载包,(c)hyperref使用选项加载包colorlinks,(d)使用\nolinkurl指令(如Ulrike在她的评论中提到的)来包裹第二个参数\href。(而且,就像 Ulrike 一样,我也发现这种方法确实不是在 LuaLaTeX 下工作。)

% !TeX program = xelatex
\documentclass{article}
\usepackage{fontspec}
\usepackage[spaces,obeyspaces,hyphens]{url}
\usepackage[colorlinks,urlcolor=blue]{hyperref}
\newfontfamily\urlfontfamily{FandolSong-Regular.otf}
\def\UrlFont{\urlfontfamily}

\begin{document}
\url{https://baike.baidu.com/item/三转一响}

\href{https://baike.baidu.com/item/三转一响}{\nolinkurl{https://baike.baidu.com/item/三转一响}}
\end{document}

相关内容