0

Windows paths use backslash, but paths in LaTeX documents require foreslash. When copying filepaths from Windows Explorer they will contain backslash.

I know that I could use a text editor using find & replace to change backslashs into a foreslashs which would be pretty painful for every path.

So, I hoped that there might be either

  • a feature in TeXStudio (Win10) to account for that
  • or a TeX function which does the replacement, e.g. like a function \bs2fs{C:\user\test\path\subdir\file.pdf}

I have found this question with answer and so far ended up in shortening my case to the following:

\documentclass[a4paper,10pt]{article}
\usepackage{pdfpages}

\makeatletter
\newcommand{\replaceBS}[2]{{\escapechar=`/ 
  \xdef#2{\expandafter\zap@space\detokenize\expandafter{#1} \@empty}}}
\makeatother

\begin{document}

\replaceBS{C:\user1\test\path\sub dir\sub dir2\file_name with Umlauts äöü.pdf}{\TeXFilename}
\includepdf[pages=-]{\TeXFilename}

\replaceBS{C:\user2\test\path\sub dir\sub dir2\file_name with Accents àèòù.pdf}{\TeXFilename}
\includepdf[pages={1,3,4,2}]{\TeXFilename}

\replaceBS{C:\user3\test\path\sub dir\sub dir3\file_name with Umlauts.pdf}}{\TeXFilename}
\includepdf[pages={4,3,2,1}]{\TeXFilename}

\replaceBS{C:\user4\test\path\sub dir\sub dir4\file_name with Umlauts.pdf}{\TeXFilename}
\includepdf[pages={1,4,3,2}]{\TeXFilename}

\end{document}

This basically works but doesn't seem to handle spaces in the path. Can't this be shortened to a simple "function" like \bs2fs{<path>} which would make the document more readable? Actually, a function which additionally handles spaces, umlauts and accents as well?

\includepdf[pages=-]        {\bs2fs{C:\user1\test\path\sub dir\sub dir2\file_name with Umlauts äöü.pdf}}
\includepdf[pages={1,3,4,2}]{\bs2fs{C:\user2\test\path\sub dir\sub dir2\file_name with Accents àèòù.pdf}}
\includepdf[pages={4,3,2,1}]{\bs2fs{C:\user3\test\path\sub dir\sub dir3\file_name with Umlauts.pdf}}
\includepdf[pages={1,4,3,2}]{\bs2fs{C:\user4\test\path\sub dir\sub dir4\file_name with Umlauts.pdf}}
6
  • 1
    well one could probably. But it looks wrong that you have so many absolute pathes in your document. That makes you document quite unportable. Commented Aug 10, 2022 at 11:57
  • the most important problem here is that if you understand how TeX tokenization process works, you'll see that there's absolutely no way to distinguish \user1 and \user 1. that having said, it's not impossible if you're willing to wrap every \includepdf statement with something like \beginreplace ... \endreplace and ensure that it does not appear in the argument of anything else.
    – user202729
    Commented Aug 10, 2022 at 12:09
  • @UlrikeFischer well,, this is just a document to merge, extract, and order pages from different PDFs. Of course, I could first copy the PDFs all into the same directory as the .tex file and hence no directories and backslashs but just a filename. But this is even more painful than find & replace \ with /.
    – theozh
    Commented Aug 10, 2022 at 12:10
  • ah if that's the fundamental problem maybe use some command-line application such as pdftk or something similar (which I think some of them uses LaTeX as back end)
    – user202729
    Commented Aug 10, 2022 at 12:10
  • @user202729 thanks, I can check pdftk. For some people a text editor will be more convenient than command-line, especially if you have many files and long paths. I was hoping for a simple solution with MiKTeX and TeXStudio because that's already available.
    – theozh
    Commented Aug 10, 2022 at 12:17

1 Answer 1

4
\documentclass[a4paper,10pt]{article}
\usepackage{pdfpages}

\newcommand\windowsincludepdf{\begingroup\catcode`\\=11 \windowsincludepdfaux}
\newcommand\windowsincludepdfaux[2][]{\endgroup\includepdf[#1]{#2}}
 
\begin{document}

\windowsincludepdf[pages=1-4]{C:\texlive\2022\texmf-dist\doc\latex\pdfpages\pdfpages.pdf}

\windowsincludepdf[pages=1-4]{C:/texlive/2022/texmf-dist/doc/latex\pdfpages\pdfpages.pdf}

\end{document}
8
  • Thank you! This looks short and clean. It seems to handle spaces and & in path or filename as well, however, no Umlauts ÄÖÜ and accents. The TeXStudio document is encoded UTF-8. Maybe, I am missing some special \usepackage{} german or french?
    – theozh
    Commented Aug 10, 2022 at 12:57
  • umlauts should work, assuming that your document is utf8 encoded and that you have a current latex. Commented Aug 10, 2022 at 13:00
  • Hmm, MiKTeX-pdfTeX 4.6 (MiKTeX 21.3 Portable) © 1982 D. E. Knuth, © 1996-2021 Hàn Thế Thành should be new enough, right?
    – theozh
    Commented Aug 10, 2022 at 13:21
  • the binary is not the point. Check the latex version in the log. (but it doesn't look up-to-date, we have 2022). Commented Aug 10, 2022 at 13:31
  • @theozh if you insist add this between the \begingroup and \catcode: \count0=128\loop\catcode\count0=12\advance\count0 by 1\relax\ifnum\count0<256\repeat (that having said I can't recommend copy pasting code that you don't understand what it does into your document, if they break later you can't debug) (and using count0 might be problematic)
    – user202729
    Commented Aug 10, 2022 at 13:39

You must log in to answer this question.

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