Making your first PDF with LaTeX and Sublime Text 2

This tutorial will walk you through the installation and setup of Sublime Text 2 for working with LaTeX. After installation we will create a simple PDF to make sure everything has been setup correctly. These instructions follow my steps installing Sublime Text 2 and setting it up for working with LaTeX on my PC running Windows 7. See Chaning’s tutorial for Mac-specific instructions. If you are one of the many moving from Sublime Text to Atom check out our new Atom tutorial on the same topic: Creating Your First PDF with LaTeX and Atom

Why Sublime Text 2?

Sublime Text 2 is a fast cross-platform text editor. For a preview of the beautiful editing/coding/writing future that awaits you go to http://www.sublimetext.com/. Sublime is free to try, though you will occasionally be encouraged to purchase a license for $59.

Why LaTeX?

LaTeX is the standard for professional typesetting.

Installing LaTeX

[EDIT: This section was added to answer several comments] Installing LaTeX varies depending on your operating system. On Windows, TeX Live is recommended, but MiKTeX will also work. Here, we’re going to use TeX Live, simply click the link above and then download the file “install-tl.zip” (see screenshot below). tug Next, find the file in your Downloads folder, right-click it, then select “Extract All …”. Confirm by clicking the “Extract” button. After the extraction is completed, the extracted folder should be displayed. Open the only folder (should start with “install-tl-” and end with the date you downloaded it). In theory you can run the Windows batch file “install-tl”; however, I received the “perl.exe has stopped working” error. What worked for me was to install using “install-tl-advanced” (see screenshot below), I also chose to “Run as administrator” (in the right-click menu) to install this for all users of my computer. install_tl Once the advanced wizard pops up, tweak any features you want to change. I toggled “Default paper size” from “a4” to “letter” and “Installation for all users” from “No” to “Yes”. Once you have made your adjustments, click on “Install TeX Live” (see below). install_tl_advanced Grab some coffee, this could take some time.

Install Sumatra PDF

Download Sumatra PDF from here (see below). sumatrapdf Double-click the downloaded file to install Sumatra PDF.

Add SumatraPDF to the path

For Sublime to open SumatraPDF for you when you build your PDF, we need to add the PDF viewer to your path. Adding SumatraPDF to your path allows your programs to open SumatraPDF without having to know exactly where you installed it. To add SumatraPDF to your path, right-click Computer from the desktop or the Start menu. Choose the “Properties” option. Select “Advanced system settings” from the menu on the left. Choose the “Advanced” tab and click the “Environmental Variables…” button near the bottom. In the “System variables” section, click on the variable named “PATH” and click the “Edit…” button. Place your cursor at the end of the text in the “Variable value:” field, add a semicolon, “;”, and enter the path to SumatraPDF. For me, the path was “C:\Program Files (x86)\SumatraPDF”. Click “OK” to accept your changes.

Install Sublime Text 2

Download Sublime Text 2 from http://www.sublimetext.com/2. Click on the appropriate version for your OS to begin the download (see below). st2 Install Sublime Text 2 (e.g., double-click the downloaded *.exe file).

Install Package Control

Install Sublime Package Control by copying the text below or by going here and following the instructions.

import urllib2,os,hashlib; h = 'eb2297e1a458f27d836c04bb0cbaf282' + 'd0e7a3098092775ccb37ca9d6b2e4b7d'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler()) ); by = urllib2.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); open( os.path.join( ipp, pf), 'wb' ).write(by) if dh == h else None; print('Error validating download (got %s instead of %s), please try manual install' % (dh, h) if dh != h else 'Please restart Sublime Text to finish installation')

