| 1 |
mtable_format_latex <- function(x, |
| 2 |
useDcolumn=getOption("useDcolumn",TRUE), |
| 3 |
colspec=if(useDcolumn) |
| 4 |
paste("D{.}{",LaTeXdec,"}{",ddigits,"}",sep="") |
| 5 |
else "l", |
| 6 |
LaTeXdec=".", |
| 7 |
ddigits=min(3,getOption("digits")), |
| 8 |
useBooktabs=getOption("useBooktabs",TRUE), |
| 9 |
toprule=if(useBooktabs) "\\toprule" else "\\hline\\hline", |
| 10 |
midrule=if(useBooktabs) "\\midrule" else "\\hline", |
| 11 |
cmidrule=if(useBooktabs) "\\cmidrule" else "\\cline", |
| 12 |
bottomrule=if(useBooktabs) "\\bottomrule" else "\\hline\\hline", |
| 13 |
interaction.sep = " $\\times$ ", |
| 14 |
sdigits=min(1,ddigits), |
| 15 |
compact=FALSE, |
| 16 |
sumry.multicol=FALSE, |
| 17 |
escape.tex=getOption("toLatex.escape.tex",FALSE), |
| 18 |
signif.notes.type=getOption("toLatex.signif.notes.type","include"), |
| 19 |
signif.notes.spec=getOption("toLatex.signif.notes.spec","p{.5\\linewidth}"), |
| 20 |
...) |
| 21 |
pf_mtable_format_latex(preformat_mtable(x), |
| 22 |
useDcolumn=useDcolumn, |
| 23 |
colspec=colspec, |
| 24 |
LaTeXdec=LaTeXdec, |
| 25 |
ddigits=ddigits, |
| 26 |
useBooktabs=useBooktabs, |
| 27 |
toprule=toprule, |
| 28 |
midrule=midrule, |
| 29 |
cmidrule=cmidrule, |
| 30 |
bottomrule=bottomrule, |
| 31 |
interaction.sep = interaction.sep, |
| 32 |
sdigits=sdigits, |
| 33 |
compact=compact, |
| 34 |
sumry.multicol=sumry.multicol, |
| 35 |
escape.tex=escape.tex, |
| 36 |
signif.notes.type=signif.notes.type, |
| 37 |
signif.notes.spec=signif.notes.spec, |
| 38 |
...) |
| 39 |
|
| 40 |
|
| 41 |
pf_mtable_format_latex <- function(x, |
| 42 |
useDcolumn=getOption("useDcolumn",TRUE), |
| 43 |
colspec=if(useDcolumn) |
| 44 |
paste("D{.}{",LaTeXdec,"}{",ddigits,"}",sep="") |
| 45 |
else "l", |
| 46 |
LaTeXdec=".", |
| 47 |
ddigits=min(3,getOption("digits")), |
| 48 |
useBooktabs=getOption("useBooktabs",TRUE), |
| 49 |
toprule=if(useBooktabs) "\\toprule" else "\\hline\\hline", |
| 50 |
midrule=if(useBooktabs) "\\midrule" else "\\hline", |
| 51 |
cmidrule=if(useBooktabs) "\\cmidrule" else "\\cline", |
| 52 |
bottomrule=if(useBooktabs) "\\bottomrule" else "\\hline\\hline", |
| 53 |
interaction.sep = " $\\times$ ", |
| 54 |
sdigits=min(1,ddigits), |
| 55 |
compact=FALSE, |
| 56 |
sumry.multicol=FALSE, |
| 57 |
escape.tex=getOption("toLatex.escape.tex",FALSE), |
| 58 |
signif.notes.type=getOption("toLatex.signif.notes.type","include"), |
| 59 |
signif.notes.spec=getOption("toLatex.signif.notes.spec", |
| 60 |
paste0("p{",signif.notes.width,"\\linewidth}")), |
| 61 |
signif.notes.width=getOption("toLatex.signif.notes.width",".7"), |
| 62 |
show.parmtypes = nrow(x$parmtab) > 1, |
| 63 |
... |
| 64 |
){ |
| 65 |
|
| 66 |
signif.notes.type <- match.arg(signif.notes.type, |
| 67 |
c("include","append","tnotes","drop")) |
| 68 |
|
| 69 |
colsep <- " & " |
| 70 |
rowsep <- "\n" |
| 71 |
tabcr <- "\\\\" |
| 72 |
|
| 73 |
pt <- x$parmtab |
| 74 |
sst <- x$summary.stats |
| 75 |
leaders <- x$leaders |
| 76 |
headers <- x$headers |
| 77 |
eq.headers <- x$eq.headers |
| 78 |
outtypes <- x$outtypes |
| 79 |
|
| 80 |
ncols <- sapply(pt[1,],ncol) |
| 81 |
if(missing(compact) && all(ncols==1)) |
| 82 |
compact <- TRUE |
| 83 |
|
| 84 |
if(compact) modelsep <- " & " |
| 85 |
else modelsep <- " && " |
| 86 |
|
| 87 |
res <- NULL |
| 88 |
has.eq.headers <- length(eq.headers) > 0 |
| 89 |
|
| 90 |
for(j in 1:ncol(pt)){ |
| 91 |
|
| 92 |
name.j <- colnames(pt)[j] |
| 93 |
pt.j <- pt[,j] |
| 94 |
|
| 95 |
ncol.j <- unique(sapply(pt.j,ncol)) |
| 96 |
stopifnot(length(ncol.j)==1) |
| 97 |
|
| 98 |
for(i in 1:length(pt.j)){ |
| 99 |
ot.ij <- outtypes[i,j] |
| 100 |
pt.ij <- pt.j[[i]] |
| 101 |
if(ot.ij == "num"){ |
| 102 |
pt.ij <- sub("(\\*+)","^{\\1}",pt.ij) |
| 103 |
pt.ij <- sub("([eE])([-+]?[0-9]+)","\\\\textrm{\\1}\\2",pt.ij) |
| 104 |
#pt.ij <- centerAt(pt.ij,at=".",integers="dot") |
| 105 |
if(!useDcolumn) |
| 106 |
pt.ij[] <- paste0("$",pt.ij,"$") |
| 107 |
} else { |
| 108 |
if(useDcolumn) |
| 109 |
pt.ij[] <- paste0("\\multicolumn{1}{c}{",pt.ij,"}") |
| 110 |
} |
| 111 |
if(show.parmtypes){ |
| 112 |
pt.ij <- rbind(" ",pt.ij) |
| 113 |
} |
| 114 |
pt.ij <- apply(pt.ij,1,paste,collapse=colsep) |
| 115 |
pt.j[[i]] <- pt.ij |
| 116 |
} |
| 117 |
pt.j <- unlist(pt.j) |
| 118 |
if(has.eq.headers){ |
| 119 |
eq.header.j <- eq.headers[[name.j]] |
| 120 |
n.eq.j <- length(eq.header.j) |
| 121 |
eq.span <- ncol.j/n.eq.j |
| 122 |
eq.header.j <- paste0("\\multicolumn{",eq.span,"}{c}{",eq.header.j,"}") |
| 123 |
eq.header.j <- paste(eq.header.j,collapse=" & ") |
| 124 |
pt.j <- c(eq.header.j,pt.j) |
| 125 |
} |
| 126 |
if(length(sst)){ |
| 127 |
sst.j <- sst[[j]] |
| 128 |
if(!useDcolumn){ |
| 129 |
sst.j <- centerAt(sst.j,at=".",integers="dot") |
| 130 |
sst.j <- paste0("$",sst.j,"$") |
| 131 |
} |
| 132 |
else if(sumry.multicol){ |
| 133 |
has.dot <- grep(".",sst.j,fixed=TRUE) |
| 134 |
sumry.spec <- if(useDcolumn && has.dot) |
| 135 |
paste("D{.}{",LaTeXdec,"}{",sdigits,"}",sep="") |
| 136 |
else "r" |
| 137 |
sst.j <- paste0("\\multicolumn{1}{",sumry.spec,"}{",sst.j,"}") |
| 138 |
} |
| 139 |
sst.j <- as.matrix(sst.j) |
| 140 |
sst.j <- centerAt(sst.j,at=".",integers="dot") |
| 141 |
sst.j <- colexpand(sst.j,ncol.j) |
| 142 |
sst.j <- apply(sst.j,1,paste,collapse=colsep) |
| 143 |
pt.j <- c(pt.j,sst.j) |
| 144 |
} |
| 145 |
|
| 146 |
res <- cbind(res,pt.j) |
| 147 |
} |
| 148 |
l.leaders <- length(leaders) |
| 149 |
if(l.leaders){ |
| 150 |
leaders <- lapply(leaders,ldxp) |
| 151 |
if(show.parmtypes){ |
| 152 |
parmtypes <- rownames(x$parmtab) |
| 153 |
for(p in parmtypes){ |
| 154 |
leaders[[p]] <- rbind(p,leaders[[p]]) |
| 155 |
} |
| 156 |
} |
| 157 |
leaders <- do.call(rbind,leaders) |
| 158 |
|
| 159 |
leaders <- gsub(" x ",interaction.sep,leaders,fixed=TRUE) |
| 160 |
if(escape.tex) |
| 161 |
leaders <- LaTeXcape(leaders) |
| 162 |
else |
| 163 |
checkLaTeXcape(leaders,"row leaders") |
| 164 |
if(has.eq.headers) |
| 165 |
leaders <- rbind("",leaders) |
| 166 |
leaders <- format(leaders,justify="left") |
| 167 |
res <- cbind(leaders,res) |
| 168 |
} |
| 169 |
res <- apply(res,1,paste,collapse=modelsep) |
| 170 |
|
| 171 |
wdld <- as.integer(nzchar(leaders[1,])) |
| 172 |
l.headers <- length(headers) |
| 173 |
hi.rules <- character() |
| 174 |
if(length(headers)){ |
| 175 |
|
| 176 |
headers1 <- Map(structure,list(""),span=ncols) # To take care of multi-eqn models |
| 177 |
headers <- c(headers,list(headers1)) |
| 178 |
headers <- rev(headers) |
| 179 |
hspan <- list() |
| 180 |
|
| 181 |
for(k in 1:length(headers)){ |
| 182 |
hspan[[k]] <- sapply(headers[[k]],attr,"span") |
| 183 |
} |
| 184 |
ghspan <- mkGrpSpan(hspan) |
| 185 |
headers <- headers[-1] |
| 186 |
ghspan <- ghspan[-1] |
| 187 |
|
| 188 |
headers <- rev(headers) |
| 189 |
hspan <- rev(hspan) |
| 190 |
ghspan <- rev(ghspan) |
| 191 |
|
| 192 |
if(!compact) |
| 193 |
wdld <- wdld + 1 |
| 194 |
for(k in 1:l.headers){ |
| 195 |
|
| 196 |
header.k <- unlist(headers[[k]]) |
| 197 |
if(escape.tex) |
| 198 |
header.k <- LaTeXcape(header.k) |
| 199 |
else |
| 200 |
checkLaTeXcape(header.k,"column headings") |
| 201 |
hspan.k <- hspan[[k]] |
| 202 |
ghspan.k <- ghspan[[k]] |
| 203 |
header.k <- paste0("\n\\multicolumn{",hspan.k,"}{c}{",header.k,"}") |
| 204 |
if(l.leaders) |
| 205 |
header.k <- c("",header.k) |
| 206 |
headers[[k]] <- paste(header.k,collapse=modelsep) |
| 207 |
|
| 208 |
n.grps <- length(hspan.k) |
| 209 |
if(!compact){ |
| 210 |
hspan.k <- hspan.k + ghspan.k - 1 |
| 211 |
hi.rule.k.start <- cumsum(c(1,hspan.k[-n.grps] + 1)) + wdld |
| 212 |
} |
| 213 |
else |
| 214 |
hi.rule.k.start <- cumsum(c(1,hspan.k[-n.grps])) + wdld |
| 215 |
hi.rule.k.end <- hi.rule.k.start + hspan.k - 1 |
| 216 |
hi.rules.k <- paste0(cmidrule,"{",hi.rule.k.start,"-",hi.rule.k.end,"}") |
| 217 |
hi.rules.k <- paste(hi.rules.k,collapse="") |
| 218 |
hi.rules[k] <- hi.rules.k |
| 219 |
} |
| 220 |
|
| 221 |
headers <- unlist(headers) |
| 222 |
res <- c(headers,res) |
| 223 |
} |
| 224 |
|
| 225 |
res <- paste0(res,tabcr) |
| 226 |
|
| 227 |
|
| 228 |
l.headers <- length(headers) |
| 229 |
|
| 230 |
csum <- l.headers |
| 231 |
|
| 232 |
sectsep.at <- integer() |
| 233 |
sectseps <- character() |
| 234 |
|
| 235 |
sec.hrules.at <- integer() |
| 236 |
sec.hrules <- character() |
| 237 |
|
| 238 |
sectionrule <- midrule |
| 239 |
|
| 240 |
for(i in 1:nrow(pt)){ |
| 241 |
sectseps <- c(sectseps, sectionrule) |
| 242 |
sectsep.at <- c(sectsep.at, csum) |
| 243 |
csum <- csum + nrow(pt[[i,1]]) |
| 244 |
if(nrow(pt) > 1) |
| 245 |
csum <- csum + 1 |
| 246 |
} |
| 247 |
if(length(sst) && any(sapply(sst,length)>0)){ |
| 248 |
sectseps <- c(sectseps, sectionrule) |
| 249 |
sectsep.at <- c(sectsep.at, csum) |
| 250 |
} |
| 251 |
if(has.eq.headers){ |
| 252 |
sectsep.at <- sectsep.at + 1 |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
res <- .insert(res, |
| 257 |
c(sec.hrules.at,sectsep.at), |
| 258 |
c(sec.hrules, sectseps) |
| 259 |
) |
| 260 |
|
| 261 |
if(l.headers > 1 || l.headers > 0 && has.eq.headers){ |
| 262 |
n.rules <- l.headers - 1 |
| 263 |
if(has.eq.headers) |
| 264 |
n.rules <- n.rules + 1 |
| 265 |
hi.rules.at <- seq.int(n.rules) |
| 266 |
res <- .insert(res,hi.rules.at,hi.rules) |
| 267 |
} |
| 268 |
res <- c(toprule,res,bottomrule) |
| 269 |
|
| 270 |
tab.spec <- character() |
| 271 |
|
| 272 |
if(l.leaders) |
| 273 |
tab.spec <- c(tab.spec,"l") |
| 274 |
for(j in 1:ncol(pt)){ |
| 275 |
if(!compact) tab.spec <- c(tab.spec,"c") |
| 276 |
ncol.j <- ncol(pt[[1,j]]) |
| 277 |
tab.spec <- c(tab.spec,rep(colspec,ncol.j)) |
| 278 |
} |
| 279 |
|
| 280 |
total.ncol <- length(tab.spec) |
| 281 |
tab.spec <- paste(tab.spec,collapse="") |
| 282 |
|
| 283 |
signif.symbols <- x$signif.symbols |
| 284 |
if(length(signif.symbols)){ |
| 285 |
signif.template <- getOption("signif.symbol.toLatex.template", |
| 286 |
signif.symbol.toLatex.default.template) |
| 287 |
signif.symbols <- format_signif_latex(signif.symbols, |
| 288 |
signif.template, |
| 289 |
dec=LaTeXdec, |
| 290 |
width=nchar(bottomrule)) |
| 291 |
if(signif.notes.type=="include"){ |
| 292 |
signif.symbols <- paste0("\\multicolumn{",total.ncol,"}{", |
| 293 |
signif.notes.spec,"}{", |
| 294 |
paste0(signif.symbols,collapse="\n"), |
| 295 |
"}\\\\") |
| 296 |
res <- c(res, signif.symbols) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
tabbegin <- paste("\\begin{tabular}{",tab.spec,"}",sep="") |
| 301 |
tabend <- "\\end{tabular}" |
| 302 |
|
| 303 |
res <- c(tabbegin,res,tabend) |
| 304 |
if(length(signif.symbols)){ |
| 305 |
if(signif.notes.type=="append"){ |
| 306 |
signif.symbols <- paste0(signif.symbols,collapse="\n") |
| 307 |
res <- c(res,"\\par",signif.symbols,"\\par") |
| 308 |
} |
| 309 |
else if(signif.notes.type=="tnotes"){ |
| 310 |
signif.symbols <- c( |
| 311 |
"\\begin{tablenotes}", |
| 312 |
paste0(c("\\item",signif.symbols),collapse="\n"), |
| 313 |
"\\end{tablenotes}" |
| 314 |
) |
| 315 |
res <- c( |
| 316 |
"\\begin{threeparttable}", |
| 317 |
res, |
| 318 |
signif.symbols, |
| 319 |
"\\end{threeparttable}" |
| 320 |
) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return(res) |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
signif.symbol.toLatex.default.template <- c("Significance: ","$$sym \\equiv p < $val$","; ") |
| 330 |
|
| 331 |
format_signif_latex <- function(syms,tmpl,width,dec="."){ |
| 332 |
title <- tmpl[1] |
| 333 |
clps <- tmpl[3] |
| 334 |
tmpl <- tmpl[2] |
| 335 |
res <- title |
| 336 |
empty.title <- paste(rep(" ",nchar(title)),collapse="") |
| 337 |
|
| 338 |
ns <- length(syms) |
| 339 |
dotrepl <- paste0("{",dec,"}") |
| 340 |
for(i in 1:ns){ |
| 341 |
sym <- names(syms)[i] |
| 342 |
thrsh <- unname(syms[i]) |
| 343 |
thrsh <- gsub(".",dotrepl,thrsh,fixed=TRUE) |
| 344 |
res.i <- sub("$sym",sym,tmpl,fixed=TRUE) |
| 345 |
res.i <- sub("$val",thrsh,res.i,fixed=TRUE) |
| 346 |
if(i < ns) |
| 347 |
res.i <- paste0(res.i,clps) |
| 348 |
len <- length(res) |
| 349 |
res.l <- res[len] |
| 350 |
n.res.l <- nchar(res.l) |
| 351 |
n.res.i <- nchar(res.i) |
| 352 |
if(n.res.l+n.res.i <= width) |
| 353 |
res[len] <- paste0(res.l,res.i) |
| 354 |
else |
| 355 |
res <- c(res,paste0(empty.title,res.i)) |
| 356 |
} |
| 357 |
res |
| 358 |
} |