Skip to content

Instantly share code, notes, and snippets.

@dpo
Last active March 2, 2020 21:56
Show Gist options
  • Save dpo/4298916 to your computer and use it in GitHub Desktop.
Save dpo/4298916 to your computer and use it in GitHub Desktop.
Sublime build definition to clean up auxiliary LaTeX files. CleanLatexFiles.sublime-build is taken from https://gist.github.com/2852859. Other files are adapted from various sources. Place all files in Packages/User/

Sublime Minimalistic LaTeX Plugin

This is a bare-bones, minimalistic, trimmed-down, lean LaTeX plugin for Sublime Text 3. It doesn't parse the log file. It doesn't provide LaTeX snippets or reference/citation completion.

In order to use this plugin, clone this repository in your ~/Library/Application Support/Sublime Text 3/Packages. In Tools>Build System, make sure that BuildLaTeXFiles is selected.

Usage

Function Command
Build ⌘-B
Preview PDF ⌘-shift-B
Clean up alt-shift-C
Run BibTeX alt-shift-B
Refresh Preview alt-shift-P

Everything Should Be a Project

Whether your document has a single or multiple source files, every document should be part of a Sublime project. This allows you to edit a source file that isn't the master source file and still be able to compile the master file with ⌘-B. The project name should be the same as the master file's base name. For example, if your master file is mydocument.tex, the project name should be mydocument.

Using Preview

By default, Preview.app is the PDF previewer. An advantage of Preview is that it ships with OSX. A disadvantage is that it doesn't automatically refresh the display when the PDF file is updated unless you go and click in the Preview window, which is inconvenient. To simulate auto-update, I included the special command alt-shift-P (P is for Preview). This command is actually a short AppleScript command that activates the Preview window, causing the PDF to be refreshed, and then switches the focus back to the Sublime Text window. The mechanism is very simplistic for now and is probably easy to fool. However, it's functional.

Using Skim

You can change the PDF previewer in BuildLaTeXFiles.sublime-build. It is currently set to Preview.app but you could also use Skim.app. Simply replace the line

"cmd": ["open", "-a", "Preview.app", "$project_path/$file_base_name.pdf"],

with

"cmd": ["open", "-a", "Skim.app", "$project_path/$file_base_name.pdf"],

Customizing

You can customize the latexmk options by editing BuildLaTeXFiles.sublime-build. However, I recommend creating a ~/.latexmkrc resource file instead. Here's an example resource file that you can just copy and paste:

$pdf_mode = 1;
$pdflatex = 'pdflatex -file-line-error -shell-escape -synctex=1 %O %S';
$pdf_previewer = 'open -a %S';
$pdf_update_method = 0;
$clean_ext = "synctex.gz bbl tdo loa";
$bibtex_use = 2;

See the latexmk man page for more options.

{
"cmd": ["bibtex", "$project_path/$project_base_name"],
"working_dir": "$project_path",
"path": "$PATH:/usr/texbin:/usr/local/bin",
"file_regex": "^(...*?):([0-9]+): ([0-9]*)([^\\.]+)",
"selector": "text.tex.latex"
}
{
"cmd": ["latexmk", "-pdf", "-f", "$project_path/$project_base_name.tex", "-outdir=$project_path"],
"shell": false, // Important!
"working_dir": "$project_path",
"path": "$PATH:/usr/texbin:/usr/local/bin",
"file_regex": "^(...*?):([0-9]+): ([0-9]*)([^\\.]+)",
"selector": "text.tex.latex",
"variants": [ // Preview PDF.
{
"cmd": ["open", "-a", "Preview.app", "$project_path/$project_base_name.pdf"],
"shell": false,
"name": "Run"
}
]
}
{
"cmd": ["latexmk", "-C"],
"working_dir": "$project_path",
"path": "$PATH:/usr/texbin:/usr/local/bin",
"file_regex": "^(...*?):([0-9]+): ([0-9]*)([^\\.]+)",
"selector": "text.tex.latex"
}
[
// Clean up LaTeX auxiliary files.
{
"keys": ["alt+shift+c"],
"command": "run_latex_build",
"args": {
"build_system": "Packages/LaTeX/CleanLatexFiles.sublime-build",
"reset_to": "Packages/LaTeX/BuildLaTeXFiles.sublime-build"
},
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.tex.latex" }
]
},
// Run BibTeX.
{
"keys": ["alt+shift+b"],
"command": "run_latex_build",
"args": {
"build_system": "Packages/LaTeX/BibTeX.sublime-build",
"reset_to": "Packages/LaTeX/BuildLaTeXFiles.sublime-build"
},
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.tex.latex" }
]
},
// Refresh Preview to simulate auto-update.
// defaults write com.apple.Preview NSAppleScriptEnabled -bool true
{
"keys": ["alt+shift+p"],
"command": "run_latex_build",
"args": {
"build_system": "Packages/LaTeX/RefreshPreview.sublime-build",
"reset_to": "Packages/LaTeX/BuildLaTeXFiles.sublime-build"
},
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.tex.latex" }
]
}
]
{
"cmd": ["osascript", "-e", "tell application \"Preview\" to activate", "-e", "tell application \"Sublime Text\" to activate"],
"working_dir": "$project_path",
"path": "$PATH:/usr/texbin:/usr/local/bin",
"file_regex": "^(...*?):([0-9]+): ([0-9]*)([^\\.]+)",
"selector": "text.tex.latex"
}
import sublime
import sublime_plugin
class RunLatexBuildCommand(sublime_plugin.WindowCommand):
def run(self, build_system, reset_to):
self.window.run_command( "set_build_system", {"file": build_system } )
self.window.run_command( "build" )
self.window.run_command( "set_build_system", {"file": reset_to})
@lawlist
Copy link

lawlist commented Mar 22, 2013

Here are various solutions to the inability of OSX texlive latexmk to use the auxiliary option (i.e., keep only the *.pdf and *.tex files in the working directory). I believe the "bjmorgan" solution expands upon the work previously done by "dpo".

https://gist.github.com/bjmorgan/4349538

http://www.sublimetext.com/forum/viewtopic.php?f=2&t=11492

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment