Package 'knitLatex'

Title: 'Knitr' Helpers - Mostly Tables
Description: Provides several helper functions for working with 'knitr' and 'LaTeX'. It includes 'xTab' for creating traditional 'LaTeX' tables, 'lTab' for generating 'longtable' environments, and 'sTab' for generating a 'supertabular' environment. Additionally, this package contains a knitr_setup() function which fixes a well-known bug in 'knitr', which distorts the 'results="asis"' command when used in conjunction with user-defined commands; and a com command (<<com=TRUE>>=) which renders the output from 'knitr' as a 'LaTeX' command.
Authors: John Shea [aut, cre]
Maintainer: John Shea <[email protected]>
License: GPL-3
Version: 0.9.0
Built: 2025-03-28 03:19:11 UTC
Source: https://github.com/coachshea/knitlatex

Help Index


knitLatex: Latex table helpers for knitr.

Description

This package was inspired by the xtable package, but allows for more fine_grained control, especially in regards to the longtable and supertabular (which is not included in xtable) environments. This package provides four functions to assist in using knitr with latex:

Details

  • xTab: creates a basic latex table.

  • lTab: creates a longtable evironment.

  • sTab: creates a supertabular environment.

  • knitr_sethooks: fixes a bug in the knit_hook chunk and provids a 'com' hook which turns knitr output into latex commands


sets and fixes knitr hooks

Description

fixes a well-known bug in the knit_hook 'chunk' and provides a hook entitle 'com'

Usage

knitr_sethooks()

Details

There is a well_known bug in the knit_hook 'chunk' which prevents using results = 'asis' in conjunction with user-defined hooks (including com, to be discussed next). Calling this function allows user-defined hooks to be called with results = 'asis' and get the expected result. This function also provides a knitr hook called 'com', by setting 'com = TRUE' in a knitr chunk, the resulting code is converted to a latex command. For example: '<<mytable, com=TRUE>>=' results in a latex command entitled '\mytable', which will produce the exact output that would have appeared in the spot of the chunk

Examples

knitr_sethooks()

Produces a latex longtable

Description

Produces a latex longtable

Usage

lTab(x, label = NULL, caption.firsthead = NULL, caption.head = NULL,
  caption.foot = NULL, caption.lastfoot = NULL,
  booktabs = .op("kLat.lTab.booktabs", "kLat.booktabs", FALSE),
  toprule = .book("kLat.toprule", booktabs, "\\toprule", "\\hline"),
  bottomrule = .book("kLat.bottomrule", booktabs, "\\bottomrule",
  "\\hline"), midrule = .book("kLat.midrule", booktabs, "\\midrule",
  "\\hline"), align = .op("kLat.lTab.align", "kLat.align", "center"),
  envir = getOption("kLat.lTab.envir", "longtable"),
  colsep = .op("kLat.lTab.colsep", "kLat.colsep", ""), coldef = .coldef(x,
  colsep), rowsep = .op("kLat.lTab.rowsep", "kLat.rowsep", ""),
  rows = .op("kLat.lTab.rows", "kLat.rows", FALSE), head = .header(x, rows),
  firsthead = NULL, foot = bottomrule, lastfoot = NULL)

Arguments

x

a data.frame or matrix to form the base of the table

label

set the table's label, defaults to an empty string

caption.firsthead, caption.head, caption.foot, caption.lastfoot

paces the caption in the firsthead, head, foot, or lastfoot respectively. It is important not to set a caption in an otherwise NULL section (although an empty string is acceptable) or strange bugs can occur. It is accaptable if the section was set by default as in head and foot. Consult the longtable documentation for a more detailed explanation of these options.

booktabs

logical value, if not set will use value of kLat.(xTab|sTab|lTab).booktabs, if not set will use value of kLat.booktabs, if not set defaults to FALSE. When TRUE toprule defaults to '\toprule', midrule to '\midrule', and botrule to '\bottomrule', when FALSE those values all default to '\hline'. Has no effect when toprule, midrule, and botrule are individually set.

toprule

sets the value for the top rule, if not set will be determined by the value of booktabs

bottomrule

sets the value for the bottom rule, if not set will be determined by the value of booktabs

midrule

sets the value for the mid rule, if not set will be determined by the value of booktabs

align

