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
867 stars 84 forks source link

Environment's label on the same line as \begin{...} #488

Closed arkandias closed 11 months ago

arkandias commented 11 months ago

Hi there,

I hope this question was not already asked (could not fin anything in the Issues...).

I like the following format for (most of) my environments:

\begin{myenv}
  The body of my environment...
\end{myenv}

So I configured latexindent with the following poly-switches:

environments:
    BeginStartsOnOwnLine: 1
    BodyStartsOnOwnLine: 1
    EndStartsOnOwnLine: 1
    EndFinishesWithLineBreak: 1

However, when I label my environments, I like the label to be placed on the same line as the \begin{...}, i.e.

\begin{myenv} \label{mylabel}
  The body of my environment...
\end{myenv}

This is automatically handled for headings (cf. 6.1.3 in the doc), but I cannot figure out how to configure this for environments (if this is possible)...

Any idea?

cmhughes commented 11 months ago

Does the following help

modifyLineBreaks: 
    commands:
        CommandStartsOnOwnLine: 
          label: -1
cmhughes commented 11 months ago

The following latexindent.yaml works:

modifyLineBreaks:
   environments:
       BeginStartsOnOwnLine: 1
       BodyStartsOnOwnLine: 1
       EndStartsOnOwnLine: 1
       EndFinishesWithLineBreak: 1

replacements:
  - substitution: s/(\\begin\{[^}]+?\})\s*(\\label\{)/$1$2/
    when: after

and called with

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

output

\begin{myenv}\label{mylabel}
    The body of my environment...
\end{myenv}
arkandias commented 11 months ago

Thank you for your solution! It works indeed, but this seems a bit like a hack (and it uses the replacement function which makes the process heavier).

I really think this should be the default behavior (or at least an option), just as in the case of headings. Most people I know place their labels on the same line as the \begin command. It is also what you can find, e.g., here:

cmhughes commented 11 months ago

You've specified BodyStartsOnOwnLine: 1 which means that the body will start on its own line.

The body of the environment starts after the begin statement as detailed in https://latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html#poly-switches-for-other-code-blocks

arkandias commented 11 months ago

Yes, I do want the body to start on its own line, but I would like the body to begin after \label{my label}.

cmhughes commented 11 months ago

as of https://github.com/cmhughes/latexindent.pl/commit/cc849d764c2babdc2b68660d61780966aae7fcd2 if you start with

\begin{myenv}\label{mylabel}The body of my environment...\end{myenv}

and use the settings

modifyLineBreaks:
   environments:
       BeginStartsOnOwnLine: 1
       BodyStartsOnOwnLine: 1
       EndStartsOnOwnLine: 1
       EndFinishesWithLineBreak: 1

fineTuning:
  environments:
    begin: \\begin\{([a-zA-Z@\*0-9_\\]+)\}\s*\\label\{[^}]+?\}
    end: \\end\{\2\}

then you receive the output

\begin{myenv}\label{mylabel}
    The body of my environment...
\end{myenv}

and there's no need to use the replacement switch. I'll get this released soon.

cmhughes commented 11 months ago

released at https://github.com/cmhughes/latexindent.pl/releases/tag/V3.23.3

cmhughes commented 11 months ago

Documented in https://latexindentpl.readthedocs.io/en/latest/sec-fine-tuning.html#lst-finetuning6-mod1

arkandias commented 11 months ago

Great! Thank you very much, this is a very nice solution!