latexindent.pl

Perl script to add indentation (leading horizontal space) to LaTeX files. It can modify line breaks before, during and after code blocks; it can perform text wrapping and paragraph line break removal. It can also perform string-based and regex-based substitutions/replacements. The script is customisable through its YAML interface.

GPL-3.0 License

Downloads
462
Stars
833
Committers
38

Bot releases are visible (Hide)

latexindent.pl - V3.13

Published by github-actions[bot] almost 3 years ago

This release implements an upgrade to the verbatim blocks, and friends. Thank you to @XuehaiPan for this contribution.
See https://github.com/cmhughes/latexindent.pl/issues/288 for discussion and history.

Summary

  1. Anything specified within noIndentBlock, verbatimEnvironments and verbatimCommands can now be specified in the form
verbatimEnvironments:
       nameAsRegex:
         name: '\w+code\*?' 
         lookForThis: 1

  1. the lookForThis field is optional, and if not present, will assumed to be 1 (this is consistent with other settings in the script)
  2. we have used nameAsRegex but it could be named as anything, for example, it could be named mintedalias
verbatimEnvironments:
       mintedalias:
         name: '\w+code\*?' 
         lookForThis: 1

  1. if the users code only contains \begin{pythoncode}...\end{pythoncode} then the above YAML settings is equivalent to
verbatimEnvironments:
       pythoncode: 1
  1. For noIndentBlock users can specify begin and end (both) or only name:

noIndentBlock:
  beginend:
    begin: regex_begin
    end: regex_end
    body: regex_body  # optional

  nameonly:
    name: regex_name
    body: regex_body  # optional

  # the above is equivalent to the follows when `regex_name` does not contain `'*'` and body is omitted
  regex_name: 1
  1. The following settings are examples of incomplete/conflicted settings, which will be ignored:
  # the followings are ignored
  none:  # incomplete settings, ignored
    lookForThis: 1

  beginonly:  # incomplete settings, ignored
    begin: regex_begin

  endonly:  # incomplete settings, ignored
    end: regex_end

  namebeginend:  # conflict settings, ignored
    begin: regex_begin
    end: regex_end
    name: regex_name

  namebegin:  # conflict settings, ignored
    begin: regex_begin
    name: regex_name

  nameend:  # conflict settings, ignored
    end: regex_end
    name: regex_name
latexindent.pl - V3.12

Published by github-actions[bot] about 3 years ago

This release implements two new features:

  • check switch available, full details at issue 215
  • lines switch available, full details at issue 247

Additionally, mainDocument is now used as a preference instead of masterDocument for the textWrap routine, although masterDocument is still supported and will be for all V3.* of latexindent.pl.

check switch: interface

  1. new switch: -k or --check which checks if the indented text is different from the original; a 'poor man's diff' is given to the log file
  2. new switch: -kv or --checkv which does the same thing as the -k switch, but additionally outputs the 'poor man's diff' to the terminal.

For both switches, then the exit codes are as you detailed in the original post; explicitly:

  • latexindent.pl will exit with 0 if the indented text is not different
  • latexindent.pl will exit with 1 if the indented text is different

check switch: demonstration 1

Let's begin with the following file, say myfile.tex

here is a nested test
\begin{one}
	to be nested to be nested
	to be nested to be nested
	to be nested to be nested
	\begin{two}
		nested text nested tex nested tex
		nested text nested tex nested tex
		nested text nested tex nested tex
		nested text nested tex nested tex
	\end{two}
\end{one}
between nests
between nests
between nests
\begin{one}
	to be nested to be nested one   
	to be nested to be nested       two
	to be nested to be nested           three
\end{one}
here is a nested test
\begin{one}
	to be nested to be nested
	to be nested to be nested
	to be nested to be nested
	\begin{two}
		nested text nested tex nested tex
		nested text nested tex nested tex
		nested text nested tex nested tex
		nested text nested tex nested tex
	\end{two}
\end{one}
more
    of this
  again
        and again

