How to escape percent sign (%) in \path or \texttt environment?

How to escape percent sign (%) in \path{...} or in \texttt{...}?

For instance compiling this code fails since % sign starts comment:

\path{%SystemRoot%\system32\drivers\etc\hosts}


The result of escaping % sign:

\path{\%SystemRoot\%\system32\drivers\etc\hosts}


is:

\%SystemRoot\%\system32\drivers\etc\hosts


How to solve the problem?

Update

The problem is caused by \footnote. Minimal not working example:

\documentclass{article}
\usepackage{url}

\begin{document}
Hi!\footnote{%SystemRoot%\system32\drivers\etc\hosts}
\end{document}

• Erh, \path{%SystemRoot%\system32\drivers\etc\hosts} works just fine when you remember to load the url package. Do please tell us where you got \path from if it was not from the url package. – daleif Nov 3 '16 at 12:31
• In general, please make self contained examples, we are fresh out of magic crystal balls. – daleif Nov 3 '16 at 12:31
• Sorry. This issue comes from quite complex and huge master thesis, but I made minimal not working example. See update. – patryk.beza Nov 3 '16 at 13:12

\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}


• The problem is that I use \path{%SystemRoot%\system32\drivers\etc\hosts} in \footnote and it doesn't work. – patryk.beza Nov 3 '16 at 12:50
• @patryk.beza There's \urldef for that matter: you define a path and assign a name to a path, and then use the symbolic name to call it. Better approach, less option to have an error, and easy to change at one place and then all the symbolic calls are automatically showing what you want: \urldef\hostspath\path{%SystemRoot%\system32\drivers\etc\hosts} and then use \hostspath wherever you want. – Manuel Nov 3 '16 at 13:07
• Nice to know @Manuel, but it doesn't solve the problem. See my original post update. – patryk.beza Nov 3 '16 at 13:14
• @patryk.beza \urldef works. The example of the update section in the question only breaks because of several typos. – Heiko Oberdiek Nov 3 '16 at 14:22
• You are right. Thank you @Manuel and @HeikoOberdiek! – patryk.beza Nov 3 '16 at 15:26

here's an approach that depends on plain tex techniques.

\documentclass{article}
\usepackage{url}
\begingroup
\catcode\%=13
\gdef\mypath{\path{%SystemRoot%\system32\drivers\etc\hosts}}
\endgroup

\begin{document}
%Hi!\footnote{\path{%SystemRoot%\system32\drivers\etc\hosts}}
Hi!\footnote{\mypath}
\end{document}
`