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

environment formatted inline #376

Closed tdegeus closed 2 years ago

tdegeus commented 2 years ago

Please provide the following when posting an issue:

original .tex code

For a slip event at interface $s$, we have $\Delta R_s > 0$ and $\Delta R_i = 0$ for $i \neq s$, inducing
\begin{equation}
    \label{eq:delta_fi}
    \Delta f_i = K\Delta R_s \,\, \mathrm{for} \,\, i \geq s ; \quad \Delta f_i=0 \,\, \mathrm{otherwise}.
\end{equation}
We can then deduce that
\begin{equation}
    \label{eq:DR_DF_multi}
    \Delta F = \frac{h K}{H} \Delta R_s \sum\limits_{i=s}^n i \, = \frac{h K}{H}\Delta R_s (n+s)(n-s+1) / 2.
\end{equation}
which we verify in \cref{fig:2c}.

yaml settings

onlyOneBackUp: 1
defaultIndent: '    '
removeTrailingWhitespace: 1

lookForAlignDelims:
  align: 0

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    removeSentenceLineBreaks: 1
    textWrapSentences: 1
fineTuning:
  namedGroupingBracesBrackets:
    name: '[0-9\.a-zA-Z@\*><_]+?'

# remove recurrent spaces
replacements:
- substitution: s/([a-zA-Z0-9.}{])\h{2,}([a-zA-Z0-9.}{])/$1 $2/sg
  when: before
- substitution: s/([a-zA-Z0-9.()}{])\h{2,}([a-zA-Z0-9.()}{]|(?:\\(?!\\)))/$1 $2/sg
  when: after

actual/given output

For a slip event at interface $s$, we have $\Delta R_s > 0$ and $\Delta R_i = 0$ for $i \neq s$, inducing \begin{equation} \label{eq:delta_fi} \Delta f_i = K\Delta R_s \,\, \mathrm{for} \,\, i \geq s ; \quad \Delta f_i=0 \,\, \mathrm{otherwise}.
\end{equation}
We can then deduce that \begin{equation} \label{eq:DR_DF_multi} \Delta F = \frac{h K}{H} \Delta R_s \sum\limits_{i=s}^n i \, = \frac{h K}{H}\Delta R_s (n+s)(n-s+1) / 2.
\end{equation}
which we verify in \cref{fig:2c}.
  1. The equations are all formatted on the same line, which I don't want.2.
  2. A spurious white line is introduced at the end

desired or expected output

For a slip event at interface $s$, we have $\Delta R_s > 0$ and $\Delta R_i = 0$ for $i \neq s$, inducing
\begin{equation}
    \label{eq:delta_fi}
    \Delta f_i = K\Delta R_s \,\, \mathrm{for} \,\, i \geq s ; \quad \Delta f_i=0 \,\, \mathrm{otherwise}.
\end{equation}
We can then deduce that
\begin{equation}
    \label{eq:DR_DF_multi}
    \Delta F = \frac{h K}{H} \Delta R_s \sum\limits_{i=s}^n i \, = \frac{h K}{H}\Delta R_s (n+s)(n-s+1) / 2.
\end{equation}
which we verify in \cref{fig:2c}.
cmhughes commented 2 years ago

Thanks for this, I'll take a look soon and see what I can do :)

cmhughes commented 2 years ago

Thanks for this, and apologies for the delay.

why does this happen?

latexindent.pl defines a sentence as having to:

So in your code, this means that

For a slip event at interface $s$, we have $\Delta R_s > 0$ and $\Delta R_i = 0$ for $i \neq s$, inducing
\begin{equation}
    \label{eq:delta_fi}
    \Delta f_i = K\Delta R_s \,\, \mathrm{for} \,\, i \geq s ; \quad \Delta f_i=0 \,\, \mathrm{otherwise}.

is a sentence. So, with your code, the line breaks within this sentence will be removed.

what can we do? option 1

We can employ poly-switches to undo the line break removal using

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    removeSentenceLineBreaks: 1
    textWrapSentences: 1
  environments:                         # <----
      BeginStartsOnOwnLine: 1           # <----
      BodyStartsOnOwnLine: 1            # <---- NEW
  mandatoryArguments:                   # <----
      label:                            # <----
        RCuBFinishesWithLineBreak: 1    # <----

which gives

For a slip event at interface $s$, we have $\Delta R_s > 0$ and $\Delta R_i = 0$ for $i \neq s$, inducing
\begin{equation}
    \label{eq:delta_fi}
    \Delta f_i = K\Delta R_s \,\, \mathrm{for} \,\, i \geq s ; \quad \Delta f_i=0 \,\, \mathrm{otherwise}.
\end{equation}
We can then deduce that
\begin{equation}
    \label{eq:DR_DF_multi}
    \Delta F = \frac{h K}{H} \Delta R_s \sum\limits_{i=s}^n i \, = \frac{h K}{H}\Delta R_s (n+s)(n-s+1) / 2.
\end{equation}
which we verify in \cref{fig:2c}.

what can we do? option 2

We can tweak what a sentence is by saying that sentences should end with \begin{<something>}

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    removeSentenceLineBreaks: 1
    textWrapSentences: 1
    sentencesEndWith:                   # <----
        other: |-                       # <----
          (?x) \\begin\{[a-zA-Z]+?\}    # <---- NEW
  environments:                         # <----
      BeginStartsOnOwnLine: 1           # <----

and in which case we receive, again,

For a slip event at interface $s$, we have $\Delta R_s > 0$ and $\Delta R_i = 0$ for $i \neq s$, inducing
\begin{equation}
    \label{eq:delta_fi}
    \Delta f_i = K\Delta R_s \,\, \mathrm{for} \,\, i \geq s ; \quad \Delta f_i=0 \,\, \mathrm{otherwise}.
\end{equation}
We can then deduce that
\begin{equation}
    \label{eq:DR_DF_multi}
    \Delta F = \frac{h K}{H} \Delta R_s \sum\limits_{i=s}^n i \, = \frac{h K}{H}\Delta R_s (n+s)(n-s+1) / 2.
\end{equation}
which we verify in \cref{fig:2c}.

other comments (your issue (2))

I couldn't replicate the behaviour you describe "spurious white line is introduced at the end"....

Does this help? Let me know :)

tdegeus commented 2 years ago

Thanks! That does indeed solve things. Concerning my issue 2: I can no longer reproduce it either with the new settings. Let me open a new issue when it occurs reproducibly.