9

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/}

And, instead of getting

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.

1
  • 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..
    – Rosie F
    May 22 at 7:09

2 Answers 2

5

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}

Follow the link: \clearurl{https://example.tld/example/something/}

\end{document}

enter image description here

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}

Follow the link: \clearurl{https://example.tld/example/something/}

\end{document}

enter image description here

17
  • 2
    Just as a side note, if you are using LaTeX 2020/10/01 or newer, expl3 is by default already loaded for you.
    – Jinwen
    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?
    – Andy3153
    May 22 at 11:57
  • 1
    @Andy3153 try it out then, MWE is attached. It has to work.
    – antshar
    May 22 at 15:49
  • 1
    @Andy3153 even with \usepackage{expl3}?
    – antshar
    May 22 at 16:43
  • 1
    @Andy3153 see the edited answer
    – antshar
    May 25 at 9:45
5

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

\documentclass{article}
\usepackage{hyperref}

\newcommand{\shortlink}[1]{\href{https://www.#1}{\texttt{#1}}}

\begin{document}

\shortlink{example.tld/example/something}

\end{document}
3
  • Doesn't satisfy exactly what I asked for, but this is a great alternative, and I will use it.
    – Andy3153
    May 21 at 21:18
  • @Andy3153 This is of course only a starting point. You may try to write your own command to satisfy your need -- there are plenty of examples on this site which might be useful to you.
    – Jinwen
    May 21 at 21:49
  • 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
    – Andy3153
    May 22 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.