[Tex/LaTex] latex and auctex, how to set up latex->dvips->ps2pdf

auctex

I am using psfrag for figures quite often and generating my figures in eps format. I would like to set up my AUCTeX to use latex->dvips->ps2pdf automatically. And at the end view the pdf file from inside Emacs with the help of AUCTeX view button.

I looked into this thread

How to replace AUCTeX's "LaTeX" command with latex->dvips->ps2pdf chain?

but I guess the options should be a bit different since I upgraded to the latest version of AUCTeX.

Any ideas on how to enable this and make it my default compile process by using my init file?

EDIT: Here is the updated part from my side. I guess I made quite some progress with the input of Arash in the below answer but there is a very small thing that is not clear. The below code is a minimal example which also uses psfrag and does what I would like to perform if I set C-c C-t C-p explicitly after saving this code in a file, say, simple.tex, namely, the steps followed are as follows,

  1. Save the below code in a file called simple.tex:
\documentclass{article}
\usepackage{psfrag}
\usepackage{graphicx}
\usepackage[sc,osf]{mathpazo}   % With old-style figures and real smallcaps.
\linespread{1.025}              % Palatino leads a little more leading
%
\begin{document}
\begin{figure}
\centering
%
\psfrag{X}[][]{$x$}
\psfrag{Y}[][]{$y$}
\psfrag{Z}[][]{$z$}
%
\psfrag{0}[][]{$0$}
\psfrag{0}[][]{$0$}
\psfrag{0.6}[][]{$0.65$}
\psfrag{1.2}[][]{$1.2$}
\psfrag{0.25}[][cl]{$0.25$}
\psfrag{0.5}[][cl]{$0.5$}
% get the image from imagebin: 
% test.eps location, https://imagebin.ca/v/3x1gUN0CmKbo
% save it as test.eps
\includegraphics[width=\textwidth]{test}
\end{figure}
\end{document}

Extra info: I have this line

(setq-default TeX-master nil)

in my init file as the AUCTeX manual suggests. To ask for the master file for each file that is open

  1. If after step 1, I do C-c C-c then emacs asks for the master file, default is to set the above file as master, just typed return

  2. After typing return, these 4 new lines are added to the file to show that it is the master file:

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:

and asks to save the file, ok, pressed y and saved it.

Then it jumped to Command (default LaTeX): which is followed by enter. Latex succesfully compiled the document to dvi format.

Now if I perform one more C-c C-c, I am getting

Cannot find "dvips and gv" viewer. Select another one in 'TeX-view-program-selection'

This was the part that was not working for viewing the output file as a pdf output although I had the following in my init file which were added after performing M-x customize variable for

TeX-PDF-mode -> setting it to on
Tex-PDF-from-DVI -> setting it to dvips - ps2pdf under Value Menu

And the exact settings added to the init file after these are turned on from inside emacs are as follows:

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(TeX-PDF-mode t)
 '(TeX-PDF-from-DVI "Dvips"))

At this point, I can not view the pdf output because it is compiled in the latex mode(just for dvi output, I believe), although the TeX-PDF-mode is set to true(t) above in the init file(which is strange).

After the above error message, as mentioned above, if I explicitly switch to pdf mode with C-c C-t C-p, then C-c C-a gives me the correct output with all the psfrag substitutions. But I still did not understand why it is not set by default as I open the document since TeX-PDF-mode is set to true.

In the end, I get what I want but I did not totally understand why the file does not open in pdf-mode while "TeX-PDF-mode t" is in the init file.

Best Answer

With the latest version of AUCTeX, if should be sufficient to customize the variable TeX-PDF-from-DVI as described in the manual. Try M-x customize-variable RET TeX-PDF-from-DVI RET , click on Value Menu and choose dvips - ps2pdf sequence, hit Apply and Save and you should be done.


I'm not sure how your workflow is, as you don't describe it. Consider this file:

\documentclass[10pt,a4paper]{article}
\begin{document}
foobar
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% TeX-PDF-mode: t
%%% TeX-PDF-from-DVI: "Dvipdfmx"
%%% End:

When you open this file, the variable TeX-PDF-from-DVI is set to "Dvipdfmx". The idea is that you compile the file with C-c C-c which runs latex on the file and you hit C-c C-c a second time which then runs dvipdfmx. You can take a shortcut with C-c C-a which runs both commands subsequently.

Here the relevant part from the manual again:

User Option: TeX-PDF-from-DVI

This option controls if and how to produce a PDF file by converting a DVI file.

When TeX-PDF-mode is non-nil, if TeX-PDF-from-DVI is non-nil too the document is compiled to DVI instead of PDF. When the document is ready, C-c C-c will suggest to run the converter to PDF or an intermediate format.

If non-nil, TeX-PDF-from-DVI should be the name of the command, as a string, used to convert the DVI file to PDF or to an intermediate format. Values currently supported are:

  • "Dvips": the DVI file is converted to PS with dvips. After successfully running it, ps2pdf will be the default command to convert the PS file to PDF.
  • "Dvipdfmx": the DVI file is converted to PDF with dvipdfmx.

When the PDF file is finally ready, the next suggested command will be to open the viewer.

This option can also be set as a file local variable, in order to use this conversion on a per-document basis.

Recall the whole sequence of C-c C-c commands can be replace by the single C-c C-a.