# Execute external bash file

This is a follow up to Get write18 to execute after file parsing

With the code

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

\usepackage{graphicx,grffile}

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

\makeatletter
\newcommand{\includecroppedgraphics}[1]{%
\filename@parse{#1}%
\edef\croppedfilename{\filename@area\filename@base.cropped.\filename@ext}%
\immediate\write18{pdfcrop #1 \croppedfilename}%
\fbox{\includegraphics[width=\linewidth]{\croppedfilename}}%
}
\makeatother

\begin{document}

\includecroppedgraphics{mypath/myimage.pdf}

\end{document}


I tried to crop a PDF. This did not work for my mutli-page PDF. What did work was the script

#!/bin/bash

temdirname="./croppedpages"

mkdir "$temdirname" pdftk "$1" burst output "$temdirname"/Page_%03d.pdf while read -d$'\0' pdfpage ; do
krop --go --autotrim -o "$pdfpage" "$pdfpage"
done < <(find "$temdirname" -print0) pdftk "$temdirname"/*.pdf output "$2" rm -R "$temdirname"


that I wrote based on

But it only works so far from the command line. Now I want to include it into the LaTeX script, but the call does not work. I thought I could simply change

\immediate\write18{pdfcrop #1 \croppedfilename}%

to

\immediate\write18{splitcropmerge.sh #1 \croppedfilename}%

In the log it says

runsystem(splitcropmerge.sh Pakete-Uebersicht/Pakete-Uebersicht.pdf Pakete-Uebe rsicht/Pakete-Uebersicht.cropped.pdf)...executed.

LaTeX Warning: File Pakete-Uebersicht/Pakete-Uebersicht.cropped.pdf' not found on input line 187.

sudo dpkg -i krop_0.5.0-1_all.deb

• Probably because you are missing the shell in \immediate\write18{splitcropmerge.sh #1 \croppedfilename}%. You should specify the shell before the script name: \immediate\write18{bash splitcropmerge.sh #1 \croppedfilename}% – Phelype Oleinik Feb 26 '18 at 15:06

Disclaimer: I tested your code after commenting the while loop that you use krop.

What is inside the \write18 command is executed exactly like if you typed it in the command prompt. If you execute:

splitcropmerge.sh mypath/myimage.pdf mypath/myimage.cropped.pdf


you'll get an error from the shell. If, on the other hand, you run:

bash splitcropmerge.sh mypath/myimage.pdf mypath/myimage.cropped.pdf


it will work (if the files exist, of course).

So do the same in the line with the \write18:

\immediate\write18{bash splitcropmerge.sh #1 \croppedfilename}%


As noted by @RobtAll, LuaLaTeX users might need to \usepackage{shellesc}. LuaLaTeX (0.87 onwards) doesn't have the same output streams that TeX had, so 18 can be any file. The package shellesc provides the \ShellEscape macro to replace TeX's \write18:

\ShellEscape{bash splitcropmerge.sh #1 \croppedfilename}%

• Also, lualatex users might need \usepackage{shellesc}. – user139954 Feb 26 '18 at 16:25
• you only need the explicit bash if the shell script is not executable and on the path, normally if you write a shell script you would make it executable, so bash` would not be needed – David Carlisle Feb 26 '18 at 17:54