set the alignment of the environment, if not set will use value of kLat.(xTab|sTab|lTab).align, if not will use value of kLat.align, if not set defaults to 'center'

envir

set the environment for the table, if not set will use the value of kLat.(xTab|sTab|lTab).envir, if not set defaults to 'tabular', 'supertabular', and 'longtable' for xTab, sTab, and lTab respectively

colsep

separator to be used between columns (i.e. '|'), if not set will use the value of kLat.(xTab|sTab|lTab).colsep, if not set will use the value of kLat.colsep, if not set defaults to an empty string. If coldef is set this value is ignored and the separators must be specificed in the coldef

coldef

sets column definition i.e. \begin{tabular}{'align'}, if not set defaults to numeric = right, character = left

rowsep

the separaotr to be used between rows (i.e. '\hline'), if not set will use the value of kLat.(xTab|sTab|lTab).rowsep, if not set will use the value of kLat.rowsep, if not set defaults to an empty string

rows

logical value to determine if rownames are included in table, if not set will use the value of kLat.(xTab|sTab|lTab).rows, if not set will use the value of kLat.rows, if not set defaults to FALSE, if TRUE the column name for the rownames column defaults to an empty string

head

sets the value for the table header, defaults to the column names; if you set this be sure to end with '\\\\'

firsthead

header on first page of table only; defaults to header; if you set this, you are responsible for setting any \hline, \toprule, or \midrule lines

foot

sets value of the table footer, defaults to the value of botrule

lastfoot

footer on last page of table only

caption

the caption for the table, unlinke xTab and sTab, there is no caption.top or caption.bottom option in longtable

Examples

lTab(mtcars)

Produces a latex supertabular environment

Description

Produces a latex supertabular environment

Usage

sTab(x, label = NULL, caption.top = NULL, caption.bottom = NULL,
  caption.table = NULL, booktabs = .op("kLat.sTab.booktabs",
  "kLat.booktabs", FALSE), toprule = .book("kLat.toprule", booktabs,
  "\\toprule", "\\hline"), bottomrule = .book("kLat.bottomrule", booktabs,
  "\\bottomrule", "\\hline"), midrule = .book("kLat.midrule", booktabs,
  "\\midrule", "\\hline"), align = .op("kLat.sTab.align", "kLat.align",
  "center"), envir = getOption("kLat.sTab.envir", "supertabular"),
  colsep = .op("kLat.sTab.colsep", "kLat.colsep", ""), coldef = .coldef(x,
  colsep), rowsep = .op("kLat.sTab.rowsep", "kLat.rowsep", ""),
  rows = .op("kLat.sTab.rows", "kLat.rows", FALSE), firsthead = NULL,
  head = .header(x, rows), tail = bottomrule, lasttail = NULL)

Arguments

x

a data.frame or matrix to form the base of the table

label

set the table's label, defaults to an empty string

caption.top

sets the caption command placing it at the top of the table

caption.bottom

sets the caption command placing it at the bottom of the table

caption.table

sets '\tablecaption' option of supertabular, which allows for a default placement of the caption, see supertabular documentation for a more detailed explanation.

booktabs

logical value, if not set will use value of kLat.(xTab|sTab|lTab).booktabs, if not set will use value of kLat.booktabs, if not set defaults to FALSE. When TRUE toprule defaults to '\toprule', midrule to '\midrule', and botrule to '\bottomrule', when FALSE those values all default to '\hline'. Has no effect when toprule, midrule, and botrule are individually set.

toprule

sets the value for the top rule, if not set will be determined by the value of booktabs

bottomrule

sets the value for the bottom rule, if not set will be determined by the value of booktabs

midrule

sets the value for the mid rule, if not set will be determined by the value of booktabs

align

set the alignment of the environment, if not set will use value of kLat.(xTab|sTab|lTab).align, if not will use value of kLat.align, if not set defaults to 'center'

envir

set the environment for the table, if not set will use the value of kLat.(xTab|sTab|lTab).envir, if not set defaults to 'tabular', 'supertabular', and 'longtable' for xTab, sTab, and lTab respectively

colsep

separator to be used between columns (i.e. '|'), if not set will use the value of kLat.(xTab|sTab|lTab).colsep, if not set will use the value of kLat.colsep, if not set defaults to an empty string. If coldef is set this value is ignored and the separators must be specificed in the coldef

coldef

