Doing this with awk
will be tricky as (AFAIK) it struggles with non-greedy regular expressions. Note that we'll need them to be non-greedy because of lines such as
\emph{multiple} occurrences on the \emph{same line}
Perl
can handle this pretty easily; you run the following script as
perl emphasize.pl myfile.tex
which will output to the terminal. Of course, you can output it into another file, such as
perl emphasize.pl myfile.tex> emphasizedtext.tex
Here's a test case:
myfile.tex
\documentclass{article}
\begin{document}
\emph{first} word is \emph{emphasized} and here's another \emph{one}.
here's another \emph{word} that is \emph{also} emphacized
\begin{enumerate}
\item \emph{should} also work
\item anywhere \emph{else too with multiple words}
\end{enumerate}
will struggle with nested: \emph{here is a \emph{nest}}
\end{document}
emhpasize.pl
#!/usr/bin/perl
use strict;
use warnings;
# for emphasized content
my @emphcontent=();
my $emphphrase='';
my $counter=0;
# loop through the lines in the INPUT file
while(<>)
{
# check for
# \emph{}...
# and make sure not to match
# %\emph{}...
# which is commented
if($_ =~ m/\\emph{(.*?)}/ and $_ !~ m/^%/)
{
# store the emphasized content
while($_ =~ m/\\emph{(.*?)}/g){
push(@emphcontent,$1);
}
}
}
# output the emphasized content
foreach $emphphrase (@emphcontent)
{
$counter++;
print $counter,". ",$emphphrase,"\n";
}
exit
output
1. first
2. emphasized
3. one
4. word
5. also
6. should
7. else too with multiple words
8. here is a \emph{nest
Or else you might prefer
# start the enumerate environment
print "\\begin{enumerate}\n";
# output the emphasized content
foreach $emphphrase (@emphcontent)
{
print "\\item ",$emphphrase,"\n";
}
# end the enumerate environment
print "\\end{enumerate}\n";
which gives
\begin{enumerate}
\item first
\item emphasized
\item one
\item word
\item also
\item should
\item else too with multiple words
\item here is a \emph{nest
\end{enumerate}
Limitations
As you'll see, this script fails with nested {}
and I have no idea how to fix it without some very convoluted code.