[Tex/LaTex] AUCTeX XeLaTeX preview fails: fontspec cannot-use-pdftex error

auctexpreviewxetex

I am struggling setting up AUCTeX preview in a way that I can use it with XeLaTeX documents. When I try to generate the previews I get the following error

cannot-use-pdftex

generated by the fontspec package. So I guess I have to force the use of XeLaTeX even when generating previews, but I didn't find anything suitable on the net.

Here is a MWE:

\documentclass[b5paper,BCOR=8mm,DIV=calc,11pt]{scrbook}
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{url}
\newcommand{\nacr}{\newacronym}

\title{Some title}
\author{}
\date{\today}
\begin{document}
\maketitle
\begin{equation}
\theta=asdf_{bsdf}
\end{equation}
\end{document}

and here is the full result:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Fatal fontspec error: "cannot-use-pdftex"
! 
! The fontspec package requires either XeTeX or LuaTeX to function.
! 
! You must change your typesetting engine to, e.g., "xelatex" or "lualatex"
! instead of plain "latex" or "pdflatex".
! 
! See the fontspec documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

Preview-LaTeX exited as expected with code 1 at Sun Apr 01 09:30:59
LaTeX: LaTeX found no preview images

Setting local variables didn't help.

If I remove:

\usepackage{fontspec}
\usepackage{xunicode}

The error is gone.

In consequence, I guess I need a way to force auctex to compile previews with xelatex.

Any ideas or existing setups?

Some additional information: AUCTeX 11.86 and gs 9.0 on debian wheezy as well as AUCTeX 11.86 on windows 7.

preview of circ.tex works fine

Best Answer

XeTeX does not generate .dvi files. Instead, it creates an 'extended .dvi (.xdv) file, which is usually converted straight to PDF format by xdvipdfmx. You can interrupt the process at the .xdv stage using xelatex -no-pdf, but that will not help with preview, as the latter requires a .dvi files. As such, the only way to work with preview and fontspec is to use LuaLaTeX in .dvi mode, using dvilualatex. (I'm not sure how to set that as the engine for AUCTeX, but that would be a separate question!)

Related Question