I have a string that is a LaTeX table. I’m trying to find the n-th (let’s say third) column and wrapping everything inside, say \emph{} without matching the delimiting dollar signs.
I’m looking for the first &...& which is second column. Then find the next &...& which is second grouping and by no coincidence a third column in the table.
My dummy example works but is a bit different, because it has text between two &...&. There’s a little thing I will tackle at a later stage – I need to put & outside the \emph{} call using back- and forward-references.
xy <- "This is &more or less& a match and here is &another one&.\nSecond line with &occurrance 1& and &occurrance 2&"
gsub("(&.*?&)|(.*?&)(.*)(&.*?&)", "\\1\\2\\3\\\\emph{\\4}", xy, perl = TRUE)
[1] "This is &more or less& a match and here is \\emph{&another one&}.\nSecond line with &occurrance 1& and \\emph{&occurrance 2&}"
When I kick it up a notch to a read set with LaTeX tables (bam!), it’s a bit different. There are no characters between two &...&, which means that one & borders two columns. Having that in mind I removed the (.*). No matter what I try, I can’t get this to work. Any tips?
library(xtable)
data(tli)
tli.table <- xtable(tli[1:5,])
x <- print.xtable(tli.table, print.results = FALSE, include.rownames = FALSE)
cat(x)
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Thu Jul 26 14:13:39 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllr}
\hline
grade & sex & disadvg & ethnicty & tlimth \\
\hline
6 & M & YES & HISPANIC & 43 \\
7 & M & NO & BLACK & 88 \\
5 & F & YES & HISPANIC & 34 \\
3 & M & YES & HISPANIC & 65 \\
8 & M & YES & WHITE & 75 \\
\hline
\end{tabular}
\end{center}
\end{table}
gsub("(&.*?&)(&.*?&)", "\\1\\\\emph{\\2}", x, perl = TRUE)
Assuming 1st column is
n <- 1(and notn <- 0), the regex that you should use for replacement of n-th column should be:and replacement string then has to be
\\1\\\\emph{\\2}\\3.So your replacement code is: