[texworks] Trigger refresh of pdf view?

Stefan Löffler st.loeffler at gmail.com
Wed Nov 20 17:04:14 CET 2013


sorry for the late reply.

On 2013-11-16 22:37, Duncan Murdoch wrote:
> I'm using TeXWorks with a preprocessor (Sweave).  I have a custom
> typesetting command set up to run the preprocessor, then run the
> result through pdflatex.  I'm having some problems.
> The file that is processed might not have the same basename as the
> .pdf file.  (e.g. chapter.Rnw might lead to book.pdf being produced.) 
> The preprocessor is capable of running a system command after
> producing the .pdf file, so I have it running "texworks book.pdf", but
> this won't update an existing view of the same file.

Actually, the preview should be updating automatically if the pdf was
opened "manually" (i.e., not automatically when opening a tex file). The
easiest way to ensure this is to open the pdf first, before any other files.

> Is there a way to tell TeXWorks to update the preview?  I was thinking
> maybe an "aftertypeset" hook could do it, but I can't find any
> examples of those.  Is there a collection somewhere of examples of
> scripts?

There is a collection of scripts (and an excellent documentation of the
scripting capabilities in Tw) by Paul Norman:
I'm not sure if any of those use the aftertypeset hook, though.
One script that does make use of the AfterTypeset hook is the log parser
script, which is bundled with Tw and also available at

Anyway, I would try something like the following (note that I haven't
tested it):

// TeXworksScript
// Title: Refresh PDF
// Description: Refreshes PDFs after typesetting finishes
// Author: Stefan Löffler
// Version: 0.1
// Date: 2013-11-20
// Script-Type: hook
// Hook: AfterTypeset

windows = TW.app.getOpenWindows()

for (i = 0; i < windows.length; i++) {
    win = windows[i]
    if (win.objectName == "PDFDocument") {

Note that this would refresh all open pdfs. Also, I wouldn't really
recommend using objectName (it should identify a windows uniquely;
currently it doesn't, so this behavior might change in the future), but
I don't have a better solution right now.


More information about the texworks mailing list