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

Insert newline after double backslash in align* environment #393

Closed dpo closed 1 year ago

dpo commented 1 year ago

original .tex code

\documentclass{article}

\begin{document}
  \begin{align*}
    e & = mc^2 \\ x & = y.
  \end{align*}
\end{document}

yaml settings

All defaults.

actual/given output

\documentclass{article}

\begin{document}
\begin{align*}
    e & = mc^2 \\ x & = y.
\end{align*}
\end{document}

desired or expected output

\documentclass{article}

\begin{document}
\begin{align*}
    e & = mc^2 \\
        x & = y.
\end{align*}
\end{document}

or even better,

\documentclass{article}

\begin{document}
\begin{align*}
       e & = mc^2
        \\ x & = y.
\end{align*}
\end{document}

Sorry if this is another basic question, but how can I automatically insert a newline after (or before) a double backslash in an align* environment? Thanks in advance!

cmhughes commented 1 year ago

Thanks for this :)

Have you seen https://latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html#poly-switches-for-double-back-slash

dpo commented 1 year ago

It appears I did not because I was searching for "backslash" in the documentation, but it's spelled "back slash" in two words in some places and in one word in others. Perhaps that could be straightened out?

Sorry for the basic questions. I'm finding the documentation to be rather daunting, though it's clear that you've put a lot of effort into it and tried to make it didactic.

Another point is that I was under the impression that defaultSettings.yaml contained all possible settings. I installed latexindent with Homebrew and

grep DBS /opt/homebrew/Cellar/latexindent/3.19.1/libexec/bin/defaultSettings.yaml

returns nothing. Is that expected?

cmhughes commented 1 year ago

Many thanks, glad that helped.

It appears I did not because I was searching for "backslash" in the documentation, but it's spelled "back slash" in two words in some places and in one word in others. Perhaps that could be straightened out?

Yes, I'll get that fixed.

Is that expected?

Yes. I don't think that there's a natural place for the dbs switches in defaultSettings.yaml, as they can be applied to many of the different code blocks : environments, mandatory /optional arguments, special. Unless I put copies in for all of these code blocks, but then they would not be activated most of the time.

If you have any tips on how to make the documentation less daunting, while still being comprehensive, let me know :) don't forget there is a quick start section!

cmhughes commented 1 year ago

As of https://github.com/cmhughes/latexindent.pl/commit/b2257d5df6303c9508251fe3414fa0798b115750 back slash has been replaced with backslash in the documentation.

It'll be part of the next release.

dpo commented 1 year ago

Many thanks. As a follow-up question, is DBSStartsOnOwnLine: 1 compatible with aligning ampersands? I have

lookForAlignDelims:
   cases: 1

but latexindent does this:

       \begin{cases}
-        +\infty       & \text{if } x \leq 0  \\
-        \phi_{k,i}(x) & \text{if } 0 < x < 1 \\
-        0             & \text{if } x \geq 1,
+        +\infty & \text{if } x \leq 0
+        \\ \phi_{k,i}(x) & \text{if } 0 < x < 1
+        \\ 0 & \text{if } x \geq 1,
       \end{cases}
cmhughes commented 1 year ago

Yes it should all be OK.

On Sat, 17 Dec 2022, 03:19 Dominique, @.***> wrote:

Many thanks. As a follow-up question, is DBSStartsOnOwnLine: 1 compatible with aligning ampersands? I have

lookForAlignDelims: cases: 1

but latexindent does this:

   \begin{cases}-        +\infty       & \text{if } x \leq 0  \\-        \phi_{k,i}(x) & \text{if } 0 < x < 1 \\-        0             & \text{if } x \geq 1,+        +\infty & \text{if } x \leq 0+        \\ \phi_{k,i}(x) & \text{if } 0 < x < 1+        \\ 0 & \text{if } x \geq 1,
   \end{cases}

— Reply to this email directly, view it on GitHub https://github.com/cmhughes/latexindent.pl/issues/393#issuecomment-1355991962, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ7CYF57452YOFIKN3QONDWNUWMLANCNFSM6AAAAAAS7IJFII . You are receiving this because you commented.Message ID: @.***>

dpo commented 1 year ago

But the diff above shows that the & are not aligned...

cmhughes commented 1 year ago

Understood.

This hasn't come up before, and it hadn't occurred to me that anyone would want to align anything after the \\.

So, I think we need a new key, something like alignContentAfterDBS which can be 0 or 1.

I hope to look at this soon.

dpo commented 1 year ago

Thank you. As a side note, why not enable discussions on this repo? I often have questions rather than issues, and other users may be able to help you answer them.

cmhughes commented 1 year ago

What are 'discussions', is it another tab in the repository....?

On Sun, 18 Dec 2022, 17:09 Dominique, @.***> wrote:

Thank you. As a side note, why not enable discussions on this repo? I often have questions rather than issues, and other users may be able to help you answer them.

— Reply to this email directly, view it on GitHub https://github.com/cmhughes/latexindent.pl/issues/393#issuecomment-1356838682, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ7CYBGIXS5DOXRDHMBW4LWN5ALJANCNFSM6AAAAAAS7IJFII . You are receiving this because you commented.Message ID: @.***>

cmhughes commented 1 year ago

I've been looking at discussions. I think the template feature would need to be implemented before opening them https://github.com/community/community/discussions/2838

dpo commented 1 year ago

Ok. I think of them as more "free form", but as you wish.

ankitp94 commented 1 year ago

Understood.

This hasn't come up before, and it hadn't occurred to me that anyone would want to align anything after the \\.

So, I think we need a new key, something like alignContentAfterDBS which can be 0 or 1.

I hope to look at this soon.

Hi Chris, I have a related request. Is it possible to set DBSFinishesWithLineBreak for some environments for which I don't want to set lookForAlignDelims equal to 1? The current documentation says Note that for these poly-switches to take effect, the name of the code block must necessarily be specified within lookForAlignDelims. Is it possible to separate the two routines? In particular, I do not want the script to change formatting of lines in align environments but ensure that all DBS end with a new line.

Thanks!

cmhughes commented 1 year ago

@ankitp94 great, thanks. Your feature request is detailed in https://github.com/cmhughes/latexindent.pl/issues/402

cmhughes commented 1 year ago

For reference, https://github.com/cmhughes/latexindent.pl/issues/402 has been implemented.

I'm working on the feature request detailed in this issue as a next priority.

cmhughes commented 1 year ago

Apologies for the delay on this.

As of https://github.com/cmhughes/latexindent.pl/commit/f0f3b00f6c23aac0ee0b39e0cc5682bec6a2c030 a new feature is implemented: alignContentAfterDoubleBackSlash.

Starting with

\begin{cases}
 +\infty & \text{if } x \leq 0 \\ 
 \phi_{k,i}(x) & \text{if } 0 < x < 1 \\ 
 0 & \text{if } x \geq 1,
\end{cases}

and using

lookForAlignDelims:
   cases:
      alignContentAfterDoubleBackSlash: 1 #<!---- NEW BIT

modifyLineBreaks:
    environments:
        DBSStartsOnOwnLine: 1
        DBSFinishesWithLineBreak: -1

gives

\begin{cases}
       +\infty       & \text{if } x \leq 0
    \\ \phi_{k,i}(x) & \text{if } 0 < x < 1
    \\ 0             & \text{if } x \geq 1,
\end{cases}
cmhughes commented 1 year ago

Released at https://github.com/cmhughes/latexindent.pl/releases/tag/V3.21, uploaded to ctan.

dpo commented 1 year ago

Looks great. Many thanks! I will test.