Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
3  
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 at 21:50
    
Do you mean: \newcommand{\modifcmd}[1]{\color{red} #1}? –  A2009 Jun 15 at 22:09
4  
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 at 22:21
add comment

2 Answers 2

up vote 2 down vote accepted
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.

share|improve this answer
add comment

Try this:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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