In your question, you say:
If not, where shall I start as an exercice to write a simple rule
checking if, for instance, commas don't have a space before them " , "
and replace with the correct ", ".
I present a solution using latexindent.pl which is available and fully documented on ctan and should be included or available in your LaTeX distribution.
As an example, I use the following file, myfile.tex:
myfile.tex
% arara: indent: {replacement: noverb, settings: local, where: mysettings.yaml, output: tmp.tex}
Space before a comma , no space before comma,and no space after.
Multiple spaces before ,and no space after, and more.
Multiple spaces before , and after.
the call to arara
Note, in particular, the call to arara
% arara: indent: {replacement: noverb, settings: local, where: mysettings.yaml, output: tmp.tex}
which tells latexindent.pl to operate using the following
latexindent.pl -l=mysettings.yaml -o tmp.tex myfile.tex -rv
You can verify this by checking arara.log. The switches of latexindent.pl are fully documented.
mysettings.yaml
The YAML file mysettings.yaml is called by latexindent.pl and contains the following
replacements:
-
# remove all horizontal spaces before commas
# note: \h+ means 'at least one horizontal space'
substitution: s/\h+,/,/sg
-
# replace multiple horizontal spaces after commas with single space
# note: \h{2,} means 'at least two horizonal spaces'
substitution: s/,\h{2,}/, /sg
-
# add spaces following commas, if there isn't one already
# note: \H means 'a character that isn't horizontal whitespace'
substitution: s/,(\H)/, $1/sg
the output: tmp.tex
The call to arara says to output the file to tmp.tex which is as follows
% arara: indent: {replacement: noverb, settings: local, where: mysettings.yaml, output: tmp.tex}
Space before a comma, no space before comma, and no space after.
Multiple spaces before, and no space after, and more.
Multiple spaces before, and after.
notes
- there are a few replacement switches of
latexindent.pl which are documented at https://latexindentpl.readthedocs.io/en/latest/sec-replacements.html
mysettings.yaml has used examples of regular expressions, see for example, Jeffrey E. F. Friedl. Mastering Regular Expressions. ISBN: 0596002890
- if you intend to use the
overwrite mechanism of latexindent.pl, do always check the output carefully before using it on anything important, and note that it does always make at least one back-up
- the above replacements could almost certainly be combined into one single replacement, but I've deliberately put them separately in the hope that it eases understanding; perhaps you could try combining them, if you'd like :)
% arara: indent: { substitution: s/\h+/ /sg}seems to be the one I need to erase extra space in code. More generally, I don't get with yaml examples how to do a simple thing like% arara: indent: { substitution: " , " to ", " }. How shall I do ? – JeT May 09 '21 at 19:25