Hyphenate \texttt

hyphenationline-breakingtypewriterurlsxetex

Before anyone says anything, yes, of course I've seen this. But, it's not what I want.

What I'm looking for is basically a way to either alter the way \texttt works, or a new command that works just like \texttt does, but, instead of hyphenating only at spaces, making it hyphenate wherever it sees fit, because in my document I have things that don't contain any sort of dashes, and they're just really long randomly-generated strings of letters and numbers, so, they won't get hyphenated with that solution. I don't care how the text gets hyphenated, I just don't want it to overfull.

Why I want this? Well, I am using a command that takes my links, and then it strips them of any protocol info (http(s)://) or www, and shows them that way inside the compiled document, but keeps them functionally the same (when they get clicked on inside the PDF). The command comes from this thread:

\usepackage{expl3}
\usepackage{hyperref}

\ExplSyntaxOn
\newcommand{\clearurl}[1]{
    \tl_set:Nn \parsed_url {#1}
    \regex_replace_all:nnN {.*:\/\/(?:www.)?(.*[^\/])\/?} {\1} \parsed_url
    \href{#1}{\texttt{\parsed_url}}
}
\ExplSyntaxOff

Best Answer

You can exploit xurl.

\documentclass{article}
\usepackage{hyperref}
\usepackage{xurl}

\ExplSyntaxOn

\NewDocumentCommand{\clearurl}{m}
  {
    \tl_set:Nn \l__clearurl_url_tl {#1}
    \regex_replace_all:nnN {.*:\/\/(?:www.)?(.*[^\/])\/?} {\1} \l__clearurl_url_tl
    \clearurl_print:nV { #1 } \l__clearurl_url_tl
  }

\tl_new:N \l__clearurl_url_tl
\cs_new_protected:Nn \clearurl_print:nn
  {
    \href{#1}{\nolinkurl{#2}}
  }
\cs_generate_variant:Nn \clearurl_print:nn { nV }

\ExplSyntaxOff

\begin{document}

\clearurl{https://www.google.it/books/edition/Mirifici_Logarithmorum_Canonis_Construct/VukHAQAAIAAJ?hl=en&gbpv=1&dq=mirifici+logarithmorum&pg=PT1&printsec=frontcover}

\end{document}

enter image description here

Related Question