Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to ease working with LaTeX and vim Apps
I spend too much time editing LaTeX documents, and my editor of choice has been long been gvim. One of the harder issues of working in LaTeX is that when you review your work, you have to switch back and forth between the viewer (I use PDFView) and the editor.

I was looking for a way to make this easier. I wanted a tool that I could use to point to a word in my viewer, and it would take me to the right line in the source file. Thus, this tip came to life.
  1. There's a package (that came with tetex) called pdfsync. This generates a lookup file.
  2. The PDFView viewer knows how to read this file, and allows running a custom script with the filename and line of code (set in preferences).
  3. I wrote a small AppleScript that talks to an open instance of vim (and launches it if it is closed), placing the cursor on the right place.
The end result is really all I wanted. I command-click on a word in my viewer, and the cursor jumps to the right spot in my editor of choice -- how fun! Hope others can find a use for this, too.

[robg adds: I haven't tested this script, and the links to the various sites are mine, so blame me for any errors in direction!]
    •    
  • Currently 2.00 / 5
  You rated: 1 / 5 (4 votes cast)
 
[13,480 views]  

An AppleScript to ease working with LaTeX and vim | 14 comments | Create New Account
Click here to return to the 'An AppleScript to ease working with LaTeX and vim' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Awesome
Authored by: rhowell on Jan 12, '07 07:41:25AM

Awesome. Awesome. Awesome.



[ Reply to This | # ]
TeXShop has this 'build-in'
Authored by: hamarkus on Jan 12, '07 08:42:01AM

TeXShop, at least when using the LaTeX package installer linked to on the TeSShop site, uses pdfsync as well. Just command-click on any text in the code or the pdf and it jumps to and highlights the corresponding part in the pdf or code, respectively.



[ Reply to This | # ]
An AppleScript to ease working with LaTeX and vim
Authored by: LegoEvan on Jan 12, '07 08:51:30AM

I use TextMate + TeXniscope. Seems to work well. I think TeXniscope would be compatible with your script as well.

---
--
Give me a lever large enough and a place to stand, and I will make use of Fudd's Law.



[ Reply to This | # ]
An AppleScript to ease working with LaTeX and vim
Authored by: jdsmith on Jan 12, '07 01:53:10PM
For users of Emacs, there is a similar method using the "source-specials" functionality in AUCTeX + XDVIK. In fact, it even goes one better: you can hit a key in Emacs to send your viewer directly to the location of your cursor, even highlighting the enclosing paragraph/figures/etc. in a red box. In reverse, you can Control-click anywhere in the XDVI window (text, figures, tables, etc.), to cue the Emacs cursor right to the corresponding block of text. AUCTex even understands multi-file documents, so if, for instance, you have a long table in a separate file, Control-clicking it in XDVI will pull it up.

Read about it how to set your Emacs up this way (along with some single keystroke LaTeX'ing + viewer reloading magic) here. This works for all platforms which support Emacs/LaTeX/xdvi. I use teTeX and xdvik from Macports, and xdvi-xaw.bin as the binary name for the running xdvi.

[ Reply to This | # ]

An AppleScript to ease working with LaTeX and vim
Authored by: tx.logic on Jan 12, '07 10:19:08PM

jdsmith notes that emacs users can exploit source specials to get source/preview searching/inverse searching with xdvi. The same functionality can be had with Emacs and TeXniscope. TeXniscope is a native coacoa app that, unlike xdvi, doesn't require X11 -- it also renders your document much more nicely than xdvi. Preview to source searching is done via pdfsync; source to preview searching is possible via a TeXniscope utility called txs-search.el, instructions for which can be found in the TeXniscope documentation.

TX.Logic



[ Reply to This | # ]
An AppleScript to ease working with LaTeX and vim
Authored by: jdsmith on Jan 12, '07 11:14:30PM

Thanks, I hadn't seen TeXniscope+Emacs. Does it highlight the area displayed? How is the speed? I stick with DVI for speed, yet it appears TeXniscope converts to PDF in real time to send to Quartz. I also enjoy the ability to turn figures off, so that very complex postscript doesn't bog things down.



[ Reply to This | # ]
But How Do You Set It Up?
Authored by: deleted_user15 on Jan 13, '07 12:42:27PM
This is totally the missing piece of the puzzle for me with regards to editing LaTeX documents under OSX. Only one problem... I've downloaded PDFView, compiled the script, but I can't seem to figure out how to set it up properly beyond this. Under Preferences > LaTeX > PDFSync support I have
PRESET:    Custom
COMMAND:   /Path/to/ViM-LaTeX.scpt
ARGUMENTS: "%file" %line
but get no love. (I reversed the "%file" and %line commands to correspond with the order the arguments seem to be read in the script.) I've also tried compiling the command as an application and running it from the command-line, but only get errors about it being unable to read the first argument.

Any ideas what I'm doing wrong? Thanks.

---
For I dipt into the future, far as human eye could see,
Saw the Vision of the world, and all the wonder that would be...

-- Alfred Lord Tennyson

[ Reply to This | # ]

Addendum: But How Do You Set It Up?
Authored by: deleted_user15 on Jan 13, '07 02:09:26PM
Okay, looks like I can get things to work right from the command-line using osascript. This suggests that I should use
PRESET:    Custom
COMMAND:   osascript
ARGUMENTS: /Path/to/ViM-LaTeX.scpt "%file" %line
which works only if all spaces are properly escaped (so " " -> "\ ").

Well, it kinda works. From the command-line this is a beautiful thing, but when I try to run it from PDFView my system goes haywire... Lines get deleted from my document in ViM, new buffers are created, saves are attempted, applications are hidden, and the Spotlight search field is repeatedly called. But at least something happens.

Thoughts on what might be causing all this?

[ Reply to This | # ]
Answering My Own Question
Authored by: deleted_user15 on Jan 13, '07 02:27:31PM
For those who're having prblems setting this up like I did (either the hint doesn't work, or it gives really pathological responses), here's what you need to do:
  1. Save the linked script somewhere as an AppleScript.
  2. In PDFView, go to Preferences > LaTeX > PDFSync Support and set the preset as Custom, the command as osascript, and the arguments to /Path\ to\ your\ copy\ of/pdf_latex.scpt "%file" %line. Note that you must escape any spaces in your paths here just as you would in a shell.
  3. Make sure you have Enable Access for Assistive Devices enabled in your "Universal Access" system preference pane.
The hint seems to assume that both steps #2 and #3 are obvious, but they weren't to me at least. Hope this helps other people who might be having problems (and sorry for the posts above!).

[ Reply to This | # ]
A bash script to do the same thing (better?)
Authored by: tpavlic on Jul 11, '07 01:54:41PM

This might be a better idea. Create a file (e.g., /usr/local/bin/gvim-pdfsync) with these contents:

#!/bin/sh

filename="$1"
filebasename=`basename "$1"`
lineNum="$2"

exec osascript \
 -e "set CR to ASCII character of 13" \
 -e "set ESC to ASCII character of 27" \
 -e "tell application \"Vim\" to activate" \
 -e "tell application \"System Events\"" \
   -e "tell process \"Vim\"" \
   -e "keystroke ESC & \":set hidden\" & CR " \
   -e "keystroke \":if bufexists('$filename')\" & CR " \
   -e "keystroke \":    buffer $filebasename\"  & CR " \
   -e "keystroke \":else \" & CR " \
   -e "keystroke \":    edit ${filename// /\\ }\" & CR " \
   -e "keystroke \":endif\" & CR " \
   -e "keystroke \":$lineNum\" & CR " \
   -e "keystroke \"zO\" " \
   -e "end tell" \
 -e "end tell"
Also make sure to chmod it 0755. You can then execute gvim-pdfsync "%file" %line to open Vim at that line %line of file %file.

This prevents an error on my system that occurs with the "buffer" line. It also handles files with spaces a little better. Finally, it is a little easier to implement in your PDFSync-compatible viewers. In fact, I have tested it in Skim and it works great. Just do:

Command: gvim-pdfsync
Arguments: "%file" %line

Hope that helps.



[ Reply to This | # ]
Update: A bash script to do the same thing (better?)
Authored by: tpavlic on Jul 11, '07 03:17:29PM
This is a better version.
filename="$1"
lineNum="$2"

[ "${filename:0:1}" == "/" ] || filename="${PWD}/$filename"  

exec osascript 
 -e "set CR to ASCII character of 13" 
 -e "set ESC to ASCII character of 27" 
 -e "tell application "Vim" to activate" 
 -e "tell application "System Events"" 
   -e "tell process "Vim"" 
   -e "keystroke ESC & ":set hidden" & CR " 
   -e "keystroke ":if bufexists('$filename')" & CR " 
   -e "keystroke ":exe \":buffer \" . bufnr('$filename')"  & CR " 
   -e "keystroke ":else " & CR " 
   -e "keystroke ":    edit ${filename// /\ }" & CR " 
   -e "keystroke ":endif" & CR " 
   -e "keystroke ":$lineNum" & CR " 
   -e "keystroke "zO" " 
   -e "end tell" 
 -e "end tell"
This will fix problems when you have multiple files open with the same base name.

[ Reply to This | # ]
Update (be sure to add /bin/sh): A bash script to do the same thing (better?)
Authored by: tpavlic on Jul 11, '07 03:18:51PM
Of course, be sure to add the #!/bin/bash at the top of the script.

[ Reply to This | # ]
Bugfix (A bash script to do the same thing (better?))
Authored by: tpavlic on Jul 11, '07 05:38:21PM
macosxhints.com escaped things that it shouldn't have. Now I'll use a code environment. You should use THIS SCRIPT. Alternatively, just use the script I've linked here.
#!/bin/bash

filename="$1"
lineNum="$2"

[ "${filename:0:1}" == "/" ] || filename="${PWD}/$filename"  

exec osascript \
 -e "set ESC to ASCII character of 27" \
 -e "tell application \"Vim\" to activate" \
 -e "tell application \"System Events\"" \
   -e "tell process \"Vim\"" \
   -e "keystroke ESC & \":set hidden\" & return " \
   -e "keystroke \":if bufexists('$filename')\" & return " \
   -e "keystroke \":exe \\\":buffer \\\" . bufnr('$filename')\"  & return " \
   -e "keystroke \":else \" & return " \
   -e "keystroke \":    edit ${filename// /\\\\ }\" & return " \
   -e "keystroke \":endif\" & return " \
   -e "keystroke \":$lineNum\" & return " \
   -e "keystroke \"zO\" " \
   -e "end tell" \
 -e "end tell"


[ Reply to This | # ]
A few remarks
Authored by: Soliman on Jul 12, '07 03:17:21PM
I modified a bit your script for my setup : since I don't like the error message of z0 if there are no folds under the cursor, I removed that line.

Since I usually run vim in a Terminal with the corresponding title set I had to change the tell application "Vim" to activate into something much more complex:

 exec osascript \
   -e "set ESC to ASCII character of 27" \
   -e "set vimFound to false" \
   -e "tell application \"Terminal\"" \
   -e "   activate" \
   -e "   repeat with theWindow in windows" \
   -e "      if name of the theWindow is \"vim\" then" \
   -e "         set frontmost of theWindow to true" \
   -e "         set vimFound to true" \
   -e "      end if" \
   -e "   end repeat" \
   -e "end tell" \
   -e "if not vimFound then" \
   -e "   tell application \"Finder\" to open POSIX file \"$filename\"" \
   -e "   tell application \"System Events\" to delay 5" \
   -e "end if" \
   -e "tell application \"System Events\"" \
   -e "   tell process \"Vim\"" \
   -e "      if vimFound then" \
   -e "         keystroke ESC & \":set hidden\" & return " \
   -e "         keystroke \":if bufexists('$filename')\" & return " \
   -e "         keystroke \":exe \\\":buffer \\\" . bufnr('$filename')\"  & return " \
   -e "         keystroke \":else \" & return " \
   -e "         keystroke \":    edit ${filename// /\\\\ }\" & return " \
   -e "         keystroke \":endif\" & return " \
   -e "      end if" \
   -e "      keystroke \":$lineNum\" & return " \
   -e "   end tell" \
   -e "end tell"
Just in case somebody else has a setup as weird as I do :)

---
Sylvain

[ Reply to This | # ]