TransWikia.com

How to make latexindent align columns in nested environments?

TeX - LaTeX Asked on March 11, 2021

The following MWE is the result of running latexindent with default settings:

documentclass[border=1in, preview]{standalone}

usepackage{amsmath}
usepackage{mathtools}
usepackage{nicematrix}

DeclarePairedDelimiterabs{lvert}{rvert}%

begin{document}
First, we obtain the eigenvalues:
begin{align*}
    abs*{
        begin{bNiceMatrix}
            4 - lambda & -1 & -1
            1 & 2 - lambda & - 1
            1 & -1 & 2 - lambda
        end{bNiceMatrix}
    } & = (4 - lambda) abs*{
        begin{bNiceMatrix}
            2 - lambda & -1
            -1 & 2 - lambda
        end{bNiceMatrix}
    } + abs*{
        begin{bNiceMatrix}
            1 & - 1
            1 & 2 - lambda
        end{bNiceMatrix}
    } - abs*{
        begin{bNiceMatrix}
            1 & 2 - lambda
            1 & -1
        end{bNiceMatrix}
    }                                                                               
      & = (4 - lambda)(lambda^2 - 4 lambda + 3) + (-lambda + 3) + (3 - lambda) 
      & = -lambda^3 + 8 lambda^2 - 21 lambda + 18                                
      & = -(lambda - 2)(lambda - 3)^2
end{align*}
end{document}

As you can see, only the alignment symbols of the align environment were aligned in the code after running latexindent. The alignment symbols within the bNiceMatrix environments (i.e., the first and second ones) were ignored. Is there a way to make them aligned in the code as well?

PDF output for reference:
minimal working example

EDIT: Note that I included the PDF output to give more context to the MWE, but my question is about code formatting using latexindent, not document layout. I apologize for the confusion.

One Answer

We need to tell latexindent.pl to look for alignment delimiters in the bNiceMatrix, as detailed in https://latexindentpl.readthedocs.io/en/latest/sec-default-user-local.html#lst-aligndelims-advanced

Starting with your .tex file, then we employ the following YAML file, say john.yaml:

lookForAlignDelims:
   bNiceMatrix: 1

and then we run

latexindent.pl -l john.yaml mwe.tex

The output is as follows:

documentclass[border=1in, preview]{standalone}

usepackage{amsmath}
usepackage{mathtools}
usepackage{nicematrix}

DeclarePairedDelimiterabs{lvert}{rvert}%

begin{document}
First, we obtain the eigenvalues:
begin{align*}
    abs*{
        begin{bNiceMatrix}
            4 - lambda & -1          & -1          
            1           & 2 - lambda & - 1         
            1           & -1          & 2 - lambda
        end{bNiceMatrix}
    } & = (4 - lambda) abs*{
        begin{bNiceMatrix}
            2 - lambda & -1          
            -1          & 2 - lambda
        end{bNiceMatrix}
    } + abs*{
        begin{bNiceMatrix}
            1 & - 1         
            1 & 2 - lambda
        end{bNiceMatrix}
    } - abs*{
        begin{bNiceMatrix}
            1 & 2 - lambda 
            1 & -1
        end{bNiceMatrix}
    }                                                                               
      & = (4 - lambda)(lambda^2 - 4 lambda + 3) + (-lambda + 3) + (3 - lambda) 
      & = -lambda^3 + 8 lambda^2 - 21 lambda + 18                                
      & = -(lambda - 2)(lambda - 3)^2
end{align*}
end{document}

You can achieve the same result without having to create a YAML file, by using the -y switch as in the following:

latexindent.pl -y lookForAlignDelims:bNiceMatrix:1 mwe.tex

about nesting alignment

The specific question about nested alignment and the customisation thereof is a known issue; see https://latexindentpl.readthedocs.io/en/latest/sec-conclusions-know-limitations.html for example. I hope to be able to address this at some point.

Correct answer by cmhughes on March 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP