0

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
4
  • the url package (used by hyperref) has standard listto say where line breaking can happen, xurl package sets that to allow breaking anywhere May 23, 2022 at 16:35
  • Yes. But, as you can see, I am not using \url, I am using something that transforms links into nicely-formatted \href-s
    – Andy3153
    May 23, 2022 at 16:38
  • sure but you can use \href to make the link with the full url and use \path` (ie\url wihout the linking) to make the link text May 23, 2022 at 17:02
  • @DavidCarlisle You're telling me that I should, on line 8 in the code I pasted in the question, replace \texttt with \path, and then use the xurl package? That doesn't work, it transforms all the links in the document in just \parsed_url, meaning it doesn't actually parse that command.
    – Andy3153
    May 23, 2022 at 17:12

1 Answer 1

2

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

You must log in to answer this question.

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