# Compilation fail with pstool: \begin{document} wrapped in \makeatletter \makeatother

I'm trying to create a presentation with LaTeX beamer using pstool, and since updating my system to Ubuntu 13.10 (pstool 1.5a, TeX Live 2013) the compilation of the pstool auxiliary TeX files (*-pstool.tex) fails, with the following message in the log:

! Improper \spacefactor.
\@->\spacefactor
\@m
l.19 \immediate \write \@
mainaux {\@percentchar <*PSTOOLLABELS>}


I could trace the problem to the following section in the created files:

\makeatletter
\def \thepage {\csname @arabic\endcsname \c@page }
\setcounter {page}{15}
\@input {presentation.oldaux}

\begin {document}
\immediate \write \@mainaux {\@percentchar <*PSTOOLLABELS>}
\makeatother
\centering \null \vfill


If I either move the \begin{document} below the \makeatother or add another \makeatletter right after it, I can manually compile the auxiliary file (latex, dvipdf), but of course this is not a solution. Apparently, \begin{document} cancels the \makeatletter. The problem is that the problematic section is automatically generated by pstool, and manually changing it for each and every EPS graphic is hardly feasible. Any ideas how I can work around this problem?

Edit: Here is a complete example of the TeX file using pstool. The errors occur during the compilation of the auxiliary file created by pstool, test-pstool.tex in this case. The EPS file test.eps contains only a box with the label "test".

\documentclass[a4paper]{beamer}

\usepackage[crop=pdfcrop]{pstool}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\EndPreamble

\begin{document}

\begin{frame}{Test}
\begin{figure}[p]
\begin{center}
\pstool[width=0.5\textwidth]{test}{
\psfrag{test}[][]{Test}
}
\caption{Test}
\end{center}
\end{figure}
\end{frame}

\end{document}

• Add \makeatother before \begin{document} and \makeatletter before \immediate\write. – egreg Oct 27 '13 at 22:41
• The problem is that this is an auxiliary file that is recreated every time pstool processes the EPS image, so that change would be overwritten. – Airtower Oct 27 '13 at 22:44
• Can you add a minimal example of a file producing the issue? Oh, and welcome to TeX.SX! – egreg Oct 27 '13 at 22:46
• Damn — I was aware of this problem and had fixed it in March, but uploading to CTAN fell off my todo list. On its way now… – Will Robertson Nov 3 '13 at 8:46

Indeed, \begin{document} resets the category code of @ doing \makeatother, but only in beamer, not in the standard classes. This is probably why the problem went unnoticed till now.

It's just a matter of adding \makeatletter in the macro the package uses for writing the auxiliary file (I'd add also \makeatother before, just by symmetry). I'm pretty sure the change will be made if the author is made aware of the problem.

As a temporary workaround you can patch the relevant macro either by copying it in your preamble and adding the missing bits:

\makeatletter
\def\pstool@write@processfile#1#2#3{%
\immediate\openout\pstool@out #2\pstool@suffix.tex\relax
[...]
% And the document body to place the graphic on a page of its own:
\noexpand\@input{\jobname.oldaux}^^J^^J%
\noexpand\begin{document}^^J%
\unexpanded{\immediate\write\@mainaux}{\pstool@auxmarker*}^^J%
\noexpand\makeatother^^J%
[...]
\immediate\closeout\pstool@out
}
\makeatother


where [...] denotes the (long) parts that should be unmodified.

A simpler strategy is patching the macro. Unfortunately, patching the macro with etoolbox can't be done because it contains ^^J. But you can with regexpatch:

\documentclass[a4paper]{beamer}

\usepackage[crop=pdfcrop]{pstool}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\EndPreamble

\usepackage{regexpatch}
\makeatletter
\xpatchcmd\pstool@write@processfile
{\noexpand\begin{document}}
{\noexpand\makeatother^^J^^J%
\noexpand\begin{document}^^J%
\noexpand\makeatletter}
{}{}
\makeatother

\begin{document}

\begin{frame}{Test}
\pstool[width=0.5\textwidth]{test}{
\psfrag{test}[][]{Test}
}
\end{frame}

\end{document}


I'm not expert with pstool, so I put \EndPreamble where it seemed better.

• Thank you, with one little edit (pending for peer review right now) the regexpatch solution works. :-) – Airtower Oct 28 '13 at 0:29
• With version 1.5b of pstool.sty (2013/03/11) the problem seems to have been solved. – egreg Nov 29 '13 at 16:17