A Sublime Text plugin for the careless

So after close to a year of procrastinating, I finally made the code that I had written about back in June last year into a plugin for sublime text 2. The plugin basically checks for a couple of mistakes that are commonly made in latex by me and hopefully other people who forget to title case titles and put spaces after punctuations. On running the command, the plugin highlights each mistake and suggests a replacement which can be accepted or rejected by the user with a single key press. IF you’re actually reading this post, do feel free to check it out at https://github.com/vaisaghvt/CheckTypos .

The process of making it into a plugin was quite straightforward since my original script was in python. After some help from the excellent NetTuts tutorial and some help from some of the existing plugins and obviously the API I had the plugin working. About 10 lines of JSON later, I had an option in the tools menu, a key mapping for mac, linux and windows and a command in the command palette.  Seriously, I’ve no idea why I waited for so long.

 

p.s. It’s now available in package control as CheckTypos plugin. Love how that things works.

Advertisements

Regex + Python to clean up my writing

After shifting to using latex (with sublime text) for writing, one of the things I’ve found rather irritating is correcting the silly mistakes I keep making. These are things like accidentally putting two spaces adjacent to each other, repeating phrases and forgetting to capitalize letters in the right places. Word used to make things easier with it’s spell check. There is a dictionary in sublime text but it works only for spelling mistakes and those aren’t always the problem. Checking the PDF and going through them looking for mistakes was obviously quite irritating.

Initial Solution

It was around this time I made a list of standard regexes that I could search for and replace using sublime text’s in built search. I put these in my sublime text latex cheatsheet for easy access. I’d just copy these from the cheat sheet and paste into the search bar and fix each error as I saw it. Obviously quite time consuming. I intended on automating this with some sort of script but just never got around to doing it. With all the other checks I had to do like whether the text appeared in a comment or an equation block or something. I had no clue how to do this in a simple bash script.

Python To the Rescue

It was at this time I was reading about someone using regexes in Python and I realised this would be an interesting way to improve my limited python skills and do something useful. And I set about making a python script that checks for the common mistakes I make (the regexes in my cheatsheet) and makes the appropriate suggestions for replacements and updates the file.

Adding new regex patterns and ways in which it has to be replaced is as simple as writing a simple function and adding a line to the list of patterns to be tested. I still need to do some basic testing on it and add more patterns but I’ve put the code up on GitHub already (link) and would be extremely grateful to anyone who checks it and gives any suggestions. I will update the Readme file and comment my code very soon (seriously.. i will ).

Finally, using the idea of functions being first class members in python for the first time was super interesting and super useful. Gives a hint of some of the biggest limitations of java; and a brilliant rant by Steve Yegge on java and functional programming: http://steve-yegge.blogspot.sg/2006/03/execution-in-kingdom-of-nouns.html.

Sublime Text Latex Cheat Sheet

(Scroll to bottom for the pdf cheat sheets) 

The quest for efficiency

I’ve been looking around a good editor for latex ever since I started using latex.

When I had this problem when I initially started using Latex during my FYP, I settled for WinEdt which seemed to be the most popular one out there. Since I had to finish my FYP report quickly (I had about a week to setup and learn latex and write my report in it), I didn’t bother experimenting with different editors. The problem for me with latex on windows was that it took me a really long time to set everything up. And for some reason, I couldn’t typeset my tex files into pdf within winedt. I’ve heard this is possible now. I’m not sure if I just never figured it out or the feature wasn’t available in earlier versions.

In any case, once I started PhD and more specifically when I was about to start my confirmation report, there were whole new complications. I had a mac at home and a pc in the lab. I would need to work on my report at both places. I could use Winedt or some such on Windows and TexShop or TexWorks on OSX. But I really didn’t like either of these apps and I wanted to learn to properly use one powerful text editor so that I could work better. So I did a check online and the most fanatical text editor ppl were ppl who used Vim or Emacs. I started using Emacs . Not entirely sure why, but I guess it’s because I found a windows version of emacs and because I heard of Aquamacs for the mac. Within a day or two, I realised that this would be pointless for me. For one, I like using my trackpad and mouse and I like having a proper GUI regardless of how powerful the app can be without it. Moving the cursor in the app was itself a pain, so I didn’t really want to do much more. I was sure there must be some modern text editor that I can use to do all the stuff I need without having to waste so much of my time trying to learn Emacs. I mean, there had to be something better than notepad++ and Textwrangler that was also free.

Sublime Text 2

