knitr::opts_chunk$set(collapse = TRUE)
cars  <- mtcars[1:10,1:5]


xTab is a function that produces a standard LaTeX table/tabular environment

For all of the examples that follow, we will be using the first ten rows and five columns of the mtcars data.frame, which we have saved in the variable 'cars'.

Important Notes


Standard xTab table

To produce a LaTeX table, simply pass a matrix or a data.frame to the xTab function.



Pass a string into the 'label' option. When not set, defaults to NULL.

xTab(cars, label = 'tab:mytable')


Place a caption at the top of the table.

xTab(cars, caption.top = 'my table')

Place a caption at the bottom of the table

xTab(cars, caption.bottom = 'my table')


Setting booktabs = TRUE sets the defaults of the toprule, midrule, and bottomrule arguments to \toprule, \midrule, and \bottomrule respectively. When using booktabs rules, regardless of whether you set booktabs = TRUE or set them individually, make sure to include \usepackage{booktabs} in your LaTeX document. When booktabs is not set, xTab looks for the value of kLat.xTab.booktabs, then kLat.booktabs, then defaults to FALSE.

xTab(cars, booktabs = TRUE)

If any of those options are explicitly set, the booktabs value has no effect.

xTab(cars, booktabs = TRUE, midrule = '\\hline')

Table Position

If not set, xTab will look for the value of kLat.xTab.position. If not set, defaults to 'ht'

xTab(cars, position = '!htbp')


By default, xTab will use the column names for the head of the table(as demonstrated in the above examples). To customize the table head simply pass in the appropriate LaTeX to the 'head' argument. The values of toprule and midrule are still used and should not be set in the head argument. If you do not want them included in your custom header, set either or both to NULL

     head = 'col1 & col 2 & col3 & \\eta & col5 \\\\')

Pass in NULL to avoid a table head. In the case of a NULL head, toprule and midrule will not be used.

xTab(cars, head = NULL)

To preserve the top and midrules, pass an empty string to head. Often, people want a toprule with no head or midrule. In that case, pass an empty string into the head argument and NULL into midule. You can then use the default toprule value (as depicted below), explicitly set toprule, or set booktabs = TRUE to set the toprule and bottomrule simultaneously.

lTab(cars, head = '', midrule = NULL)



When including row names in a table, by default xTab will use an empty column name for the 'rownames' column. When rows is not set, xTab looks for the value of kLat.xTab.rows, then klat.rows, then defaults to false.

xTab(cars, rows = TRUE)

Rownames with custom header

When providing a custom head with rows set to TRUE, remember to account for the extra column produced by the row names

     rows =  TRUE,
     head = 'rows & col1 & col2 & col3 & \\eta & col5 \\\\')

Row separator

Any arbitrary LaTeX command can be inserted between each row, but the most common are \hline and \midrule. To use \midrule, \usepackage{booktabs} must be declared in the preamble of the LaTeX document, but booktabs = TRUE does not need to be set on the table. When rowsep is not set, xTab looks for the value of kLat.xTab.rowsep, then kLat.rowsep, then defaults to an empty string.

xTab(cars, rowsep = '\\hline')
xTab(cars, rowsep = '\\midrule')


Column alginment

Explicitly set the column definitions. If this is set, colsep will have no effect and you must handle column separation within this declaration. Defaults to 'r' for numeric vector columns and 'l' for character vector columns.

xTab(cars, coldef ='rlc|l|p{5cm}')

Column separator

Place any arbitrary LaTeX between each column. Will have no effect if coldef is set.

xTab(cars, colsep = '|')

Try the knitLatex package in your browser

Any scripts or data that you put into this service are public.

knitLatex documentation built on May 2, 2019, 5:23 a.m.