Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:47:23+00:00 2026-06-08T11:47:23+00:00

I have a string that is a LaTeX table. I’m trying to find the

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:47:26+00:00Added an answer on June 8, 2026 at 11:47 am

    Assuming 1st column is n <- 1 (and not n <- 0), the regex that you should use for replacement of n-th column should be:

    (?m)^(?=[^&\n\r]*&)((?:[^&]*&){n-1})\\s*([^&]*?)\\s*(&|\\\\)
                                    ↑
                                    └ replace this n-1 with real number
    

    and replacement string then has to be \\1\\\\emph{\\2}\\3.

    So your replacement code is:

    input <- "% latex table generated in R 2.15.1 by xtable 1.7-0 package\n% Thu Jul 26 17:49:09 2012\n\\begin{table}[ht]\n\\begin{center}\n\\begin{tabular}{rlllr}\n  \\hline\ngrade & sex & disadvg & ethnicty & tlimth \\\\ \n  \\hline\n  6 & M & YES & HISPANIC &  43 \\\\ \n    7 & M & NO & BLACK &  88 \\\\ \n    5 & F & YES & HISPANIC &  34 \\\\ \n    3 & M & YES & HISPANIC &  65 \\\\ \n    8 & M & YES & WHITE &  75 \\\\ \n   \\hline\n\\end{tabular}\n\\end{center}\n\\end{table}\n"
    
    n <- 1
    regex <- paste(c('(?m)^(?=[^&\n\r]*&)((?:[^&]*&){', n-1, '})\\s*([^&]*?)\\s*(&|\\\\)'), collapse='')
    cat(gsub(regex, "\\1\\\\emph{\\2}\\3", input, perl = TRUE))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really long table in LaTeX that spans several pages and is
I have a table with many values inside of it that might have single
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
I have string that look like Array that fetched from other webservice like this
I have string that displays UTF-8 encoded characters, and I want to convert it
I have a String that holds the string representation of a byte value. String
I have a string that contains HTML image elements that is stored in a
I have a String that looks like this String = Förpackning Flaska (375 ml)
I have a string that looks like this: <name>-<gender>-<age>.jpg I want to be very
I have a string that says 15:00:00 how can I limit the length of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.