cmhughes / 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.
GNU General Public License v3.0
884 stars 84 forks source link

Indentation of last argument in mandatory arguments of tabularray (new interface) #378

Closed kiryph closed 2 years ago

kiryph commented 2 years ago

original .tex code

from the documentation of tabularray: Section 2.4.1 Cells and Spancells in New Interfaces, page 18.

\documentclass{article}
\usepackage{tabularray}

\begin{document}
\begin{tblr}{
  hlines = {white},
  vlines = {white},
  cell{1,6}{odd} = {teal7},
  cell{1,6}{even} = {green7},
  cell{2,4}{1,4} = {red7},
  cell{3,5}{1,4} = {purple7},
  cell{2}{2} = {r=4,c=2}{c,azure7},
}
  Alpha   & Beta  & Gamma   & Delta   \\
  Epsilon & Zeta  & Eta     & Theta   \\
  Iota    & Kappa & Lambda  & Mu      \\
  Nu      & Xi    & Omicron & Pi      \\
  Rho     & Sigma & Tau     & Upsilon \\
  Phi     & Chi   & Psi     & Omega   \\
\end{tblr}
\end{document}

default yaml settings

❯ latexindent -vv
3.18, 2022-06-12
latexindent.pl lives here: /usr/local/Cellar/latexindent/3.18/libexec/bin/latexindent.pl
defaultSettings.yaml lives here /usr/local/Cellar/latexindent/3.18/libexec/bin/defaultSettings.yaml
project home: https://github.com/cmhughes/latexindent.pl

actual/given output

\documentclass{article}
\usepackage{tabularray}

\begin{document}
\begin{tblr}{
    hlines = {white},
    vlines = {white},
    cell{1,6}{odd} = {teal7},
    cell{1,6}{even} = {green7},
    cell{2,4}{1,4} = {red7},
    cell{3,5}{1,4} = {purple7},
            cell{2}{2} = {r=4,c=2}{c,azure7},
        }
    Alpha   & Beta  & Gamma   & Delta   \\
    Epsilon & Zeta  & Eta     & Theta   \\
    Iota    & Kappa & Lambda  & Mu      \\
    Nu      & Xi    & Omicron & Pi      \\
    Rho     & Sigma & Tau     & Upsilon \\
    Phi     & Chi   & Psi     & Omega   \\
\end{tblr}
\end{document}

desired or expected output

the last mandatory argument and the closing } of the mandatory arguments should not get extra indentation.

\documentclass{article}
\usepackage{tabularray}

\begin{document}
\begin{tblr}{
    hlines = {white},
    vlines = {white},
    cell{1,6}{odd} = {teal7},
    cell{1,6}{even} = {green7},
    cell{2,4}{1,4} = {red7},
    cell{3,5}{1,4} = {purple7},
    cell{2}{2} = {r=4,c=2}{c,azure7},
    }
    Alpha   & Beta  & Gamma   & Delta   \\
    Epsilon & Zeta  & Eta     & Theta   \\
    Iota    & Kappa & Lambda  & Mu      \\
    Nu      & Xi    & Omicron & Pi      \\
    Rho     & Sigma & Tau     & Upsilon \\
    Phi     & Chi   & Psi     & Omega   \\
\end{tblr}
\end{document}

I am not sure whether the closing } of the mandatory arguments should have no indentation at all (see original code). Is there an option for this?

cmhughes commented 2 years ago

Thanks for this, it looks interesting! I appreciate you using the issue template, that's really helpful.

I hope to get to this over the next few days, and I'll post back with details.

cmhughes commented 2 years ago

Thanks, this was interesting!

solution

Put the following in latexindent.yaml

noAdditionalIndent:
    tblr: 
        mandatoryArguments: 1

fineTuning:
    keyEqualsValuesBracesBrackets:
      name: |-
          (?x)
          [                 #        |           
             a-z            #        |
             A-Z            #  at least one of these
             0-9            #        | 
             @\*            #        | 
             _\/.:\#\-      #        | 
          ]+                #        | 
          [                 #    <
             a-z            #    <
             A-Z            #    <
             0-9            #  0 or more of these
             @\*            #    <
             _\/.           #    <
             \h\{\}:\#\-    #    <
             ,              # <!-------NEW BIT: allow commas
          ]*?               #

and run

