19

I've been using Texmaker for ~5 years now and prefer it over other editors because of its flexibility.

Is there a way to auto-indent LaTeX code in TeXmaker so that the code becomes more manageable ?

  • 4
    one such code beautification tool is latexindent by cmhughes which can be integrated into TeXmaker by user command and automated by arara. As such TeXmaker does not have any full pledged indentation(i suppose). – texenthusiast Jul 31 '13 at 17:57
  • BTW Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – texenthusiast Jul 31 '13 at 18:05
  • @texenthusiast Good idea, but I would be a little concerned that TeXmaker may have some sort of write-protection on the file in question. – Sean Allred Jul 31 '13 at 20:52
  • 2
    @SeanAllred Although I did not try this personally, but I hope: Options-->Configure-->Editor-->tick on check for external changes might work. – texenthusiast Jul 31 '13 at 21:10
  • I always had trouble, getting the suggestion of @texenthusiast to work in TeXStudio under Linux. Is there any recent development in 2016? – k1next Apr 10 '16 at 9:27
9

Upvoted comment converted to answer to clear off the Q from unanswered list.

Such indentation and code beautification is made possible with latexindent tool by cmhughes which can be integrated into TeXmaker by user command and automated by arara. As such TeXmaker does not have any full pledged indentation.

Also one might need to change settings of TeXmaker to notice the indentation changes done by arara and latexindent externally as shown below

Open TeXmaker: Options-->Configure-->Editor-->tick on check for external changes might be needed.

  • 4
    Thanks for the solution! A quick how-to would be useful though. There's gotta be a quicker, more stackexchange-ish way to explain than linking to a 19-page manual and a 137-page manual. – BenB Oct 4 '16 at 22:29
  • Anyone down to try this? – Greg Hilston Aug 4 at 21:56
6

This answer should provide you with two options to quickly indent your .tex file without the need to read both manuals.

arara manual: Link
latexindent manual: Link

Option 1: Semi-Automated way with file-reload

Using latexindent from command line perfectly indents your .tex file. If you use latexindent -w yourfile.tex and then click File -> Reload document from file your .tex file is indented.

latexindent itself is shipped with your Tex* installation. If you get perl warnings when typing latexindent -version using the command line, such should be solved using

cpan
install YAML::Tiny
install Getopt::Long
install File::HomeDir
q

Credit: How to configure TexLive 2015 to use latexindent.pl script with Kile

Option 2: Automated way using 'arara' (Recommended)

arara is also shipped with your Tex* installation.

What is arara?

Basically it just automates various LaTeX calls. In Texmaker you can just set up a custom command in Users -> own commands. The command should be arara %.tex. This will apply arara on all your .tex files in side your directory.

What arara will do with the .tex file can then be specified inside the file. For me, I need to compile my file with 2x XeLaTex, 1x Biber (for bibtex) and again 1x XeLaTeX. Subsequently, I added the following lines into my .tex file:

% arara: xelatex
% arara: xelatex
% arara: biber
% arara: xelatex

Then you just execute your custom command in Texmaker and your file will compile as you want.

To check if arara is correctly installed on your system just type arara in your command line and you should see some overview options.

arara and latexindent

latexindent can also be called from arara inside your .tex file every time you compile it. To combine the two, follow this guide: How to configure TexLive 2015 to use latexindent.pl script with Kile

Edit [11/2017]: arara v4 comes with an 'indent' rule which is using latexindent in the background. There is no installer yet but you can compile v4 easily by following the instructions in the README.

You can then just add % arara: indent: {some.options.here} to your .tex file.
I currently use % arara: indent: {overwrite: yes}.
This rule overwrites your .tex file with the indented version created by latexindent and (optionally) saves a backup. See section 3.2 from the latexindent manual for the options.

NOTES:

  • However, this way still ends up with compile errors for me using Texmaker. Running arara from command line works fine with latexindent. Subsequently I suggest to use option 1 or to run arara from command line (my current approach) until I figured out what is causing the error using Texmaker.

  • latexindent (Version 2.1R) was not updated since two years when writing this answer (https://github.com/cmhughes/latexindent.pl) and is causing several perl related syntax warnings (Unescaped left brace in regex is deprecated, passed through in regex) when being executed from command line. They do not seem affect its usage and the package maintainer said that these are already fixed in the dev version (2.2R). You can get it from here. latexindent version 3 is in preparation.

  • latexindent is currently at version 3.3 and running smoothly. Happy indenting :) [11/2017]

  • Thanks for this :) version 3.0 was released at the end of Feb 2017, perhaps you could update your answer? :) – cmhughes Mar 4 '17 at 22:25
  • @cmhughes Any changelog for v3.0? Besides the second note that was already addressed in v2.2 dev. Could not find the changelog in your repo. Happy to update my answer then! – pat-s Mar 5 '17 at 21:15
  • Sure, no problem :) see github.com/cmhughes/latexindent.pl/pull/56 and github.com/cmhughes/latexindent.pl/commits/master. Let me know if you need anything else :) – cmhughes Mar 6 '17 at 18:21
  • Could this answer be updated with what to do for the perl.exe error for a windows machine? – Austin Downey Sep 19 '17 at 2:58
  • @cmhughes I finally updated my answer :) – pat-s Nov 10 '17 at 17:15
4

I would like to suggest a slight modification of Option 1) by pat-s.

At least in TeXstudio, under

Options -> Configure TeXstudio -> Build

you have the option to add a "User Command". Define a command latexindent -w %.tex. Save and return to your document, you can now click on

Tools -> User -> Your Command

This can also be called via shortcut, i.e. I use alt + shift + F1 to autoindent the complete document.

  • This was incredibly easy to do and worked first time! It took longer than expected and I questioned if I set it up correctly, so be warned that it may take a moment. – Greg Hilston Aug 4 at 21:58
  • In OSX, the menu is in Preferences -> Build I'm unable to get the command to run on OSX – Greg Hilston Aug 5 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy