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?

1 Answer 1

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}
7
  • 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? Jan 28, 2013 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. Jan 28, 2013 at 11:31
  • Apologies - it was a typo that was confusing me. Thanks again for the solution. Jan 28, 2013 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} Feb 11, 2013 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 Feb 11, 2013 at 17:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .