3

How do I check if the PDF file produced by LaTeX is currently opened by an application or not?

Martin Scharrer
  • 262,582
Regis Santos
  • 14,463
  • 2
    I don't understand your question. Can you please elaborate and clarify a little more? – Gonzalo Medina Apr 15 '11 at 02:47
  • I think he means if it is in use, e.g., if you want to write over it. – Emre Apr 15 '11 at 03:13
  • i need: if file.pdf is open then close... else acroread file.pdf – Regis Santos Apr 15 '11 at 03:34
  • 7
    As far as I can tell, this has nothing to do with LaTeX. – Jan Hlavacek Apr 15 '11 at 03:56
  • Are you trying to synchronously edit and view the resulting PDF? And you want to close the PDF if you accidentally left it open in Acrobat? – Emre Apr 15 '11 at 04:49
  • yes, in command line. – Regis Santos Apr 15 '11 at 04:52
  • 2
    That is going to be operating system dependent. I can see implementing it in AppleScript on Mac OS, but it doesn't sound like that's an option for you. – Matthew Leingang Apr 15 '11 at 11:47
  • 2
    @JanHlavacek: It's still on-topic though, it's about "(La)TeX related software and tools like BibTeX, LyX, LaTeX editors, viewers, and converters" ([faq]). When you want to compile, you need to close the document first in some viewers. In order to do so, you need to check if it's open first. – doncherry Nov 10 '11 at 17:46
  • 1
    @RegisdaSilva: Then would you please edit your question? – Martin Schröder Nov 10 '11 at 17:57
  • The answer to your question involves the filesystem; in fact it's not a problem regarding a software ( it's simply misleading to talk about Adobe Reader or something like that ) or the operative system but it's about how the files on disk are organized and what are the feature of the filesystem you are actually using. Considering the filesystem as main topic it's almost impossible to give a generic answer, it's possible to give a specific answer but only if you gives us the names of your filesystem and the name of your operative system. – Micro Nov 10 '11 at 17:07

3 Answers3

7

I assume that your question is related to the fact that PDFLaTeX cannot write to a file if it is open in Acrobat Reader. The older versions of Acrobat Reader could be closed and reopend with external DDE commands but not versions 9 and X as far as I know.

Acrobat Reader can also not support the new synctex technology, so the best here is Sumatra PDF for the windows environment. You do not need to close it when compiling your document and it supports the inverse search via synctex. Some of the editors such as WinEdt on windows supports it directly and the inverse search works very well. WinShell and TexStudio (formerly known as TexMakerX) for windows can be configured to use it with synctex support. Alternatively use one of the editors with buildin viewers with synctex support such as TexWorks, TexMaker or TexStudio.

matth
  • 12,381
Danie Els
  • 19,694
4

Since you mentioned that you're using linux in one of the comments, the standard way to determine if a programme is currently accesing a file is the lsof command. Example:

user@host:~$ lsof /path/to/filename.pdf
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
okular  3961  user   26r   REG   0,21  5357342 12608996 /path/to/filename.pdf

Reading over the comments, you said you wanted to kill that process, here's a one-liner for it.

kill $(lsof -Fp /path/to/filename | egrep -o "[0-9]+")

Explanation: lsof finds the filename & -Fp option pulls the PID , egrep (grep with extended regex syntax that I'm more comfortable with) grabs only the numbers since it prints pXXXX. This is wrapped in $() and evaluated first, then the kill is run. Thanks to kahen for knowing the command line option for getting the PID

EricR
  • 3,117
  • 9
  • 29
  • 33
  • This can be improved. lsof supports setting its output format with the -F option. Try this: touch ./FOO; tail -f ./FOO & /lsof -Fp ./FOO; rm ./FOO (pay attention to the PID of tail, so you can kill it later and remember to delete ./FOO) – kahen Nov 10 '11 at 18:59
  • Cheers. Changes added. I'd never stuck lsof in a one liner before so I just went the lazy solution of awk/grep – EricR Nov 10 '11 at 19:01
  • @RegisdaSilva, is this the correct answer? If so it would be helpful for others if you would mark it as such. – EricR Nov 12 '11 at 16:57
1

Since you are using linux, I can tell you that you probably should not need to check if the file is open.

When I compile the .tex file (usually with pdflatex), I'm also viewing the .pdf document with either of these:

  • xpdf: simply press r and it will refresh the document.
  • evince: it refreshes automatically.
  • True, in Linux Acrobat doesn't lock the PDF, and you can also press Ctrl+R to refresh. – alfC Nov 21 '14 at 23:57