1

I have a LaTeX-document where I have lines of the form

\section*{3.1}
\addcontentsline{toc}{section}{3.1}

or in general

\section*{x.y}
\addcontentsline{toc}{section}{x.y}

for x between 1 and 31 and y between 1 and 12 inclusively, (dates). I would like to change those lines to the form

\section*{x.y.}
\addcontentsline{toc}{section}{x.y.}

Is there some easy script to do that?

| improve this question | | | | |
0

You can use sed, match the before and after parts of the line and put a . in the middle. The command s/REGEXP/REPLACEMENT/ performs a regular expression replacement. \(…\) delimits a group; \1 and \2 in the replacement text refer to these groups.

sed <original.tex >replaced.tex \
    -e 's/^\(\\section\\*{[0-9]*\.[0-9]*\)\(}\)$/\1.\2/' \
    -e 's/^\(\\addcontentsline{toc}{section}{[0-9]*\.[0-9]*\)\(}\)$/\1.\2/'

Another way to use sed is to replace the last } by .} if the line matches the desired pattern:

sed <original.tex >replaced.tex \
    -e '/^\\section\\*{[0-9]*\.[0-9]*}\)$/ s/}$/.&/' \
    -e '/^\\addcontentsline{toc}{section}{[0-9]*\.[0-9]*}/ s/}$/.&/'

You can also use \| to combine two patterns but in this case I think it makes the code shorter but less clear.

If you want to replace the file in place, under Linux, you can use

sed -i -e … somefile.tex
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.