If I to include a graphic I have to manually replace backslashes '\' with slashes '/', which isn't very convenient especially if the path is long. But Latex is all about convenience and automation, right?

So I wonder if the a macro/package that would take a Windows path and replace backslashes with slashes and return it, preferably could handle sdpaces in the path. Intended use for including graphics and listings like this

\includegraphics{"\windowspath{my path1\my path2\my image.png}"}
\lstinputlisting{"\windowspath{my path1\my path2\myscript.m}"}

I found something similar here but it seems to define the converted path as a new variable, rather then returning it.

  • 2
    It should be rather rare to ever need a full path, \includegraphics{"my image"} would be enough in most cases. The syntax you suggest would be very tricky as the space after \my is never tokenised it will have been absorbed while tex parsed the command \my so while you could detokenise the string and turn \ to / you can not reliably tell if there should be a space after \my – David Carlisle Nov 7 '17 at 19:17
  • why do you have to "replace \ with / ? How did the original string get entered? I ask as it would be better to fix that process so the / version gets entered into the file. – David Carlisle Nov 7 '17 at 19:27
  • - But Latex is all about convenience and automation, right? - I have good news and bad news... – percusse Nov 7 '17 at 19:39
  • @percusse please tell them, I'm very curious! – Skillmon Nov 7 '17 at 19:42
  • I have good and bad news. The good: it is easily possible to write a macro which contains paths with \ in them and return the same path with / instead of \ . The bad: it won't work in \input and I don't think it would in \includegraphics or similar. – Skillmon Nov 7 '17 at 19:44

The listofitems package can convert the backslashes and preserve the spaces (even if they immediately follow the \!). However, the name itself cannot be within a group, as was shown in the OP's example.

However, converting this demonstration to a usable macro will still take a bit of work.

\catcode`?=0 %
?catcode`\\=12 %
?greadlist?nameA{"\windowspath\my path1\ my path2\my image.png"}
?greadlist?nameB{"\windowspath\my path1\my path2\myscript.m}


enter image description here

  • Will it be possible to create a single expandable macro from this? I don't think so, because of the necessary cat-code changes. – Skillmon Nov 7 '17 at 20:06
  • @Skillmon That's the hard part, isn't it. – Steven B. Segletes Nov 7 '17 at 20:06
  • I have little idea on (La)TeX internals, but isn't it possible to temporarily change the catcode, perform the transformation, and then change it back? – Andrey Pro Dec 2 '17 at 13:56
  • @AndreyPro The way I have set it up, it does that. I placed the \catcode changes inside a group{...}. Once leaving the group, the \catcodes revert to their original settings. Inside the group, I made sure to use \greadlist which is a \global form of \readlist, so that the list survives leaving the group, even as the \catcodes do not survive leaving the group. – Steven B. Segletes Dec 2 '17 at 16:41
  • @AndreyPro you can't usefully change the catcodes after the characters have been read, so any such command is like \verb and can not be used in the argument of another command, which makes it more or less unusable for \input or \includegraphics etc. – David Carlisle Dec 2 '17 at 17:41

The simplest way is probably just to locally define any macros you need to generate the file path




{\def\c{/c}\def\tmp{/tmp}\def\my{/my" "}\def\picture{/picture}

\includegraphics{\c\tmp\my pics\picture.png}



inputs the file \c\tmp\my pics\picture.png as seen from the log:

[2 </c/tmp/my pics/picture.png>] (./file.aux) )


(see the transcript file for additional information)</usr/local/texlive/2017/te
Output written on file.pdf (2 pages, 75385 bytes).
Transcript written on file5.log.
  • This is no better than do it manually. The whole point is to be able to copy paste the path from Windows Explorer and have the Latex to transform it. – Andrey Pro Dec 2 '17 at 13:42
  • @AndreyPro I'm not sure that it's reasonable to expect things to be copied from one system to another and be legal syntax in the result – David Carlisle Dec 2 '17 at 13:46
  • Of course not per default, that is why asked for a macro that would be able to translate between them. – Andrey Pro Dec 2 '17 at 13:59
  • @AndreyPro but doing it within tex requires the to be legal syntax.... – David Carlisle Dec 2 '17 at 14:15

A somewhat simple implementations in expl3 could be the following. Note however, that it isn't fully expandable and therefore doesn't work in \input and the like:



  \str_set:Nn \l_tmpa_str { #1 }
  \regex_replace_all:nnN { \\ } { / } \l_tmpa_str
  \str_use:N \l_tmpa_str}

\windowspath{\fo o.tex}

%\input{\windowspath{\f o}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.