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
875 stars 83 forks source link

EndFinishesWithLineBreak : 3 / BeginStartsOnOwnLine : 3 don't seem to work #160

Closed xaltsc closed 5 years ago

xaltsc commented 5 years ago

Using latexindent 3.5.3 as provided by tlmgr.

Trying, for instance to run latexindent -l=env-mlb12.yaml test.tex (as in https://latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html#lst-env-mlb12) doesn't yield the expected output: no lines are jumped after \end{...}.

test.tex:


\begin{defn}
  This is a definition.
\end{defn}
\begin{prop}
  This is a proposition.
\end{prop}

output:

\begin{defn}
  This is a definition.
\end{defn}
\begin{prop}
  This is a proposition.
\end{prop}

desired output:


\begin{defn}
  This is a definition.
\end{defn}

\begin{prop}
  This is a proposition.
\end{prop}
xaltsc commented 5 years ago

Curiously enough, with BeginStartsOnOwnLine, the first line is jumped, the only one that's no preceded by an \end element.

cmhughes commented 5 years ago

Hello! Thanks for this. Is your example minimal? If not, can you strip it down, and also post what your desired output would be?

Note that setting a polyswitch to 3 is described as add then blank line mode in https:// latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html#poly-switches

I wonder if the following would be helpful: https://github.com/cmhughes/latexindent.pl/issues/146

On Wed, 3 Apr 2019, 03:06 xaltsc, notifications@github.com wrote:

Curiously enough, with BeginStartsOnOwnLine, the first line is jumped, the only one that's no preceded by an \end element.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/cmhughes/latexindent.pl/issues/160#issuecomment-479298693, or mute the thread https://github.com/notifications/unsubscribe-auth/ACHxYIalvP-cficSAx1Ea_hjMRmGGqhMks5vdAytgaJpZM4cZdL7 .

xaltsc commented 5 years ago

I've edited the initial message with minimal examples and desired output, however, using alok1.yaml edited for it to work with my example doesn't do anything.

It seems to work only when both begin and end are on the same line.

xaltsc commented 5 years ago

For instance:

test2.tex

\begin{defn}
  This is a definition.
\end{defn} \begin{prop}
  This is a proposition.
\end{prop}

output

\begin{defn}
    This is a definition.
\end{defn}

\begin{prop}
    This is a proposition.
\end{prop}

With both set up to 3.

cmhughes commented 5 years ago

Thanks, that's helpful.

In which case, this is the expected behaviour and will be addressed in the development of https://github.com/cmhughes/latexindent.pl/issues/146

My current thinking is that I'll create a new value for the polyswitch (4) which will add a blank line.

Development is slow at the moment as I have a heavy work load but I do intend to address this. Thanks again

xaltsc commented 5 years ago

Super, thanks! For now, a quick sed will do the job.

Also, it would be nice if two consecutive begin/end would not be separated by a blank line.

cmhughes commented 5 years ago

Can you elaborate on your second comment? :)

xaltsc commented 5 years ago

I made the following quick-and-dirty script:

#!/bin/sh

perl -0777pe 's/(\\end[^\n]+)\n+/\1 /g' "$1" | perl -0777pe 's/\n+\s*(\\begin)/ \1/g' - > "$1.tmp.tex"
latexindent -m -l "$1.tmp.tex"
rm "$1.tmp.tex"

On

\begin{defn}
  This is a definition.
\end{defn}
test
\begin{prop}
  \begin{tikzcd}
    This is \arrow[r] a commutative diagram.
  \end{tikzcd}
\end{prop}

It outputs the following

\begin{defn}
  This is a definition.
\end{defn} 

test

\begin{prop}
  \begin{tikzcd}
    This is \arrow[r] a commutative diagram.
  \end{tikzcd} 

\end{prop} 

The desired output would be

\begin{defn}
  This is a definition.
\end{defn} 

test

\begin{prop}
  \begin{tikzcd}
    This is \arrow[r] a commutative diagram.
  \end{tikzcd} 
\end{prop} 

Which is rather annoying and pointless. An earlier version of this script would also have added a line between the two begins, I don't know what this is due to, but I suspect the option -0777.

The intermediary file .tmp.text is

\begin{defn}
  This is a definition.
\end{defn} test \begin{prop} \begin{tikzcd}
    This is \arrow[r] a commutative diagram.
  \end{tikzcd} \end{prop} 

So this is due to latexindent I think.

xaltsc commented 5 years ago

I don't understand perl, so I can't investigate the source code, but from what I've read in the trace logs, perhaps this is due to the fact that latexindent seems to edit the file linearly, where it would make more sense to edit it semantically, ie. construct a tree of the file structure and edit it from the top to the bottom. But this may be cumbersome to implement.

cmhughes commented 5 years ago

As of https://github.com/cmhughes/latexindent.pl/commit/cd8312d1a597aa92ecf5eb4a857fa58410ea8be0 a new poly-switch value exists which facilitates what you would like :)

Starting with

xaltsc.tex

\begin{defn}
  This is a definition.
\end{defn}
test
\begin{prop}
  \begin{tikzcd}
    This is \arrow[r] a commutative diagram.
  \end{tikzcd}
\end{prop}

and the YAML in

xaltsc.yaml

modifyLineBreaks:
    environments:
      defn:
        EndFinishesWithLineBreak: 4
      prop:
        BeginStartsOnOwnLine: 4

then if we run

latexindent.pl -m xaltsc.tex -l=xaltsc.yaml

then we receive

\begin{defn}
    This is a definition.
\end{defn}

test

\begin{prop}
    \begin{tikzcd}
        This is \arrow[r] a commutative diagram.
    \end{tikzcd}
\end{prop}

another optoin

You might not wish to specify the settings on a per-name basis, and may just want to specify the switch for all environments:

modifyLineBreaks:
    environments:
        EndFinishesWithLineBreak: 4
        BeginStartsOnOwnLine: 4

this gives the following output


\begin{defn}
    This is a definition.
\end{defn}

test

\begin{prop}

    \begin{tikzcd}
        This is \arrow[r] a commutative diagram.
    \end{tikzcd}

\end{prop}

It's immediately obvious that this isn't ideal, as there are some blank lines between sequential begin and end statements.

Thanks to https://github.com/cmhughes/latexindent.pl/issues/155, we have an option. Using the following YAML

xaltsc2.yaml

modifyLineBreaks:
    environments:
        EndFinishesWithLineBreak: 4
        BeginStartsOnOwnLine: 4
replacements:
  -
    substitution: |- 
      s/(\\begin\{.*\})\h*\R\R(\h*\\begin)/$1\n$2/sg
    when: after
  -
    substitution: |- 
      s/(\\end\{.*\})\h*\R\R(\h*\\end)/$1\n$2/sg
    when: after

and running

latexindent.pl -r -m xaltsc.tex -l=xaltsc2.yaml 

then we receive


\begin{defn}
    This is a definition.
\end{defn}

test

\begin{prop}
    \begin{tikzcd}
        This is \arrow[r] a commutative diagram.
    \end{tikzcd}
\end{prop}

which is as desired :)

I'm aiming for a release soon.

Thanks for writing, this has been helpful! :)

cmhughes commented 5 years ago

Implemented as of https://github.com/cmhughes/latexindent.pl/pull/169 upload to ctan soon.