I would like to replace every backslash \
inside a macro (string) with a forwardslash /
.
The macro is intended to contain a filename in Windows notation which should be replaced by a valid (La)TeX compatible filename. I know that it is preferable to process such strings before using them in a source file, but: is it possible to do it with (La)TeX?
My MWE is:
\documentclass{article}
\usepackage{etoolbox}
% The following macro is a placeholder.
% It should replace '\' by '/'
% #1: input
% #2: output
\newcommand{\replaceBS}[2]{%
\edef\x{\noexpand\edef\noexpand#2{\noexpand\detokenize{\expandonce#1}}}%
\x%
}
\begin{document}
\def\WindowsFilename{c:\files\examples\pictures\myfile.png}
\replaceBS{\WindowsFilename}{\TeXFilename}
Current result: \texttt{\TeXFilename}
Expected result: \texttt{c:/files/examples/pictures/myfile.png}
\end{document}
A solution for \replaceBS
should be compilable with pdflatex
.
I've tried things with the xstring
package so far without success. Any solution with TeX, LaTeX3, special packages,… is welcome.
And just to be sure: Yes, I know how to do a replacement with an editor or an external tool 😉
Best Answer