1. Introduction

1.1. Thanks

I first created latexindent.pl to help me format chapter files in a big project. After I blogged about it on the TeX stack exchange (“A Perl Script for Indenting Tex Files” n.d.) I received some positive feedback and follow-up feature requests. A big thank you to Harish Kumar (Kumar 2013) who helped to develop and test the initial versions of the script.

The YAML-based interface of latexindent.pl was inspired by the wonderful arara tool; any similarities are deliberate, and I hope that it is perceived as the compliment that it is. Thank you to Paulo Cereda and the team for releasing this awesome tool; I initially worried that I was going to have to make a GUI for latexindent.pl, but the release of arara has meant there is no need.

There have been several contributors to the project so far (and hopefully more in the future!); thank you very much to the people detailed in Section 11 for their valued contributions, and thank you to those who report bugs and request features at (“Home of Latexindent.pl” n.d.).

1.2. License

latexindent.pl is free and open source, and it always will be; it is released under the GNU General Public License v3.0.

Before you start using it on any important files, bear in mind that latexindent.pl has the option to overwrite your .tex files. It will always make at least one backup (you can choose how many it makes, see page page:onlyonebackup) but you should still be careful when using it. The script has been tested on many files, but there are some known limitations (see Section 10). You, the user, are responsible for ensuring that you maintain backups of your files before running latexindent.pl on them. I think it is important at this stage to restate an important part of the license here:

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

There is certainly no malicious intent in releasing this script, and I do hope that it works as you expect it to; if it does not, please first of all make sure that you have the correct settings, and then feel free to let me know at (“Home of Latexindent.pl” n.d.) with a complete minimum working example as I would like to improve the code as much as possible.

Warning

Before you try the script on anything important (like your thesis), test it out on the sample files in the test-case directory (“Home of Latexindent.pl” n.d.).

If you have used any version 2.* of ``latexindent.pl``, there are a few changes to the interface; see :numref:`app:differences` and the comments throughout this document for details.

1.3. Quick start

If you’d like to get started with latexindent.pl then simply type

latexindent.pl myfile.tex

from the command line.

We give an introduction to latexindent.pl using Listing 1; there is no explanation in this section, which is deliberate for a quick start. The rest of the manual is more verbose.

Listing 1 quick-start.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}
\begin{document}
A quick start 
    demonstration for latexindent.pl.
    \begin{myenv}
      The body of environments and 
      other code blocks
        receive indentation.
    \end{myenv}
\end{document}

Running

latexindent.pl quick-start.tex

gives Listing 2.

Listing 2 quick-start-default.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}
\begin{document}
A quick start
demonstration for latexindent.pl.
\begin{myenv}
	The body of environments and
	other code blocks
	receive indentation.
\end{myenv}
\end{document}
Example 1

Running

latexindent.pl -l quick-start1.yaml quick-start.tex

gives Listing 3.

Listing 3 quick-start-mod1.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}
\begin{document}
A quick start
demonstration for latexindent.pl.
\begin{myenv}
 The body of environments and
 other code blocks
 receive indentation.
\end{myenv}
\end{document}
Listing 4 quick-start1.yaml
defaultIndent: " "

See Section 5.4.

Example 2

Running

latexindent.pl -l quick-start2.yaml quick-start.tex

gives Listing 5.

Listing 5 quick-start-mod2.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}
\begin{document}
A quick start
demonstration for latexindent.pl.
\begin{myenv}
   The body of environments and
   other code blocks
   receive indentation.
\end{myenv}
\end{document}
Listing 6 quick-start2.yaml
indentRules:
  myenv: "   "

See Section 5.8.

Example 3

Running

latexindent.pl -l quick-start3.yaml quick-start.tex

gives Listing 7.

Listing 7 quick-start-mod3.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}
\begin{document}
A quick start
demonstration for latexindent.pl.
\begin{myenv}
The body of environments and
other code blocks
receive indentation.
\end{myenv}
\end{document}
Listing 8 quick-start3.yaml
noAdditionalIndent:
  myenv: 1

See Section 5.8.

Example 4

Running

latexindent.pl -m -l quick-start4.yaml quick-start.tex

gives Listing 9.

Listing 9 quick-start-mod4.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}
\begin{document}
A quick start
demonstration for latexindent.pl.
\begin{myenv}
	The body of environments and
	other code blocks
	receive indentation.
\end{myenv}
\end{document}
Listing 10 quick-start4.yaml
modifyLineBreaks:
    textWrapOptions:
        columns: 20

Full details of text wrapping in Section 6.1.

Example 5

Running

latexindent.pl -m -l quick-start5.yaml quick-start.tex

gives Listing 11.

Listing 11 quick-start-mod5.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}
\begin{document}
A quick start
demonstration for latexindent.pl.
\begin{myenv}
	The body of
	environments and
	other code blocks
	receive
	indentation.
\end{myenv}
\end{document}
Listing 12 quick-start5.yaml
modifyLineBreaks:
    textWrapOptions:
        columns: 20
        blocksFollow:
           other: '\\begin\{myenv\}'

Full details of text wrapping in Section 6.1.

Example 6

Running

latexindent.pl -m -l quick-start6.yaml quick-start.tex

