7

I am using the hyperref package and I want to make a command for writing \hrefs once with their protocol removed. For example,

  • https://google.com becomes linked google.com
  • http://google.com becomes linked google.com

normally to do this I could write

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\href{https://google.com}{google.com}
\end{document}

however, it would be ideal if I could create a command that works like so

\newcommand{\uri}[1]{
  \href{#1}{
    % somehow remove http[s]?:\/\/ from #1
  }
}
7

With no special characters in the URL, it's quite easy:

\documentclass{article}
\usepackage{hyperref}

\newcommand{\rhref}[1]{%
  \href{#1}{\stripprotocol#1\stripprotocol}%
}
\def\stripprotocol#1//#2\stripprotocol{#2}

\begin{document}

\rhref{http://google.com}

\rhref{https://tex.stackexchange.com}

\end{document}

If you insist on using l3regex:

\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn
\NewDocumentCommand{\rhref}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \regex_replace_once:nnN { \A .*? // } { } \l_tmpa_tl
  \href{#1}{\l_tmpa_tl}
}
\ExplSyntaxOff

\begin{document}

\rhref{http://google.com}

\rhref{https://tex.stackexchange.com}

\end{document}

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.