# Using url in a command adds unwanted spaces

With url-Package imported like this

\usepackage[obeyspaces, hyphens]{url}


defining a command like this

\newcommand{\dirpath}[1]{{\url{#1}}}


will when used like this

\dirpath{Documents\BliBla\bludsdus\dssdsd}


produce spaces in the output:

For comparison:

\dirpath{Documents\BliBla\bludsdus\dssdsd}\linebreak\linebreak
\url{Documents\BliBla\bludsdus\dssdsd}


How can one prevent these spaces to be added? I want to use the command to define how to format directory paths in my document.

• \newcommand{\dirpath}{\url} But there's already \path. Jun 20, 2017 at 15:44

You can use the \DeclareUrlCommand and specify before and after text for the url via \UrlLeft and \UrlRight:

\documentclass{article}

\usepackage[obeyspaces, hyphens]{url}

\DeclareUrlCommand{\dirpath}{\def\UrlLeft{\textrm{}}\def\UrlRight{\textrm{''}}}

\begin{document}

\dirpath{Documents\BliBla\bludsdus\dssdsd}

\end{document}