If we run

latexindent.pl  --check myfile.tex -s -y "defaultIndent: '  '"
echo $?

Then, crucially, the exit code is 1

If we run

latexindent.pl  --checkv myfile.tex -s -y "defaultIndent: '  '"
echo $?

then the 'simple diff' is output as follows:

       @@ 3 -- 11 @@
       -	to be nested to be nested
       -	to be nested to be nested
       -	to be nested to be nested
       -	\begin{two}
       -		nested text nested tex nested tex
       -		nested text nested tex nested tex
       -		nested text nested tex nested tex
       -		nested text nested tex nested tex
       -	\end{two}
       +  to be nested to be nested
       +  to be nested to be nested
       +  to be nested to be nested
       +  \begin{two}
       +    nested text nested tex nested tex
       +    nested text nested tex nested tex
       +    nested text nested tex nested tex
       +    nested text nested tex nested tex
       +  \end{two}
       @@ 17 -- 19 @@
       -	to be nested to be nested one   
       -	to be nested to be nested       two
       -	to be nested to be nested           three
       +  to be nested to be nested one
       +  to be nested to be nested       two
       +  to be nested to be nested           three
       @@ 23 -- 31 @@
       -	to be nested to be nested
       -	to be nested to be nested
       -	to be nested to be nested
       -	\begin{two}
       -		nested text nested tex nested tex
       -		nested text nested tex nested tex
       -		nested text nested tex nested tex
       -		nested text nested tex nested tex
       -	\end{two}
       +  to be nested to be nested
       +  to be nested to be nested
       +  to be nested to be nested
       +  \begin{two}
       +    nested text nested tex nested tex
       +    nested text nested tex nested tex
       +    nested text nested tex nested tex
       +    nested text nested tex nested tex
       +  \end{two}
       @@ 34 -- 36 @@
       -    of this
       -  again
       -        and again
       +of this
       +again
       +and again

--lines switch available syntax

  • 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;

lines switch: demonstration 2

Starting with

here is a nested test
\begin{one}
		first block, second line
		first block, third line
		first block, fourth line
	\begin{two}
		second block, first line
		second block, second line
		second block, third line
		second block, fourth line
	\end{two}
\end{one}

and running

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

gives

here is a nested test
\begin{one}
first block, second line
first block, third line
first block, fourth line
	\begin{two}
		second block, first line
		second block, second line
		second block, third line
		second block, fourth line
	\end{two}
\end{one}

lines switch: demonstration 3

Running

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

gives

here is a nested test
\begin{one}
first block, second line
first block, third line
first block, fourth line
	\begin{two}
		second block, first line
second block, second line
second block, third line
second block, fourth line
	\end{two}
\end{one}
latexindent.pl - V3.11

Published by github-actions[bot] about 3 years ago

Upgrade to textWrap

This release implements an upgrade to textWrap: in particular, a new feature beforeFindingChildCodeBlocks to be
used with perCodeBlockBasis.

This was implemented in relation to the issues raised in issue 158
and issue 228. Demonstrations are given within these threads, and are documented.

GitHub Actions

This has been implemented to create and publish latexindent.exe automatically. Hopefully
more automated actions will follow.

latexindent.pl - V3.10.1

Published by cmhughes about 3 years ago

changes to defaults:

  • textWrap: Huge now set to overflow by default; this means that words and other strings of characters will not be broken mid-word by the text wrapping routine. Users are encouraged not to change this, but have the option to do so.

enhancements:

example: issue 85

from https://github.com/cmhughes/latexindent.pl/issues/85 starting with

\matrix {
c01 & c02 \\
c_{11} & c12 \\
}

gives, by default

\matrix {
	c01    & c02 \\
	c_{11} & c12 \\
}

example: issue 162

from https://github.com/cmhughes/latexindent.pl/issues/162 starting with

\begin{align}
A & =\begin{array}{cc}
BBB & CCC\\
E & F
\end{array}\\
Z & =\begin{array}{cc}
Y & X\\
W & V
\end{array}
\end{align}

