removing braces statements containing nested braces inside
3
votes
5
answers
663
views
A typical latex problem:
\SomeStyle{\otherstyle{this is the \textit{nested part} some more text...}}
Now I want to remove all
\SomeStyle{...}
but not the content. Content contains nested braces. The line above should become:
\otherstyle{this is the \textit{nested part} some more text...}
Questions:
1. Does any latex editor offer a way to do it?
2. What editor/script does it?
3. How to do it with sed? [🤓]
My solution is a bash script using sed.
1. prepare text: mark replace string with ascii bell, add newline after each brace
2. loop: find { -> add X to hold space, find } -> remove X from hold space, hold space empty -> remove closing }
3. restore newlines and ascii bell to previous
The script works but fails with:
\badstyle{w}\badstyle{o}\badstyle{r}\badstyle{d}
Iit will become:
wo}rd}
the branching to :f seems not to work.
F=$(sed 's|\\|\\\\|g;s|{|\\{|g' <<< "$1" )
# mark all removestrings with ascii bell and newline
# add newline after each { and }
SEDpre='
s|'"$F"'|\a%\n|g
s|\{|\{\n|g
s|\}|\}\n|g
'
SEDpost='
:a;N;$!ba;
s|\a%\n||g
s|\{\n|\{|g
s|\}\n|\}|g
'
# count the brackets
SED='
/\a%/{
:a
n
:f
/\{/{x;s|$|X|;x;ba}
/\}/{x;
s|X||;
/^$/{x;bb}
x
ba
}
}
b
:b
/\}/{
s|\}||;
N;
s|\n||;
/\a%/bf
}
'
sed -r -E "$SEDpre" "$2" | sed -rE "$SED" | sed -rE "$SEDpost"
Asked by Thierry Blanc
(153 rep)
Feb 6, 2025, 10:15 AM
Last activity: Feb 8, 2025, 07:19 PM
Last activity: Feb 8, 2025, 07:19 PM