# AUCTeX XeLaTeX preview fails: fontspec cannot-use-pdftex error

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
$$\theta=asdf_{bsdf}$$
\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

-
As you are using preview, you need a .dvi file. LuaLaTeX can generate these, XeLaTeX cannot. (No idea on how to force the use of dvilualatex, I'm afraid.) –  Joseph Wright Apr 1 '12 at 8:20
you have to run xelatex with xelatex -no-pdf, but this generates an extended dvi which can only be converted into a pdf with xdvipdfmx –  Herbert Apr 1 '12 at 8:31
OK, looks like I have ot understand a little bit more about what happens within preview. Does 'xelatex -no-pdf' create the preview images during a normal run? Or does the preview run only use the dvi file? Are the preview images only created during the 'C-c C-p'? Then 'xelatex -no-pdf' shouldn't help really, right? –  louis cypher Apr 1 '12 at 15:33

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!)