3

I'm parsing user HTML to LaTeX templates. The HTML comes from a rich text editor, so of course the incoming HTML syntax can be almost anything. There's one case that breaks LaTeX that I have to handle.

<u><a href="http://www.google.com">text</a></u>

This HTML, which I do not control, is parsed by a neutral parser into LaTeX templates. It might become something like:

\ul{\url{http://www.google.com}}

Due to the nature of the input, and the nature of the parser, I must assume I have some sort of underline tag wrapping some sort of link tag. This is a required constraint for answering this question. I do not have direct control over the input, and the parser is neutral. I can control what the parser outputs per per tag, such as <u> input can be compiled into \uline, but I cannot change the structure of the AST.

See what happens when I include the ulem package and wrap the link with \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/}}

Now the link is underlined, but does not wrap:

non broken link

This is despite the ulem documentation saying "Unlike regular underlining, ulem allows line breaks".

I have also tried using the soul package and the \ul command:

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

Which chokes with the error:

! Argument of \hyper@n@rmalise has an extra }.
l.9 ...ne-text/uline-text/uline-text/uline-text/}}

I do not directly control the creation of the LaTeX source code, so I cannot directly modify the order of the tags. Please read this question carefully before voting. I am not asking how to underline a link tag.

4
  • Can you change \UrlFont to be \uline? Or put \url{\uline{http://longcatislong.long}} or similar?
    – bombcar
    Commented Mar 28, 2017 at 0:30
  • I can underline the URL font, but anything else requires changing the actual input supplied to my program, which means trying to guess-mangle the input HTML to avoid specific LaTeX bugs (like attempting to swap url and underline wrapping), which isn't ideal.
    – Andy Ray
    Commented Mar 28, 2017 at 0:35
  • \uline lies: Text: \uline{test.com/uline-text/uline-text/uline-text/uline-text/uline-text/…} fails, too
    – bombcar
    Commented Mar 28, 2017 at 0:42
  • Adding \- to suggest linebreaks seems to help ....
    – bombcar
    Commented Mar 28, 2017 at 0:47

2 Answers 2

9

the pdf link annotation itself can render an underline.

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

enter image description here

see also


As requested, using the above to underline or not depending on an outer command:

enter image description here

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

and... enter image description here

\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}
7
  • Due to the nature of my input, I must assume the url is wrapped an underline tag. HTML is parsed and converted to latex. Removing the wrapping underline would mean having to hack the compiler to try to guess when that case is present. I can turn the u and a tag into any latex command I want, but not omit one, or change their order.
    – Andy Ray
    Commented Mar 28, 2017 at 1:39
  • @AndyRay I'm sorry but that constraint makes no sense, it is like saying you have want to translate from English to German but have to translate one word at a time and not omit any words or change the word order. Rather than use such a broken translation you would be better to just translate the whole thing in one go as something like \typehtml{<u><a href=....</u>} and then parse the html markup in tex either directly or using typehtml or xmltex or similar packages. Commented Mar 28, 2017 at 6:39
  • Understanding the nature of the constraint isn't needed for answering the question. I don't think it would be worth it to fully explain the constraint either. I will try typehtml. I would also be curious to know if I can implement my own command that iterates over the line line wrapped/broken url output, and underlines each fragment.
    – Andy Ray
    Commented Mar 28, 2017 at 17:08
  • 1
    @AndyRay really it doesn't make sense, latex and html are structurally quite different so you can't expect to map an html parse tree to normal latex commands one tag at a time, but I extended the answer to show underlined or not as requested. Commented Mar 28, 2017 at 21:08
  • 1
    @AndyRay really it's only a couple of lines of tex code to define \uwibble to see if it contains \url and set the border property or not and if not is ulem or soul or whetever else. This is just a simple one-level case of the general issue that the translation needs to be context sensitive and potentially have access to a whole branch of the parse tree, not just a single production Commented Mar 28, 2017 at 21:50
3

This hack seems to work:

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

Note the \- used to hint for \uline

3
  • Does this require knowledge of where the line break will be? And is there documentation on what this adjascent tag \href{...}{\url{...}} syntax means? Doesn't seem to work with \url, which I'm not yet sure if it matters for my case.
    – Andy Ray
    Commented Mar 28, 2017 at 1:06
  • You can add as many \- as you need, but the other answer's better. \url seems to break \uline.
    – bombcar
    Commented Mar 28, 2017 at 1:27
  • 1
    You can also use \allowbreak if you don't want duplicate hyphens (which could be potentially misleading)
    – Alec
    Commented Feb 2, 2021 at 12:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .