Autoindent in Texmaker?

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.


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]


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.