# Reformatting a LaTeX document by perl

I have a LaTeX document in which the modifications in the text and equations are done in red color in order for them to be distinguished from unmodified text:

{\color{red} MODIFIED TEXT OR EQUATION GOES HERE.}


This modification can be a single word, a phrase, a sentence, a paragraph, or an equation. For example:

 \pi \rho \hat{\bf x} \int\omega_{\color{red} \theta}


or

{\color{red} This work presents the numerical study'' of convection in vertical annuli;}


How can I write a perl one-liner which can replace {\color{red} MODIFIED TEXT OR EQUATION GOES HERE.} to MODIFIED TEXT OR EQUATION GOES HERE.? I mean to remove {\color{red} and the right curly brace } throughout the entire LaTeX document.

-
A more sensible approach to this is to replace \color{red} with a macro. Then you can just redefine this macro as your see fit. In your latter case, with a blank. The braces most of the time are not functional, so I would not worry about them. –  Faheem Mitha Jun 15 '14 at 21:50
Do you mean: \newcommand{\modifcmd}[1]{\color{red} #1}? –  A2009 Jun 15 '14 at 22:09
Something along those lines, yes. Generally strategies that take advantage of the fact that TeX/LaTex are programming languages, albeit unconventional ones, are more robust and effective than alternatives that rely for example on text manipulation. –  Faheem Mitha Jun 15 '14 at 22:21

perl -p -i -e 's/{\\color{red}(.*)}/\1/g' filename


explanation of the flags(from http://stackoverflow.com/a/6302045/330830):

-p: Places a printing loop around your command so that it acts on each line of standard input. Used mostly so Perl can beat the pants off awk in terms of power AND simplicity :-)

-e: Allows you to provide the program as an argument rather than in a file. You don't want to have to create a script file for every little Perl one-liner.

-i: Modifies your input file in-place (making a backup of the original). Handy to modify files without the {copy, delete-original, rename} process.

-

Try this:

$echo "{\color{red} MODIFIED TEXT OR EQUATION GOES HERE.}" | \ perl -pe 's/\{\\color\{red\} (.*?)\}/$1/g'
MODIFIED TEXT OR EQUATION GOES HERE.

-