2012-08-02 19 views
12

Chciałbym dostosować xtable do eksportu do LaTeX. Wiem, że niektóre pytania dotyczą tutaj xtable, ale nie mogłem znaleźć konkretnych rzeczy, których szukam.Dostosuj xtable

Oto przykład jak moja tabela może wyglądać tak:

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] <- "" 

który tworzy:

  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 

W rzeczywistości, ta tabela jest importowane z pliku .csv-jak data.frame z my.table <- read.delim("filename.csv", sep=",", header=TRUE)

Teraz tworzę tabelę LaTeX z 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) 

Oto otrzymany kod LaTeX:

\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, a teraz to jest to, co chciałbym zmienić:

1) Włóż \midrule po drugim rzędzie zamiast po pierwszym . 2) Zmienne kolory wierszy tabeli, wstawiając \rowcolors{2}{gray!25}{white} w środowisku sidewaystable (lub normalnym table). 3) Obracanie nazw kolumn o 45 ° 4) Wstaw \centering zamiast center - środowisko w przypadkach, gdy chcę wyśrodkować tabelę.

Jakieś pomysły, jak to osiągnąć?

+2

(1) mogą być wykonywane ręcznie przy użyciu 'add.to.row'. Inni myślę, że być może będziesz musiał użyć innego narzędzia. 'latex' w ** Hmisc ** jest zwykle bardziej elastyczny niż' xtable'. – joran

Odpowiedz

11

Trzeba trochę przetwarzaniem wstępnym, dodatkowy argument przekazany do print.xtable i trochę 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") 

Jeśli potrzebujesz innego środowiska zaczepy niż tabular można 1) dodać nową zmienną:

TABULAR = "tabular" 

2) Przełóż to wartość print.xtable tak:

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

3) Zmiana postprocessingu dla naprzemiennych kolorach:

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

Wynik:

enter image description here

+1

Nie trzeba już bezpośrednio zastępować środowiska "centrum". Podniosłem wsparcie dla polecenia '\ centering' jako problemu (# 2104) i zostało to naprawione przez Davida Scotta kilka miesięcy temu. – Alastair

+0

Witaj @redmode, czy mógłbyś dodać rozwiązanie xtable do tego pytania https://stackoverflow.com/questions/43098950/how-to-rotate-a-table-left-margin-name-with-knitr-and-xtable ? – skan