Search code examples
rlatexsweavextable

How do I print superscripts in a table using xtable and sweave?


So my problem statement is as follows : I have defined a data frame in my Sweave document (.Rnw extension) using the following code:

<<label=table2_1,echo=FALSE>>=
    table2_1_rows <- c('Students with compulsory Evaluations',
            'Teachers with compulsory evaluations1',
            'Teachers without Evaluation2',
            'Students without compulsory evaluations3'
            )
    table2_1_data <- c(1,2,3,4)
    table2_1final <- data.frame(table2_1_rows,table2_1_data)
@

<<label=tab1,echo=FALSE,results=tex>>=

    print(xtable(table2_1final,caption= ' ',align="|c|c|c|c|c|c|"),include.rownames=FALSE)
@

How do I make xtable print the numbers 1,2,3 (following the word Evaluation) as superscripts?


Solution

  • The actual superscript is quite easy. \\textsuperscript{1} in conjunction with sanitize.text.function = identity will get you there.

    I had to make some other changes to your example. There are too many columns in align and the underscores in the variable names cause problems compiling tex.

    <<label=table2_1,echo=FALSE>>=
    require(xtable)
        table2_1_rows <- c('Students with compulsory Evaluations',
                'Teachers with compulsory evaluations\\textsuperscript{1}',
                'Teachers without Evaluation\\textsuperscript{2}',
                'Students without compulsory evaluations\\textsuperscript{3}'
                )
        table2_1_data <- c(1,2,3,4)
        table2_1final <- data.frame(table2_1_rows,table2_1_data)
        names(table2_1final) <- c("rows", "data")
    @
    
    <<label=tab1,echo=FALSE,results=tex>>=
    
        print(xtable(table2_1final,caption= ' ',align="|c|c|c|"),include.rownames=FALSE, 
              sanitize.text.function = identity)
    @