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 585.

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

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 586.

Listing 586 myfile-mod1.tex
 1Before the environments
 2\begin{one}
 3first block, first line
 4first block, second line
 5first block, third line
 6\begin{two}
 7second block, first line
 8		second block, second line
 9		second block, third line
10		second block, fourth line
11	\end{two}
12\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 156

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 587.

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

The following two calls are equivalent:

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

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 585 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 158

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 588.

Listing 588 myfile-mod3.tex
 1Before the environments
 2\begin{one}
 3first block, first line
 4first block, second line
 5first block, third line
 6	\begin{two}
 7		second block, first line
 8second block, second line
 9second block, third line
10second block, fourth line
11	\end{two}
12\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 159

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 589.

Listing 589 myfile-mod4.tex
 1Before the environments
 2\begin{one}
 3	first block, first line
 4	first block, second line
 5	first block, third line
 6	\begin{two}
 7		second block, first line
 8		second block, second line
 9	second block, third line
10	second block, fourth line
11	\end{two}
12\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 160

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 590.

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

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 591.

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

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 163

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

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

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

We can demonstrate interaction with the -m switch (see Section 6); in particular, if we use Listing 479, Listing 463 and Listing 464 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 593.

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