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

Unexpected indentation after square brackets used for open intervals #507

Closed AntoineMissier closed 7 months ago

AntoineMissier commented 7 months ago

Please provide the following when posting an issue:

original .tex code

\begin{enumerate}
\item 
The function $u: x \mapsto 1-x + \ln (2x)$ is defined on $]0,+\infty[$ and we obtain 
its derivative $u'$ :
\[u'(x)=-1+\frac{2}{2x}=-1+\frac{1}{x}=\frac{1-x}{x}. \]

\item 
Here we calculate the limits at 0 and $+\infty$ :

\item 
We have $x >0$ on $]0,+\infty[$, thus $u'(x)$ has the same sign as $1-x$.

The maximum of $u$ is $u(1)=\ln2$.

\end{enumerate}

yaml settings


indentAfterItems:
    itemize: 0
    enumerate: 0
    description: 0
    list: 0
specialBeginEnd:
    displayMath:
        begin: '\\\['
        end: '\\\]'
        lookForThis: 1
    inlineMath:
        begin: '(?<!\$)(?<!\\)\$(?!\$)'
        end: '(?<!\\)\$(?!\$)'
        lookForThis: 1
    displayMathTeX:
        begin: '\$\$'
        end: '\$\$'
        lookForThis: 1
    specialBeforeCommand: 0

actual/given output

\begin{enumerate}
    \item
    The function $u: x \mapsto 1-x + \ln (2x)$ is defined on $]0,+\infty[$ and we obtain
                its derivative $u'$ :
                \[u'(x)=-1+\frac{2}{2x}=-1+\frac{1}{x}=\frac{1-x}{x}. \]

                \item
                Here we calculate the limits at 0 and $+\infty$ :

                \item
                We have $x >0$ on $]0,+\infty[$, thus $u'(x)$ has the same sign as $1-x$.

    The maximum of $u$ is $u(1)=\ln2$.

\end{enumerate}

desired or expected output

\begin{enumerate}
    \item
    The function $u: x \mapsto 1-x + \ln (2x)$ is defined on $]0,+\infty[$ and we obtain
    its derivative $u'$ :
    \[u'(x)=-1+\frac{2}{2x}=-1+\frac{1}{x}=\frac{1-x}{x}. \]

    \item
    Here we calculate the limits at 0 and $+\infty$ :

    \item
    We have $x >0$ on $]0,+\infty[$, thus $u'(x)$ has the same sign as $1-x$.

    The maximum of $u$ is $u(1)=\ln2$.

\end{enumerate}

anything else

When I eliminate the open brackets in the first interval, the problem disappears. What should I change in the defaultSettings.yaml to manage these square brackets correctly ?

cmhughes commented 7 months ago

Thanks for this, I hope to get to it soon. Apologies for the delay.

cmhughes commented 7 months ago

I present two options:

option 1

indentAfterItems:
    itemize: 0
    enumerate: 0
    description: 0
    list: 0
specialBeginEnd:
    specialBeforeCommand: 1

option 2

indentAfterItems:
    itemize: 0
    enumerate: 0
    description: 0
    list: 0
fineTuning:
  commands:
    name: (?!infty)[+a-zA-Z@\*0-9_\:]+?

Both options give your desired output

\begin{enumerate}
    \item
    The function $u: x \mapsto 1-x + \ln (2x)$ is defined on $]0,+\infty[$ and we obtain
    its derivative $u'$ :
    \[u'(x)=-1+\frac{2}{2x}=-1+\frac{1}{x}=\frac{1-x}{x}. \]

    \item
    Here we calculate the limits at 0 and $+\infty$ :

    \item
    We have $x >0$ on $]0,+\infty[$, thus $u'(x)$ has the same sign as $1-x$.

    The maximum of $u$ is $u(1)=\ln2$.

\end{enumerate}

explanation

The issue arises because of \infty[ which latexindent.pl sees as the start of a command. The above solutions both tell latexindent.pl that this is not the case.

AntoineMissier commented 7 months ago

Ok it works fine. I prefer the option 2 because it preserves indentation inside macros different from \infty. Thanks Chris

cmhughes commented 7 months ago

Great, glad it worked, happy indentation!