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

Text wrapping: character count seems to ignore commands #499

Closed OlivierMartineau closed 8 months ago

OlivierMartineau commented 9 months ago

original .tex code

\documentclass{article}
\begin{document}

\paragraph{So what?} This long line is not perfectly wrapped, it seems the initial `paragraph{something}' is not taken into account in the count of characters. The following lines get nicely wrapped as intended, no problem with this.
\paragraph{And with colors?} \emph{This long line is not perfectly wrapped, it seems the command `emph' command is not taken into in the count of characters. The following lines get nicely wrapped as intended, no problem with this.}

\begin{description}
    \item[Thing1:] This long line is not perfectly wrapped, it seems the initial `item[Thing1:]' is not taken into account in the count of characters. The following lines get nicely wrapped as intended, no problem with this.
    \item[Thing2:] This long line is also not perfectly wrapped, it seems the initial `item[Thing2:]' is not taken into account in the count of characters either. The following lines get nicely wrapped as intended, no problem with this.
\end{description}

\end{document}

yaml settings

modifyLineBreaks:
  textWrapOptions:
      columns: 100                         # number of columns
      verbatim: 0
      blocksFollow:
        other: \\\]|\\item(?:\h|\s*\[)|\\\w+\{

actual/given output

\documentclass{article}
\begin{document}

\paragraph{So what?} This long line is not perfectly wrapped, it seems the initial `paragraph{something}' is not taken
into account in the count of characters. The following lines get nicely wrapped as intended, no
problem with this.
\paragraph{And with colors?} \emph{This long line is not perfectly wrapped, it seems the command `emph' command is not taken into in
    the count of characters. The following lines get nicely wrapped as intended, no problem with this.}

\begin{description}
    \item[Thing1:] This long line is not perfectly wrapped, it seems the initial `item[Thing1:]' is not taken
          into account in the count of characters. The following lines get nicely wrapped as intended, no
          problem with this.
    \item[Thing2:] This long line is also not perfectly wrapped, it seems the initial `item[Thing2:]' is not
          taken into account in the count of characters either. The following lines get nicely wrapped as
          intended, no problem with this.
\end{description}

\end{document}

desired or expected output

\documentclass{article}
\begin{document}

\paragraph{So what?} This long line is not perfectly wrapped, it seems the initial 
`paragraph{something}' is not taken into account in the count of characters. The following lines get
nicely wrapped as intended, no problem with this.
\paragraph{And with colors?} \emph{This long line is not perfectly wrapped, it seems the command 
    `emph' command is not taken into in the count of characters. The following lines get nicely 
    wrapped as intended, no problem with this.}

\begin{description}
    \item[Thing1:] This long line is not perfectly wrapped, it seems the initial `item[Thing1:]' is 
          not taken into account in the count of characters. The following lines get nicely wrapped 
          as intended, no problem with this.
    \item[Thing2:] This long line is also not perfectly wrapped, it seems the initial 
          `item[Thing2:]' is not taken into account in the count of characters either. The following 
          lines get nicely wrapped as intended, no problem with this.
\end{description}

\end{document}

anything else

Sorry if I missed an option It seems the initial \paragraph{something} is not taken into account in the count of characters for wrapping. I see a similar problem with \item[something] commands. A command like \emph{...}or\textcolor{olive}{...} placed after or before \paragraph{something} also gets ignored.

cmhughes commented 9 months ago

Apologies for the delay, I'm hoping to get to this in the next few days.

OlivierMartineau commented 9 months ago

Are you kidding? I don't see a delay, you're pretty quick to respond, dear. Thank you very much 🙏

cmhughes commented 9 months ago

attempt 1

If we start with

modifyLineBreaks:
  textWrapOptions:
      columns: 60
      when: after
      blocksBeginWith:
        other: \\paragraph\{[^}]+?\}
      blocksFollow:
        headings: 0                       # 0/1
        other: \\\]|\\item(?:\h|\s*\[)
indentRules:
  item: " "

then we receive

\documentclass{article}
\begin{document}

\paragraph{So what?} This long line is not perfectly
wrapped, it seems the initial `paragraph{something}' is not
taken into account in the count of characters. The
following lines get nicely wrapped as intended, no problem
with this. \paragraph{And with colors?} \emph{This long
line is not perfectly wrapped, it seems the command `emph'
command is not taken into in the count of characters. The
following lines get nicely wrapped as intended, no problem
with this.}

