politza / pdf-tools

Emacs support library for PDF files.
GNU General Public License v3.0
1.57k stars 162 forks source link

PDF Tools as TeX-view-program-selection #128

Closed DrWaleedAYousef closed 8 years ago

DrWaleedAYousef commented 9 years ago

Thanks so much for this GREAT tool.

I use the following in my .emacs: (setq TeX-view-program-selection '(output-pdf "PDF Tools") ) (setq auto-revert-interval 0.5) (setq auto-revert-verbose nil)

My question is two-fold: 1- I have multi-file LaTeX document (with the master file defined at the end of each file); when I compile the opened pdf does not revert automatically. 2- When I compile from one of the tex files (not the master file) the pdf goes automatically to some weird page different from the one on the LaTeX source unless I hit C-c C-g it goes to the right place but on the old pdf file not the newly compiled one.

politza commented 9 years ago

You shouldn't use auto-revert for this, because of the inherent race - condition. See this thread https://github.com/politza/pdf-tools/issues/58 .

DrWaleedAYousef commented 9 years ago

So, do you mean I should remove the two statements of auto-revert above and replace them by: (defun th/pdf-view-revert-buffer-maybe (file) (when-let ((buf (find-buffer-visiting file))) (with-current-buffer buf (when (derived-mode-p 'pdf-view-mode) (pdf-view-revert-buffer nil t)))))

(add-hook 'TeX-after-TeX-LaTeX-command-finished-hook

'th/pdf-view-revert-buffer-maybe)

I did that already and emacs gives me: th/pdf-view-revert-buffer-maybe: Symbol's function definition is void: when-let

Also, I am surprised because setting the auto-revert-interval was working fine and all of the sudden it stopped working days ago!!

politza commented 9 years ago

Yes.

DrWaleedAYousef commented 9 years ago

Thanks so much but it seems you replied without seeing my modification to the comment, in which I say I get a "void" error. Yes.

— Reply to this email directly or view it on GitHub https://github.com/politza/pdf-tools/issues/128#issuecomment-143651199.

DrWaleedAYousef commented 9 years ago

Please, just re-read my comment On Sep 28, 2015 8:32 AM, "Waleed Yousef" wyousef@fcih.net wrote:

Thanks so much but it seems you replied without seeing my modification to the comment, in which I say I get a "void" error. Yes.

— Reply to this email directly or view it on GitHub https://github.com/politza/pdf-tools/issues/128#issuecomment-143651199.

politza commented 9 years ago

(require subr-x)

-------- Ursprüngliche Nachricht --------
Von: Waleed Yousef notifications@github.com
Datum:28.09.2015 8:34 (GMT+01:00)
An: politza/pdf-tools pdf-tools@noreply.github.com
Cc: politza politza@fh-trier.de
Betreff: Re: [pdf-tools] PDF Tools as TeX-view-program-selection (#128)
Please, just re-read my comment On Sep 28, 2015 8:32 AM, "Waleed Yousef" wyousef@fcih.net wrote: > Thanks so much but it seems you replied without seeing my modification to > the comment, in which I say I get a "void" error. > Yes. > > — > Reply to this email directly or view it on GitHub > https://github.com/politza/pdf-tools/issues/128#issuecomment-143651199. > > — > Reply to this email directly or view it on GitHub.
DrWaleedAYousef commented 9 years ago

Thanks so much; it works. One remaining thing is this. If the pdf buffer is displaying page, e.g., 15; after I compile the buffer goes automatically to the beginning of the document. I want it to keep displaying page 15.

Thanks so much

DrWaleedAYousef commented 9 years ago

Oh, one more clue before you reply. I use latex-extra. so, the problem I described appears when I use C-c C-a. However, when i use the normal latex C-c C-c it works fine!!

Keks-Dose commented 8 years ago

@wyousef

Please, if you have the code for using pdf-tools as a TeX-view-program, please publish it here.

DrWaleedAYousef commented 8 years ago

@Keks-Dose

here you go; in .emacs write:

;;; Begin Code Here ;;; (pdf-tools-install) ;;for view pdfs (load "pdftools") ;;for spooling to pdf. (setq TeX-view-program-selection '((output-dvi "Evince") ;; This is not necessary (output-pdf "PDF Tools") ;; <-- THIS one (output-html "xdg-open") ;; This is not necessary ))

(require 'subr-x) (defun th/pdf-view-revert-buffer-maybe (file) (when-let ((buf (find-buffer-visiting file))) (with-current-buffer buf (when (derived-mode-p 'pdf-view-mode) (pdf-view-revert-buffer nil t)))))

(add-hook 'TeX-after-TeX-LaTeX-command-finished-hook

'th/pdf-view-revert-buffer-maybe)

;;; END CODE HERE;;;

Keks-Dose commented 8 years ago

@wyousef Thank you for the quick answer!

(load "pdftools")

? =>

(load "pdf-tools") ?

With this change it works!

EDIT:

After changing the source file (foo.tex) and running LaTeX again, Emacs throws the error:

error in process sentinel: th/pdf-view-revert-buffer-maybe: Symbol's function definition is void:  
when-let

Emacs does not display the updated PDF. So the lines

(add-hook 'TeX-after-TeX-LaTeX-command-finished-hook
#'th/pdf-view-revert-buffer-maybe)

need to be changed. Do I get it right, that the second line include »maybe«, because you are still considerung how to tackle the situation?

DrWaleedAYousef commented 8 years ago

You can contact the author better. On Nov 15, 2015 11:49 AM, "Keks-Dose" notifications@github.com wrote:

@wyousef https://github.com/wyousef Thank you for the quick answer.

(load "pdftools")

? =>

(load "pdf-tools") ?

And I get the error message:

Wrong type argument: listp, output-pdf

when I try to open a pdf.

— Reply to this email directly or view it on GitHub https://github.com/politza/pdf-tools/issues/128#issuecomment-156794911.

Keks-Dose commented 8 years ago

@politza Please, could you have a look at the code for automatic update of the pdf everytime the pdf is changed on disk? See my conversation with wyousef. Thank you.

The viewer is much faster than okular, I'd really appreciate to use it with LaTeX in Emacs.

politza commented 8 years ago

Keks-Dose notifications@github.com writes:

@politza Please, could you have a look at the code for automatic update of the pdf [...]

Just install the latest AUCTeX version (11.89) and then

(add-hook 'TeX-after-TeX-LaTeX-command-finished-hook 'TeX-revert-document-buffer)
Keks-Dose commented 8 years ago

@politza Thank you very much. This works when I start compiling from inside the *.tex buffer. But with latexmk running in the background, PDFView does not load the new PDF.

Sorry for 1) hi-jacking this thread and 2) asking stupid questions. I've been searching quite a while and nowhere is a small howto about using your great tool with the new AUCTeX (11.89). I'm a user...

politza commented 8 years ago

Keks-Dose notifications@github.com writes:

[...] But with latexmk running in the background, PDFView does not load the new PDF.

So you're using M-x compile (which has nothing to do with AUCTeX) ?

Keks-Dose commented 8 years ago

latexmk is a per script, http://users.phys.psu.edu/~collins/software/latexmk-jcc/ . Once started, every time the source *.tex file is changed on disk, it will run the appropriate version of LaTeX (pdfLaTeX, LuaLaTeX, TeX, whatever). Within seconds I get a new PDF with my latest changes.

PDF-Tools is fast. With some finetuning (making use of formats: https://www.ctan.org/tex-archive/macros/latex/contrib/mylatexformat?lang=de , autosave) the compilation is fast as well.

This perl script has to be started on command line. Whether I open yakuake as shell or eshell by emacs doesn't matter: It has nothing to do with AUCTeX. But of course one can customize AUCTeX to use latexmk: http://tex.stackexchange.com/questions/10561/compile-using-latexmk-in-emacs . There is even a package auctex-latexm.el for Emacs: https://github.com/tom-tan/auctex-latexmk

Until today I used Okular, which reloads a PDF from disk after a change. I'm not sure that Emacs sends a message to Okular.

If you came to the conclusion that latexmk is not your cup of tea, please tell me. I can bother the AUCTeX- mailinglist with my unqualified questions, or try on help-gnu-emacs@gnu.org . But maybe the automagic reload of a PDF after a change on disk should be a feature of PDF-Tools itself.

politza commented 8 years ago

Keks-Dose notifications@github.com writes:

If you came to the conclusion that latexmk is not your cup of tea, please tell me

I'm just trying to figure out, how you run it. If AUCTeX runs latexmk via compile, something like this may work (untested).

(add-hook 'compilation-finish-functions (lambda (buffer status) (when (and (buffer-live-p TeX-command-buffer) (equal status "finished\n")) (with-current-buffer TeX-command-buffer (TeX-revert-document-buffer (expand-file-name (TeX-active-master (TeX-output-extension))))))))

Anyway, TeX-revert-document-buffer with the filename of the PDF needs to run after the new PDF was created.

. I can bother the AUCTeX- mailinglist

Try emacs.stackexchange. Usually, you'll find someone who is eager to figure this out.

But maybe the automagic reload of a PDF after a change on disk should be a feature of PDF-Tools itself.

Maybe it should.

Keks-Dose commented 8 years ago

I found something in the manpage of latexmk, heading »LIST OF CONFIGURATION VARIABLES USABLE IN INITIALIZATION FILES« (aka .latexmkrc)

$pdf_update_command [ ]

When the pdf previewer is set to be updated by running a command, this is the command that is run. See the information for the variable $pdf_update_method.

$pdf_update_method [1 under UNIX, 3 under MS-Windows]

How the pdf viewer updates its display when the pdf file has changed. See the information on the variable $dvi_update_method for the codes. (Note that information needs be changed slightly so that for the value 4, to run a command to do the update, the command is specified by the variable $pdf_update_command, and for the value 2, to specify update by signal, the signal is specified by $pdf_update_signal.)

Note that acroread under MS-Windows (but not UNIX) locks the pdf file, so the default value is then 3.

Arranging to use a command to get a previewer explicitly updated requires three variables to be set. For example:

$pdf_previewer = "start xpdf -remote %R %O %S";

$pdf_update_method = 4;

$pdf_update_command = "xpdf -remote %R -reload";

The first setting arranges for the xpdf program to be used in its "remote server mode", with the server name specified as the rootname of the TeX file. The second setting arranges for updating to be done in response to a command, and the third setting sets the update command.

$pdf_update_signal [Under UNIX: SIGHUP, which is a system-dependent value]

The number of the signal that is sent to the pdf viewer when it is updated by sending a signal -- see the information on the variable $pdf_update_method. The default value is the one appropriate for gv on a UNIX system.

So latexmk provides a way to inform the viewer about the update of the PDF.

politza commented 8 years ago

Keks-Dose notifications@github.com writes:

So latexmk provides a way to inform the viewer about the update of the PDF.

So, that solves your problem then.

adouzzy commented 8 years ago

A working version of .latexmkrc Cheers,

$pdf_previewer="emacsclient -c -e '(find-file %S)'"; $pdflatex='pdflatex %O -interaction=nonstopmode %S'; $pdf_update_method = 4; $pdf_update_command = "emacsclient -e '(th/pdf-view-revert-buffer-maybe %S)'";

Keks-Dose commented 8 years ago

@adouzzy Hi! Thank you! You are using a function defined in the message of @wyousef above: (th/pdf-view-revert-buffer-maybe) . So I put the code he posted, beginning with (require 'subr-x) into my emacs.

On the first run, latexmk compiles and opens a window for the pdf. But on the second run, it does not update, because of this message:

Running 'emacsclient -e '(th/pdf-view-revert-buffer-maybe "151113-myfile.pdf")'' ------------ *ERROR*: Symbol's function definition is void: when-let Collected error summary (may duplicate other messages): update_view: Command for 'update_view' gave return code 256

I'm a user and don't have knowledge to dig into lisp -- why is "when-let" undefined?

adouzzy commented 8 years ago

@Keks-Dose when-let is a function defined in subr-x. (require 'subr-x) should fix it.

I ran emacs version 25.0.50.1 with --daemon.(compiled from git, the latest 24.5 should work) I run latexmk with -pvc option. latexmk runs as a daemon, once it found related tex files are changed, it would recompile. So there is no need for a second run. First run latexmk pdf should be opein in an emacs frame. Then you edit the tex file and save. The pdf file should be updated shortly automatically.

Keks-Dose commented 8 years ago

@adouzzy @politza @wyousef Thank you for your help and patience. Nobody had the error with »when-let« but me. I downloaded the current version of subr-x from emacs and replaced the old version. And now it works! YEAH.

adouzzy commented 8 years ago

@Keks-Dose maybe you can close this issue.

Keks-Dose commented 8 years ago

@adouzzy I did not open this thread, I hi-jacked it :-)

gdkrmr commented 8 years ago

One remark on a recent change in auctex:

TeX-after-TeX-LaTeX-command-finished-hook’s value is (TeX-revert-document-buffer)

  This variable is obsolete since 11.89;
  use ‘TeX-after-compilation-finished-functions’ instead.
  This variable may be risky if used as a file-local variable.

Documentation:
Not documented as a variable.

so the entry in the .emacs must be:

(add-hook 'TeX-after-compilation-finished-functions 'TeX-revert-document-buffer)
DrWaleedAYousef commented 8 years ago

Great; thanks so much. But his solves half of the problem. It reverts the pdf buffer; however, it takes me to another page than the current. I have to do C-c C-g to go to the same pdf page.

gdkrmr notifications@github.com writes:

One remark on a recent change in auctex:

TeX-after-TeX-LaTeX-command-finished-hook’s value is (TeX-revert-document-buffer)

This variable is obsolete since 11.89; use ‘TeX-after-compilation-finished-functions’ instead. This variable may be risky if used as a file-local variable.

Documentation: Not documented as a variable.

so the entry in the .emacs must be:

(add-hook 'TeX-after-compilation-finished-functions 'TeX-revert-document-buffer)

— Reply to this email directly or view it on GitHub.*

gdkrmr commented 8 years ago

For me C-c C-a and C-c C-c View go to the right place in the pdf.

DrWaleedAYousef commented 8 years ago

Suppose that you open your pdf at page 10, and editing the corresponding latex place. When you do C-c C-a it compiles, and the pdf page changes to, e.g., page 30. Then, I have to do C-c C-g to go to page 10 again. However, just before the latest update of Linux and Emacs (last week) I did not have to do that. Just C-c C-a compiles and update without changing the page. That is, I did not have to do C-c C-g. I do not know what happened after this latest update.

But your previous email partially solved the pdf update, but not yet the page issue.

gdkrmr notifications@github.com writes:

For me C-c C-a and C-c C-c View go to the right place in the pdf.

— Reply to this email directly or view it on GitHub.*

DrWaleedAYousef commented 8 years ago

The problem that I describe above appears only for a multi-file document. When the document is just one TeX file; C-c C-a goes to the right page. Any idea???

Enrico68 commented 8 years ago

@adouzzy Thanks!! Few lines of your latexmrc configuration saves me from the hell of pdf-syncs problems