Geoff
Geoff

Reputation: 1007

R and Latex using xtable with baseline alignment

I'm using knitr/sweave to dynamically produce and include R code into my latex document. A minimal code I have is:

list <- c("Bike",  "Train", "Bike",  "Bike",  "Bike",  "Train", "Bike",  "Bike", "Bike",  "Moped", "Bus",   "Moped", "Train", "Bus", "Moped", "Bike","Moped")

library(xtable)
print(xtable(table(list)),floating=FALSE,booktabs=TRUE)

This produces as output:

\begin{tabular}{rr}
  \toprule
 & list \\ 
  \midrule
Bike &   8 \\ 
  Bus &   2 \\ 
  Moped &   4 \\ 
  Train &   3 \\ 
   \bottomrule
\end{tabular}

What I want is to top align the tabular environment to the baseline, i.e. \begin{tabular}[t]{rr}...\end{tabular}.

Looking through the xtable documentation there is table.placement but this introduces a table environment. I tried tabular.environment and set it to tabular[t] but that didn't work as it gave \begin{tabular[t]}

How can this issue be solve?

Upvotes: 0

Views: 82

Answers (1)

user2554330
user2554330

Reputation: 44788

The xtable package doesn't support adding that optional argument, but you could add it yourself by editing the result. For example,

list <- c("Bike",  "Train", "Bike",  "Bike",  "Bike",  "Train", "Bike",  "Bike", "Bike",  "Moped", "Bus",   "Moped", "Train", "Bus", "Moped", "Bike","Moped")

library(xtable)
lines <- print(xtable(table(list)), 
               floating = FALSE, booktabs = TRUE, print.result = FALSE)
lines <- sub("\\begin{tabular}", "\\begin{tabular}[t]", lines, fixed = TRUE)
cat(lines)
#> % latex table generated in R 3.6.1 by xtable 1.8-4 package
#> % Mon Jan 13 09:02:27 2020
#> \begin{tabular}[t]{rr}
#>   \toprule
#>  & list \\ 
#>   \midrule
#> Bike &   8 \\ 
#>   Bus &   2 \\ 
#>   Moped &   4 \\ 
#>   Train &   3 \\ 
#>    \bottomrule
#> \end{tabular}

Upvotes: 1

Related Questions