4

Why is this snippet:

\newcommand{\Subject}{Skeleton}
\url{http://www.cheatsheet.jim.pm?subject=\lowercase{\Subject{}}}

rendered to this?

http://www.cheatsheet.jim.pm?subject=\lowercase{Skeleton}

and not the intended:

http://www.cheatsheet.jim.pm?subject=skeleton
4
  • I believe that url makes use of verbatim.
    – Sigur
    Apr 8, 2013 at 14:32
  • You are probably right @Sigur, but why does \Subject{} get expanded but not \lowercase?
    – SlimJim
    Apr 8, 2013 at 14:33
  • \lowercase is not an expandable command. Apr 8, 2013 at 14:34
  • @sigur: url proves a \verb-like command but does not use verbatim to do so. Apr 8, 2013 at 15:52

2 Answers 2

9

\lowercase (and {}) are not expandable so you need to change the order of execution:

\documentclass{article}
\usepackage{url}

\newcommand{\Subject}{Skeleton}

\newcommand\lcURL[1]{%
  \edef\tmp{\lowercase{\noexpand\url{http://www.cheatsheet.jim.pm?subject=#1}}}%
  \tmp}

\begin{document}


\lcURL{\Subject}

\end{document}

If you need to remove space you need to use expansion only if you are not going to interfere with \url. Fortunately LaTeX has a built-in command to remove spaces via expansion:

\newcommand{\Subject}{Skeleton bones}
\makeatletter
\newcommand\lcURL[1]{%
  \edef\tmp{\lowercase{\noexpand\url{http://www.cheatsheet.jim.pm?subject=\zap@space#1 \@empty}}}%
  \tmp}
\makeatother

produces

http://www.cheatsheet.jim.pm?subject=skeletonbones
2
  • it works but I have no clue how, know I want to remove spaces from the \Subject with \StrSubstitute{#1}{ }{} but I cannot get it to compile and show properly, I tried putting inside the lcURL at different places as well as a function RemoveSpace that mimics the magic from lcURL but all in vain. The most common error is ! Argument of \reserved@a has an extra }.
    – SlimJim
    Apr 8, 2013 at 21:41
  • 1
    I don't think \StrSubstitute works via expansion so it will interfere with \url, see update. Apr 8, 2013 at 22:04
3

Just to see how one can use \StrSubstitute in this context:

\documentclass{article}
\usepackage{url,xstring}

\newcommand{\Subject}{Ske Le Ton}

\newcommand\lcURL[1]{%
  \begingroup
  \StrSubstitute{#1}{ }{}[\tmp]%
    \edef\tmp{%
      \lowercase{%
        \endgroup
        \noexpand\url{http://www.cheatsheet.jim.pm?subject=\tmp}%
      }%
    }%
  \tmp
}

\begin{document}

X\lcURL{\Subject}X

X\url{http://www.cheatsheet.jim.pm?subject=skeleton}X

\end{document}

With \begingroup and \endgroup we make sure that any definition of \tmp vanishes after usage.

enter image description here

You must log in to answer this question.

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