After copying the above text, open up Sublime Text 2, press CTRL+`, paste the copied text (CTRL+V), and press enter. You will need to restart Sublime Text 2 to complete the installation.

Install LaTeXTools

Open Sublime Text 2. Enter CTRL+SHIFT+P, to open the Command Palette. Start typing “install” then hit ENTER to select “Package Control: Install Package”.

Install Package

Type “install” in the Command Palette to pull up the Install Package option.

Once the new option list pops up (press CTRL+SHIFT+P if it is not open), type “latextools” until you see the LaTeXTools plugin selected. Hit ENTER to install LaTeXTools. [IMPORTANT: If you are having any issues, check the ReadMe link for OS-specific setup instructions.]

Directing the builder to TeXLive

The default within LaTeXTools is to build your PDF using the program from MikTeX. This is perfect if you installed MikTeX. However, if you installed TeXLive, you need to do one more thing to get things up and running. In Sublime Tex 2, go to the “Preferences > Browse Packages …” menu item. Open the LaTeXTools folder. Open the file “LaTeX.sublime-build”. Find the section that says:

 // *** BEGIN MikTeX 2009 *** "cmd": ["texify", "-b", "-p", "--tex-option=\"--synctex=1\"" ], "path": "", // *** END MikTeX 2009 *** 

Comment out the lines between “BEGIN MikTeX 2009” and “END MikTeX 2009” by selecting them, then pressing CTRL+/ (these were lines 72 through 77 for me). Scroll down to the TeXLive section and uncomment the lines between “BEGIN TeXLive 2011” and “END TeXLive 2011” (these were lines 89 through 94 for me) by selecting them and then pressing CTRL+/. Your build file should now look match the following snippet:

 // *** BEGIN TeXLive 2011 *** "cmd": ["latexmk", "-cd", "-e", "\\$pdflatex = 'pdflatex %O -interaction=nonstopmode -synctex=1 %S'", //"-silent", "-f", "-pdf"], "path": "C:\\texlive\\2011\\bin\\win32;$PATH", // *** END TeXLive 2011 *** 

Now, save the sublime-build file.

Your First PDF

Now that you have installed Sublime Text 2 (ST2) and LaTeXTools, open a new file in ST2 (using the menu or CTRL+N,CMD+N). Copy and paste the following LaTeX source into your new file:

\documentclass{article} \title{Title} \author{Your Name} \begin{document} \maketitle{} \section{Introduction} This is where you will write your content. \end{document}

Save the file (using the menu File>Save or CTRL+S,CMD+S on OS X) as first.tex. After saving, build the PDF (using the menu Tools>Build or CTRL+B, CMD+B on OS X). You should see the PDF previewer display your first PDF. Congratulations!

Next Steps

Create and use snippets in our next tutorial to increase your productivity.

LaTeX References

http://en.wikibooks.org/wiki/LaTeX/ http://tex.stackexchange.com/

Comments

  1. It just says COULD NOT COMPILE! and then a bunch of jibberish

    • @seioguw: Could you copy and paste the jibberish? Did it say something like “Attempted command: …”? It should have included the command it attempted to send.

      Also, what is your operating system.

  2. I’m getting the same error. Here’s my output. I’m on a Mac.

    COULD NOT COMPILE!

    Attempted command:latexmk -cd -e $pdflatex = ‘pdflatex %O -interaction=nonstopmode -synctex=1 %S’ -f -pdf /Users/snanand/testFile.tex

    Thanks.

  3. Hi Jon: I got this error when I tried to view the work into PDF:

    [Compiling C:\Windows\first.tex]

    COULD NOT COMPILE!

    Attempted command:texify -b -p –tex-option=”–synctex=1″ C:\Windows\first.tex

    • @azib: I added the section “Directing the builder to TeXLive” which should fix your error. This happened because LaTeXTools defaults to building PDFs with the program provided with the MikTeX distribution. Since you installed TeXLive, you will need to comment-out some lines and uncomment others so Sublime Text 2 calls the right program. Hope this helps!

  4. Hi Jon: It was working for several days then it went back to the old COULD NOT COMPILE! again. Exactly the same problem.

    I tried following your “Directing the builder to TeXLive”, but the file just have the below:

    /* Let's not preview for now. This should be OK on Win/MikTeX, too (?) */
    /* Make sure latexmk is on the $PATH, AND add it to .MacOSX/environment.plist */
    
    {
    	"cmd": ["latexmk", 
    	"-e", "\\$pdflatex = 'pdflatex %O -file-line-error --max-print-line=200 -synctex=1 %S'",
    	"-e", "\\$pdf_previewer = qq{if [ -n \"`ps axww | grep Skim | grep -v grep`\" ] ; then osascript -e 'tell app \"Skim\" to revert front document' ; else open -a Skim %O %S ; fi}",
    	//"-e", "\\$pdf_previewer = qq{/Applications/Skim.app/Contents/SharedSupport/displayline -g -r 0 %S}",
    	"-pv", "-pdf",  "$file"],
    	"working_dir": "$file_path", /* crucial or latexmk will spew stuff in /!!! */
    	"file_regex": "^(...*?):([0-9]+): ([0-9]*)([^\\.]+)",
    	"selector": "text.tex.latex",
    
    	// Windows-specific command
    	// ASSUMES sumatrapdf is default viewer
    	// Only problem is that it comes to the foreground
    	"windows":
    		{
    			"cmd": ["texify", "-b", "-p",
    			"--tex-option=\"--synctex=1\"", 
    			"--tex-option=\"--max-print-line=200\"", 
    			"--tex-option=\"-file-line-error\"",
    			"--run-viewer",
    			"--viewer-option=\"-reuse-instance\"",
    			"$file"]
    		}
    }
    • Ooops! Sorry wrong file.

      I did what the section told me, Still, I’m having this error.

      [Compiling D:\REIS files\Impact of RD on solar PV penetration.tex]

      COULD NOT COMPILE!

      Attempted command:texify -b -p –tex-option=”–synctex=1″ D:\REIS files\Impact of RD on solar PV penetration.tex

      • @azib: texify is the command for MikTeX. If you have TeXLive installed (and want to use it) you will need to check which lines are commented out in your build file. Otherwise you need to install MikTeX and make sure the path to texify is added to your environmental variable.

        • @Jon: Now it’s working very well!

          Apparently, the path I used to work on is wrong. Instead of

          “path”: “C:\\texlive\\2011\\bin\\win32;$PATH”, it should be

          “path”: “C:\\texlive\\2012\\bin\\win32;$PATH”. The year is incorrectly specified. I also had the wrong section commented out. My bad.. ๐Ÿ™‚

          Thanks for the support, Jon.

  5. I can’t get it to work too. It says could not compile. Here’s the stuff:

    [Compiling C:\Users\viper_000\Documents\LaTex Files\Trying – June 19, 2013.tex]

    COULD NOT COMPILE!

    Attempted command:texify -b -p –tex-option=”–synctex=1″ C:\Users\viper_000\Documents\LaTex Files\Trying – June 19, 2013.tex

    I tried reading the LaTexReadme but I was confused. And I also have MikTex, not TeXLive.

    • Check first that MikTeX has been installed correctly by opening a command line (Open the Start menu, type “cmd” then press ENTER) and enter the following command:

      texify -p "C:\Users\viper_000\Documents\LaTex Files\Trying โ€“ June 19, 2013.tex"
      

      If MikTeX is installed correctly, this should create “Trying – June 19, 2013.pdf” and all the auxiliary files in the same directory as your tex source file.

      It is also possible there is an auxiliary file blocking the compilation. If there are any other files starting with “Trying โ€“ June 19, 2013” (aside from the “*.tex” file), delete them and try to build from Sublime Text 2 again.

      • I’m not familiar on how to use the command line. When I paste that on, it says ‘textify’ is not recognized as an internal or external command.

        • @Marco: If texify is not recognized as a command, either MikTeX is not installed (in which case you will need to install it), or you need to add the directory where the texify command is located to your PATH environmental variable. The directory should look something like this in Windows: “C:\Program Files\MiKTeX 2.7\miktex\bin“. For instructions on setting the PATH environmental variable see this answer. This older guide can walk you through the setup on Windows XP.

  6. For whatever its worth, I got “Package Control Messages” that say:

    Package Control Messages
    ========================

    LaTeXTools:
    ———-

    LaTeXTools Plugin version 2013-06-19 changelog:

    A few more fixes and new featurettes ๐Ÿ™‚

    New features:

    – \texttt{} snippet (C-l,C-t) (contributed by Ray Fang)
    – Completions for amsmath equations (see LaTeX.sublime-completions)

    Bugs fixed:

    – Updated README to reflect the new Skim preset for ST2. Yay!
    – Fixed sections regexp so \section* etc. is captured (P. Kazmierczak)

  7. Thanks Jon for your tutorials–they are all great resources. Very helpful and recommend to anyone starting up for the first time!

  8. Hi! i could compile but the sumatra didn’t show the pdf! the path is correct?

    • @Lourdes: The path to SumatraPDF was C:\Program Files (x86)\SumatraPDF for me. Depending on your setup, the correct path for you may be C:\Program Files\SumatraPDF. The path you should use depends on where SumatraPDF was installed on your machine.

      • Zach Winston says:

        I’m having the same problem. My path is identical to yours. What should I do?

      • I seem to be getting the same error of not being able to show the pdf automatically with SumatraPDF… My Path is “C:\Program Files (x86)\SumatraPDF”, which is correct.

        I get the following error in the Sublime Text 2 Console:
        Windows, Calling Sumatra
        Traceback (most recent call last):
        File “.\sublime_plugin.py”, line 356, in run_
        File “.\jumpToPDF.py”, line 96, in run
        File “.\subprocess.py”, line 633, in __init__
        File “.\subprocess.py”, line 842, in _execute_child
        WindowsError: [Error 2] The system cannot find the file specified

        I tried looking elsewhere for solutions, but to no avail. Would you have any other suggestions for getting it to work? It’s somewhat tedious to always need to open SumatraPDF and the file.

        Thanks in advance!

        • Well, funny enough, after trying different solutions for 1-2 hours, I finally got it working and it was simply reinstalling Sublime Text 2! I must’ve had an older version (though it shouldn’t have been that old, a couple months at most), and that was the only thing I wasn’t fresh installing from following these steps. Thanks for this guide!

  9. Zach Winston says:

    Sorry ignore last comment. Thanks so much for your help!

  10. Thanks so much for this helpful guide.

    Mine compiles fine, but unfortunately Sublime just will not sync with SumatraPDF no matter what I try. There are no errors in the consol, it just says “cannot launch viewer: Make sure it is on your PATH”. I’ve added the correct path as described here, and I’ve set the inverse search command-line in Sumatra, so I’m not sure what’s going on ๐Ÿ™

    Any other ideas of what I might be doing wrong??

    Cheers, Jayden.

  11. PS, my Path in system properties reads: “C:\texlive\2013\bin\win32; C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe” Does this look right??

  12. Thanks for this guide. I really like sublime text as a latex editor, but it doesn’t seem to be able to utilize the miktex packages or install missing .sty files on the fly. Is there something I’m doing wrong here?

    I have the full installation of miktex 2.9 yet sublime text is not accessing any of them, requiring me to put all of the .sty files in the same directory as the file I’m trying to texify.

    Thanks.

  13. Hi Jonathan. Thanks for the great guide! Successfully set up on my Mac, but can’t figure it out on Windows 7.
    I get the same error as azib:

    TraditionalBuilder: Invoking texify…
    COULD NOT COMPILE!
    Attempted command:texify -b -p –tex-option=”–synctex=1″ CV_2013.tex

    And when I opened the ~\Packages\LaTeXTools\LaTeX.sublime-build.OLD file the file looked like what azib originally posted. When I opened the LaTeX.sublime-build file there was a message at the top that said:

    // DO NOT TOUCH THIS FILE!
    // All configuration now occurs in LaTeXTools Preferences.sublime-settings
    // Put a copy of *that* file in your User directory and customize at will

    I followed the instructions but couldn’t find any file containing similar code to what you described in the “Directing the builder to TeXLive” section. The LaTeXTools Preferences.sublime-settings file is confusing and I don’t know how to direct the builder to TeXLive. I tried the following with no success. Please help ๐Ÿ™‚

    “windows”: {
    // Path used when invoking tex & friends; “” is fine for MiKTeX
    // For TeXlive 2011 (or other years) use
    // “texpath” : “C:\\texlive\\2011\\bin\\win32;$PATH”,
    “texpath” : “C:\\texlive\\2013\\bin\\win32;$PATH”, //since I have the 2013 version
    // TeX distro: “miktex” or “texlive”
    “distro” : “miktex”
    },

  14. Some Change in: “Directing the builder to TeXLive”:
    It seems that LaTeXTools changed the procedure how to directing the builder to TeXLive:

    Now you have to change the settings in User\LaTeXTools.sublime-settings instead of changing them in LATeXTools\LaTeX.sublime-build

    Maybe you can add this information in your very helpful tutorial.

  15. Shareen Joshi says:

    Thank you SO MUCH for this! You have singlehandedly boosted my productivity!!!!!!!!!!!!!!!! AWESOME. THANK YOU. THANK YOU. THANK YOU!

    Shareen Joshi

  16. Robin Xu says:

    Hi Jonathan,

    Thanks for the tutorial, but I’m still having a bit of an issue. When I compile my file, the console reads “TraditionalBuilder: Invoking texify… done”, but no PDF appears. When I use LaTeXTools: View PDF, Sumatra PDF pops up, but states “Error loading C:\test.pdf”.

    Any ideas as to what I need to change?

    Thanks.

  17. Hello,

    I have followed your guide and installed Sublime, LaTeXTools and SumatraPDF – the only difference is I am using MikTeX –
    But I keep getting this error –

    [Compiling C:\Users\Apurva\SkyDrive\Uni Stuff\LaTeX documents\assign14.tex]

    TraditionalBuilder: Invoking texify… done.

    Errors:

    C:/Users/Apurva/SkyDrive/Uni Stuff/LaTeX documents/assign14.tex:6: LaTeX Error: File `url.sty’ not found. [\date]

    [Done!]

    I don’t know how to fix his. Any help would be brilliant!

    Thanks

  18. I have done everything but when I try and build my first pdf I receive the following message:

    [Error 2] The system cannot find the file specified

  19. Great tutorial! I used notepad++ with latetex once and now I wanted to learn how to work in latex with sublime. This was exactly what I was looking for. I only prefer MikTex over Tex Live. Thank you!

    As Axel said there was change in LaTeXTools settings (do not use LaTeX.sublime-build, but LaTeXTools.sublime-settings in the User dir). Everything is described in LaTeXTools readme.

    Thank you once again
    Aneta

  20. Dear John,

    I have been using LaTex on S.T.2 for some years now. Just thought I would drop in and say thanks for all your work and support!

    Benjamin

  21. Thank you so much for this! It was a simple and straight forward instruction to do something I wouldn’t have been able to do by my self.

    I used the new ST3 and you can basically skip the step on configuring the latex-build file.

  22. Good job, sir!.. Thanks

  23. Really great instructions! Thanks!

Leave a Reply