LEdHow to use alternative PDF-viewers (especially PDF-XChange)

Information and discussion about LEd, an integrated LaTeX environment for Windows
Post Reply
grbn
Posts: 6
Joined: Fri Mar 27, 2009 3:58 pm

How to use alternative PDF-viewers (especially PDF-XChange)

Post by grbn »

Using LEd is nice, but if you are inexperienced in writing batch-files it might be difficult to configure it to work with alternative viewers.

I was not satisfied using Adobe Reader and prefer PDF-XChange viewer, so I wanted to change that. Furthermore, I wanted to be able to compile a new pdf using pdflatex while the old one is still open (in default, you have to close it by hand first). After a lot of trial and error it works now. It is not a very elegant solution (i am not a programmer) but it works, so I thought to place it here so someone else can use it. If someone has a better/less quirky solution, please reply on this topic.

You need to change three things: change the arguments given to pdflatex.bat, edit pdflatex.bat, and use a vbs file to close the open document properly. So there we go:

1: open tex_cmd.gd with a text editor. You can find the file in your LEd installation directory, submap \Definitions. Go to line the line defining the pdflatex command, which should start like

Code: Select all

"11007=11007=Command.PDFLaTeX=PDFLaTeX=/l53007/l=exec:pdflatex.bat"
followed by a few parameters. Change these into:

Code: Select all

<MAINFILEDIR> <MAINFILENAMEEXT> <MAINFILEDISK> <BatchesDir> <MAINFILENAME>
. Now there are 5 arguments passed to the .bat file which you need to edit now.

2: go to the LEd\Batches directory, copy and backup your PdfLaTeX.bat to for instance PdfLaTeX.bat.bak, and edit PdfLaTex.bat using a text editor to the following:

Code: Select all

C:
cd %4
ClosePdf.vbs
%3
cd %1
pdflatex.exe %2
%5.pdf
So what does this mean? The %4 for instance means the fourth argument passed to this bat file, which is <BatchesDir> now, which is the directory of the batch files of LEd. You should read a bat file like a set of command line commands, so what now happens is: the script goes to the batches directory, runs ClosePdf.vbs, goes to your LaTeX-project directory, creates a pdf, and opens it in your viewer. So what still misses is a script for closing the pdf-file that is already open. We will fix that in step three.

3: Go to the batch directory of LEd again (\Batches). Create a new text document, and type the following:

Code: Select all

' VB Script Document, to close a pdf before running pdflatex
option explicit 
dim WshShell
set WshShell = WScript.CreateObject("WScript.Shell") 
WScript.Sleep 50 

'Start the viewer if it is not yet started
WshShell.Exec("C:\Program Files\PDF-XChange Viewer\pdf-viewer\PDFXCview.exe")

'Change foces to viewer to be able to send keys to it, and sent Ctrl+w to close the active document
WshShell.AppActivate "PDF-XChange Viewer"
WScript.Sleep 0
WshShell.SendKeys "^w"
WScript.Sleep 0

'Change focus to LEd to observe the compilation.
WshShell.AppActivate "LEd"
So this starts the pdf viewer if it was not yet open, changes focus to it and sends the keystroke Ctrl+w to it. This is the usual keystroke to close a document. Immediately after this, it switches back to LEd.

Last remarks:
  • if this does not work, first make sure that the paths are correct. If this is correct and you don't get error messages, you might want to increase the timers in the vbs script change WScript.Sleep 0 to WScript.Sleep 500 for instance, and see if this solves the problem.
  • The scripting is fairly general, so mutatis mutandis this might work for other pdf viewers too (Foxit, Sumatra etc).

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

How to use alternative PDF-viewers (especially PDF-XChange)

Post by T3. »

You don't have to send keys to PDF-XChange. There is a command line option /close that you can use. This batch file should be equivalent to your solution (not tested):

Code: Select all

@echo off
%3
cd %1
PDFXCview /close %2
pdflatex %2
start "" PDFXCview %2
I assumed that PDF-XChange binaries are on the search path. If not, replace PDFXCview with a fully qualified path.

Cheers,

Tomek
grbn
Posts: 6
Joined: Fri Mar 27, 2009 3:58 pm

How to use alternative PDF-viewers (especially PDF-XChange)

Post by grbn »

Thanks a lot! A knew there should be a better solution :-)

Your code needed some small improvements: %2 refers to file.tex so this does not work, that was why I was using %5. The code below is working, and now the vbs script is made obsolete now.

Code: Select all

%3
cd %1
start PDFXCview /close %5.pdf
pdflatex.exe %2
%5.pdf
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

How to use alternative PDF-viewers (especially PDF-XChange)

Post by T3. »

grbn wrote:Your code needed some small improvements: %2 refers to file.tex [...]
Ah, in that case you can use (in qoutes, in case of a file name with spaces): "%~dpn0.pdf" (on NT based systems %~dpn0 expands to the full file name without extension).
grbn wrote:

Code: Select all

%3
cd %1
start PDFXCview /close %5.pdf
pdflatex.exe %2
%5.pdf
Don't launch PDFXCview with start command before pdflatex. This spawns PDFXCview and doesn't wait for it to finish. Compilation will fail if pdf is not closed before pdflatex is executed.

Cheers,

Tomek
grbn
Posts: 6
Joined: Fri Mar 27, 2009 3:58 pm

How to use alternative PDF-viewers (especially PDF-XChange)

Post by grbn »

Thanks for replying. Indeed, using "%dpn0.pdf" worked fine.

Using the 'start' command did in fact work fine on my system, removing it worked fine too. Could you please explain a bit more what is the difference between the two?

Another question: do you know a good website where I can find these kind of batch command basics?

So now I use this code, without having to change the .gd file of LEd:

Code: Select all

%3
cd %1
PDFXCview /close "%~dpn2.pdf"
pdflatex.exe %2
"%~dpn2.pdf"
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

How to use alternative PDF-viewers (especially PDF-XChange)

Post by T3. »

grbn wrote:Using the 'start' command did in fact work fine on my system, removing it worked fine too. Could you please explain a bit more what is the difference between the two?
I don't want to go into details here. It's not a batch programming forum after all. In short, start launches a command but doesn't wait until it is finished (think of it as executing the command in the background) and the next command is executed immediately.
grbn wrote:Another question: do you know a good website where I can find these kind of batch command basics?
If you want to learn more about batch programming techniques you can take a look at this and this and this sites. For specific questions you can post to this discussion group or contact me privately.

Cheers,

Tomek
grbn
Posts: 6
Joined: Fri Mar 27, 2009 3:58 pm

Re: How to use alternative PDF-viewers (especially PDF-XChange)

Post by grbn »

Thanks a lot for you help.
Post Reply