我正在将用户 HTML 解析为 LaTeX 模板。HTML 来自富文本编辑器,因此传入的 HTML 语法当然可以是任何语法。有一种情况会破坏 LaTeX,我必须处理。
<u><a href="http://www.google.com">text</a></u>
这个 HTML 不受我控制,它被一个中立的解析器解析为 LaTeX 模板。它可能会变成类似这样的内容:
\ul{\url{http://www.google.com}}
由于输入的性质以及解析器的性质,我必须假设我有某种下划线标记包裹着某种链接标记。这是回答这个问题的必要约束。我无法直接控制输入,解析器是中立的。我可以控制解析器每个标签的输出,例如<u>
输入可以编译成\uline
,但我无法更改 AST 的结构。
看看当我加入乌莱姆打包并包装链接\uline
:
\usepackage{ulem}
Text: \uline{\url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}}
现在链接带有下划线,但是没有换行:
尽管ulem 文档说“与常规下划线不同,ulem 允许换行”。
我也尝试过使用灵魂包和\ul
命令:
\usepackage{soul}
Text: \ul{\url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}}
哪个因错误而阻塞:
! Argument of \hyper@n@rmalise has an extra }.
l.9 ...ne-text/uline-text/uline-text/uline-text/}}
我不直接控制 LaTeX 源代码的创建,因此我无法直接修改标签的顺序。请在投票前仔细阅读这个问题。我不是在问如何给链接标签加下划线。
答案1
pdf链接注释本身可以呈现下划线。
\documentclass[letterpaper, 10pt]{article}
\usepackage[letterpaper, margin=1in]{geometry}
\usepackage[breaklinks]{hyperref}
\hypersetup{urlbordercolor=0 0 0,pdfborderstyle={/S/U/W 1}}
\begin{document}
Text: \url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}
\par \end{document}
也可以看看
根据要求,使用上述命令来加下划线或不加下划线取决于外部命令:
\documentclass[letterpaper, 10pt]{article}
\usepackage[letterpaper, margin=1in]{geometry}
\usepackage[breaklinks]{hyperref}
\hypersetup{urlbordercolor=0 0 0,pdfborderstyle={/W 0}}
\def\uwibble#1{{\hypersetup{pdfborderstyle={/S/U/W 1}}#1}}
\begin{document}
Text: \url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}
Text: \uwibble{\url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}}
Text: \url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}
Text: \uwibble{\url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}}
\end{document}
\documentclass[letterpaper, 10pt]{article}
\usepackage[letterpaper, margin=1in]{geometry}
\usepackage[breaklinks]{hyperref}
\usepackage{ulem}
\hypersetup{urlbordercolor=0 0 0,pdfborderstyle={/W 0}}
\def\uwibble#1{{\xuwibble#1\relax\relax\xxuwibble{#1}}}
\def\xuwibble#1#2\xxuwibble{%
\def\z{[#1][#2]}\show\z
\ifx\url#1%
\hypersetup{pdfborderstyle={/S/U/W 1}}%
\else
\expandafter\uline
\fi}
\begin{document}
Text: \url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}
Text: \uwibble{\url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}}
Text: \url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}
Text: \uwibble{\url{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}}
Text: \uwibble{something not a url} zzzz
\end{document}
答案2
这个 hack 似乎有效:
\documentclass[letterpaper, 10pt]{article}
\usepackage{hyperref}
\usepackage[normalem]{ulem}
\begin{document}
Text: \href{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}{\uline{http://test.com/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline\--text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/uline-text/}}
\par \end{document}
注意\-
用于提示\uline