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}}
theozh
  • 125
  • 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. – Ulrike Fischer Aug 10 '22 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 Aug 10 '22 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 Aug 10 '22 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 Aug 10 '22 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 Aug 10 '22 at 12:17
  • TeX is not really meant to be a language to do programming in. Unless you learn TeX programming for learning TeX programming's sake (or have no other option), it's usually not worth it. There's always the option of, you know, use a Python script or something else to generate TeX file if you insist. – user202729 Aug 10 '22 at 13:16

1 Answers1

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}
Ulrike Fischer
  • 327,261
  • 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 Aug 10 '22 at 12:57
  • umlauts should work, assuming that your document is utf8 encoded and that you have a current latex. – Ulrike Fischer Aug 10 '22 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 Aug 10 '22 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). – Ulrike Fischer Aug 10 '22 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 Aug 10 '22 at 13:39
  • @UlrikeFischer last update with MiKTeX mpm.exe was May 2021. I updated again, and now it seems to work with umlauts. I can't believe that the umlaut issue was only "fixed" in the last year. Lucky me. Anyway, thanks again for your kind help! – theozh Aug 10 '22 at 13:49
  • @user202729 thank you for your suggestion. Ulrike's solution works after an update. Well, if I shouldn't use programs or code which I don't fully understand, maybe then I shouldn't use LaTeX? ;-) – theozh Aug 10 '22 at 13:52
  • @theozh well, sticking to only what the package documentation support (and no additional "hacks") should allow you to do most things, but there's a not-small chance you would not be satisfied with what you get... – user202729 Aug 10 '22 at 13:55