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

  • SEARCH
  • Home
  • 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 8870391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:43:02+00:00 2026-06-14T17:43:02+00:00

I’d like to customize xtable for export into LaTeX. I know there are some

  • 0

I’d like to customize xtable for export into LaTeX. I know there are some questions abot xtable here, but I couldn’t find the specific things I’m looking for.

Here is an example of how my table might look like:

my.table <- data.frame(Specifiers=c("","Spec1", "Spec2", "Spec3"),
    Values1 = c("N=10", 1.03, 1.71, 2.25),
    Values2 = c("N=20", 1.32, 1.79, 2.43))
colnames(my.table)[1] <- ""

Which creates:

         Values1 Values2
1          N=10    N=20
2 Spec1    1.03    1.32
3 Spec2    1.71    1.79
4 Spec3    2.25    2.43

In fact, this table is imported from a .csv-file as data.frame with my.table <- read.delim("filename.csv", sep=",", header=TRUE)

Now I create a LaTeX table with xtable:

latex.tab <- xtable(my.table, caption=c("Stats"))
print(latex.tab, file="Summarystats.tex",
  floating.environment='sidewaystable',
  include.rownames=FALSE,
  booktabs=TRUE,
  latex.environment=NULL)

Here is the resulting LaTeX code:

\begin{sidewaystable}[ht]
\begin{tabular}{lllllll}
  \toprule
 & Values1 & Values2 \\ 
  \midrule
               N=10  &  N=20 \\
     Spec1  &  1.03  &  1.32 \\
     Spec2  &  1.71  &  1.79 \\
     Spec3  &  2.25  &  2.43 \\

   \bottomrule
\end{tabular}
\end{sidewaystable}

Ok, and now this is what I’d like to change:

1) Insert \midrule after the second row instead of after the first.
2) Alternating colours of the rows of this table by inserting \rowcolors{2}{gray!25}{white} within the sidewaystable (or normal table) environment.
3) Rotating column names by 45°
4) Insert \centering instead of the center-environment in cases when I want to center the table.

Any ideas on how to achieve this?

  • 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-14T17:43:04+00:00Added an answer on June 14, 2026 at 5:43 pm

    You need some pre-processing, extra argument passed to print.xtable and some post-processing:

    my.table <- data.frame(Specifiers=c("","Spec1", "Spec2", "Spec3"),
                           Values1 = c("N=10", 1.03, 1.71, 2.25),
                           Values2 = c("N=20", 1.32, 1.79, 2.43))
    colnames(my.table)[1] <- ""
    
    # Pre-processing: rotates column names by 45 degrees
    head = apply(as.array(names(my.table)), 1, function(x) paste("\\rotatebox{45}{", x, "}"))
    head = paste(head, c(rep("&", length(head)-1), "\\\\\n"), collapse="")
    
    latex.tab <- xtable(my.table, caption=c("Stats"))
    ltable = print(latex.tab, file="", # File is empty, post-processing needed
          floating.environment='sidewaystable',
          include.rownames=FALSE,
          include.colnames=FALSE, # No colnames
          booktabs=TRUE,
          latex.environment="center", # Or NULL
          # Adds some extra-text after the rows specified in pos.
          # Adds new \midrule and comments old one.
          # Adds pre-processed names of columns
          add.to.row=list(pos=as.list(c(0, 0, 1)), command=as.vector(c(head, "%", "\\midrule\n"))))
    
    # Post-processing: replaces \begin{center} with \centering
    ltable = sub("\\begin{center}\n", "\\centering\n", ltable, fixed=TRUE)
    ltable = sub("\\end{center}\n", "\n", ltable, fixed=TRUE)
    
    # Post-processing: adds alternating colours
    ltable = sub("\\begin{tabular}",
                 "\\rowcolors{2}{gray!25}{white}\n\\begin{tabular}",
                ltable, fixed=TRUE)
    
    # Writes output to the file
    cat(ltable, file="Summarystats.tex")
    

    If you need other tabs environment than tabular you can
    1) add new variable:

    TABULAR = "tabular"
    

    2) Pass it’s value to print.xtable like this:

    ...
    tabular.environment=TABULAR,
    ...
    

    3) Change your post-processing for alternating colors:

    ltable = sub(sprintf("\\begin{%s}", TABULAR),
                 sprintf("\\rowcolors{2}{gray!25}{white}\n\\begin{%s}", TABULAR),
                 ltable, fixed=TRUE)
    

    Result:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words

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.