Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
share|improve this question
    
I believe that url makes use of verbatim. –  Sigur Apr 8 '13 at 14:32
    
You are probably right @Sigur, but why does \Subject{} get expanded but not \lowercase? –  SlimJim Apr 8 '13 at 14:33
    
\lowercase is not an expandable command. –  David Carlisle Apr 8 '13 at 14:34
    
@sigur: url proves a \verb-like command but does not use verbatim to do so. –  Matthew Leingang Apr 8 '13 at 15:52
add comment

2 Answers

up vote 9 down vote accepted

\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
share|improve this answer
    
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 '13 at 21:41
1  
I don't think \StrSubstitute works via expansion so it will interfere with \url, see update. –  David Carlisle Apr 8 '13 at 22:04
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.