EDIT: See this answer if you also want your links to not overfull (they'll hyphenate wherever they see fit though)

I have seen this, but biblatex is used there, I don't use that in my case.

Is there a way for me to do:

\url{https://example.tld/example/something/}


Or, if it's possible, maybe even:

\url{https://www.example.tld/example/something/}


https://www.example.tld/example/something/


as an output in the document

to get this:

example.tld/example/something


but still have it pointing to the full link?

Is there no solution to this? Do I just have to \href all my links?:

\href{https://www.example.tld/example/something/}{example.tld/example/something}


Or is there anything that automatically takes care of this?

I am asking this because, if I type the link in the \url command like I'd want it to be displayed, it isn't always going to work when it gets clicked on.

• If you don't want to include both the full URL and the short version in your TeX source, how do you expect any automatic caretaking to determine the one you don't include? Not every URL begins with https://www.. May 22 at 7:09

Here's LaTeX3 based solution that uses regex to perform an advanced match like in the post you refer.

\documentclass{article}
\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

\begin{document}

\end{document}


Note that there are dozen of packages that already load expl3 (for instance xparse), so you might even not need to use \usepackage{expl3}

# Edit:

There's discussed in comments problem with hyphenation because of the way \texttt works. Here's a fix based on this answer.

\documentclass{article}
\usepackage{expl3}
\usepackage{hyperref}

\makeatletter
\DeclareRobustCommand\ttfamily
{\not@math@alphabet\ttfamily\mathtt
\fontfamily\ttdefault\selectfont\hyphenchar\font=-1\relax}
\makeatother
\DeclareTextFontCommand{\mytexttt}{\ttfamily\hyphenchar\font=\/\relax}

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

\begin{document}

\end{document}


• Just as a side note, if you are using LaTeX 2020/10/01 or newer, expl3 is by default already loaded for you. May 22 at 11:56
• That's actually what I was asking for originally! But, does it only work inside LaTeX3 (I'm using XeLaTeX myself)? And, when will it be ready to use? May 22 at 11:57
• @Andy3153 try it out then, MWE is attached. It has to work. May 22 at 15:49
• @Andy3153 even with \usepackage{expl3}? May 22 at 16:43
• @Andy3153 see the edited answer May 25 at 9:45

You can define a new command for this propose, for example as:

\documentclass{article}
\usepackage{hyperref}


• What I meant is that it solves my problem differently, not that it does it badly. In fact, I'm currently using this in my setup. What I originally asked for was a method to give it the full link, which will contain http(s)://, and then may or may not contain www.`, and remove it in the PDF output, but still keep it internally to make it go to the right path. Your method instead takes a much simpler approach of taking the already-shortened link, and then add everything necessary to it, which is really good too May 22 at 11:52