0

I'm writing documentation that makes frequent reference to Windows-style paths, eg "K:\Folder1\folder2\filename". I see in a badly organized LaTeX user's guide that some special characters can be displayed by preceding them with '\' itself—'\#', for example—but that doesn't work for the backslash itself. How do I produce that character?


Part 2: I tried using the \verb command, and it seems to work: I can use "K:\verb-\-Folder1\verb-\-folder2\verb-\-filename" and get what I want. But this morning I decided that I should put that path into my first \newcommand, and I can't figure out how to avoid the error message "\verb ended by end of line".

I have tried shorter \newcommands with a backslash in them, enough to satisfy myself that this error message is certainly untrue; even with short lines I get this message. But I notice in the user's guide that the very next error message listed is "\verb illegal in command argument". LaTeX isn't showing me that message, but is it true? Is there no way to use \verb inside a \newcommand? Because if so, it seems kind of odd; a line of text with a lot of \verb commands seems an ideal candidate for a \newcommand shortcut.

3
  • 1
    Welcome to TeX.SE!
    – Mensch
    Commented Jan 4, 2023 at 16:31
  • 1
    I'm pretty sure it's true that \verb isn't allowed in \newcommand (I think because it's a fairly messy command to begin with). But in your particular case, have you tried \verb-K:\Folder1\folder2\filename-?
    – Teepeemm
    Commented Jan 4, 2023 at 19:14
  • LOL! No, that never occurred to me! Thanks for the idea, whatever else others add. Commented Jan 7, 2023 at 21:36

2 Answers 2

7

You can use url package to set a path (at least as long as you don't use umlauts and similar in the names), or set the path manually with \textbackslash, or use menukeys with a slash as input:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{url}
\usepackage{menukeys}
\newmenustylesimple*{mystyle}{\ttfamily\CurrentMenuElement}[\textbackslash]{blacknwhite}
\renewmenumacro{\directory}[/]{mystyle}
\begin{document}
url:

\path{K:\Folder1\folder2\filename}

manual:

K:\textbackslash Folder1\textbackslash folder2\textbackslash filename

menu keys:

\directory{K:/Folder1/folder2/filename}

\end{document}

enter image description here

4

Following up @Teepeemm's suggestion, you can define a command in expl3 that takes a v argument (verbatim):

\NewDocumentCommand{\mypath}{v}{#1}

expl3

MWE

\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand { \mypath } { v } {
  #1
}
\ExplSyntaxOff

\begin{document}

\mypath{K:\Folder1\folder2\filename}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .