How do I check if the PDF file produced by LaTeX is currently opened by an application or not?
-
2I 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
-
7As 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
-
2That 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 Answers
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 05:01
-
-
3TeXworks is available for Linux, too, and is already packaged for most common Linux distributions, like Debian, Ubuntu, Suse, Fedora, ... – Apr 15 '11 at 05: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 07:32
-
4But 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: http://forums.adobe.com/thread/395299 – Jan Hlavacek Apr 15 '11 at 12:38
-
-
1There are DDE commands for 9 and X as well, search the internet for acroviewR10 or acroviewR10+DDE. – matth Nov 10 '11 at 16:57
-
The problem is not the program creating the PDF (e.g. pdfTeX), but the viewer: Some of these (e.g. Adobe Reader and Foxit 5.x) lock the file so that it can not be written to. The solution for this is simple: Use a PDF viewer that doesn't lock the file, e.g. Sumatra PDF. – Martin Schröder Nov 10 '11 at 17: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
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
- 3,117
- 9
- 29
- 33
-
This can be improved.
lsofsupports setting its output format with the-Foption. 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.
- 2,455
-
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