# pdfcrop command with shell escape magic comment not working in TeXstudio [duplicate]

I would like to use pdfcrop to get a tight crop of this table (credit to Bernard) from my other question.

I want to execute pdfcrop every time the document is compiled, so I have included at the end of the document the command \immediate\write18{pdfcrop table.pdf}, where table.pdf is the name of the pdf that is generated after the source code is compiled.

Since I am using TeXstudio, I have added the following magic comments at the top of the document:

% !TeX program = xelatex
% !TeX TXS-program:compile = txs:///xelatex/[--shell-escape]


The first magic comment tells TeXstudio to compile the document with xelatex, and the second magic comment is supposed to enable the shell escape interaction, as explained in this answer.

However, when I run the code, I only get the original table but not the cropped table. So this tells me that there might be something that is preventing the execution of the \immediate\write18{pdfcrop table.pdf} command. But I get no errors or warnings.

What am I missing?

Note: I run Windiws 8.1 and have MiKTeX, Ghostscript and the ActiveState distribution of Perl installed in my machine.

Here is the source code. It compiles with xelatex only. Please make sure that you save the source code as table.tex.

% !TeX program = xelatex
% !TeX TXS-program:compile = txs:///xelatex/[--shell-escape]
% !TeX spellcheck = en_GB

\documentclass{standalone}
\usepackage{array, tabularx, arydshln}
\usepackage{geometry}
\usepackage{pst-node, multido}
\usepackage{fontspec}
\setsansfont{Arial}
\usepackage{layouts}

\begin{document}
\begin{pspicture}(-8.6,-8)(10,6)
\sffamily\renewcommand\arraystretch{1.8}\centering
\fontsize{10.5}{13}\selectfont
\psset{linecolor=red}
\rput(0,0){\begin{tabularx}{1.2\textwidth}{|*{12}{ >{\centering\arraybackslash}X|}}
\multicolumn{12}{c}{TITLE} \\
\hline
\multicolumn{12}{|c|}{SOME INFORMATION} \\
\hline
\multicolumn{4}{|c|}{GROUP 1} & \multicolumn{2}{c|}{GROUP 2} & \multicolumn{2}{c|}{GROUP 3} & \multicolumn{4}{c|}{GROUP 4} \\
A & B & A & B & \multicolumn{2}{c|}{C} & \multicolumn{2}{c|}{D} & & & & \\
AA & AA & BB & BB & CC & DD & AA & BB & EE & FF & GG & HH \\
\hline
10 & 10 & 10 & 10 & 10 & 10 & 10 & 10 & 10 & 10 & 10 & 10 \\
9 & 9 & 9 & 9 & 9 & 9 & 9 & 9 & 9 & 9 & 9 & 9 \\
8 & 8 & 8 & 8 & 8 & 8 & 8 & 8 & \circlenode{c9}{8} & 8 & 8 & 8 \\
\hdashline
7 & 7 & 7 & 7 & 7 & 7 & 7 & 7 & 7 & \circlenode{c10}{7} & 7 & 7 \\
6 & \circlenode{c2}{6} & 6 & 6 & 6 & 6 & 6 & 6 & 6 & 6 & 6 & 6 \\
\rnode{R1}{5} & \rnode{R2}{5} & \rnode{R3}{5} & \rnode{R4}{5} & \rnode{R5}{5} & \rnode{R6}{5} & \rnode{R7}{5} & \rnode{R8}{5} & \rnode{R9}{5} & \rnode{R10}{5} & \rnode{R11}{\circlenode{c11}{5}} & \rnode{R12}{5} \\
\circlenode{c1}{4} & 4 & 4 & 4 &\circlenode{c5}{4} & 4 & \rnode{t7}{4} & \circlenode{c8}{4} & 4 & 4 & 4 & 4 \\
3 & 3 & 3 & 3 & 3 & 3 & \rnode{b7}{3} & 3 & 3 & 3 & 3 & 3 \\
\hdashline
2 & 2 & \circlenode{c3}{2} & \circlenode{c4}{2} & 2 & 2 & 2 & 2 & 2 & 2 & 2 & \circlenode{c12}{2} \\
1 & 1 & 1 & 1 & 1 & \circlenode{c6}{1} & 1 & 1 & 1 & 1 & 1 & 1 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\hline
\end{tabularx}}
\midAB(t7)(b7){m7}\rput(m7){\circlenode[framesep=0pt]{c7}{3,5}}%
\multido{\ia=1+1, \ib=2+1}{11}{\ncline{c\ia}{c\ib}}
\pnode[-0.85cm, 0](R1){R0}
\pnode[0.85cm, 0](R12){R13}
\psset{linewidth=0.5pt, linecolor=black, nodesep=3pt}
\multido{\ia=0+1, \ib=1+1}{13}{\ncline{R\ia}{R\ib}}
\end{pspicture}
\end{document}
\immediate\write18{pdfcrop table.pdf}


## marked as duplicate by egreg xetex StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Jun 30 '16 at 20:54

• Nothing is executed after \end{document} so put the line starting with \immediate .... before \end{document}. If still no go, check the log file table.log for the runsystem keyword and post the entire line here (it says if the shell command was executed or not). – alwaysask Jun 30 '16 at 20:49
• @alwaysask I did as you said and it worked! But wait, that's not the end of the story. It only works when I click on the "Compile" button (table.log: runsystem(pdfcrop table.pdf)...executed.). But it doesn't work when I click on the "Build & View" button (table.log: runsystem(pdfcrop table.pdf)...disabled (restricted).). Is there an explanation for this? – codeaviator Jun 30 '16 at 23:06
• Remove or double comment % !TeX program = xelatex and it will work with "Build & View" as well. – alwaysask Jul 1 '16 at 7:05