1

My question

How can I replace strings in my code during compilation.

Context

So far I use arara like "magic" lines at the beginning of my code to call latexindent

% arara: pdflatex: { shell: yes }
% arara: pdflatex: { shell: yes }
% arara: indent: { overwrite : yes }
% arara: indent: { trace : yes }

It corrects the indentation perfectly.

I get the feeling I could use arara during compilation to correct systematically typsetting issues and/or replace string of characters.

Am I wrong about this ability through arara ?

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 ", ".

My poor knowledge in coding leads me to a manual "find and replace" in an editor. Inefficient...

JeT
  • 3,020
  • you can't edit the file while pdflatex is using it (well you can but it won't do anything useful) you could of course get arara to call sed or perl or any other batch editor to edit the file before running it, just as you are running latexindent. Personally I wouldn't do that as it means editing the file every time, and probably disturbing your editor which means it has to detect the edit and re-load the file every time you run latex which seems sub-optimal. – David Carlisle May 06 '21 at 10:00
  • @DavidCarlisle Very Clear, merci ! I'll follow your piece of advice. – JeT May 06 '21 at 10:03
  • latexindent has a replacement switch, which you might be able to use, see https://latexindentpl.readthedocs.io/en/latest/sec-replacements.html and, for arara, chapter 7, page 119 of version 6.1.0. But, this wouldn't be during compilation – cmhughes May 07 '21 at 16:47
  • @cmhughes Very helpful link thank you. It really helped to unlock the use of arara. Not sur I use it well though. % 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
  • 1
    @JeT see my answer :) hope it's helpful! – cmhughes May 15 '21 at 12:02
  • @cmhughes Cristal clear answer. Merci ! – JeT May 15 '21 at 14:48

1 Answers1

2

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 :)
cmhughes
  • 100,947