\begin{description}
    \item[Thing1:] This long line is not perfectly wrapped,
     it seems the initial `item[Thing1:]' is not taken into
     account in the count of characters. The following
     lines get nicely wrapped as intended, no problem with
     this.
    \item[Thing2:] This long line is also not perfectly
     wrapped, it seems the initial `item[Thing2:]' is not
     taken into account in the count of characters either.
     The following lines get nicely wrapped as intended, no
     problem with this.
\end{description}

\end{document}
----|----|----|----|----|----|----|----|----|----|----|----|
   5   10   15   20   25   30   35   40   45   50   55   60

This is not quite as desired.....

attempt 2

modifyLineBreaks:
  textWrapOptions:
      columns: 60
      when: after
      blocksFollow:
        headings: 0                       # 0/1
        other: \\\]|\\item(?:\h|\s*\[)|\\paragraph\{
indentRules:
  item: " "
  paragraph: ""
indentAfterItems:
  document: 1
itemNames:
  paragraph: 1

gives

\documentclass{article}
\begin{document}

\paragraph{So what?} This long line is not perfectly
wrapped, it seems the initial `paragraph{something}' is not
taken into account in the count of characters. The
following lines get nicely wrapped as intended, no problem
with this.
\paragraph{And with colors?} \emph{This long line is not
perfectly wrapped, it seems the command `emph' command is
not taken into in the count of characters. The following
lines get nicely wrapped as intended, no problem with
this.}

\begin{description}
    \item[Thing1:] This long line is not perfectly wrapped,
     it seems the initial `item[Thing1:]' is not taken into
     account in the count of characters. The following
     lines get nicely wrapped as intended, no problem with
     this.
    \item[Thing2:] This long line is also not perfectly
     wrapped, it seems the initial `item[Thing2:]' is not
     taken into account in the count of characters either.
     The following lines get nicely wrapped as intended, no
     problem with this.
\end{description}

\end{document}
----|----|----|----|----|----|----|----|----|----|----|----|
   5   10   15   20   25   30   35   40   45   50   55   60
cmhughes commented 8 months ago

Let me know if you need anything else from me on this.

OlivierMartineau commented 8 months ago

Thank you for your answer.

You seem to think it should be some configuration problem... And you may very be right: I think my comprehension is not entirely correct.

What I want is every line to be wrapped at around 100 characters, including \item[ What I see is that if I have more than one \somecommand{argument} on the same line (e.g. both \paragraph{this} and \cite{that}) the line is wrapped around 120 characters. And that's the exact point that I don't get.

Here is my whole configuration, if you mind checking it. I added \\\w+\{ so that any command is taken into account for blocksFollow. But I have to confess that I'm not really sure what this command does exactly. I thank you wholeheartedly for your amazing work and support, and don't take it wrong, but ... I find the documentation a bit confusing...

onlyOneBackUp: 1
indentAfterItems:
  itemize: 1
  itemize*: 1
  enumerate: 1
  enumerate*: 1
  description: 1
  description*: 1
  list: 1
modifyLineBreaks:
  preserveBlankLines: 1
  condenseMultipleBlankLinesInto: 1
  textWrapOptions:
    columns: 100 # number of columns
    multipleSpacesToSingle: 1
    removeBlockLineBreaks: 1
    when: before # before/after
    comments:
      wrap: 1
      inheritLeadingSpace: 1
    blocksFollow:
      headings: 1
      commentOnPreviousLine: 1
      par: 1
      blankLine: 1
      verbatim: 0
      filecontents: 1
      other: \\\]|\\item\s*(?:\h|\[)|\\\w+\{
    blocksBeginWith:
      A-Z: 1
      a-z: 1
      0-9: 0
      other: 0 # regex
    blocksEndBefore:
      commentOnOwnLine: 1
      verbatim: 0
      filecontents: 1
      other: \\begin\{|\\\[|\\end\{
    huge: overflow # forbid mid-word line breaks overflow
    separator: ''