I spent quite some time trying to figure out how to integrate latexindent (what a nice tool by the way!) in TeXShop. I was quite surprised to see that there was no solution out of the box (latexindent is now quite standard and is integrated by default in several IDE, like latex-workshop on VSCode or TeXiFy in IntelliJ's).

So I ended up writing up my own AppleScript to format my code with latexindent with a simple shortcut (Cmd + I for me). The operation can be reverted with Cmd + Z (this would not be the case if one modifies the .tex file directly using an external script).

Since this could be of some use for someone else, I'm posting it here. It's my first time with AppleScript (and macros in TeXShop more generally) so I would be very happy to have some feedback or suggestions to improve this functionality.

Also, if I missed an already existing solution, please let me know!

1 Answer 1


To use this script, open the Macro Editor in the "Macros" menu, click on "New Item" and copy-paste the content below. You can give your macro any name (e.g. "latexindent") and an optional key (e.g. "i" if you want to launch the script with Cmd + I). Then click on "Save".

Please report any bug on this thread.

--This script runs latexindent on the current document.

set filepath to #FILEPATH#
set docname to #DOCUMENTNAME#

if filepath = "" then
    display dialog "You must save the document before using latexindent." buttons {"OK"} default button "OK" with icon note
    return false
end if

tell application "TeXShop" to save document docname

    --Don't forget the "without altering line endings", otherwise LF will be converted to CR and trailing blank lines will be removed.
    set indentedText to do shell script ("/Library/TeX/texbin/latexindent -m -l " & filepath) without altering line endings
on error number errnum
    display dialog ("latexindent returned error number " & errnum) buttons {"OK"} default button "OK" with icon stop
    return false
end try

tell application "TeXShop"
    set previousOffset to offset of selection of document docname
    set offset of selection of document docname to -1
    set n to offset of selection of document docname
    set offset of selection of document docname to 0
    set length of selection of document docname to n
    --set originalText to content of selection of document docname
    set content of selection of document docname to indentedText
    set offset of selection of document docname to previousOffset
    save document docname
end tell

return true
  • This doesn't seem to work for me: Script Runner puts up a dialogue saying "latexindent returned error number 2". (OS 13.5.2 with a current 5.18 TeXShop).
    – Alan Munn
    Sep 30 at 14:16
  • Thanks for the feedback. Exit code 2 means that "defaultSettings.yaml could not be read". Do you get the same error running /Library/TeX/texbin/latexindent -m -l myfile.tex from a terminal?
    – Arkandias
    Sep 30 at 15:58
  • No, I don't. From the terminal things seem to work correctly. (Initially they hadn't because I was missing modules, but that's a separate issue unrelated to the script. It seems that latexindent doesn't work out of the box with a standard MacTeX distribution).
    – Alan Munn
    Sep 30 at 16:00
  • @AlanMunn According to the documentation, there's a Mac version which doesn't need perl. Or there's a script to install the required perl modules. There's also one for Windows users without perl and one for Ubuntu Linux users without perl. (Why Ubuntu in particular?) Though presumably this requires the perl version.
    – cfr
    Oct 1 at 3:33
  • @cfr At least with a standard MacTeX install, the Mac version that doesn't need Perl doesn't seem evident. So I just installed Perl with homebrew and then used the helper script. That seemed the easiest route for me.
    – Alan Munn
    Oct 1 at 15:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .