8. The –lines switch

latexindent.pl can operate on a selection of lines of the file using the –lines or -n switch.

The basic syntax is –lines MIN-MAX, so for example

latexindent.pl --lines 3-7 myfile.tex
latexindent.pl -n 3-7 myfile.tex

will only operate upon lines 3 to 7 in myfile.tex. All of the other lines will not be operated upon by latexindent.pl.

The options for the lines switch are:

  • line range, as in –lines 3-7
  • single line, as in –lines 5
  • multiple line ranges separated by commas, as in –lines 3-5,8-10
  • negated line ranges, as in –lines !3-5 which translates to –lines 1-2,6-N, where N is the number of lines in your file.

We demonstrate this feature, and the available variations in what follows. We will use the file in Listing 513.

Listing 513 myfile.tex
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Before the environments
\begin{one}
	first block, first line
	first block, second line
	first block, third line
	\begin{two}
		second block, first line
		second block, second line
		second block, third line
		second block, fourth line
	\end{two}
\end{one}
Example 31

We demonstrate the basic usage using the command

latexindent.pl --lines 3-7 myfile.tex -o=+-mod1

which instructs latexindent.pl to only operate on lines 3 to 7; the output is given in Listing 514.

Listing 514 myfile-mod1.tex
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Before the environments
\begin{one}
first block, first line
first block, second line
first block, third line
\begin{two}
second block, first line
		second block, second line
		second block, third line
		second block, fourth line
	\end{two}
\end{one}

The following two calls to latexindent.pl are equivalent

latexindent.pl --lines 3-7 myfile.tex -o=+-mod1
latexindent.pl --lines 7-3 myfile.tex -o=+-mod1

as latexindent.pl performs a check to put the lowest number first.

Example 32

You can call the lines switch with only one number and in which case only that line will be operated upon. For example

latexindent.pl --lines 5 myfile.tex -o=+-mod2

instructs latexindent.pl to only operate on line 5; the output is given in Listing 515.

Listing 515 myfile-mod2.tex
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Before the environments
\begin{one}
	first block, first line
	first block, second line
first block, third line
	\begin{two}
		second block, first line
		second block, second line
		second block, third line
		second block, fourth line
	\end{two}
\end{one}

The following two calls are equivalent:

latexindent.pl --lines 5 myfile.tex
latexindent.pl --lines 5-5 myfile.tex
Example 33

If you specify a value outside of the line range of the file then latexindent.pl will ignore the lines argument, detail as such in the log file, and proceed to operate on the entire file.

For example, in the following call

latexindent.pl --lines 11-13 myfile.tex

latexindent.pl will ignore the lines argument, and operate on the entire file because Listing 513 only has 12 lines.

Similarly, in the call

latexindent.pl --lines -1-3 myfile.tex

latexindent.pl will ignore the lines argument, and operate on the entire file because we assume that negatively numbered lines in a file do not exist.

Example 34

You can specify multiple line ranges as in the following

latexindent.pl --lines 3-5,8-10 myfile.tex -o=+-mod3

which instructs latexindent.pl to operate upon lines 3 to 5 and lines 8 to 10; the output is given in Listing 516.

Listing 516 myfile-mod3.tex
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Before the environments
\begin{one}
first block, first line
first block, second line
first block, third line
	\begin{two}
		second block, first line
second block, second line
second block, third line
second block, fourth line
	\end{two}
\end{one}

The following calls to latexindent.pl are all equivalent

latexindent.pl --lines 3-5,8-10 myfile.tex
latexindent.pl --lines 8-10,3-5 myfile.tex
latexindent.pl --lines 10-8,3-5 myfile.tex
latexindent.pl --lines 10-8,5-3 myfile.tex

as latexindent.pl performs a check to put the lowest line ranges first, and within each line range, it puts the lowest number first.

Example 35

There’s no limit to the number of line ranges that you can specify, they just need to be separated by commas. For example

latexindent.pl --lines 1-2,4-5,9-10,12 myfile.tex -o=+-mod4

has four line ranges: lines 1 to 2, lines 4 to 5, lines 9 to 10 and line 12. The output is given in Listing 517.

Listing 517 myfile-mod4.tex
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Before the environments
\begin{one}
	first block, first line
	first block, second line
	first block, third line
	\begin{two}
		second block, first line
		second block, second line
	second block, third line
	second block, fourth line
	\end{two}
\end{one}

As previously, the ordering does not matter, and the following calls to latexindent.pl are all equivalent

latexindent.pl --lines 1-2,4-5,9-10,12 myfile.tex
latexindent.pl --lines 2-1,4-5,9-10,12 myfile.tex
latexindent.pl --lines 4-5,1-2,9-10,12 myfile.tex
latexindent.pl --lines 12,4-5,1-2,9-10 myfile.tex

as latexindent.pl performs a check to put the lowest line ranges first, and within each line range, it puts the lowest number first.

Example 36

You can specify negated line ranges by using ! as in

latexindent.pl --lines !5-7 myfile.tex -o=+-mod5

which instructs latexindent.pl to operate upon all of the lines except lines 5 to 7.

In other words, latexindent.pl will operate on lines 1 to 4, and 8 to 12, so the following two calls are equivalent:

latexindent.pl --lines !5-7 myfile.tex
latexindent.pl --lines 1-4,8-12 myfile.tex

The output is given in Listing 518.

Listing 518 myfile-mod5.tex
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Before the environments
\begin{one}
	first block, first line
	first block, second line
	first block, third line
	\begin{two}
		second block, first line
	second block, second line
	second block, third line
	second block, fourth line
	\end{two}
\end{one}
Example 37

You can specify multiple negated line ranges such as

latexindent.pl --lines !5-7,!9-10 myfile.tex -o=+-mod6

which is equivalent to:

latexindent.pl --lines 1-4,8,11-12 myfile.tex -o=+-mod6

The output is given in Listing 519.

Listing 519 myfile-mod6.tex
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Before the environments
\begin{one}
	first block, first line
	first block, second line
	first block, third line
	\begin{two}
		second block, first line
	second block, second line
		second block, third line
		second block, fourth line
	\end{two}
\end{one}
Example 38

If you specify a line range with anything other than an integer, then latexindent.pl will ignore the lines argument, and operate on the entire file.

Sample calls that result in the lines argument being ignored include the following:

latexindent.pl --lines 1-x myfile.tex
latexindent.pl --lines !y-3 myfile.tex
Example 39

We can, of course, use the lines switch in combination with other switches.

For example, let’s use with the file in Listing 520.

Listing 520 myfile1.tex
1
2
3
4
5
6
7
Before the environments
\begin{one}
	first block, first line
	first block, second line
	first block, third line
	\begin{two} body \end{two}
\end{one}

We can demonstrate interaction with the -m switch (see Section 6); in particular, if we use Listing 415, Listing 399 and Listing 400 and run

latexindent.pl --lines 6 myfile1.tex -o=+-mod1 -m -l env-mlb2,env-mlb7,env-mlb8 -o=+-mod1

then we receive the output in Listing 521.

Listing 521 myfile1-mod1.tex
1
2
3
4
5
6
7
8
9
Before the environments
\begin{one}
	first block, first line
	first block, second line
	first block, third line
\begin{two}
	body
\end{two}
\end{one}