0

have a long table and I would like to do a custom column width, with a footnote,

the R code looks like below and align() to specific column width does not working? many thanks in advance,

x <- matrix(rnorm(1000), ncol = 2)
x.big <- xtable(x, caption = "A \\code{longtable} spanning several pages")

add.to.row <- list(pos = list(0), command = NULL)
command <- paste0("\\hline\n\\endhead\n",
                  "\\hline\n",
                  "\\multicolumn{", dim(x)[2] + 1, "}{l}",
                  "{\\footnotesize Continued on next page}\n",
                  "\\endfoot\n",
                  "\\endlastfoot\n")
add.to.row$command <- command
print(x.big,
      align=c(

        "p{0.1cm}|","p{0.1cm}|",
        "p{20cm}|"),

      hline.after=c(-1), add.to.row = add.to.row, file="x.big.tex",
      tabular.environment = "longtable")

1 Answer 1

2

mwe

With so may clues ("does not working" plus an incomplete
R code and nothing about the LaTeX part where it should work), what is really going wrong it is impossible to know.

BTW, there are nothing wrong with that align (except for the absurd widths for this data). Instead, one evident problem in that the caption will be at the bottom of several pages! Another probable mistakes are:

  1. Use of \code{} that is unknown for a standard document.
  2. Do not load the xtable (library(xtable))
  3. Export the table to x.big.tex when the code of the question is part of R chunk is a Sweave/knitr document to be showed directly, or alternatively do not import the table in the main LaTeX document (i.e, \input{x.big.tex}).
  4. Use longtable when xtable.floating = TRUE (not fatal but will produce an annoying warning)

Considering all of this, a working code, showing columns of different widths, could be:

Example.Rnw:

\documentclass{article}
\usepackage{longtable,array}
\begin{document}
<<results='asis',echo=F>>=
library(xtable)
options(xtable.floating = FALSE)
x <- matrix(rnorm(100), ncol = 2)
x.big <- xtable(x, 
align=c("|c","|>{\\centering}p{6em}","|>{\\centering\\arraybackslash}p{12em}|"),  
                caption = "A \\texttt{longtable} spanning several pages")
add.to.row <- list(pos = list(0), command = NULL)
command <- paste0("\\hline\n\\endhead\n",
                  "\\hline\n",
                  "\\multicolumn{", dim(x)[2] + 1, "}{l}",
                  "{\\footnotesize Continued on next page}\n",
                  "\\endfoot\n",
                  "\\endlastfoot\n")
add.to.row$command <- command
print(x.big,
      hline.after=c(-1), 
      add.to.row = add.to.row, 
      tabular.environment = "longtable",
      caption.placement ="top"
      )
@
\end{document}

NOTE: The vertical rules are only to see the width of the columns in this example. In real tables please avoid vertical rules and use booktabs for the horizontal rules.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .