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
864 stars 84 forks source link

[Bug] Incorrect indentation amount when have a long config with tabualrray package #541

Closed Mikachu2333 closed 3 months ago

Mikachu2333 commented 3 months ago

When using the tabularray package, latexindent always incorrectly indents the configuration section of the tabularray, as shown in the following example.

MWE (After formatted)

\documentclass{ctexrep}%Chinese report
\usepackage{tabularray}%tabular

\begin{document}
\begin{tblr}[long,
    label= {market_fuyanshan}
    ]{
    cells = {c,m},
    hline{1-2,5}= {-}{1pt},
            hline{3-4} = {-}{},
            vlines,
            row{1} = {cmd=\bfseries},
    }
    XXX & YYY & ZZZ \\
    AAA & DDD & GGG \\
    BBB & EEE & HHH \\
    CCC & FFF & III
\end{tblr}
\end{document}

VSCode Latex Workshop settings

After testing, this bug is not related to parameters such as --GCString and --yaml=...

[
"latex-workshop.latexindent.args": [
        "--cruft=%DIR%/",
        "%TMPFILE%",
        "--modifylinebreaks",
        "--GCString",
        "--yaml=defaultIndent: '    '"
    ]
]

Output

Success

[09:54:23.646][Format][TeX] Formatting LaTeX. The command is latexindent:["--cruft=%WS1%/","%WS1%/__latexindent_temp_main.tex","--modifylinebreaks","--GCString","--yaml=defaultIndent: '    '"].
[09:54:23.740][Format][TeX] Formatted %WS1%/main.tex

desired or expected output

Lines from 9 to 11 should not be indneted with 2*defaultIndent as the following shows.

\documentclass{ctexrep}%Chinese report
\usepackage{tabularray}%tabular

\begin{document}
\begin{tblr}[long,
    label= {market_fuyanshan}]{
    cells = {c,m},
    hline{1-2,5}= {-}{1pt},
    hline{3-4} = {-}{},
    vlines,
    row{1} = {cmd=\bfseries},
    }
    XXX & YYY & ZZZ \\
    AAA & DDD & GGG \\
    BBB & EEE & HHH \\
    CCC & FFF & III
\end{tblr}
\end{document}
cmhughes commented 3 months ago

Apologies for the delay I'm hoping to get to this soon

cmhughes commented 3 months ago

starting with

\documentclass{ctexrep}%Chinese report
\usepackage{tabularray}%tabular

\begin{document}
\begin{tblr}[long,
    label= {market_fuyanshan}
    ]{
    cells = {c,m},
    hline{1-2,5}= {-}{1pt},
            hline{3-4} = {-}{},
            vlines,
            row{1} = {cmd=\bfseries},
    }
    XXX & YYY & ZZZ \\
    AAA & DDD & GGG \\
    BBB & EEE & HHH \\
    CCC & FFF & III
\end{tblr}
\end{document}

and using

