\path
of package url
directly supports percent characters, unless it is used inside arguments of other macros. \path
(or \url
) changes the category codes of special characters before and for reading its argument.
\documentclass{article}
\usepackage{url}
\usepackage[T1]{fontenc}% for \textbackslash inside \texttt
\begin{document}
\path{%SystemRoot%\system32\drivers\etc\hosts}
\texttt{\%SystemRoot\%\textbackslash system32\textbackslash drivers%
\textbackslash etc\textbackslash hosts}
\end{document}

\texttt
can also be used, but special characters are more cumbersome. The percent character can be generated by \%
(or \@percentchar
, which needs \makeatletter
). The backslash can be given in text mode by \textbackslash
. Font encoding T1
fixes the outdated OT1
encoding with respect to the backslash in the typewriter font family.
Path in footnotes
\urldef
works fine, the update section of the question misspells the command: \hostspath
vs. \hostpath
. Also, there are two invisible Unicode characters inside hosts
: U+200B ZERO WIDTH SPACE and U+200C ZERO WIDTH NON-JOINER. The corrected version with corrected macro name and removed garbage characters:
\documentclass{article}
\usepackage{url}
\urldef\hostpath\path{%SystemRoot%\system32\drivers\etc\hosts}
\begin{document}
% \vspace*{\fill} % only for smaller image for answer
Hi!\footnote{\hostpath}
\end{document}

\path{%SystemRoot%\system32\drivers\etc\hosts}
works just fine when you remember to load theurl
package. Do please tell us where you got\path
from if it was not from theurl
package. – daleif Nov 3 '16 at 12:31