8

I would like to write a macro similar to the \href command from the hyperref package, but which takes a single argument. The URL is generated by making the argument lowercase.

Here is a minimal example:

\documentclass{article}
\usepackage{hyperref}
\newcommand\mylink[1]{\href{www.\lowercase{#1}.com}{#1}}

\begin{document}
\mylink{MyURL}
\end{document}

However, the \lowercase command is not expanded, and the link points to www.\lowercase{MyURL}.com. Is there a way to solve this problem?

lockstep
  • 250,273

1 Answers1

11
\newcommand\mylink[1]{\lowercase{\href{www.#1.com}}{#1}}

will work.

As requested in comments a more complicated example:

\documentclass{article}
\usepackage{hyperref}

\newcommand\mylink[1]{\lowercase{\href{www.#1.com}}{#1}}
\newcommand\mymail[2]{%
\lowercase{\def\tmpa{#1}}%
\uppercase{\def\tmpb{#2}}%
\href{mailto:\tmpa.\tmpb}{#1.#2}}

\begin{document}
\mylink{MyURL}

\mymail{David}{Example.ORG}

\end{document}
David Carlisle
  • 757,742
  • Thanks, that's great. Do you know any reason why this stops working if I put in my class file rather than the preamble? Or do I need to check for conflicting settings? – Prince Goulash Jan 28 '13 at 11:07
  • should make no difference unless something else redefines it add \show\mylink after \begin{document} and tex will stop and show you the definition, if it isn't your definition something else is defining it. – David Carlisle Jan 28 '13 at 11:31
  • Apologies - it was a typo that was confusing me. Thanks again for the solution. – Prince Goulash Jan 28 '13 at 12:15
  • Surprising that it works. But out of curiosity: How to expand stuff in \href? I might want to do something like: \href{mailto:\lowercase{#1}.\uppercase{#2}}{#1.#2} – Frederick Nord Feb 11 '13 at 17:07
  • Things expand automatically in \href: the problem is that \lowercase is not an expandable command, I'll add code to the answer for this – David Carlisle Feb 11 '13 at 17:36
  • Some other comments https://tex.stackexchange.com/questions/513914/hrefurltext-doesnt-open-the-url-in-new-browser#comment1299566_513914 https://tex.stackexchange.com/questions/200577/make-hyperref-links-open-in-new-browser-window-for-easier-navigation/201952#comment466594_200590 claims that \href requires initial http:// or https:// to work while this answer seems to not put that. Any idea? – user202729 May 14 '23 at 10:17
  • @user202729 this answer lowercased the string requested, whether that string is in fact a valid url that would work in the pdf file was apparently not commened on at the time... also it assumes a real (ascii) url, \lowercase would mess up utf8 in pdftex (probably I should add an expl3 case change answer) – David Carlisle May 14 '23 at 10:37