modern latex with bbedit and tectonic

2021-08-05

Now that BBEdit offers Language Server Protocol features, it can be configured with two AppleScripts to become a powerful LaTeX environment that offers:

Thanks to Tectonic, the LaTex environment is at the same time updated and lightweight (it installs only what is required by the TeX document).

"BBEdit 14 and LaTeX/BibTeX typesetting warnings" BBEdit 14 and LaTeX/BibTeX typesetting warning

Requirements

Skim has support for BBEdit for backward search (Preferences → Sync → Preset: BBEdit)

TexLab gets recognized automatically by BBEdit.

Create the following two scripts in BBEdit scripts folder.

01)TeX Compile PDF.scpt

set msg to "no"
tell application "Finder" to if not (exists "/usr/local/bin/tectonic" as POSIX file) then set msg to "tectonic not found at /usr/local/bin/tectonic. Please install it."
if not (exists application "Skim") then set msg to "Skim not present. Please install it."
if not (exists application "iTerm") then set msg to "iTerm2 not present. Please install it."

if msg is not equal to "no" then
	display alert msg
	return
end if

tell application "BBEdit"
	set numdocs to the number of every text document
	repeat with ii from 1 to numdocs
		if (modified of text document ii) then
			save text document ii
		end if
	end repeat
end tell

(* If there are open documents, then "do your thing" *)
if numdocs > 0 then
	tell application "BBEdit"
		set mytexfilepath to get file of active document of window 1
		set myline to startLine of selection
	end tell
	set mytexfilename to do shell script "/usr/bin/basename " & (the quoted form of POSIX path of mytexfilepath) & " .tex"
	set mytexdirname to do shell script "/usr/bin/dirname " & (the quoted form of POSIX path of mytexfilepath)
	set mypdfpath to mytexdirname & "/" & mytexfilename & ".pdf"
	set cmdTex to "/usr/local/bin/tectonic --synctex " & (the quoted form of POSIX path of mytexfilepath)
	set cmdPdf to "/Applications/Skim.app/Contents/SharedSupport/displayline -b -r -g " & myline & " " & (the quoted form of POSIX path of mypdfpath) & " " & (the quoted form of POSIX path of mytexfilepath)
	try
		tell application "iTerm"
			set numterms to count windows
			if numterms > 0 then
				tell current session of tab 1 of current window
					write text cmdTex
					write text cmdPdf
				end tell
			else
				create window with default profile
				set numtabs to count tabs of current window
				tell current session of tab numtabs of current window
					write text cmdTex
					write text cmdPdf
				end tell
			end if
		end tell
	end try
end if

02)TeX Open PDF.scpt

set msg to "no"
tell application "Finder" to if not (exists "/usr/local/bin/tectonic" as POSIX file) then set msg to "tectonic not found at /usr/local/bin/tectonic. Please install it."
if not (exists application "Skim") then set msg to "Skim not present. Please install it."
if not (exists application "iTerm") then set msg to "iTerm2 not present. Please install it."

if msg is not equal to "no" then
	display alert msg
	return
end if

tell application "BBEdit"
	set numdocs to the number of every text document
	repeat with ii from 1 to numdocs
		if (modified of text document ii) then
			save text document ii
		end if
	end repeat
end tell

(* If there are open documents, then "do your thing" *)
if numdocs > 0 then
	tell application "BBEdit"
		set mytexfilepath to get file of active document of window 1
		set myline to startLine of selection
	end tell
	set mytexfilename to do shell script "/usr/bin/basename " & (the quoted form of POSIX path of mytexfilepath) & " .tex"
	set mytexdirname to do shell script "/usr/bin/dirname " & (the quoted form of POSIX path of mytexfilepath)
	set mypdfpath to mytexdirname & "/" & mytexfilename & ".pdf"
	set cmdPdf to "/Applications/Skim.app/Contents/SharedSupport/displayline -b -r " & myline & " " & (the quoted form of POSIX path of mypdfpath) & " " & (the quoted form of POSIX path of mytexfilepath)
	try
		tell application "iTerm"
			set numterms to count windows
			if numterms > 0 then
				tell current session of tab 1 of current window
					write text cmdPdf
				end tell
			else
				create window with default profile
				set numtabs to count tabs of current window
				tell current session of tab numtabs of current window
					write text cmdPdf
				end tell
			end if
		end tell
	end try
end if

Assign two shortcuts to the execution of the scripts. The two scripts try to cope with some errors (e.g., missing files, no open files), but are not perfect (e.g., no check that there is a LaTeX file opened).


I do not use a commenting system anymore, but I would be glad to read your feedback. Feel free to contact me.