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

  • 2
    I don't understand your question. Can you please elaborate and clarify a little more? – Gonzalo Medina Apr 15 '11 at 2:47
  • 7
    As far as I can tell, this has nothing to do with LaTeX. – Jan Hlavacek Apr 15 '11 at 3:56
  • 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

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.

  • 1
    @Regis da Silva: Unfortunately I do not know much about Linux. As far a I know is Evince a lot more friendly and has limited synctex support. – Danie Els Apr 15 '11 at 5:01
  • 3
    TeXworks is available for Linux, too, and is already packaged for most common Linux distributions, like Debian, Ubuntu, Suse, Fedora, ... – user2574 Apr 15 '11 at 5:46
  • 3
    @Regis da Silva: Evince, for example, reloads the document if it is changed. You don't have to close/open anything. Other viewers like Okular probably do the same. – Andrey Vihrov Apr 15 '11 at 7:32
  • 4
    But in linux, nothing stops you from writing over a pdf file that is open in adobe reader. As for relaoding an open file on Adobe reader, look at this discussion: forums.adobe.com/thread/395299 – Jan Hlavacek Apr 15 '11 at 12:38
  • 1
    There are DDE commands for 9 and X as well, search the internet for acroviewR10 or acroviewR10+DDE. – matth Nov 10 '11 at 16:57

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

  • 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.