sets column definition i.e. \begin{tabular}{'align'}, if not set defaults to numeric = right, character = left

rowsep

the separaotr to be used between rows (i.e. '\hline'), if not set will use the value of kLat.(xTab|sTab|lTab).rowsep, if not set will use the value of kLat.rowsep, if not set defaults to an empty string

rows

logical value to determine if rownames are included in table, if not set will use the value of kLat.(xTab|sTab|lTab).rows, if not set will use the value of kLat.rows, if not set defaults to FALSE, if TRUE the column name for the rownames column defaults to an empty string

firsthead

header on first page of table only

head

header to appear at the top of every page of table

tail

footer on bottom of every page of table

lasttail

footer on last page of table only

Examples

sTab(mtcars)
sTab(mtcars,
  caption.top = 'my super table',
  booktabs = TRUE,
  rows = TRUE)

Produces a latex table

Description

Produces a latex table

Usage

xTab(x, label = NULL, caption.top = NULL, caption.bottom = NULL,
  position = getOption("kLat.xTab.position", "ht"),
  booktabs = .op("kLat.xTab.booktabs", "kLat.booktabs", FALSE),
  toprule = .book("kLat.toprule", booktabs, "\\toprule", "\\hline"),
  bottomrule = .book("kLat.botrule", booktabs, "\\bottomrule", "\\hline"),
  midrule = .book("kLat.midrule", booktabs, "\\midrule", "\\hline"),
  align = .op("kLat.xTab.align", "kLat.align", "center"),
  envir = getOption("kLat.xTab.envir", "tabular"),
  colsep = .op("kLat.xTab.colsep", "kLat.colsep", ""), coldef = .coldef(x,
  colsep), rowsep = .op("kLat.xTab.rowsep", "kLat.rowsep", ""),
  rows = .op("kLat.xTab.rows", "kLat.rows", FALSE), head = .header(x, rows),
  foot = bottomrule)

Arguments

x

a data.frame or matrix to form the base of the table

label

set the table's label, defaults to an empty string

caption.top

sets the caption command placing it at the top of the table

caption.bottom

sets the caption command placing it at the bottom of the table

position

sets the position of the table i.e. \begin{table}['position'], defaults to 'ht'

booktabs

logical value, if not set will use value of kLat.(xTab|sTab|lTab).booktabs, if not set will use value of kLat.booktabs, if not set defaults to FALSE. When TRUE toprule defaults to '\toprule', midrule to '\midrule', and botrule to '\bottomrule', when FALSE those values all default to '\hline'. Has no effect when toprule, midrule, and botrule are individually set.

toprule

sets the value for the top rule, if not set will be determined by the value of booktabs

bottomrule

sets the value for the bottom rule, if not set will be determined by the value of booktabs

midrule

sets the value for the mid rule, if not set will be determined by the value of booktabs

align

set the alignment of the environment, if not set will use value of kLat.(xTab|sTab|lTab).align, if not will use value of kLat.align, if not set defaults to 'center'

envir

set the environment for the table, if not set will use the value of kLat.(xTab|sTab|lTab).envir, if not set defaults to 'tabular', 'supertabular', and 'longtable' for xTab, sTab, and lTab respectively

colsep

separator to be used between columns (i.e. '|'), if not set will use the value of kLat.(xTab|sTab|lTab).colsep, if not set will use the value of kLat.colsep, if not set defaults to an empty string. If coldef is set this value is ignored and the separators must be specificed in the coldef

coldef

sets column definition i.e. \begin{tabular}{'align'}, if not set defaults to numeric = right, character = left

rowsep

the separaotr to be used between rows (i.e. '\hline'), if not set will use the value of kLat.(xTab|sTab|lTab).rowsep, if not set will use the value of kLat.rowsep, if not set defaults to an empty string

rows

logical value to determine if rownames are included in table, if not set will use the value of kLat.(xTab|sTab|lTab).rows, if not set will use the value of kLat.rows, if not set defaults to FALSE, if TRUE the column name for the rownames column defaults to an empty string

head

sets the value for the table header, defaults to the column names; if you set this be sure to end with '\\\\'

foot

sets value of the table footer, defaults to the value of botrule

Examples

xTab(mtcars)
xTab(mtcars, label='my table', caption.top='tab:mytable', booktabs=TRUE)
xTab(mtcars, head='col1 & col2 & \\eta\\\\')