# how to automatically open pdf file after dvi2ps2pdf in emacs

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

(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.

-
If it's a bug you should really report it to Emacs (M-x report-emacs-bug RET) or AUCTeX (M-x TeX-submit-bug-report RET) instead of trying to work around. If it will be fixed upstream, everyone will benefit, not just you ;-). –  giordano Oct 13 '13 at 9:20
i think i'll try to report this to SumatraPDF first :) –  davyjones Oct 13 '13 at 10:15
One option would be to become more comfortable with command-line and set up your own function similar to my first example, which obviates the need to rely upon AUCTeX. Another option would be for you to open up tex.el and take a look at (defcustom TeX-expand-list . . . and perhaps create your own variables that format file names in a manner that SumatraPDF likes. You can customize your own variable in whatever format you like -- e.g., by using split-string, and concat. –  lawlist Oct 13 '13 at 21:41
In my first example, I used: (setq base-file (car (split-string (buffer-file-name) "\\."))) and (setq pdf-file (concat base-file ".pdf")). If your buffer-file-name is c:/davy/jones/foo.tex, then base-file is c:/davy/jones/foo. Then, pdf-file concatenates a .pdf onto the end of base-file. This gives you a *.pdf file name that SumatraPDF likes. My line of code (start-process "displayline" nil sumatra "-forward-search" tex-file line pdf-file) is shorthand for C:/SumatraPDF/SumatraPDF.exe -forward-search c:/davy/jones/foo.tex [line-number] c:/davy/jones/foo.pdf. –  lawlist Oct 13 '13 at 22:09

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.

-
@giodano,great! thanks very much! the only thing i need to do is to stay in the pdflatex mode. btw, what if i want to perform this in the dvi mode and i do not want to switch to the pdf mode to open the pdf file, otherwise i would get the dvi file to open, or in the presence of psfrag package, the ps file will open in gsview? –  davyjones Oct 11 '13 at 15:48
@davyjones it wouldn't be a clean solution, but you can append SumatraPDF %s.pdf to the list of programs to be run by the dvi2ps2pdf command. –  giordano Oct 11 '13 at 17:09
@giodano,saddly simply appending does not work... as i stated in the original question, that would cause SumatraPDF to open two broken file, say, foo and .pdf. i think we could define a function viewpdf first as found in link, but i would not how to use it inside the double quotes again, do you have any suggestions? many thanks again! –  davyjones Oct 11 '13 at 17:25
@davyjones has the file spaces in its name? Appending && okular %s.pdf (Okular is my PDF viewer) works for me. Try protecting the file name with quotes: \"%s.pdf\" instead of simply %s.pdf. –  giordano Oct 11 '13 at 17:31
@giodano, sadly, it failed again... SumatraPDF will open two broken files, one with no name, the other with name bar".pdf". if i use SumatraPDF .\\%s.pdf, SumatraPDF will open one broken file named "bar".pdf. it still fails if i change SumatraPDF to AcroRd32. is it because i'm using emacs+auctex on windows system? –  davyjones Oct 11 '13 at 20:04