gives Listing 13.

Listing 13 quick-start-mod6.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}\begin{document}
A quick start
demonstration for latexindent.pl.\begin{myenv}
	The body of environments and
	other code blocks
	receive indentation.
\end{myenv}
\end{document}
Listing 14 quick-start6.yaml
modifyLineBreaks:
    environments:
        BeginStartsOnOwnLine: -1

This is an example of a poly-switch; full details of poly-switches are covered in Section 6.3.

Example 7

Running

latexindent.pl -m -l quick-start7.yaml quick-start.tex

gives Listing 15.

Listing 15 quick-start-mod7.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}
\begin{document}
A quick start
demonstration for latexindent.pl.
\begin{myenv}
	The body of environments and
	other code blocks
	receive indentation.\end{myenv}\end{document}
Listing 16 quick-start7.yaml
modifyLineBreaks:
    environments:
        EndStartsOnOwnLine: -1

Full details of poly-switches are covered in Section 6.3.

Example 8

Running

latexindent.pl -l quick-start8.yaml quick-start.tex

gives Listing 17; note that the preamble has been indented.

Listing 17 quick-start-mod8.tex
\documentclass{article}
\usepackage[
	inner=2.5cm,
]{geometry}
\begin{document}
A quick start
demonstration for latexindent.pl.
\begin{myenv}
	The body of environments and
	other code blocks
	receive indentation.
\end{myenv}
\end{document}
Listing 18 quick-start8.yaml
indentPreamble: 1

See Section 5.3.

Example 9

Running

latexindent.pl -l quick-start9.yaml quick-start.tex

gives Listing 19.

Listing 19 quick-start-mod9.tex
\documentclass{article}
\usepackage[
inner=2.5cm,
]{geometry}
\begin{document}
	A quick start
	demonstration for latexindent.pl.
	\begin{myenv}
		The body of environments and
		other code blocks
		receive indentation.
	\end{myenv}
\end{document}
Listing 20 quick-start9.yaml
noAdditionalIndent:
  document: 0

See Section 5.8.

1.4. Required perl modules

If you receive an error message such as that given in Listing 21, then you need to install the missing perl modules.

Listing 21 Possible error messages
Can't locate File/HomeDir.pm in @INC (@INC contains: /Library/Perl/5.12/darwin-thread-multi-2level /Library/Perl/5.12 /Network/Library/Perl/5.12/darwin-thread-multi-2level /Network/Library/Perl/5.12 /Library/Perl/Updates/5.12.4/darwin-thread-multi-2level /Library/Perl/Updates/5.12.4 /System/Library/Perl/5.12/darwin-thread-multi-2level /System/Library/Perl/5.12 /System/Library/Perl/Extras/5.12/darwin-thread-multi-2level /System/Library/Perl/Extras/5.12 .) at helloworld.pl line 10.
BEGIN failed--compilation aborted at helloworld.pl line 10.

latexindent.pl ships with a script to help with this process; if you run the following script, you should be prompted to install the appropriate modules.

perl latexindent-module-installer.pl

You might also like to see https://stackoverflow.com/questions/19590042/error-cant-locate-file-homedir-pm-in-inc, for example, as well as Section 12.1.

1.5. About this documentation

As you read through this documentation, you will see many listings; in this version of the documentation, there are a total of 621. This may seem a lot, but I deem it necessary in presenting the various different options of latexindent.pl and the associated output that they are capable of producing.

The different listings are presented using different styles:

Listing 22 demo-tex.tex
demonstration .tex file

This type of listing is a .tex file.

Listing 23 fileExtensionPreference
47fileExtensionPreference:
48  .tex: 1
49  .sty: 2
50  .cls: 3
51  .bib: 4

This type of listing is a .yaml file; when you see line numbers given (as here) it means that the snippet is taken directly from defaultSettings.yaml, discussed in detail in Section 5.

Listing 24 modifyLineBreaks
501modifyLineBreaks:
502  preserveBlankLines: 1                 # 0/1
503  condenseMultipleBlankLinesInto: 1     # 0/1

This type of listing is a .yaml file, but it will only be relevant when the -m switch is active; see Section 6 for more details.

Listing 25 replacements
621replacements:
622  - amalgamate: 1
623  - this: latexindent.pl
624    that: pl.latexindent
625    lookForThis: 0
626    when: before

This type of listing is a .yaml file, but it will only be relevant when the -r switch is active; see Section 7 for more details.

1.6. A word about regular expressions

As you read this documentation, you may encounter the term regular expressions. I’ve tried to write this documentation in such a way so as to allow you to engage with them or not, as you prefer. This documentation is not designed to be a guide to regular expressions, and if you’d like to read about them, I recommend (Friedl, n.d.).

“A Perl Script for Indenting Tex Files.” n.d. Accessed January 23, 2017. http://tex.blogoverflow.com/2012/08/a-perl-script-for-indenting-tex-files/.

Friedl, Jeffrey E. F. n.d. Mastering Regular Expressions.

“Home of Latexindent.pl.” n.d. Accessed January 23, 2017. https://github.com/cmhughes/latexindent.pl.

Kumar, Harish. 2013. “Early Version Testing.” November 10, 2013. https://github.com/harishkumarholla.