# Using commands inside \url?

How does one use commands inside \url{..}?

MWE:

\documentclass{article}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{url}

\newcommand{\placeholder}[1]{\textcolor{red!60!black}{\textlangle#1\textrangle}}

\begin{document}
\url{Hello \placeholder{name}}.
\end{document}


which produces:

Hello \placeholder{name}.


and not the expected:

Hello <name>.


This is a contrived example. In reality I have defined several custom url commands (Macros for formatting & breaking classnames, file paths etc?), and it is in those commands that I want to use the placeholder. But I believe the solution for this simpler case is the same as for the custom urlcommands.

-
Please provide a bit more information as to what you're looking to achieve and what an argument to the macro \placeholder might realistically be expected to look like. E.g., are you looking to typeset a URL string in a certain color, are you trying to "hide" the real URL string with a placeholder, or something else? –  Mico Apr 5 '14 at 11:44
Why can't you use Hello \texttt{\placeholder{name}}? –  Herbert Apr 5 '14 at 11:58
The definition of \placeholder is included in the MWE. It is exactly that which I expect it to look like; dark red text surrounded by < and >. For example, to signify that domain is a placeholder in the url http://domain.com/ I want it to look like this: http://<domain.com>/. –  johnrl Apr 5 '14 at 11:59
@Herbert \texttt does not break nicely like \url and custom url commands does. See tex.stackexchange.com/questions/152972/… –  johnrl Apr 5 '14 at 12:00
Untried, but doesn't \expandafter\url{\placeholder{xx}} work? –  hugovdberg Apr 5 '14 at 12:18

\UrlLeft and \UrlRight can be used for the purpose of adding angle brackets, see section "6 Yet more flexibility" url's documentation. Example:

\documentclass{article}

\usepackage{url}
\DeclareUrlCommand\url{%
\def\UrlLeft{\langle}%
\def\UrlRight{\rangle}%
}

\begin{document}
\url{http://www.example.org/}
\end{document}


Remarks:

• \langle and \rangle are used instead of the text mode commands, because the implicitly used math mode of \url.

## Package hyperref

Command \url is redefined to add additional code needed by hyperref, thus it cannot be redefined directly. The old meaning is stored and uses as \Hurl, thus this is the macro that can be redefined:

\documentclass{article}

\DeclareUrlCommand\Hurl{%
\def\UrlLeft{\langle}%
\def\UrlRight{\rangle}%
}

\begin{document}
\url{http://www.example.org/}
\end{document}


The angle brackets are part of the link, but not part of the URL for the link. If the brackets should not be colorized by option colorlinks, then the color could be reset to \normalcolor for the fences:

\documentclass{article}

\DeclareUrlCommand\Hurl{%
\def\UrlLeft{\begingroup\normalcolor\langle\endgroup}%
\def\UrlRight{\begingroup\normalcolor\rangle\endgroup}%
}

\begin{document}
\url{http://www.example.org/}
\end{document}


If the current color should be used instead of \normalcolor, then it becomes a little more complicate. Package xcolor has the concept of a current color . and this color can be saved via \colorlet and uses later inside the url command:

\documentclass{article}

\usepackage{xcolor}
\usepackage{letltxmacro}

\LetLtxMacro\OriginalUrl\url
\DeclareRobustCommand*{\url}{%
\colorlet{beforeurl}{.}%
\OriginalUrl
}
\DeclareUrlCommand\Hurl{%
\def\UrlLeft{\begingroup\color{beforeurl}\langle\endgroup}%
\def\UrlRight{\begingroup\color{beforeurl}\rangle\endgroup}%
}

\begin{document}
\color{blue}\url{http://www.example.org/}
\end{document}


-
Thanks, but I don't see how to use this when the purpose is to only put a part of the url in brackets? I may not have been clear about that in my post. I have reformulated it to be more clear. –  johnrl Apr 5 '14 at 13:19
@johnrl: Then it's not a URL. There are many packages vor verbatim stuff (alltt, listings, ...). –  Heiko Oberdiek Apr 5 '14 at 13:25
do you know any better solutions to do this then? tex.stackexchange.com/questions/152972/… –  johnrl Apr 5 '14 at 15:57

Not sure I understand the requirements, but here is some custom \verb more or less extracted from some code I needed on another occasion. As the alltt environment from the alltt package it treats \, {, and } normally. Additionally it treats spaces as in regular tex source, thus allowing line breaks. And this is an inline, not display "environment".

To be used as \myverb!stuff! or \myverb+stuff+ etc... (no empty line in argument, to force multiple spaces, use \ \ \ for example as in standard text).

\documentclass{article}
\usepackage{xcolor}
\usepackage{textcomp}

\makeatletter
% DEFINITIONS FOR SOME CUSTOM INLINE VERBATIM À LA ALLTT
% customize as desired:
\def\MicroFont{\normalfont\ttfamily\hyphenchar\font 45 }

\def\mydo@noligs #1%
% jfbu: modification to \do@noligs  (added space after #1) is mandatory if
% space is not made active via \@vobeyspaces in verbatim stuff.
{%
\catcode #1\active
\begingroup \lccode \~=#1\relax
\lowercase {\endgroup \def ~{\leavevmode \kern \z@ \char #1 }}%
}%

% custom inline verbatim allowing line-breaks and like in alltt environment, \,
% { and } have their standard behavoir. For example an hyphenation point may be
% added with \-. NOTA BENE: spaces are treated as elsewhere, thus multiple
% spaces in source collapsed into only one in printed result. Line breaks are
% possible.
\def\myverb
{%
\relax \ifmmode\hbox\else\leavevmode\null\fi
\bgroup \MicroFont
\let\do\mydo@noligs  \verbatim@nolig@list
\let\do\@makeother   \dospecials
\catcode32 10
\catcode92 0 \catcode123 1 \catcode125 2
\frenchspacing
\@myverb
}%
\def\@myverb #1{\catcode#1\active\lccode\~#1\lowercase{\let~\egroup}}%

\makeatother

\newcommand{\placeholder}[1]{\textcolor{red!60!black}{\textlangle#1\textrangle}}

\begin{document}\thispagestyle{empty}
\myverb+Hello \placeholder{name}
Hello \placeholder{name}
Hello \placeholder{name}
Hello \placeholder{name}
Hello \placeholder{name}
Hello \placeholder{name}
Hello \placeholder{name}
Hello \placeholder{name}+ and this works also in math mode $a^n +\myverb?b^n=c^n?$. Back to normal.
\end{document}


I would try

\newcommand{\urlholder}[1]{\textcolor{red!60!black}{\textlangle\url{#1}\textrangle}}


mwe:

\documentclass{article}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{url}

\newcommand{\urlholder}[1]{\textcolor{red!60!black}{\textlangle\url{#1}\textrangle}}

\begin{document}

Hello \urlholder{name and \name}

\end{document}


-
Good idea. Unfortunately this does not work as intended. I want the whole sentence to be a URL, not just the name. And of course that should not be hardcoded. –  johnrl Apr 5 '14 at 11:16
which sentence? –  jfbu Apr 5 '14 at 15:29
Sorry for the confusion. I updated the example. Please recheck. –  johnrl Apr 5 '14 at 17:58
well, then it's not a URL. So you just want to achieve some verbatim printing? –  jfbu Apr 5 '14 at 18:15
I was advised to use the URL package for defining custom verbatim breaking here tex.stackexchange.com/questions/152972/… - so yes. –  johnrl Apr 5 '14 at 19:23