Currfile Paths – Change Path Separator in Currfile

currfile

How do I change the path separator from \ to / ?

enter image description here

I use MikteX on Windows.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[abspath]{currfile}

\begin{document}

Absolute directory: \texttt{\currfileabsdir}

Absolute path: \texttt{\currfileabspath}
 
\end{document}

Edit : I'm looking to use currfileabsdir in an \input{}.

Best Answer

If you wish to change the definition of a macro like \currfileabsdir so that explicit backslash-characters of category 12(other) are replaced by (forward) slash-characters of category 12(other), you can try something like this:

\documentclass{article}
\usepackage[abspath]{currfile}

\ExplSyntaxOn
\cs_set:Npn \ReplaceBackslashesWithForwardSlashesInMacro #1
{
  \exp_args:Nno \use:n { \str_set:Nn \l_tmpa_str} {#1} 
  \regex_replace_all:nnN{\\}{/} \l_tmpa_str
  \exp_args:NnV \use:n {\cs_gset:Npn #1 } \l_tmpa_str
}
\ExplSyntaxOff

\begin{document}

Before patching \verb|\currfileabsdir|:

\texttt{\currfileabsdir}

\ReplaceBackslashesWithForwardSlashesInMacro{\currfileabsdir}

After patching \verb|\currfileabsdir|:

\texttt{\currfileabsdir}

\bigskip\hrule\bigskip

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Let's define some other macro whose expansion contains backslashes:
\begingroup
\catcode`\/=0
\catcode`\\=12
/gdef/SomeMacroWithBackslashes{\home\knoppix\Desktop\Zu Sichern\LaTeX Code\}
/endgroup
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Before patching \verb|\SomeMacroWithBackslashes|:

\texttt{\SomeMacroWithBackslashes}

\ReplaceBackslashesWithForwardSlashesInMacro{\SomeMacroWithBackslashes}

After patching \verb|\SomeMacroWithBackslashes|:

\texttt{\SomeMacroWithBackslashes}

\end{document}

enter image description here

You say you use MiKTeX on Windows - thus I doubt that the mixture of slashes and backslashes is the only source of your problems:

  1. Section "Canonicalize separators" of the Microsoft .NET article "File path formats on Windows systems", url: https://docs.microsoft.com/en-us/dotnet/standard/io/file-path-formats, says:

    "All forward slashes (/) are converted into the standard Windows separator, the back slash (\). If they are present, a series of slashes that follow the first two slashes are collapsed into a single slash."

    So a mixture of slashes and backslashes should be possible in many situations with recent Windows platforms on the side of routines belonging to Windows' Advanced Programmer Interfaces.

    But I don't know how LaTeX's macros/routines for parsing/splitting directory paths and file names handle backslashes... (In the past I used to delve into such details, but over time so many things were changed and nowadays I am sort of tired of keeping myself up to date. When I really need to know the current state of the art, then I research which of the many documentations actually gives information and try to look things up - here is a list with a few links: Kpathsea manual, source2e.pdf, source3.pdf, interface3.pdf, TeXBook/source, tex.web/Computers & Typesetting Volume B, TeX: The Program , TDS, e-TeX-manual, XeTeX reference guide, XeTeX companion, LuaTeX reference manual, pdfTeX user manual, ISO 32000 (PDF), Web2C manual, xparse-manual, MiKTeX manual, The TeX Live Guide .)

    I wrote "Many situations" because with some Windows platforms there are situations where a mixture of slashes and backslashes doesn't work. E.g., I experienced problems on the command prompt under old legacy platforms like MS-DOS/Windows 3.1, Windows 95 and Windows 98. E.g., Chris Hoffman mentions in the section "How does it Matter?" of the article "Why Windows Uses Backslashes and Everything Else Uses Forward Slashes", url: https://www.howtogeek.com/181774/why-windows-uses-backslashes-and-everything-else-uses-forward-slashes/, which was updated in July 31, 2018, that paths with slashes instead of backslashes don't work out within the "Open" dialogue. That article comes along with a nice screenshot of how with this dialogue the error-message "The file name is invalid" is displayed on a Windows 7 machine.

  2. Section "4 Absolute paths" of the manual of the package currfile says:

    The information which ends up in the definiton of the macro \currfileabsdir is retrieved from a file \jobname.fls.
    If a file \jobname.fls is not available for reading during the LaTeX-run, the expansion of \currfileabsdir yields emptiness.
    In order to have the file \jobname.fls created, the TeX-engine needs to be invoked with the compiler-option -recorder.

    With MiKTeX, however, there is another peculiarity: With MiKTeX in the current LaTeX-run information is retrieved from the file \jobname.fls created during the previous LaTeX-run before having that destroyed and created anew.

    This implies: During the first LaTeX-run there is no file \jobname.fls coming from a previous LaTeX-run available for reading. Thus during the first LaTeX-run in MiKTeX expanding \currfileabsdir yields emptiness, thus in MiKTeX during the first LaTeX-run \input{\currfileabsdir-AnotherFile} is like \input{-AnotherFile}. Probably this circumstance is a source for problems.

So rather than replacing backslashes by (forward) slashes, you might need a routine for finding out if the current latex-run is the first latex-run and in this case providing defaults for the definitions of \currfileabsdir. This might be done, e.g., via writing directives for defining a macro to .aux-file and checking whether the macro in question is defined: During the first LaTeX-run there is no .aux-file yet, thus directives for defining macros coming from the .aux-file cannot be carried out. In subsequent LaTeX-runs directives are carried out and macros are defined.

Alternatively checking whether the expansion of \currfileabsdir yields emptiness and, if this is the case, overriding the empty definition of \currfileabsdir etc with some defaults might be a viable option, too.

Yet another approach could be checking right at the start, even before \documentclass, via \IfFileExists if \jobname.fls already exists and is available for reading, and setting a flag accordingly. In the preamble, if the flag is set accordingly, have the empty definition of \currfileabsdir etc overridden with some defaults.

Related Question