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'))

Table 1: Plasma Concentrations
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
Table 2: Plasma Concentrations

> writeLines(as.ltable(x,grid=TRUE,caption='grid is TRUE'))

Table 3: 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)))

Table 4: Includes Walls
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+ ))

Table 5: Custom Breaks
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'+ ))

Table 6: Custom Justify
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')+ ))

Table 7: Decimal Align
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+ ))

Table 8: Not Verbatim
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')+ ))

Table 9: Custom Column Width
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')))

Table 10: Row Colors
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+ )+ )

Table 11: Row Groups
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')+ )+ )

Table 12: Column Groups
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+ )+ )

Table 13: Row and Column Groups
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