gives, by default,

\begin{align}
	A & =\begin{array}{cc}
		     BBB & CCC \\
		     E   & F
	     \end{array} \\
	Z & =\begin{array}{cc}
		     Y & X \\
		     W & V
	     \end{array}
\end{align}

example: issue 212

from https://github.com/cmhughes/latexindent.pl/issues/212 starting with

\begin{equation}
    \begin{aligned}
        x =& x^2 \mqty[a_{11}  & a_{12} \\ a_{21} & a_{22}] =& f(x) \\
        y =& f(y) =& y^2 \\
        z =& f(z) =& z^2
    \end{aligned}
\end{equation}

gives, by default,

\begin{equation}
	\begin{aligned}
		x = & x^2 \mqty[a_{11} & a_{12} \\ a_{21} & a_{22}] =& f(x) \\
		y = & f(y) =           & y^2    \\
		z = & f(z) =           & z^2
	\end{aligned}
\end{equation}

example: issue 251

from https://github.com/cmhughes/latexindent.pl/issues/251 starting with

\begin{tabular}{ll}
  Testing & Line 1                        \\
  Testing & Line 2                        \\
  Testing & Line 3 \verb|X| \\
  Testing & Line 4                        \\
\end{tabular}

gives, by default,

\begin{tabular}{ll}
	Testing & Line 1          \\
	Testing & Line 2          \\
	Testing & Line 3 \verb|X| \\
	Testing & Line 4          \\
\end{tabular}
latexindent.pl - V3.10

Published by cmhughes over 3 years ago

enhancements

bug fix

demonstration 1: fineTuning trailing comments

starting with

some before text
 \href{Handbook%20for%30Spoken%40document.pdf}{my document}
some after text 

and using

modifyLineBreaks:
    textWrapOptions:
        columns: 80
        huge: overflow
        all: 1
        perCodeBlockBasis: 1
    removeParagraphLineBreaks:
        all: 1

fineTuning:
    trailingComments:
      notPreceededBy: '(?:(?<!Handbook)(?<!for)(?<!Spoken))'

then the output is (using -m switch):

some before text \href{Handbook%20for%30Spoken%40document.pdf}{my document} some after text

demonstration 2: noIndentBlock with begin and end

starting with

some before text
        this code
                won't
     be touched
                    by
             latexindent.pl!
some after text 

and using

noIndentBlock:
    demo:
        begin: 'some\hbefore'
        body: '.*?'
        end: 'some\hafter\htext'
        lookForThis: 1

gives the output

some before text
        this code
                won't
     be touched
                    by
             latexindent.pl!
some after text

Note that lookForThis is optional in the noIndentBlock field; default is 1.

demonstration 3: spacesBeforeAmpersand

Starting with

\begin{equation}
    \begin{aligned}
    & a = b, \\
    & c = d.
    \end{aligned}
\end{equation}

\begin{equation}
    \begin{aligned}
    a & = b, \\
    c & = d.
    \end{aligned}
\end{equation}

and using

defaultIndent: ' '

noAdditionalIndent:
  aligned: 1

lookForAlignDelims:
   aligned: 
      spacesBeforeAmpersand: 
        default: 2
        leadingBlankColumn: 0

then the result is

\begin{equation}
 \begin{aligned}
 & a = b, \\
 & c = d.
 \end{aligned}
\end{equation}

\begin{equation}
 \begin{aligned}
 a  & = b, \\
 c  & = d.
 \end{aligned}
\end{equation}

The fields default and leadingBlankColumn both take integer values that represent the spaces to be added before ampersands. They are both optional.

latexindent.pl - V3.9.3

Published by cmhughes over 3 years ago

A minor release that updates log file creation https://github.com/cmhughes/latexindent.pl/issues/145; in particular, users can specify, for example,

latexindent.pl -g /dev/null myfile.tex