latexindent.pl -l myfile.tex

and you receive your desired output

\documentclass{article}
\usepackage{tabularray}

\begin{document}
\begin{tblr}{
    hlines = {white},
    vlines = {white},
    cell{1,6}{odd} = {teal7},
    cell{1,6}{even} = {green7},
    cell{2,4}{1,4} = {red7},
    cell{3,5}{1,4} = {purple7},
    cell{2}{2} = {r=4,c=2}{c,azure7},
    }
    Alpha   & Beta  & Gamma   & Delta   \\
    Epsilon & Zeta  & Eta     & Theta   \\
    Iota    & Kappa & Lambda  & Mu      \\
    Nu      & Xi    & Omicron & Pi      \\
    Rho     & Sigma & Tau     & Upsilon \\
    Phi     & Chi   & Psi     & Omega   \\
\end{tblr}
\end{document}

explanation

The part that causes the default behaviour to struggle is the lines

cell{1,6}{odd} = {teal7},

From latexindent.pl point of view, this is not a key=<braces/brackets> code block, because, by default, commas are not allowed in the "name" part. So, we change the default by using the fineTuning field. So, using

fineTuning:
    keyEqualsValuesBracesBrackets:
      name: |-
          (?x)
          [                 #        |           
             a-z            #        |
             A-Z            #  at least one of these
             0-9            #        | 
             @\*            #        | 
             _\/.:\#\-      #        | 
          ]+                #        | 
          [                 #    <
             a-z            #    <
             A-Z            #    <
             0-9            #  0 or more of these
             @\*            #    <
             _\/.           #    <
             \h\{\}:\#\-    #    <
             ,              # <!-------NEW BIT: allow commas
          ]*?               #

gives

\documentclass{article}
\usepackage{tabularray}

\begin{document}
\begin{tblr}{
        hlines = {white},
        vlines = {white},
        cell{1,6}{odd} = {teal7},
        cell{1,6}{even} = {green7},
        cell{2,4}{1,4} = {red7},
        cell{3,5}{1,4} = {purple7},
        cell{2}{2} = {r=4,c=2}{c,azure7},
    }
    Alpha   & Beta  & Gamma   & Delta   \\
    Epsilon & Zeta  & Eta     & Theta   \\
    Iota    & Kappa & Lambda  & Mu      \\
    Nu      & Xi    & Omicron & Pi      \\
    Rho     & Sigma & Tau     & Upsilon \\
    Phi     & Chi   & Psi     & Omega   \\
\end{tblr}
\end{document}

This is good, but not exactly what you wanted; to remove the indentation from the mandatory argument, we modify https://latexindentpl.readthedocs.io/en/latest/sec-default-user-local.html#lst-myenv-noadd5 and use

noAdditionalIndent:
    tblr: 
        mandatoryArguments: 1

fineTuning:
    keyEqualsValuesBracesBrackets:
      name: |-
          (?x)
          [                 #        |           
             a-z            #        |
             A-Z            #  at least one of these
             0-9            #        | 
             @\*            #        | 
             _\/.:\#\-      #        | 
          ]+                #        | 
          [                 #    <
             a-z            #    <
             A-Z            #    <
             0-9            #  0 or more of these
             @\*            #    <
             _\/.           #    <
             \h\{\}:\#\-    #    <
             ,              # <!-------NEW BIT: allow commas
          ]*?               #

and then receive

\documentclass{article}
\usepackage{tabularray}

\begin{document}
\begin{tblr}{
    hlines = {white},
    vlines = {white},
    cell{1,6}{odd} = {teal7},
    cell{1,6}{even} = {green7},
    cell{2,4}{1,4} = {red7},
    cell{3,5}{1,4} = {purple7},
    cell{2}{2} = {r=4,c=2}{c,azure7},
    }
    Alpha   & Beta  & Gamma   & Delta   \\
    Epsilon & Zeta  & Eta     & Theta   \\
    Iota    & Kappa & Lambda  & Mu      \\
    Nu      & Xi    & Omicron & Pi      \\
    Rho     & Sigma & Tau     & Upsilon \\
    Phi     & Chi   & Psi     & Omega   \\
\end{tblr}
\end{document}
cmhughes commented 2 years ago

Let me know if you need any further details on this.

kiryph commented 2 years ago

Thanks for your help. Sorry for the delayed reply.