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

Another unexpected indentation with square brackets used for open intervals #511

Closed AntoineMissier closed 7 months ago

AntoineMissier commented 7 months ago

Please provide the following when posting an issue:

original .tex code

\documentclass[a4paper,11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{ibrackets}

\begin{document}

$f(x)= \dfrac{1}{(1-x)^2}$ on $]-\infty, 1[$.
$f$ is convex because
\[ f''(x) = \frac{6}{(1-x)^4} \geq 0. \]
And the tangent in 0 is such that
\[ y=f'(0)(x-0) + f(0) \text{ with } f'(0)=2 \text{ and } f(0)=1, \]
So we can say that, for all $x \in ]-\infty, 1[$,
$f(x) \geq 2x+1$.

\end{document}

yaml settings

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

actual/given output

\begin{document}

$f(x)= \dfrac{1}{(1-x)^2}$ on $]-\infty, 1[$.
            $f$ is convex because
                \[ f''(x) = \frac{6}{(1-x)^4} \geq 0. \]
                And the tangent in 0 is such that
                \[ y=f'(0)(x-0) + f(0) \text{ with } f'(0)=2 \text{ and } f(0)=1, \]
                So we can say that, for all $x \in ]-\infty, 1[$,
$f(x) \geq 2x+1$.

\end{document}

desired or expected output

$f(x)= \dfrac{1}{(1-x)^2}$ on $]-\infty, 1[$.
$f$ is convex because
\[ f''(x) = \frac{6}{(1-x)^4} \geq 0. \]
And the tangent in 0 is such that
\[ y=f'(0)(x-0) + f(0) \text{ with } f'(0)=2 \text{ and } f(0)=1, \]
So we can say that, for all $x \in ]-\infty, 1[$,
$f(x) \geq 2x+1$.

anything else

How to completely deactivate any indentation after square brackets, but not with braces?

cmhughes commented 7 months ago

Many thanks.

I present two options, both give your desired output:

option 1

fineTuning:
  commands:
    name: (?!infty)[+a-zA-Z@\*0-9_\:]+?
  namedGroupingBracesBrackets:        # <!-- NEW
    name: [.a-zA-Z@\*><]+?            # <!-- NEW

options 2

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

specialBeginEnd:
  specialBeforeCommand: 1
AntoineMissier commented 7 months ago

The two solutions work fine. I thought the option 2 could break indentation inside a command with braces, but not! Only with square brackets as expected, it's perfect! But I don't understand what causes this issue. Thanks Chris

cmhughes commented 7 months ago

Great, glad it helped :)

The issue comes from the first line in your code

$f(x)= \dfrac{1}{(1-x)^2}$ on $]-\infty, 1[    $.
                                                            ^^^

In particular, by default, latexindent looks for 'named braces/brackets' that can have a number in them, such as 1 in your case.

If you specify specialBeforeCommand: 1 then this won't be an issue, because the $...$ will be found first, and then the named braces/brackets routine won't be a problem.

But, if you leave specialBeforeCommand: 0 then we need to tell latexindent that named braces/brackets can not have numbers, hence the fineTuning.

AntoineMissier commented 7 months ago

Thanks for the answer :-) Indeed, the problem occurs with the '1['. If I replace $]-\infty, 1[$ by $]-\infty, 1]$, no unexpected indentation is produced. I don't know in which cases a routine to find 'named braces/brackets' could be useful, I think you have good reasons for that and your script is great and really helpful.

cmhughes commented 7 months ago

Great, glad it works!