fineTuning:
  keyEqualsValuesBracesBrackets:
    name: |-
      (?x)
      [a-zA-Z@\*0-9_\/.:\#-]+
      (?:
        [a-zA-Z@\*0-9_\/.\h:\#-]
        \{
          [a-zA-Z@\*0-9_\/.\h:\#-,]+?
        \}
      )*?

gives the following


\documentclass{ctexrep}%Chinese report
\usepackage{tabularray}%tabular

\begin{document}
\begin{tblr}[long,
        label= {market_fuyanshan}
    ]{
        cells = {c,m},
        hline{1-2,5}= {-}{1pt},
        hline{3-4} = {-}{},
        vlines,
        row{1} = {cmd=\bfseries},
    }
    XXX & YYY & ZZZ \\
    AAA & DDD & GGG \\
    BBB & EEE & HHH \\
    CCC & FFF & III
\end{tblr}
\end{document}
Mikachu2333 commented 3 months ago
\begin{tblr}[long,
      label= {market_fuyanshan}
  ]

Sry for bother, I'd like to know if it is possible to format the content inside the first square brackets ([ and ], e.g. text \begin{tblr}[long,) as well, as shown below.

(As a roob in regex, I could hardly understand the docs for too few examples... ☹)

\begin{tblr}[
        long,
        label= {market_fuyanshan}
    ]{
        cells = {c,m},
        hline{1-2,5}= {-}{1pt},
        hline{3-4} = {-}{},
        vlines,
        row{1} = {cmd=\bfseries},
    }
    XXX & YYY & ZZZ \\
    AAA & DDD & GGG \\
    BBB & EEE & HHH \\
    CCC & FFF & III
\end{tblr}

Besides, maybe the ]{ should also be standardized in one of the situation as follows. Sometimes,

]
{

sometimes ]{

cmhughes commented 3 months ago

Try the following

modifyLineBreaks:
  optionalArguments:
    tblr:
      OptArgBodyStartsOnOwnLine: 1        # -1,0,1,2,3,4
fineTuning:
  keyEqualsValuesBracesBrackets:
    name: |-
      (?x)
      [a-zA-Z@\*0-9_\/.:\#-]+
      (?:
        [a-zA-Z@\*0-9_\/.\h:\#-]
        \{
          [a-zA-Z@\*0-9_\/.\h:\#-,]+?
        \}
      )*?

which gives

\documentclass{ctexrep}%Chinese report
\usepackage{tabularray}%tabular

\begin{document}
\begin{tblr}[
        long,
        label= {market_fuyanshan}
    ]{
        cells = {c,m},
        hline{1-2,5}= {-}{1pt},
        hline{3-4} = {-}{},
        vlines,
        row{1} = {cmd=\bfseries},
    }
    XXX & YYY & ZZZ \\
    AAA & DDD & GGG \\
    BBB & EEE & HHH \\
    CCC & FFF & III
\end{tblr}
\end{document}
Mikachu2333 commented 1 month ago

Try the following

Sorry for bother, I met some problems when useing it.

Original / Formatted

\documentclass{ctexrep}%Chinese report
\usepackage{tabularray,xcolor,ulem}%tabular,color,underline
\usepackage[final=true, colorlinks, hidelinks, bookmarks, bookmarksnumbered=true]{hyperref}%href

\begin{document}
\begin{tblr}[
        long,
    ]{
        cells={c,m},
        row{1,3,5,7} = {5ex},
        row{9} = {7ex},
    }
    {%
        \uline{\href{https://affinity.serif.com/zh-cn/designer}{Affinity® Designer}}、%
        \uline{\href{https://www.gimp.org/}{GIMP}}、%
        \uline{\href{https://krita.org/zh-cn}{Krita}}、%
    \uline{\href{https://www.adobe.com/cn/creativecloud/roc/business.html}{Adobe® Photoshop}} \\
    \uline{\href{https://survivesjtu.github.io/SJTU-Application}{上海交通大学飞跃手册}}       \\
        \uline{\href{https://gitee.com/hanyaner/witjij}{武工大计院新生入学建议}}%
    }                                                                                         \\
    {%
    \large\textbf{格式与疑难问题}%
    }                                                                                         \\
    {%
    {\uline{\href{https://github.com/CTeX-org/forum/issues}{GitHub CTeX Forum}}、%
            \uline{\href{https://tex.stackexchange.com}{TeX StackExchange}}、%
    \uline{\href{https://www.latexstudio.net}{LaTeX工作室}}}                                  \\
    \uline{\href{https://github.com/lvjr/tabularray}{tabularray}}%
    }                                                                                         \\
    {%
    \large\textbf{\textcolor{red}{其他所有为本指南、地图提供建议与改进意见的同学、教师等}}%
    }
\end{tblr}
\end{document}

What I want

\documentclass{ctexrep}%Chinese report
\usepackage{tabularray,xcolor,ulem}%tabular,color,underline
\usepackage[final=true, colorlinks, hidelinks, bookmarks, bookmarksnumbered=true]{hyperref}%href

\begin{document}
\begin{tblr}[
        long,
    ]{
        cells={c,m},
        row{1,3,5,7} = {5ex},
        row{9} = {7ex},
    }
    {%
    \uline{\href{https://affinity.serif.com/zh-cn/designer}{Affinity® Designer}}、%
    \uline{\href{https://www.gimp.org/}{GIMP}}、%
    \uline{\href{https://krita.org/zh-cn}{Krita}}、%
    \uline{\href{https://www.adobe.com/cn/creativecloud/roc/business.html}{Adobe® Photoshop}} \\
    \uline{\href{https://survivesjtu.github.io/SJTU-Application}{上海交通大学飞跃手册}}       \\
    \uline{\href{https://gitee.com/hanyaner/witjij}{武工大计院新生入学建议}}%
    }                                                                                         \\
    {%
    \large\textbf{格式与疑难问题}%
    }                                                                                         \\
    {%
    {\uline{\href{https://github.com/CTeX-org/forum/issues}{GitHub CTeX Forum}}、%
    \uline{\href{https://tex.stackexchange.com}{TeX StackExchange}}、%
    \uline{\href{https://www.latexstudio.net}{LaTeX工作室}}}                                  \\
    \uline{\href{https://github.com/lvjr/tabularray}{tabularray}}%
    }                                                                                         \\
    {%
    \large\textbf{\textcolor{red}{其他所有为本指南、地图提供建议与改进意见的同学、教师等}}%
    }
\end{tblr}
\end{document}

Possiable reasons

  1. \\
  2. multirow
  3. text like
    {%
    \uline{\href{https://xxx}{xxx}}、%
    \uline{\href{https://xxx}{xxx}}\\
    \uline{\href{https://xxx}{xxx}}
    }\\

A thousand thanks for you!

Mikachu2333 commented 1 month ago

Original / Formatted

Here is a pic for this after compile

图片

图片

Mikachu2333 commented 1 month ago

图片