General overview
The goal is to match the content of all \foo occurrences to transform it into <p>content of \foo</p>.
The details
The goal is to match some LaTeX macro’s content from it opening bracket to the closing bracket.
But, then, two problems could occur. With greedy, if their is a closing bracket later after the macro ending in the same line like in lorem ipsum \foo{dolor} sit amet et consectetur \bar{}, then s/\\foo{.*}/\1/ will match dolor} sit amet et consectetur \bar{}.
But, with non-greedy, I could match the closing bracket of a second macro inside \\foo. As example with lorem ipsum \foo{dolor \bar{sit amet} et consecteur} quia adipt with s/\\foo{.\{-}}/\1/ will match dolor \bar{sit amet.
In both little and greedy cases I fail to match the macro content and only the macro content.
The Question
So, how to match the macro content from the opening bracket to the corresponding closing bracket?
Alternative question: am I wrong to use sed, and then should I use a more dedicated LaTeX parsing tool?
perl provides this functionality with a package Text::Balanced
I used perl to fix Latex output of the following form:
From:
To:
which yields:
with the following code:
The commented lines were used to debug the code. The following link describes the package:
https://metacpan.org/pod/Text::Balanced