so that the log file will not be created. Prior to this release, latexindent.pl would fail if the log file could not be created.

The other minor tweak to the logging procedure is that the FATAL method has been created, for situations in which latexindent is called to act on files that do not exist.

latexindent.pl - V3.9.2

Published by cmhughes over 3 years ago

A minor release to include the bugfix for the noindent block bug reported at #266

latexindent.pl - V3.9.1

Published by cmhughes over 3 years ago

Minor updates to documentation, details at https://github.com/cmhughes/latexindent.pl/issues/255 and https://github.com/cmhughes/latexindent.pl/issues/259

Minor updates to fineTuning:

  • keyEqualsValuesBracesBrackets, https://github.com/cmhughes/latexindent.pl/issues/257 which means that, by default, keyEqualsValuesBracesBrackets no longer begin with braces.
  • namedGroupingBracesBrackets, https://github.com/cmhughes/latexindent.pl/issues/258 now starts with a lower-case 'n' in fineTuning so as to be consistent with its appearance elsewhere in defaultSettings.yaml. As this was my mistake, LatexIndent/NamedGroupingBracesBrackets.pm will read from NamedGroupingBracesBrackets (note the upper-case 'N') and a warning is given to the logfile. I plan to support both fineTuning:namedGroupingBracesBrackets and fineTuning:NamedGroupingBracesBrackets for V3.*, but if V4.0 is created, then only namedGroupingBracesBrackets will be supported. I recommend users migrate to namedGroupingBracesBrackets
latexindent.pl - V3.9

Published by cmhughes over 3 years ago

V3.9, March 14, 2021

Small enhancements:

  • https://github.com/cmhughes/latexindent.pl/issues/242, update to -l switch so that instead of only looking for localSettings.yaml, it will also look for (in order): latexindent.yaml, .localSettings.yaml and .latexindent.yaml. Note that you don't need to have all 4, and that for most uses, just 1 of these files should be enough. As previously, using -l=myfile.yaml will only load myfile.yaml and now using -l=myfile.yaml+ will assume that you want to load as many of the following that exist: myfile.yaml, localSettings.yaml, latexindent.yaml, .localSettings.yaml, .latexindent.yaml
  • https://github.com/cmhughes/latexindent.pl/issues/239, update to commandCodeBlocks

bug fixes:

latexindent.pl - V3.8.3

Published by cmhughes almost 4 years ago

This is a minor release that implements fixes for the following issues:

The documentation has been updated with appropriate examples, including details of how to produce alignment such as the following:

\begin{tikzpicture}
	\path (A) edge              node {0,1,L}(B)
	          edge              node {1,1,R} (C)
	      (B) edge [loop above] node {1,1,L}(B)
	          edge              node {0,1,L}(C)
	      (C) edge              node {0,1,L}(D)
	          edge [bend left]  node {1,0,R}(E)
	      (D) edge [loop below] node {1,1,R}(D)
	          edge              node {0,1,R}(A)
	      (E) edge [bend left]  node {1,0,R} (A);
\end{tikzpicture}

and a patch to the textWrap routine, so that spaces are preserved (or not) according to the new field tabstop

x       y

Settings

modifyLineBreaks:
    textWrapOptions:
        columns: 80
        tabstop: 9

Pull request, for reference: https://github.com/cmhughes/latexindent.pl/pull/227

latexindent.pl - V3.8.2

Published by cmhughes over 4 years ago

latexindent.pl -

Published by cmhughes over 4 years ago

latexindent.pl -

Published by cmhughes over 4 years ago

latexindent.pl -

Published by cmhughes over 4 years ago

latexindent.pl -

Published by cmhughes over 4 years ago

latexindent.pl -

Published by cmhughes over 4 years ago

latexindent.pl -

Published by cmhughes over 4 years ago

latexindent.pl -

Published by cmhughes over 4 years ago

latexindent.pl -

Published by cmhughes over 4 years ago

latexindent.pl -

Published by cmhughes over 4 years ago