[Tex/LaTex] how to automatically open pdf file after dvi2ps2pdf in emacs

dviemacspdfpssumatrapdf

i want to let emacs automatically open pdf file after i type C-c C-c dvi2ps2pdf RET, so i add the following code in my .emacs file

;;dvi2ps2pdf
(add-to-list 'TeX-command-list
 (list "dvi2ps2pdf" " dvips %s.dvi | ps2pdf %s.ps %s.pdf | SumatraPDF %o"
 'TeX-run-command nil t))

it works fine, well, until i find that it fails if the tex file, say, foo.tex, has \usepackage{psfrag} in it as follows,

% foo.tex
\documentclass{article}
\usepackage{psfrag}
\begin{document}
\[ e^x=\sum_{n=0}^{\infty}\frac{x^n}{n!}\]
\end{document}

SumatraPDF would open foo.dvi instead of foo.pdf.

if i type C-c C-n and start all over, it works fine again, as long as i do not compile and produce a new foo.div.

after compiling and foo.div is created,

if i replace %o with %f, SumatraPDF would open foo.ps

if i replace %o with %s.pdf, SumatraPDF would open two broken files foo and .pdf

if i replace SumatraPDF %o with start \"\" %s.pdf, Adobe Reader will correctly open foo.pdf

this is frustrating. how can i edit the code to let SumatraPDF take over everything? and how to use those substitution %o %f %s properly? excuse me that i'm really inexperienced, but it seems that they belong to different conventions? thanks very much!

UPDATE


with @giordano's help, i tend to believe that this is a inherent problem of SumatraPDF (Adobe Reader too, i think).

i think there's no problem on linux or mac. on windows, however, in DOS commands, i find that the following are the same, say,

  • copy foo.pdf bar.pdf
  • copy "foo".pdf bar.pdf
  • copy "foo.pdf" bar.pdf

other external commands such as dvips and ps2pdf do not distinguish the cases, either. SumatraPDF, however, treats foo.pdf or "foo.pdf" as one same file, and "foo".pdf as two separate files foo and .pdf and even opens them.

i think that's why SumatraPDF always fails to open %s.pdf. it is a file name parsing flaw´╝î if i'm right, SumatraPDF is not intend to open files like .pdf on windows system, and it should be able to detect that automatically.

p.s.

i also tried to manipulate the string %s.pdf(="foo".pdf) and tried to strip the double quotes in ways of lisp or DOS command. the quotes won't go away and i failed miserably due to inexperience.

do you guys have any ideas? thanks!

UPDATE


thanks to @giordano, i gave a shot on cygwin. (i had cygwin install for doc view anyway, no trouble at all.) it turns out that SumatraPDF works completely fine on cygwin, i.e., it accepts file names such as "foo".pdf. so i decided to redirect all command line calls to cygwin. i added the following to .emacs file (see emacswiki),

 ;; Sets your shell to use cygwin's bash, if Emacs finds it's running
  ;; under Windows and c:\cygwin exists. Assumes that C:\cygwin\bin is
  ;; not already in your Windows Path (it generally should not be).
  ;;
  (let* ((cygwin-root "c:/cygwin")
         (cygwin-bin (concat cygwin-root "/bin")))
    (when (and (eq 'windows-nt system-type)
         (file-readable-p cygwin-root))

      (setq exec-path (cons cygwin-bin exec-path))
      (setenv "PATH" (concat cygwin-bin ";" (getenv "PATH")))

      ;; By default use the Windows HOME.
      ;; Otherwise, uncomment below to set a HOME
      ;;      (setenv "HOME" (concat cygwin-root "/home/eric"))

      ;; NT-emacs assumes a Windows shell. Change to bash.
      (setq shell-file-name "bash")
      (setenv "SHELL" shell-file-name) 
      (setq explicit-shell-file-name shell-file-name)))

after that, SumatraPDF recognizes %s.pdf just fine.

there is one problem, though. dvi2ps runs differntly on cygwin and cannot proceed, i put the question somewhere else.

p.s.

code of SumatraPDF has been improved to accomodate cases like "foo".pdf. hopefully we'll see how it work very soon.

Best Answer

The solution is much simpler than what you've done so far: define a single command that runs latex, dvips and ps2pdf. In this way, when TeX-PDF-mode is active the PDF will be opened by your default PDF viewer without any further change. In your init file you can add something like this:

(add-to-list
      'TeX-command-list
      '("dvi2ps2pdf" "%(latex) %s && dvips %s.dvi && ps2pdf %s.ps %s.pdf" TeX-run-command nil t))

I'm not familiar with DOS batch scripting, in UNIX shells && is the logic AND, the following command is run if the previous one exits successfully, I don't know what's the batch analogous.

Related Question