This is an issue with < and > being active at the wrong time. It appears at a time when the landscape option (and xetex driver for pdfpages) will try to do
\special {pdf: put @thispage <</Rotate #1>>}
I didn't try to disentangle completely the sequence of events (but see update below), but loading babel after pdfpages fixes the issue. I recall from another question I contributed to that spanish has some problems in its handling of active characters, but I need to find that link.
The link is https://tex.stackexchange.com/a/233355/4686 but the underlying mechanism is not the one at works here.
\documentclass{article}
%\usepackage[spanish]{babel}% provokes problems with active <, >
\usepackage{pdfpages}
\usepackage[spanish]{babel}
\begin{document}
%\tracingifs1
%\tracingmacros1
\includepdf[landscape]{example-image-16x9.pdf}
\end{document}
% Local Variables:
% TeX-engine: xetex
% End:
compiles fine (although perhaps the outcome has problems with margins).
Update
One sees in package pdfpages that it does
\AtBeginDocument{%
\let\AM@orig@landscape\landscape
\let\AM@orig@endlandscape\endlandscape
\RequirePackage{pdflscape}%
\let\landscape\AM@orig@landscape
\let\endlandscape\AM@orig@endlandscape
}
The important thing is thus that the \RequirePackage{pdflscape} is done at a very late time. If the user loads babel before pdfpages then Babel will have activated the shorthands at that time. And inside package pdflscape one finds
\def\PLS@AtBeginShipout{%
\global\setbox\AtBeginShipoutBox\vbox{%
\special{pdf: put @thispage <</Rotate #1>>}%
\box\AtBeginShipoutBox
}%
where the < and > are active characters due to Babel+spanish. If I replace this by
\def\PLS@AtBeginShipout{%
\global\setbox\AtBeginShipoutBox\vbox{%
\special{pdf: put @thispage \string<\string</Rotate #1\string>\string>}%
\box\AtBeginShipoutBox
}%
then your MWE compiles with no error, even with babel loaded before pdfpages. Moreover near the bottom of the file pdflscape.sty I see a use of <, > in an \@whilenum loop, here also < and > should better not be active, although here they do not cause problem because they only fetch the next token I believe to check for << or >>.
It can not be said that pdflscape should sanitize the catcodes of < and >, as then zillions of packages should too.
Is it really necessary that pdfpages loads pdflscape during \AtBeginDocument hook ? perhaps an \AtEndOfPackage would be enough ?
polyglossiainstead ofbabelin xelatex and lualatex.... But I can't check now. – Rmano Jan 28 '16 at 07:38pdfpages,babelandspanishare used? – Javier Bezos Jan 28 '16 at 14:34\listfilescan produce a result.... compiling withbabelloaded second, I getpdfpages.sty 2015/09/18 v0.5d,babel.sty 2016/01/08 3.9n,spanish.ldf 2016/01/15 v5.0o. AndXeTeX, Version 3.14159265-2.6-0.99992 (TeX Live 2015) (preloaded format=xelatex 2016.1.20)– Jan 28 '16 at 17:39pdfpagesversion v0.5d andbabel3.9n. Thanks! – Emilio Lazo Jan 29 '16 at 05:34pdfpagesI could reproduce the error, and jfbu is (almost) right in the answer below. I don't think loading a package when the document has started is a good idea, but if done catcodes should be sanitized (except if we want the catcodes as used in the document body, of course), as done in font encodings with\nfss@catcodes. Note the example fails even with the current version ofspanish, with a bug fixed - catcodes are now activated at the right time, butpdfpagesis loaded too late. I think you should contact the maintainer ofpdfpages. – Javier Bezos Jan 29 '16 at 18:37