How to use pgfplots table in Latex
A few commonly used methods to construct tables in latex.
Table loading methods
1. Load a table through reading inline data
Example 1
- \pgfplotstabletypeset [<format>] {<inline data>}
- col sep = {<character>}
- row sep = {<character>}
- columns/{<column name>}/.style = {<type>}
Output:
2. Load a table through reading file
The following screenshot shows file “example.dat” and file “example.csv”.
Example 2(a)
- \pgfplotstabletypeset [<format>] {<file name>}
- Lines starting with
#
in files are considered to be comment lines and are ignored
Outputs are exactly the same as the output in Example 1.
Example 2(b)
Load a table such that the output table only contains several specific columns.
- columns={<cloumn name>,<column name>,···}
Output:
Example 2(c)
Load a table such that the output table only contains several specific rows.
- skip rows between index = {<begin>} {<end>}
Output:
Example 2(d)
Load a table without column names line in the text file.
The following screenshot shows file “example2.dat”. The first line in this file is not the column names line.
- columns/<column name> or <column index>/.style = {column name = <character>}
Output:
Row and Column formatting methods
Example 3(a)
Style the first row, last row, first column, last column and any designated column.
- column type = r/l/c: align right, left or center.
- every head row/.style = {}, every last row/.style = {}, every first column/.style = {}, every last column/.style = {}
- before row = {<TEX code>}, after row = {<TEX code>}: contain TEX code which will be installed before the first cell in a row or after the last cell in a row.
- column type/.add={<before>}{<after>}
- columns/<column name>/.style = {}
Output:
Example 3(b)
Style all the even(or odd) columns(or rows).
- every even column/.style = { }, every odd column/.style = { } (Index starts with 0).
- every even row/.style = { }, every odd row/.style = { } (The first row is supposed to be a “head” row and does not count. Indexing starts with 0).
Output:
Example 3(c)
Using math expression to assign contents for designated column.
- columns/<column name>/.style = {preproc/expr = {<math expression>}}
- returns the raw input datum.
Output:
Single cell formatting methods
Example 4
Style the content of a single cell.
- every row <index> column <index>/.style = {postproc cell content/.style = {@cell content={ }}}
- returns the raw input datum.
Output:
Number formattting options
Example 5(a)
Display numbers in fixed format – round the number to a fixed number of digits.
- /pgf/number format/.cd
- fixed
- precision = {<number>}: sets the desired rounding precision.
- fixed zerofill: enables zero filling for any number drawn in fixed format.
- 1000 sep = {<character>} sets thousands separator.
Output:
Example 5(b)
Display numbers in scientific format.
- sci
- sci zerofill: enables zero filling for any number drawn in scientific format.
Output:
Example 5(c)
Display numbers either in fixed or sci format, depending on the order of magnitude.
- std
- std = <lower bound> : <upper bound>: define a range, using fixed format inside and sci format outside.
Output:
Example 5(d)
Display numbers as fractions.
- frac
- frac whole = <boolean>: configures whether complete integer parts shall be placed in front of the fractional part.
Output:
Example 5(e)
Display date format.
- date type = {<data format>} (other macros: \month, \monthname, \weekday, \weekdayname, \weekdayshortname).
Output:
Column creation methods
Example 6(a)
- create col/set list = {<comma-separated-list>}
Output:
Example 6(b)
- create col/expr = {<math expression>}
- \thisrow{<col name>}: returns the current row’s value stored in the designated column.
Output is exactly the same as the output in Example 6(a).
Output the generated tabular code into a latex file
Example 7
- outfile = <file name>
- \input{<file name>} generate exactly the same tabular.
Output is exactly the same as the output in Example 1.
Reference: Manual for Package PgfplotsTable (Version 1.13).