2

I use, in agreement with the post of SE, the command \link.

\documentclass[10pt]{article}
\usepackage{bookmark}
\usepackage[table]{xcolor}
\usepackage{tabularx}

\newcommand{\link}[1]{%
    {%
        \def\textbf##1{##1}%
        \def\textit##1{##1}%
        \def\textcolor##1##2{##2}%
        \def\protect##1##2{##2}%
        \edef\_{\string_}%
        \edef\&{\string&}%
        \xdef\tmp{\noexpand\href{#1}}}%
    \ttfamily%
    \tmp{#1}%
}

\begin{document}

    \begin{tabularx}{\linewidth}{|l|l|}
        \link{https://www.\textcolor{blue}{longlink}.com/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/} & Long link description\tabularnewline
        \link{www.\textcolor{blue}{longlinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglink}.net} & Long link description\tabularnewline
    \end{tabularx}

\end{document}

and I get:

Compilation output

So… two questions:

  • How can I change the text of \link to get the hyphenation on the slash character without the hyphen char?

and if there isn't any slash character

  • How can I change the text of \link to get hyphenation anywhere but without the hyphen char?
BnG
  • 49
  • Maybe https://tex.stackexchange.com/a/108003 can help you (i.e., the breakurl package with anythingbreaks option)? – Marijn Jan 08 '19 at 21:32
  • No, with \usepackage[breaklinks=true]{hyperref} i get: Option clash for package hyperref, and \sloppy doesn't work. – BnG Jan 08 '19 at 21:37
  • I've linked to a specific answer which does not mention the breaklinks option for hyperref, it uses a separate package for breaking links (called breakurl). Did you try that? – Marijn Jan 08 '19 at 21:39
  • I'm sorry, \usepackage{breakurl} or \usepackage[anythingbreaks]{breakurl} don't work either. – BnG Jan 08 '19 at 21:41
  • 1
    I tried it myself now and it does not work indeed - and many other possible solutions also seem to fail for this specific case. I hope someone here comes along to solve it. – Marijn Jan 08 '19 at 22:16

1 Answers1

4

The idea is the same, but using more powerful expl3 methods.

With \link{<link>} you get the same as before, but with the possibility of breaking lines at slashes.

With \link*{<link>} you allow breaking at any letter. However this requires changing \textcolor{blue}{<string>} to \blue{<string>} otherwise also blue would be considered for breaking. Define similar commands for all colors you need and update the equivalences.

\documentclass[10pt]{article}
\usepackage{bookmark}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{xparse}

\NewDocumentCommand{\blue}{m}{\textcolor{blue}{#1}}

\ExplSyntaxOn
\NewDocumentCommand{\link}{sm}
 {
  \group_begin:
  \cs_set_eq:NN \textbf \use:n
  \cs_set_eq:NN \textit \use:n
  \cs_set_eq:NN \blue \use:n
  \cs_set:Npx \_ { \c_underscore_str }
  \cs_set:Npx \& { \c_ampersand_str }
  \tl_gset:Nx \g_tmpa_tl { #2 }
  \group_end:
  \tl_set:Nn \l_tmpa_tl { #2 }
  \IfBooleanTF{#1}
   {
    \regex_replace_all:nnN { [A-Za-z] } { \0 \c{linebreak}[0] } \l_tmpa_tl
   }
   {
    \tl_replace_all:Nnn \l_tmpa_tl { / } { /\linebreak[0] }
   }
  \group_begin:
  \ttfamily
  \exp_args:NVV \href \g_tmpa_tl \l_tmpa_tl
  \group_end:
 }
\ExplSyntaxOff

\begin{document}

\noindent
\begin{tabularx}{\linewidth}{|>{\raggedright}X|l|}
\link{https://www.\blue{longlink}.com/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/} & Long link description\tabularnewline
\link*{www.\blue{longlinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglink}.net} & Long link description\tabularnewline
\end{tabularx}

\end{document}

enter image description here

It is also possible to adopt the “break everywhere” strategy when there is no slash:

\documentclass[10pt]{article}
\usepackage{bookmark}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{xparse}

\NewDocumentCommand{\blue}{m}{\textcolor{blue}{#1}}

\ExplSyntaxOn
\NewDocumentCommand{\link}{m}
 {
  \group_begin:
  \cs_set_eq:NN \textbf \use:n
  \cs_set_eq:NN \textit \use:n
  \cs_set_eq:NN \blue \use:n
  \cs_set:Npx \_ { \c_underscore_str }
  \cs_set:Npx \& { \c_ampersand_str }
  \tl_gset:Nx \g_tmpa_tl { #1 }
  \group_end:
  \tl_set:Nn \l_tmpa_tl { #1 }
  \regex_match:nnTF { / } { #1 }
   {
    \tl_replace_all:Nnn \l_tmpa_tl { / } { /\linebreak[0] }
   }
   {
    \regex_replace_all:nnN { [A-Za-z] } { \0 \c{linebreak}[0] } \l_tmpa_tl
   }
  \group_begin:
  \ttfamily
  \exp_args:NVV \href \g_tmpa_tl \l_tmpa_tl
  \group_end:
 }
\ExplSyntaxOff

\begin{document}

\noindent
\begin{tabularx}{\linewidth}{|>{\raggedright}X|l|}
\link{https://www.\blue{example}.com/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/} & Long link description\tabularnewline
\link{www.\blue{longlinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglink}.net} & Long link description\tabularnewline
\end{tabularx}

\end{document}

The output is the same.

egreg
  • 1,121,712
  • Is it possible to get the second behavior in case of non-existence of the slash? – BnG Jan 08 '19 at 23:06
  • I get Use of @undeclaredcolor doesn't match its definition. \end{tabularx} if i try to use another color like \textcolor{red}. – BnG Jan 08 '19 at 23:08
  • 1
    @BnG Yes, it's possible and I added it. As I said, you need to define \red for \textcolor{red} similarly as for \blue and to add the line \cs_set:Npn \red \use:n in the code. – egreg Jan 08 '19 at 23:10
  • Doh :-) !!! Is there no way to avoid the definition of all colors… or even better, to delete all the commands starting with \ – BnG Jan 08 '19 at 23:15
  • @BnG Maybe, but the regular expression would be quite complicated. I think that defining semantic commands instead of using \textcolor is better; I used \blue and \red, but you can use whatever name that conveys the idea you want to express. This has also the advantage that you can redefine the colors uniformly in case of need, without chasing through the document. – egreg Jan 08 '19 at 23:34
  • Thanks @egreg. It works. Can you still show me what I should do for defining all \textcolor without \blue definition? – BnG Jan 09 '19 at 16:09
  • @BnG Sorry but I don't understand. Instead of \blue you can use whatever name is not already taken. – egreg Jan 09 '19 at 16:14
  • Ok. So I can't define, with expl3, something like \cs_set_eq:NN \textcolor {color} \use: n like the example shown in the question \def\textcolor##1##2{##2}. – BnG Jan 09 '19 at 16:22
  • @BnG Sorry, but that doesn't make sense. There are two sorts of problems: the \cs_set_eq:NN part is for removing the color declarations when building the URL; the \blue command is necessary for adding the break points in the printed and formatted URL. – egreg Jan 09 '19 at 16:23