3

This problem only shows up when compiling to HTML.

I have some long URL's and a URL with underscore do not break inside table when compiled to HTML as it does when compiled to PDF.

I tried all the tricks I could find, and htlatex still does not want to break the URL inside the table. MWE

\documentclass[12pt]{article}%
\usepackage{longtable}
\usepackage{hyperref}
\makeatletter %from http://tex.stackexchange.com/questions/3033/forcing-linebreaks-in-url
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\makeatother

%\usepackage[hyphenbreaks]{breakurl}
%\usepackage[hyphens]{url}

\begin{document}
\begin {longtable}{|p{.2in}|p{2.2in}|p{1in}|p{.8in}|}\hline
1&
\url{http://www.foo.com/questions/aaaaaa/aaaaaaa-aa-aaa-aaaaaaaaa-aaaaaaaaaa-aaaaa-aa-aaaaaaa-aaaaaa}
&
more&1/5/2016
\\\hline
\end{longtable}

Another table

\begin {longtable}{|p{.2in}|p{2.2in}|p{1in}|p{.8in}|}\hline
2&
\url{https://www.foo.com/post/aaa_aaa_aaaa_aaaaaaaa_aaaaa_aa_aaaaaaaaa_aaaaaa_aaa_aaa_aaaaaaaaa_aa_aaaaaaa_aaa_aaaaaaaa_aaaaaaa}
&another good question&1/5/2016\\\hline
\end{longtable}    
another one

\begin {longtable}{|p{.2in}|p{2.2in}|p{1in}|p{.8in}|}\hline
1&
aaaaaa aaaaaaa aa aaa aaaaaaaaa aaaaaaaaaa aaaaa aa aaaaaaa 
aaaaaa aaaaa aaaaaaaaaa aaaaaaaa aaaaa aaaaaa
aaaaaa aaaaaaa aa aaa aaaaaaaaa aaaaaaaaaa aaaaa aa aaaaaaa 
&more&1/5/2016
\\\hline
\end{longtable}
\end{document}

When compiled to pdf using pdflatex, this is the output, as expected:

Mathematica graphics

When compiled to HTML using

 htlatex foo.tex "htm,p-width"

this is the output

Mathematica graphics

The problem shows up only with url with underscore. With hyphens in URL, it seems to do it fine.

Is there a way to make tex4ht break URL with underscore also?

TL 2015

1 Answer 1

3

tex4ht doesn't do anything with line breaks in URLs. It works for hyphens because browsers do that, but they don't break words with underscores. We need to insert zero-width space character after underscores to enable the linebreaking. Actions on certain characters may be defined using \g@addto@macro\UrlSpecials command, possibly in the .cfg file:

\Preamble{xhtml,p-width}
\makeatletter
\def\novspace{\HCode{&\#x200B;}}
\g@addto@macro\UrlSpecials{\do\_{\_\novspace}}
\makeatother
\begin{document}
\EndPreamble

we defined the \novspace command which inserts XML entity for zero width space unicode character and this command is inserted after the underscore using

\g@addto@macro\UrlSpecials{\do\_{\_\novspace}}

the result:

enter image description here

4
  • Thanks Michal. Nice solution. But for some reason it only works on Firefox and IE 11. On Chrome it does not wrap. This might be a browser thing. Earlier I found a hack using CSS in the .cfg file, but that worked only in Chrome and not in Firefox! Chrome version 50.0.2661.94 m. FF version 46.0.1
    – Nasser
    May 9, 2016 at 17:36
  • @Nasser that's definitely a browser thing. There are also some related CSS properties - word-break and word-wrap, they don't seem to do anything in Firefox, maybe it would work in Chrome
    – michal.h21
    May 9, 2016 at 21:17
  • yes, those what I tried and they work in Chrome only. But there is supposed to be a way to do it using CSS that will work in all browsers, based on what I read, but all the attempts I made failed so far. Not worth it. So I gave up. Your solution is simple and works. Who cares about Chrome users :)
    – Nasser
    May 9, 2016 at 22:29
  • @Nasser I've also tried the CS way first, but it didn't work in Firefox, only after that I've tried the actual solution. I would care about Chrome users and add also the CSS, it will work for everybody then (maybe except Links users :)
    – michal.h21
    May 10, 2016 at 6:16

You must log in to answer this question.

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