4

I am trying to use write18 to crop a pdf. The code is

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

\usepackage{graphicx}

\newcommand{\filename}{}
\newcommand{\croppedfilename}{}

\begin{document}

    \makeatletter
    \renewcommand{\filename}{mypath/myimage.pdf}
    \filename@parse{mypath/myimage.pdf}
    \renewcommand{\croppedfilename}{\filename@area\filename@base.cropped.\filename@ext}
    \makeatother

    filename: \filename

    croppedfilename: \croppedfilename

    \immediate\write18{pdfcrop mypath/myimage.pdf \croppedfilename}%

    \immediate\write18{pdfcrop \filename \croppedfilename}%

    \the\pdfshellescape

\end{document}

Here \the\pdfshellescape is 1.

The line \immediate\write18{pdfcrop mypath/myimage.pdf \croppedfilename} does produce my cropped pdf, but \immediate\write18{pdfcrop \filename \croppedfilename} does not. Of course I need the second version in order to put it into a command.

Make42
  • 1,772

1 Answers1

7

If you check the log, you'll see

runsystem(pdfcrop mypath/myimage.pdf mypath/myimage.cropped.pdf)...executed.

runsystem(pdfcrop mypath/myimage.pdfmypath/myimage.cropped.pdf)...executed.

Notice the lack of a space in the second line. What's happening is the usual TeX behaviour of skipping spaces after macro names: in your case \filename. Thus you need to make sure a space is 'seen' there:

\immediate\write18{pdfcrop \filename\space\croppedfilename}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • Thanks! This does however not work in a function... I made a new question, because I did not ask this specifically here and the reason seems to be the parsing: https://tex.stackexchange.com/questions/417373/get-write18-to-execute-after-file-parsing – Make42 Feb 26 '18 at 13:10