Tag Archives: master file

Sublime Text: Always run a single file in a project no matter what is focused

When I program, I often try to split up my code into modules. This requires that I use different files. However, I also want to run my code from a “main” or a “master” file as well. It’s pretty annoying in Sublime Text, or any text editor, to have to switch to your main file to run your project.

I’ve talked about how to do this in TextMate but how can you pull this off in Sublime Text? Let me show you how.

  1. Create a Sublime Text project for your program. You can do this by using the menu Project > Save Project As…”.
  2. Open the settings for your project using the menu Project > Edit Project.
  3. You’ll get a mostly empty text window. Modify this window to include a build definition, such as the following one for Python:
    • "build_systems": [
        {
          "name": "PyProj",
          "cmd": ["python", "-u", "$file"],
          "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
          "selector": "source.python"
        }
      ]
      
  4. Modify the line with “cmd” and change “$file” to the master file you always want to run. For example, if your main file is called pyproj.py, then change it to pyproj.py.
Add the "build_systems" JSON field in your project settings file, accessible through the Project menu > Edit project.

Add the “build_systems” JSON field in your project settings file, accessible through the Project menu > Edit project.

That’s it! While you’re editing your project settings, you can make other customizations as well as you see fit.

Obviously, this isn’t limited to just setting a file to be run no matter what file you’re focused on. You could use this to add command-line arguments to a single project, to run project-specific scripts for building and testing, or to otherwise customize the way the “Control-B” (Command-B on Mac OS X) works when you are running the project. You can essentially define your own build system on a per-project basis in a project file’s “build_systems” section and use any information in Sublime Text’s build system documentation to do so. Unfortunately, the build system documentation doesn’t really describe this because it’s focused more toward Package development.

For more information about how project settings work, look at Sublime Text’s documentation for build systems and for projects!

Enabling the LaTeX Master File on Mac OS X (and also some general Mac OS X LaTeX notes)

First of all, what is LaTeX? LaTeX is a typesetting tool. It allows you to type stuff and then formats it all together for you into a nice pretty document, usually in PDF. In this respect, it’s quite similar to Microsoft Word. The differences between the two, however, are in the way that you format the document.

Microsoft Word is visual. That is, if you want to adjust something, you usually have to do it manually. You drag a figure into the right spot, you drag the margin to the right width, or  you drag the paragraph into the correct location. Anyone who’s worked with Word for any amount of time though has encountered issues with formatting, especially when moving things around. You add a paragraph and boom, suddenly your pictures are all off of the edge of your screen!

LaTeX works differently because you specify the behaviour and the appearance of the document in addition to the text. LaTeX extensively uses templates to format your document so that you get a document that looks great no matter what you put into it. In this respect, it’s a little like HTML in that what you type into the screen doesn’t look like the final product – you need another program to make it look like the final result.

Anyway, this isn’t a LaTeX tutorial so to speak. What I really wanted to talk about are a couple of options for LaTeX on Mac OS X.

TeXShop

TeXShop is a simple GUI that allows you to easily type and compile your LaTeX.

For a long time, I used TexShop as my main LaTeX editor for the Mac. It’s a pretty nifty editor – it has good support for common LaTeX templates, it allows you to click back and forth between the rendered PDF and the position in your document to edit, and it’s very clean and simple to use. If you are looking to get up and running quickly, I highly recommend it.

The complete package for Mac is located here: http://www.tug.org/mactex

I am pretty sure that when you install MacTex, you get it by default, but if you don’t, you can get it here. http://pages.uoregon.edu/koch/texshop/

TextMate

I bought TextMate for myself a few years ago but haven’t really used it for LaTeX until recently. I began to write my thesis, and it involved many files in the project. TextMate has a nice drawer that lists the files in the project and therefore is easier to manage than multiple TeXShop windows. For this reason, I began to move to TextMate.

However, TextMate needs a little bit of modification to make it work with the ease of TeXShop.

Textmate and Skim

Skim and TextMate. The project drawer is only one reason why TextMate is a strong editor for LaTeX.

PDFSync and TextMate

To get pdfsync working, which allows you to click between your rendered PDF file and the text file in which you’re editing that text, you should get Skim. Skim is an alternative PDF viewer that was originally designed to effectively export PDF annotations. There are a few options in the LaTeX bundle in TextMate to make pdfsync work with Skim, and an option in Skim to have it work with TextMate.

Download Skim here. It’s open-source.

The LaTeX Master file: Compile a Project from any File

One of the great things with LaTeX is the use of a “LaTeX Master File”. This allows you to specify what the “main file” is of your project so you don’t have to select it every time you want to compile your LaTeX project. In my example, the master file is “IrwinKwan_PhD_Dissertation.tex” and it imports the other files. There is an option in the LaTeX bundle of TextMate to set the master file but that isn’t enough.

In TexShop or any other Editor

To use a master file setting in any LaTeX project, put the following in every one of your .tex files:

%!TEX root=your_master_file.tex

where “your_master_file.tex” should be the actual name of the master file of your project. This works in TeXShop, in TextMate, or in any other text editor.

But, you can also do this in TextMate in a more specific matter as I’ve written below.

Setting the Master File in TextMate

What you need to do is to set an environment variable. Do that as follows.

The Textmate Info Box

Click on the "Info" box. Don't forget to de-select any files you might have selected first.

First, deselect files in the drawer view. To do this click on some white space below the drawer. If you need to, resize your window so you can reach it.

Second, click on the little “I” at the bottom of the drawer.

You’ll get a new window that asks for Environment Variables. Add a variable using the little “+” icon. The variable should be labelled TM_LATEX_MASTER. Give it the value equivalent to the full path of your LaTeX Master file. In my case, that’s “/Users/irwink/uvic/research/thesis/trunkIrwinKwan_PhD_Dissertation.tex”.

Setting variables in the variables window

Add the TM_LATEX_MASTER variable. The value is the full path to your master file.

Once you do that, you can then press Command-R and compile your project from any file you select (except for the master one, ironically). That was a real time-saver for me and allowed me to preview my changes from any file I was working on.