That’s about when I came across Sublime Text (http://www.sublimetext.com/). It’s powerful, beautiful, has a lot of customizable key bindings, plugin development opportunities (using Python) and most usefully at that point of time it was cross platform. And yep, it was completely free. You could choose to donate to the developer (Jon Skinner) to a licensed version. However, the unlicensed version did all that the licensed version did but with an annoying pop up that comes up at every 10th save or something.

I use it generally for C/C++, python, xml, html and latex. You can use it for all sorts of things. The app also has a console which can be used for doing all sorts of things. For one, when working with python you can actually compile your files and run them from Sublime Text itself. Please take a look at the website for their features. There seems to be a lot of activity on the plugin and package development front over the last few months.

To start installing and discovering packages. First install Sublime Text 2  (http://www.sublimetext.com/2) and then install package control (http://wbond.net/sublime_packages/package_control). And voila! Installing and discovering packages becomes as easy as taking candy from a baby.

Now to start using latex on sublime, you’ll want to install the latexTools package (http://tekonomist.wordpress.com/about/). It provides all sorts of features to you including forward and inverse search, wrapping text in environments and commands, auto completion of references, etc. Combined with the shortcut keys provided by Sublime Text and Papers2’s Manuscripts I have now an Amazing setup for writing my papers. Things have never been easier.

But to actually make full use of these, a cheat sheet is absolutely essential. So I’ve created a pdf of key bindings in sublime and latex tools that’ll be most useful for anyone using sublime for latex.

Cheat sheet:

Windows:  sublimelatexsheet – Windows (p.s. any and all suggestions are welcome)

Mac : sublimelatexsheet -Mac

Tex files for cheat sheet: Tex source files for cheat sheet

*Edit : added some regular expressions that I use for checking to the cheatsheet.

*Edit: Changed for new plugin settings
Update: Installing and updating latextools is a lot easier now. And the plugin is a lot more awesome now. I don’t even need Paper’s manuscripts (which is available now in windows also). Most of this article is old but the latex tools plugin is still updated as of Oct. 24 2012.

*Edit: For updated regexes and a plugin to use these in sublime text please refer to my newer post : https://vaisaghvt.wordpress.com/2013/04/17/a-sublime-text-plugin-for-the-careless/

Why you might not want to use Word in the first place…

In my last post I talked about how to use MS Word properly for reports.  However, for any report more than than a few pages long with a few references, equations and pictures MS Word starts getting cumbersome to use. Placing pictures in a huge report is generally very irritating. It never stays where you want it and sometimes it just disappears from the page altogether.In fact, the difficulty in putting in figures is the best reason to not use Word.  Refering to specific chapters or sections or figures can be difficult. Restructuring the report messes everything. And in a report of a reasonable size , this is bound to happen.

Much more importantly, when you write a report in Word, you’re constantly thinking of how things should look and where they should come, the fonts to use and the styles to use. If you don’t, you’re most likely going to have to restructure a lot after you’ve written everything and this will lead to the same problems I said earlier.  Also, while most people do have office, or at least some software like Pages or Writer that converts the docx files to a “usable” format, a “usable” format almost never looks like it does in Word also there are (increasingly) a lot of people who don’t use Word.

LaTeX is an alternative I’d suggest. Firstly, Latex isn’t for everyone. It’s plain irritating to use if you’re new to it and just writing something small.  But for anything like a thesis or your final year report or your dissertation, LaTeX is the way to go.

In latex, you simply write the text in a generic text file called a .tex file and typeset it using Latex to get a PDF file. The tex file can be opened anywhere on any system since it’s simply a text file. You don’t care about the content and placement, you simply write your content and put in references for images. Latex takes care of putting images properly and getting your references to figures correct. Writing equations is made easy and the equations look beautiful. In fact your whole report looks beautiful and very very professional. Page numbers, tables of content, bibliographies, etc. can be generated with a single command and their formats and styles can be changed easily. Most conferences specify their prefered style and to change styles is as simple as writing a single command or copying a few lines from the specified format.

I won’t go into details here since there are a lot of resources online for this. But I would just like to ask everyone with at least some time to spare to try it. Most resources on setting up latex are just a google or bing search away. Do contact me if you want any help/advice on starting using latex or if you want a few templates to get you started (I’ll upload them anyway, when I get a little time). A few resources to get you started:

http://www.osnews.com/story/10766 : An excellent , slightly long article on why you might want to use latex and also provides a hello world and other basic stuff in latex…

http://www.stdout.org/~winston/latex/latexsheet.pdf : a cheat sheet for latex necessary for anyone using latex regularly.

http://en.wikibooks.org/wiki/LaTeX : A comprehensive and detailed wiki about everything you might want to know in latex.

http://ftp.itb.ac.id/pub/CTAN/info/lshort/english/lshort.pdf : A not so short introduction to latex.

http://www.ats.ucla.edu/stat/latex/ : A bunch of links for latex.