Formatting Tables with latexpdf
November 21, 2026
Tim Bergsma
Introduction
The R package ’latexpdf’ supports rich aesthetics for embedding tables in PDF documents, illustrated below. Be sure to set chunk option ’results’ to ’tex’. Although illustrations us as.ltable(), many arguments are passed through to as.tabular(). See also as.pdf() for creating stand-alone table images.
Examples
We make a sample data frame.
> x <- data.frame(+ study=c(rep('PROT01',5),NA),+ subject=rep(c(1001,1002),each=3),+ time=c(0,1,2,0,1,2),+ conc=c(0.12,34,5.6,.5,200,NA)+ )> x
study subject time conc1 PROT01 1001 0 0.122 PROT01 1001 1 34.003 PROT01 1001 2 5.604 PROT01 1002 0 0.505 PROT01 1002 1 200.006 <NA> 1002 2 NANow we try various invocations.
> library(latexpdf)> writeLines(as.ltable(x))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
|
> writeLines(as.ltable(x,environments=NULL))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
|
> writeLines(as.ltable(x,caption='Plasma Concentrations',label='pctab'))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
|
> writeLines(as.ltable(x,caption='Plasma Concentrations',cap.top=FALSE))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
|
> writeLines(as.ltable(x,grid=TRUE,caption='grid is TRUE'))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
|
> writeLines(as.ltable(x,grid=TRUE,caption='Includes Walls',walls=1,rules=c(1,2,1)))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
|
> writeLines(as.ltable(x,grid=TRUE,caption='Custom Breaks',+ colbreaks=c(0,2,0),rowgroups=x$subject+ ))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
|
> writeLines(as.ltable(x,grid=TRUE,caption='Custom Justify',+ numjust='left',charjust='right'+ ))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
|
> writeLines(as.ltable(x,grid=TRUE,caption='Decimal Align',+ justify=c('center','left','right','decimal')+ ))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34 |
| PROT01 | 1001 |
2 |
5.6 |
| PROT01 | 1002 |
0 |
0.5 |
| PROT01 | 1002 |
1 |
200 |
1002 |
2 |
|
> writeLines(as.ltable(x,grid=TRUE,caption='Not Verbatim',+ justify=c('center','left','right','decimal'),+ verbatim=FALSE+ ))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 | 0 | 0.12 |
| PROT01 | 1001 | 1 | 34 |
| PROT01 | 1001 | 2 | 5.6 |
| PROT01 | 1002 | 0 | 0.5 |
| PROT01 | 1002 | 1 | 200 |
| 1002 | 2 |
> writeLines(as.ltable(x,grid=TRUE,caption='Custom Column Width',+ justify=c('center','left','right','decimal'),+ colwidth=c(NA,NA,NA,'2cm')+ ))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12
|
| PROT01 | 1001 |
1 |
34
|
| PROT01 | 1001 |
2 |
5.6
|
| PROT01 | 1002 |
0 |
0.5
|
| PROT01 | 1002 |
1 |
200
|
1002 |
2 |
|
> writeLines(as.ltable(x,caption='Row Colors',rowcolors=c('white','lightgray')))
| study | subject | time | conc |
|---|---|---|---|
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
|
> writeLines(+ as.ltable(+ x,+ caption='Row Groups',+ rowgroups=as.character(x$subject),+ rowgrouplabel='groups',+ rowgrouprule = 2+ )+ )
| groups | study | subject | time | conc |
|---|---|---|---|---|
| \multirow{3}{*}{1001} | PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 | |
| PROT01 | 1001 |
2 |
5.60 | |
| \multirow{3}{*}{1002} | PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 | |
1002 |
2 |
|
> writeLines(+ as.ltable(+ x,+ caption='Column Groups',+ colgroups=c('demographic','demographic','clinical','clinical')+ )+ )
| demographic | clinical | ||
|---|---|---|---|
| study | subject | time | conc |
| PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 |
| PROT01 | 1001 |
2 |
5.60 |
| PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 |
1002 |
2 |
| |
> writeLines(+ as.ltable(+ x,+ caption='Row and Column Groups',+ rowgroups=as.character(x$subject),+ colgroups=c('demographic','demographic','clinical','clinical'),+ rowgrouprule = 1,+ grid=TRUE+ )+ )
| demographic | clinical | |||
|---|---|---|---|---|
| study | subject | time | conc | |
| \multirow{3}{*}{1001} | PROT01 | 1001 |
0 |
0.12 |
| PROT01 | 1001 |
1 |
34.00 | |
| PROT01 | 1001 |
2 |
5.60 | |
| \multirow{3}{*}{1002} | PROT01 | 1002 |
0 |
0.50 |
| PROT01 | 1002 |
1 |
200.00 | |
1002 |
2 |
| ||