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

Issue with `modifyLineBreaks:environments:BodyStartsOnOwnLine` (with beamer overlays) #462

Closed niveK77pur closed 1 year ago

niveK77pur commented 1 year ago

original .tex code

\documentclass{beamer}

\begin{document}

\begin{frame}{Frame Title}
  \begin{onlyenv}<1>
    I'm here
  \end{onlyenv}
  \begin{onlyenv}<2>
    Where did I go?
  \end{onlyenv}
  \begin{uncoverenv}<3,5-6>
    Hello
  \end{uncoverenv}
  \begin{onlyenv}<4->
    World
  \end{onlyenv}
  \begin{center}
    This
    \begin{altenv}<2-4,6>{(}{)}{[}{]}
      word
    \end{altenv}
  \end{center}
\end{frame}

\end{document}

yaml settings

Any value from 1 through 4 causes issues. Note that a value of 2 prevents the issue from breaking the document.

modifyLineBreaks:
  environments:
    BodyStartsOnOwnLine: 1

actual/given output

With BodyStartsOnOwnLine: 1

\documentclass{beamer}

\begin{document}

\begin{frame}{Frame Title}
    \begin{onlyenv}
        <1>
        I'm here
    \end{onlyenv}
    \begin{onlyenv}
        <2>
        Where did I go?
    \end{onlyenv}
    \begin{uncoverenv}
        <3,5-6>
        Hello
    \end{uncoverenv}
    \begin{onlyenv}
        <4->
        World
    \end{onlyenv}
    \begin{center}
        This
        \begin{altenv}
            <2-4,6>{(}{)}{[}{]}
            word
        \end{altenv}
    \end{center}
\end{frame}

\end{document}

With BodyStartsOnOwnLine: 2

Note that the trailing % here prevents the document from breaking, and keeps the overlay working as it should.

\documentclass{beamer}

\begin{document}

\begin{frame}{Frame Title}%
    \begin{onlyenv}%
        <1>
        I'm here
    \end{onlyenv}
    \begin{onlyenv}%
        <2>
        Where did I go?
    \end{onlyenv}
    \begin{uncoverenv}%
        <3,5-6>
        Hello
    \end{uncoverenv}
    \begin{onlyenv}%
        <4->
        World
    \end{onlyenv}
    \begin{center}
        This
        \begin{altenv}%
            <2-4,6>{(}{)}{[}{]}
            word
        \end{altenv}
    \end{center}
\end{frame}

\end{document}

With BodyStartsOnOwnLine: 3 and BodyStartsOnOwnLine: 4

\documentclass{beamer}

\begin{document}

\begin{frame}

{Frame Title}
    \begin{onlyenv}

        <1>
        I'm here
    \end{onlyenv}
    \begin{onlyenv}

        <2>
        Where did I go?
    \end{onlyenv}
    \begin{uncoverenv}

        <3,5-6>
        Hello
    \end{uncoverenv}
    \begin{onlyenv}

        <4->
        World
    \end{onlyenv}
    \begin{center}
        This
        \begin{altenv}

            <2-4,6>{(}{)}{[}{]}
            word
        \end{altenv}
    \end{center}
\end{frame}

\end{document}

desired or expected output

Overlay specifications are not considered as body of the environment. Example for a value of BodyStartsOnOwnLine: 1.

\documentclass{beamer}

\begin{document}

\begin{frame}{Frame Title}
    \begin{onlyenv}<1>
        I'm here
    \end{onlyenv}
    \begin{onlyenv}<2>
        Where did I go?
    \end{onlyenv}
    \begin{uncoverenv}<3,5-6>
        Hello
    \end{uncoverenv}
    \begin{onlyenv}<4->
        World
    \end{onlyenv}
    \begin{center}
        This
        \begin{altenv}<2-4,6>{(}{)}{[}{]}
            word
        \end{altenv}
    \end{center}
\end{frame}

\end{document}

anything else

Not sure if related and maybe warrants its own issue, but I noticed here that \begin{frame}{Frame title} also has a % added for a value of 2. Same happens if I have a \begin{frame}[fragile] or \begin{frame}[fragile]{Frame title}. They all have a trailing % added, even though they were already on their own line; as you can see, it works fine for the \begin{center} which doesn't have additional arguments. For a value of 3 or 4, these arguments are even completely separated from the environment, which also causes a breaking modification to the document (the arguments become part of the body, not arguments to the environment anymore).

cmhughes commented 1 year ago

Thanks for this.

solution

You can use the replacement switch to assist with this:

modifyLineBreaks:
  environments:
    BodyStartsOnOwnLine: 1
replacements:
  -
    when: after
    substitution: |-
      s/(\\begin\{[^}]+?\})\s*(<[^>]+?>)/$1$2/sg

so you need to run

latexindent.pl -m -r -l myfile.tex

which gives

\documentclass{beamer}

\begin{document}

\begin{frame}{Frame Title}
    \begin{onlyenv}<1>
        I'm here
    \end{onlyenv}
    \begin{onlyenv}<2>
        Where did I go?
    \end{onlyenv}
    \begin{uncoverenv}<3,5-6>
        Hello
    \end{uncoverenv}
    \begin{onlyenv}<4->
        World
    \end{onlyenv}
    \begin{center}
        This
        \begin{altenv}<2-4,6>{(}{)}{[}{]}
            word
        \end{altenv}
    \end{center}
\end{frame}

\end{document}

explanation

As detailed in the Table at https://latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html#id67, the poly-switch BodyStartsOnOwnLine corresponds to the line break immediately after the begin statement.

If you'd prefer not to employ the r switch, then perhaps you can switch off the BodyStartsOnOwnLine on a per-name basis, for example:

modifyLineBreaks:
  environments:
    BodyStartsOnOwnLine: 1
    onlyenv:
      BodyStartsOnOwnLine: 0

I hope this helps!

niveK77pur commented 1 year ago

I see, thank you very much for the detailed explanations!

I do remember glancing at that table before but apparently I had decided to ignore what exactly it was showing. It seems that there is no issue after all and things are working exactly as intended; I must have misunderstood what exactly BodyStartsOnOwnLine does and is supposed to do.

Following your comments, I will opt to disable the setting, and instead enable it on a per-name basis if I need it.

I will proceed to close this issue as everything has been resolved and clarified! Thanks again!

cmhughes commented 1 year ago

Wonderful, thanks so much :) happy indenting!

On Tue, Aug 29, 2023 at 6:43 PM Kevin Laurent Biewesch < @.***> wrote:

I see, thank you very much for the detailed explanations!

I do remember glancing at that table before but apparently I had decided to ignore what exactly it was showing. It seems that there is no issue after all and things are working exactly as intended; I must have misunderstood what exactly BodyStartsOnOwnLine does and is supposed to do.

Following your comments, I will opt to disable the setting, and instead enable it on a per-name basis if I need it.

I will proceed to close this issue as everything has been resolved and clarified! Thanks again!

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