10

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.

Andy3153
  • 361
  • 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 '22 at 07:09

2 Answers2

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}

Jinwen
  • 8,518
  • Doesn't satisfy exactly what I asked for, but this is a great alternative, and I will use it. – Andy3153 May 21 '22 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 '22 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 '22 at 11:52
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

antshar
  • 4,238
  • 9
  • 30
  • 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 '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 '22 at 11:57
  • @Andy3153 LaTeX3 isn't some kind of a compiler like pdflatex, lualatex, xelatex etc. LaTeX3 is just a new generation of coding language in TeX. It originally written all in pure TeX primitives, so treat it as another one package. Read more about it here: https://www.latex-project.org/help/documentation/ltx3info.pdf – antshar May 22 '22 at 12:10
  • @antshar I know that it isn't a compiler, but, what it actually is feels more confusing to me. I don't know how right it is, but I like to think about it more like of a specification. The reason I mentioned what compiler I use is because I don't know if/how I can use LaTeX3-specific stuff inside it, or if it is handled automatically. – Andy3153 May 22 '22 at 15:35
  • 1
    @Andy3153 try it out then, MWE is attached. It has to work. – antshar May 22 '22 at 15:49
  • Yep. It worked for me, and even how @Jinwen said, without loading the expl3 package. Switching to this as a solution – Andy3153 May 22 '22 at 15:52
  • Wait. I later figured the reason it worked was because I compiled with pdfLaTeX on accident, instead of compiling with XeLaTeX. So, it doesn't work for me. – Andy3153 May 22 '22 at 16:42
  • 1
    @Andy3153 even with \usepackage{expl3}? – antshar May 22 '22 at 16:43
  • @antshar Nope, it was my fault. I tried to place the \parsed_url command that was inside the \href inside a \texttt (at line 9). Then, I realized I have to place the #1 at line 7 in that to make the text in \ttfamily.

    EDIT: Weirdly enough, that worked in pdfLaTeX when I tested it.

    – Andy3153 May 22 '22 at 16:57
  • @antshar I just realized that what I did still doesn't work. How do I put the link through a \texttt{}? Plus, should we move to chat? – Andy3153 May 22 '22 at 17:11
  • @Andy3153 \href already prints out text in typewriter font, so you don't need additional \texttt The screenshot that's attached to my answer is the exact output you get when you compile the MWE. – antshar May 22 '22 at 17:14
  • @antshar Weirdly enough, not for me...? This is what I get by running your MWE, regardless of whether I run it with pdfLaTeX or XeLaTeX: https://imgur.com/a/l1SAeCE EDIT: Also, I think that you mean that \url always puts links through \texttt, \href doesn't, and it makes sense why not – Andy3153 May 22 '22 at 17:22
  • @Andy3153 make sure you compile only my MWE without any other code of yours. Compile in a separate file to guarantee that nothing else affects the behavior of hyperref package. – antshar May 22 '22 at 17:29
  • @antshar I just showed you in my image that it's literally your MWE, copy-pasted. My only addition is the first line, which is just a comment that lets vimtex know it has to compile with xelatex: https://imgur.com/a/l1SAeCE – Andy3153 May 22 '22 at 17:32
  • @Andy3153 the link to your image doesn't work for me. Btw, you were right, I double checked and \href indeed prints normal font unlike \url. Sorry for that. Now I've just edited the post to support typewriter font. – antshar May 22 '22 at 17:46
  • @antshar Yes, my bad. The link didn't work for some reason. If it matters, here's one that works: https://i.imgur.com/lYQVOqv.png Now, I've got another question: how do I deal with hyphenation of long links inside this? I don't care how they get hyphenated, I just don't want it to overfull – Andy3153 May 23 '22 at 11:32
  • 1
    @Andy3153 see the edited answer – antshar May 25 '22 at 09:45