Sunday, September 9, 2012

Compiling LaTeX in gedit with one key

This might be useful for a very small niche, but just in case anyone else needs it, well I hope you land here.

The scenario is simple: You like using the popular gedit text editor for writing your latex presentations and papers (as I write this gedit is the default text editor in ubuntu and many other linux distributions related to GNOME). To see changes fast you want to have a keypress that will update the pdf file which you may have open in your favorite viewer (and love how you don't have to do anything for it to refresh automatically).

If you go to Tools > Manage External Tools, you can run your own commands by pressing certain keys. Using the Enviroment Variables that can be found in the documentation, you can write a simple little bash script that will run both latex and dvipdf with only one press key:




#!/bin/sh
TEXNAME=$GEDIT_CURRENT_DOCUMENT_NAME
DVINAME=${TEXNAME%".tex"}".dvi"
latex $TEXNAME
dvipdf $DVINAME


Note that to change the extension from .tex to .dvi I used the string DVINAME=${TEXNAME%".tex"}".dvi" thanks to root45 at askubuntu.com for this.
I was first compelled to use: ${$TEXNAME:0:${#texname}-3}"dvi" but this substitution is not accepted by gedit (although it's perfectly valid in bash).

So in the end this is how your external tool should look like:

After that well define the shortcut key, and the applicability only to LaTeX files. You may want to set the output to be displayed in the "Bottom Pane" to see errors when there are some. I just prefer to save screen space and if it doesn't update the document then I run these commands on a terminal to troubleshoot.

2 comments:

  1. Great!
    Thank you!
    =)

    I actually change the latex to pdflatex, and it works perfectly for me!

    Cheers

    ReplyDelete
  2. I actually had to download a freaking extra plugin to do this AND IT DIDN'T WORK so this post was awesome. Thank you sooo muuuuch!

    I also use pdflatex but it's great :D

    ReplyDelete