diff options
author | Karl Berry <karl@freefriends.org> | 2008-08-09 23:39:38 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2008-08-09 23:39:38 +0000 |
commit | f41811b55dfce3a5f59b953b68457b0aa5491baf (patch) | |
tree | c0346bf5128a5e21c5909eb116121b28de0fd24b /Master/texmf-dist/doc/latex/pgfplots/pgfplotstable.tex | |
parent | 607cf34a8ca9e08fbb89b52ee710467a72b5b99f (diff) |
pgfplots update (10aug08)
git-svn-id: svn://tug.org/texlive/trunk@10222 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/doc/latex/pgfplots/pgfplotstable.tex')
-rw-r--r-- | Master/texmf-dist/doc/latex/pgfplots/pgfplotstable.tex | 603 |
1 files changed, 531 insertions, 72 deletions
diff --git a/Master/texmf-dist/doc/latex/pgfplots/pgfplotstable.tex b/Master/texmf-dist/doc/latex/pgfplots/pgfplotstable.tex index e2e3c74fa37..f32467d7779 100644 --- a/Master/texmf-dist/doc/latex/pgfplots/pgfplotstable.tex +++ b/Master/texmf-dist/doc/latex/pgfplots/pgfplotstable.tex @@ -25,12 +25,14 @@ \usepackage{array} \usepackage{colortbl} \usepackage{booktabs} +\usepackage{eurosym} \pgfqkeys{/codeexample}{% every codeexample/.style={ width=4cm, /pgfplots/every axis/.append style={legend style={fill=graphicbackground}} }, + narrow/.style={width=7cm}, tabsize=4, } @@ -50,13 +52,19 @@ \section{Introduction} \PGFPlotstable\ is a lightweight sub-package of \PGFPlots\ which employs its table input methods and the number formatting techniques to convert tab-separated tables into tabulars. -It's input is a text file containing space separated rows, possibly starting with column names. Its output is a \LaTeX\ tabular which contains selected columns of the text table, rounded to the desired precision, printed in the desired number format (fixed point, integer, scientific etc). +Its input is a text file containing space separated rows, possibly starting with column names. Its output is a \LaTeX\ tabular\footnote{Please see the remarks in section~\ref{sec:pgfplotstable:context} for plain \TeX\ and Con\TeX t.} which contains selected columns of the text table, rounded to the desired precision, printed in the desired number format (fixed point, integer, scientific etc). It is used with \begin{codeexample}[code only] -\usepackage{pgfplots} +\usepackage{pgfplotstable} +% recommended: +%\usepackage{booktabs} +%\usepackage{array} +%\usepackage{colortbl} \end{codeexample} -\noindent and requires \PGFPlots\ and \PGF\ installed. +\noindent and requires \PGFPlots\ and \PGF\ $ \ge 2.00$ installed. + +Knowledge of |pgfkeys| is useful for a deeper insight into this package. Please refer to the \PGF\ manual,~\cite[section pgfkeys]{tikz} if you want a deeper insight into |pgfkeys|. Otherwise, simply skip over to the examples provided in this document. \section{Text table input format} \PGFPlotstable\ works with plain text file tables in which entries (``cells'') are separated by a separation character. The initial separation character is ``white space'' which means ``at least one space or tab''. Those tables can have a header line which contains column names and most other columns typically contain numerical data. @@ -65,17 +73,17 @@ It is used with \begin{codeexample}[code only] # Convergence results # fictional source, generated 2008 -level dof error1 error2 info grad(log(dof),log(error1)) quot(error1) -1 9 2.50000000e-01 7.57858283e-01 48 0 0 -2 25 6.25000000e-02 5.00000000e-01 25 -1.35691545e+00 4 -3 81 1.56250000e-02 2.87174589e-01 41 -1.17924958e+00 4 -4 289 3.90625000e-03 1.43587294e-01 8 -1.08987331e+00 4 -5 1089 9.76562500e-04 4.41941738e-02 22 -1.04500712e+00 4 -6 4225 2.44140625e-04 1.69802322e-02 46 -1.02252239e+00 4 -7 16641 6.10351562e-05 8.20091159e-03 40 -1.01126607e+00 4 -8 66049 1.52587891e-05 3.90625000e-03 48 -1.00563427e+00 3.99999999e+00 -9 263169 3.81469727e-06 1.95312500e-03 33 -1.00281745e+00 4.00000001e+00 -10 1050625 9.53674316e-07 9.76562500e-04 2 -1.00140880e+00 4.00000001e+00 +level dof error1 error2 info grad(log(dof),log(error2)) quot(error1) +1 4 2.50000000e-01 7.57858283e-01 48 0 0 +2 16 6.25000000e-02 5.00000000e-01 25 -3.00000000e-01 4 +3 64 1.56250000e-02 2.87174589e-01 41 -3.99999999e-01 4 +4 256 3.90625000e-03 1.43587294e-01 8 -5.00000003e-01 4 +5 1024 9.76562500e-04 4.41941738e-02 22 -8.49999999e-01 4 +6 4096 2.44140625e-04 1.69802322e-02 46 -6.90000001e-01 4 +7 16384 6.10351562e-05 8.20091159e-03 40 -5.24999999e-01 4 +8 65536 1.52587891e-05 3.90625000e-03 48 -5.35000000e-01 3.99999999e+00 +9 262144 3.81469727e-06 1.95312500e-03 33 -5.00000000e-01 4.00000001e+00 +10 1048576 9.53674316e-07 9.76562500e-04 2 -5.00000000e-01 4.00000001e+00 \end{codeexample} Lines starting with `|%|' or `|#|' are considered to be comment lines and are ignored. @@ -88,7 +96,7 @@ There is future support for a second header line which must start with `|$flags \begin{codeexample}[code only] # Convergence results # fictional source generated 2008 -level,dof,error1,error2,info,{grad(log(dof),log(error1))},quot(error1) +level,dof,error1,error2,info,{grad(log(dof),log(error2))},quot(error1) 1,9,2.50000000e-01,7.57858283e-01,48,0,0 2,25,6.25000000e-02,5.00000000e-01,25,-1.35691545e+00,4 3,81,1.56250000e-02,2.87174589e-01,41,-1.17924958e+00,4 @@ -104,16 +112,16 @@ level,dof,error1,error2,info,{grad(log(dof),log(error1))},quot(error1) \begin{codeexample}[] \pgfplotstabletypesetfile[col sep=comma]{pgfplotstable.example1.csv} \end{codeexample} - You may call |\pgfplotstableset{col sep=comma}| once in your preamble if all your tables uses commas as column separator. + You may call |\pgfplotstableset{col sep=comma}| once in your preamble if all your tables use commas as column separator. - Please note that if cell entries (for example column names) contain the separation character, you need to enclose the column entry in \emph{braces}: |{grad(log(dof),log(error1)}|. If you want to use unmatched braces, you need to write a backslash before the brace. For example the name `|column{withbrace|' needs to be written as `|column\{withbrace|'. + Please note that if cell entries (for example column names) contain the separation character, you need to enclose the column entry in \emph{braces}: |{grad(log(dof),log(error2)}|. If you want to use unmatched braces, you need to write a backslash before the brace. For example the name `|column{withbrace|' needs to be written as `|column\{withbrace|'. Furthermore, if you need empty cells in case |col sep=space|, you have to provide |{}| to delimit such a cell since |col sep=space| uses \emph{at least} one white space (consuming all following ones). The value |col sep=braces| is special since it actually uses two separation characters. Every single cell entry is delimited by an opening and a closing brace, \marg{entry}, for this choice. Furthermore, any wite spaces (spaces and tabs) between cell entries are \emph{skipped} in case |braces| until the next \marg{entry} is found. \end{key} -\begin{command}{\pgfplotstabletypesetfile[\meta{optional arguments}]\marg{file name}} +\begin{command}{\pgfplotstabletypesetfile\oarg{optional arguments}\marg{file name}} This command loads the table file \marg{file name} and typesets it using the current configuration of number formats and table options. \begin{codeexample}[] \pgfplotstabletypesetfile{pgfplotstable.example1.dat} @@ -126,7 +134,6 @@ level,dof,error1,error2,info,{grad(log(dof),log(error1))},quot(error1) \end{codeexample} \end{command} -% FIXME : 'header' option! \begin{command}{\pgfplotstableread\marg{file name}\marg{\textbackslash macro}} Loads the table \marg{file name} into a \TeX-macro \meta{\textbackslash macro}. This macro can than be used several times. \begin{codeexample}[] @@ -137,10 +144,20 @@ level,dof,error1,error2,info,{grad(log(dof),log(error1))},quot(error1) \end{codeexample} \end{command} -\begin{command}{\pgfplotstabletypeset[\meta{optional arguments}]\marg{\textbackslash macro}} +\begin{command}{\pgfplotstabletypeset\oarg{optional arguments}\marg{\textbackslash macro}} Works in the same way as |\pgfplotstabletypesetfile|, but it accepts an already loaded table. \end{command} +\begin{key}{/pgfplots/table/header=\marg{boolean} (initially true)} + Configures if column names shall be identified during input operations. + + The `header' is the first non-comment line, but only if this line contains non-numerical entries. If the first non-comment line contains at least one non-numerical entry (for example `|a name|'), each entry in this line is supposed to be a column name. + + If the first non-comment line contains only numerical data, it is used as data row. In this case, column indices will be assigned as column ``names''. + + Use |header=false| to force this last case, i.e. even if the first line contains strings, they won't be recognised as column names. +\end{key} + \section{Selecting columns} \begin{key}{/pgfplots/table/columns=\marg{comma-separated-list} (initially all available ones)} Selects particular columns the table. If this option is missing, all available columns will be selected. @@ -151,6 +168,16 @@ level,dof,error1,error2,info,{grad(log(dof),log(error1))},quot(error1) \end{codeexample} \end{key} +\begin{pgfplotstablealiaskey} + Assigns the new name \meta{new col name} for the column denoted by \meta{real col name}. Afterwards, accessing \meta{new col name} will use the data associated with column \meta{real col name}. + + You can use |columns/|\meta{new col name}|/.style| to assign styles for the alias, not for the original column name. + + If there exists both an alias and a column of the same name, the column name will be preferred. Furthermore, if there exists a |create on use| statement with the same name, this one will also be preferred. + + In case \meta{new col name} contains characters which are required for key settings, you need to use braces around it: ``|alias/{name=wi/th,special}/.initial={othername}|''. +\end{pgfplotstablealiaskey} + \begin{pgfplotstablecolumnkey} Sets all options in \marg{key-value-list} exclusively for \marg{column name}. @@ -172,9 +199,9 @@ level,dof,error1,error2,info,{grad(log(dof),log(error1))},quot(error1) {pgfplotstable.example1.dat} \end{codeexample} If your column name contains commas `|,|', slashes `|/|' or equal signs `|=|', you need to enclose the column name in braces. -\begin{codeexample}[width=7cm] +\begin{codeexample}[narrow] \pgfplotstabletypesetfile[ - columns={dof,error1,{grad(log(dof),log(error1))}}, + columns={dof,error1,{grad(log(dof),log(error2))}}, columns/error1/.style={ column name=$L_2$, sci,sci zerofill,sci subscript, @@ -182,22 +209,33 @@ level,dof,error1,error2,info,{grad(log(dof),log(error1))},quot(error1) columns/dof/.style={ int detect, column name=\textsc{Dof}}, - columns/{grad(log(dof),log(error1))}/.style={ + columns/{grad(log(dof),log(error2))}/.style={ column name=slopes $L_2$, fixed,fixed zerofill, precision=1} ] {pgfplotstable.example1.dat} \end{codeexample} + If your tables don't have column names, you can simply use integer indices instead of \marg{column name} to refer to columns. If you have column names, you can't set column styles using indices. \end{pgfplotstablecolumnkey} +\begin{pgfplotstabledisplaycolumnkey} + Applies all options in \marg{key-value-list} exclusively to the column which will appear at position \meta{index} in the output table. + + In contrast to the |table/columns/|\meta{name} styles, this option refers to the output table instead of the input table. Since the output table has no unique column name, you can only access columns by index. + + Indexing starts with~$\meta{index}=0$. + + Display column styles override input column styles. +\end{pgfplotstabledisplaycolumnkey} + \begin{key}{/pgfplots/table/column type=\marg{tabular column type} (initially c)} Contains the column type for |tabular|. If all column types are empty, the complete argument is skipped (assuming that no |tabular| environment is generated). Use |\pgfplotstableset{column type/.add=|\marg{before}\marg{after}|}| to \emph{modify} a value instead of overwriting it. The |.add| key handler works for other options as well. -\begin{codeexample}[width=7cm] +\begin{codeexample}[narrow] \pgfplotstabletypesetfile[ columns={dof,error1,info}, column type/.add={|}{}% results in '|c' @@ -224,36 +262,53 @@ level,dof,error1,error2,info,{grad(log(dof),log(error1))},quot(error1) \end{stylekey} \begin{stylekey}{/pgfplots/table/dec sep align=\marg{header column type} (initially c)} - A style which employs the current decimal separator as alignment character. + A style which aligns numerical columns at the decimal separator. The first argument determines the alignment of the header column. - The style |dec sep align| actually introduces two new |tabular| columns, namely |r@{}l|. It introduces multicolumns for column names accordingly and handles numbers which do not have a decimal separator. - + The style |dec sep align| actually introduces two new |tabular| columns, namely |r@{}l|. It introduces multicolumns for column names accordingly and handles numbers which do not have a decimal separator. + + Please note that you need |\usepackage{array}| for this style. \begin{codeexample}[] +% requires \usepackage{array} \pgfplotstabletypesetfile[ - columns={dof,error1,error2,info,{grad(log(dof),log(error1))}}, + columns={dof,error1,error2,info,{grad(log(dof),log(error2))}}, columns/error1/.style={dec sep align}, columns/error2/.style={sci,sci subscript,sci zerofill,dec sep align}, columns/info/.style={fixed,dec sep align}, - columns/{grad(log(dof),log(error1))}/.style={fixed,dec sep align} + columns/{grad(log(dof),log(error2))}/.style={fixed,dec sep align} ] {pgfplotstable.example1.dat} \end{codeexample} Or with comma as decimal separator: \begin{codeexample}[] +% requires \usepackage{array} \pgfplotstabletypesetfile[ use comma, - columns={dof,error1,error2,info,{grad(log(dof),log(error1))}}, + columns={dof,error1,error2,info,{grad(log(dof),log(error2))}}, columns/error1/.style={dec sep align}, columns/error2/.style={sci,sci subscript,sci zerofill,dec sep align}, columns/info/.style={fixed,dec sep align}, - columns/{grad(log(dof),log(error1))}/.style={fixed,dec sep align} + columns/{grad(log(dof),log(error2))}/.style={fixed,dec sep align} ] {pgfplotstable.example1.dat} \end{codeexample} It may be advisable to use the |zerofill| variants to force at least one digit after the decimal separator. + + Please note that this style overwrites |column type|, |assign cell content| and some number formatting settings. +\end{stylekey} + +\begin{stylekey}{/pgfplots/table/sci sep align=\marg{header column type} (initially c)} + A style which aligns numerical columns at the exponent in scientific representation. + + The first argument determines the alignment of the header column. + + It works similiarly to |dec sep align|, namely by introducing two artificial columns |r@{}l| for alignment. + + Please note that you need |\usepackage{array}| for this style. + + Please note that this style overwrites |column type|, |assign cell content| and some number formatting settings. \end{stylekey} \begin{stylekey}{/pgfplots/table/dcolumn=\marg{tabular column type}\marg{type for column name} (initially \{D\{.\}\{.\}\{2\}\}\{c\})} @@ -262,7 +317,7 @@ level,dof,error1,error2,info,{grad(log(dof),log(error1))},quot(error1) \begin{stylekey}{/pgfplots/table/every first column} A style which is installed for every first column only. -\begin{codeexample}[width=7cm] +\begin{codeexample}[narrow] \pgfplotstabletypesetfile[ every head row/.style={before row=\hline,after row=\hline\hline}, every last row/.style={after row=\hline}, @@ -284,7 +339,7 @@ A style which is installed for every last column only. A style which is installed for every column with even column index (starting with~$0$). { \pgfplotstableset{ - columns={dof,error1,{grad(log(dof),log(error1))},info}, + columns={dof,error1,{grad(log(dof),log(error2))},info}, columns/error1/.style={ column name=$L_2$, sci,sci zerofill,sci subscript, @@ -292,14 +347,14 @@ A style which is installed for every column with even column index (starting wit columns/dof/.style={ int detect, column name=\textsc{Dof}}, - columns/{grad(log(dof),log(error1))}/.style={ + columns/{grad(log(dof),log(error2))}/.style={ column name=slopes $L_2$, fixed,fixed zerofill, precision=1}} \begin{codeexample}[code only] \pgfplotstableset{ - columns={dof,error1,{grad(log(dof),log(error1))},info}, + columns={dof,error1,{grad(log(dof),log(error2))},info}, columns/error1/.style={ column name=$L_2$, sci,sci zerofill,sci subscript, @@ -307,14 +362,14 @@ A style which is installed for every column with even column index (starting wit columns/dof/.style={ int detect, column name=\textsc{Dof}}, - columns/{grad(log(dof),log(error1))}/.style={ + columns/{grad(log(dof),log(error2))}/.style={ column name=slopes $L_2$, fixed,fixed zerofill, precision=1}} \end{codeexample} -\begin{codeexample}[width=7cm,graphic=white] -% \usepackage{colortbl} +\begin{codeexample}[narrow,graphic=white] +% requires \usepackage{colortbl} \pgfplotstabletypesetfile[ every even column/.style={ column type/.add={>{\columncolor[gray]{.8}}}{} @@ -331,25 +386,36 @@ A style which is installed for every column with odd column index (starting with \begin{command}{\pgfplotstablecol} During the evaluation of row or column options, this command expands to the current columns' index. \end{command} +\label{pgfplotstable:page:tablerow} \begin{command}{\pgfplotstablerow} During the evaluation of row or column options, this command expands to the current rows' index. \end{command} +\begin{command}{\pgfplotstablecols} + During the evaluation of row or column options, this command expands to the total number of columns in the output table. +\end{command} +\begin{command}{\pgfplotstablerows} + During evaluation of \emph{columns}, this command expands to the total number of \emph{input} rows. You can use it inside of |row predicate|. + + During evaluation of \emph{rows}, this command expands to the total number of \emph{output} rows. +\end{command} \section{Configuring row appearance} -\begin{key}{/pgfplots/before row=\marg{\TeX\ code}} +The following styles allow to configure the final table code \emph{after any cell contents have been assigned}. + +\begin{key}{/pgfplots/table/before row=\marg{\TeX\ code}} Contains \TeX\ code which will be installed before the first cell in a row. \end{key} -\begin{key}{/pgfplots/after row=\marg{\TeX\ code}} +\begin{key}{/pgfplots/table/after row=\marg{\TeX\ code}} Contains \TeX\ code which will be installed after the last cell in a row (i.e. after |\\|). \end{key} -\begin{stylekey}{/pgfplots/every even row} +\begin{stylekey}{/pgfplots/table/every even row} A style which is installed for each row with even row index. The first row is supposed to be a ``head'' row and does not count. Indexing starts with~$0$. { \pgfplotstableset{ - columns={dof,error1,{grad(log(dof),log(error1))}}, + columns={dof,error1,{grad(log(dof),log(error2))}}, columns/error1/.style={ column name=$L_2$, sci,sci zerofill,sci subscript, @@ -357,14 +423,14 @@ A style which is installed for every column with odd column index (starting with columns/dof/.style={ int detect, column name=\textsc{Dof}}, - columns/{grad(log(dof),log(error1))}/.style={ + columns/{grad(log(dof),log(error2))}/.style={ column name=slopes $L_2$, fixed,fixed zerofill, precision=1}} \begin{codeexample}[code only] \pgfplotstableset{ - columns={dof,error1,{grad(log(dof),log(error1))}}, + columns={dof,error1,{grad(log(dof),log(error2))}}, columns/error1/.style={ column name=$L_2$, sci,sci zerofill,sci subscript, @@ -372,14 +438,14 @@ A style which is installed for every column with odd column index (starting with columns/dof/.style={ int detect, column name=\textsc{Dof}}, - columns/{grad(log(dof),log(error1))}/.style={ + columns/{grad(log(dof),log(error2))}/.style={ column name=slopes $L_2$, fixed,fixed zerofill, precision=1}} \end{codeexample} -\begin{codeexample}[width=7cm,graphic=white] -% \usepackage{booktabs} +\begin{codeexample}[narrow,graphic=white] +% requires \usepackage{booktabs} \pgfplotstabletypesetfile[ every head row/.style={ before row=\toprule,after row=\midrule}, @@ -389,8 +455,8 @@ A style which is installed for every column with odd column index (starting with {pgfplotstable.example1.dat} \end{codeexample} -\begin{codeexample}[width=7cm,graphic=white] -% \usepackage{booktabs,colortbl} +\begin{codeexample}[narrow,graphic=white] +% requires \usepackage{booktabs,colortbl} \pgfplotstabletypesetfile[ every even row/.style={ before row={\rowcolor[gray]{0.9}}}, @@ -405,28 +471,91 @@ A style which is installed for every column with odd column index (starting with } \end{stylekey} -\begin{stylekey}{/pgfplots/every odd row} +\begin{stylekey}{/pgfplots/table/every odd row} A style which is installed for each row with odd row index. The first row is supposed to be a ``head'' row and does not count. Indexing starts with~$0$. \end{stylekey} -\begin{stylekey}{/pgfplots/every head row} +\begin{stylekey}{/pgfplots/table/every head row} A style which is installed for each first row in the tabular. This can be used to adjust options for column names or to add extra lines/colours. \end{stylekey} -\begin{stylekey}{/pgfplots/every first row} +\begin{stylekey}{/pgfplots/table/every first row} A style which is installed for each first \emph{data} row, i.e. after the head row. \end{stylekey} -\begin{stylekey}{/pgfplots/every last row} +\begin{stylekey}{/pgfplots/table/every last row} A style which is installed for each last \emph{data} row. \end{stylekey} +\section{Determining cell contents} +The conversion from an unprocessed input table to a final typesetted |tabular| code uses three stages for every cell, +\begin{enumerate} + \item Preprocessing, + \item Typesetting, + \item Postprocessing. +\end{enumerate} +The main idea is to select one typesetting algorithm (for example ``format my numbers with the configured number style''). This algorithm usually doesn't need to be changed. Fine tuning can then be done using zero, one or more preprocessors and postprocessors. Preprocessing can mean to select only particular rows or to apply some sort of operation before the typesetting algorithm sees the content. Postprocessing means to apply fine-tuning to the resulting \TeX\ output -- for example to deal with empty cells or to insert unit suffixes or modify fonts for single cells. + +\subsection{Typesetting cells} +\begin{codekey}{/pgfplots/table/assign cell content} + Allows to redefine the algorithm which assigns cell contents. The argument |#1| is the (unformatted) contents of the input table. + + The resulting output needs to be written to |/pgfplots/table/@cell content|. + + Please note that you may need special attention for |#1=|\marg{}, i.e. the empty string. This may happen if a column has less rows than the first column. \PGFPlotstable\ will balance columns automatically in this case, inserting enough empty cells to match the number of rows of the first column. + + Please note further that if any column has more entries than the first column, these entries will be skipped and a warning message will be issued into the log file. + + This key is evaluated inside of a local \TeX\ group, so any local macro assignments will be cleared afterwards. +\end{codekey} + +\begin{key}{/pgfplots/table/assign cell content as number} + This here is the default implementation of |assign cell contents|. + + It invokes |\pgfmathprintnumberto| and writes the result into |@cell content|. +\end{key} + +\begin{stylekey}{/pgfplots/table/string type} + A style which redefines |assign cell contents| to simply return the ``raw'' input data, that means as text column. This assumes input tables with valid \LaTeX\ content (verbatim printing is not supported). +\end{stylekey} + + +\subsection{Preprocessing cells} +\begin{codekey}{/pgfplots/table/preproc cell content} + Allows to \emph{modify} the contents of cells \emph{before} |assign cell contents| is called. + + The semantics is as follows: before the preprocessor, |@cell content| contains the raw input data (or, maybe, the result of another preprocessor call). After the preprocessor, |@cell content| is filled with a -- possibly modified -- value. The resulting value is then used as input to |assign cell contents|. + + In the default settings, |assign cell contents| expects numerical input. So, the preprocessor is expected to produce numerical output. + + It is possible to provide multiple preprocessor directives using |.append code| or |.append style| key handlers. + + In case you don't want (or need) stackable preprocessors, you can also use `|#1|' to get the raw input datum as it is found in the file. +\end{codekey} + +\begin{stylekey}{/pgfplots/table/multiply -1} + Appends code to current |preproc cell content| value which multiplies every cell with $-1$. +\begin{codeexample}[] +\pgfplotstableset{ + columns={dof,error2,slopes2}, + columns/error2/.style={sci,sci zerofill}, + columns/slopes2/.style={dec sep align,empty cells with={\ensuremath{-}}}, + create on use/slopes2/.style= + {create col/gradient loglog={dof}{error2}}} + +\pgfplotstabletypesetfile{pgfplotstable.example1.dat} + +\pgfplotstabletypesetfile[columns/slopes2/.append style={multiply -1}] + {pgfplotstable.example1.dat} +\end{codeexample} +\end{stylekey} + \begin{codekey}{/pgfplots/table/row predicate} A boolean predicate which allows to select particular rows of the input table. The argument |#1| contains the current row's index (starting with~$0$, not counting comment lines or column names). The return value is assigned to the \TeX-if |\ifpgfplotstableuserow|. If the boolean is not changed, the return value is true. -\begin{codeexample}[width=7cm] -% \usepackage{booktabs} +\begin{codeexample}[narrow] +% requires \usepackage{booktabs} \pgfplotstabletypesetfile[ every head row/.style={ before row=\toprule,after row=\midrule}, @@ -441,15 +570,17 @@ A style which is installed for every column with odd column index (starting with ] {pgfplotstable.example1.dat} \end{codeexample} - Please note that |row predicate| is applied \emph{before} any other option which affects row appearance. For example, the even/odd styles refer to those rows which are selected. + Please note that |row predicate| is applied \emph{before} any other option which affects row appearance. It is evaluated before |assign cell contents|. For example, the even/odd row styles refer to those rows for which the predicate returns |true|. In fact, you can use |row predicate| to truncate the complete table before it as actually processed. + + During |row predicate|, the macro |\pgfplotstablerows| contains the total number of \emph{input} rows. Furthermore, |row predicate| applies only to the typeset routines, not the read methods. If you want to plot only selected table entries with |\addplot table|, use the \PGFPlots\ coordinate filter options. \end{codekey} \begin{stylekey}{/pgfplots/table/skip rows between index=\marg{begin}\marg{end}} A style which appends an |row predicate| which discards selected rows. The selection is done by index where indexing starts with~$0$. Every row with index $\meta{begin} \le i < \meta{end}$ will be skipped. -\begin{codeexample}[width=7cm] -% \usepackage{booktabs} +\begin{codeexample}[narrow] +% requires \usepackage{booktabs} \pgfplotstabletypesetfile[ every head row/.style={ before row=\toprule,after row=\midrule}, @@ -462,25 +593,99 @@ A style which is installed for every column with odd column index (starting with \end{codeexample} \end{stylekey} +\begin{stylekey}{/pgfplots/table/select equal part entry of=\marg{part no}\marg{part count}} + A style which overwrites |row predicate| with a subset selection predicate. The idea is to split the current column into \marg{part count} equally sized parts and select only \marg{part no}. -\section{Determining cell contents} -\begin{codekey}{/pgfplots/table/assign cell content} - Allows to redefine the algorithm which assigns cell contents. The argument |#1| is the (unformatted) contents of the input table. + This can be used to simulate multicolumn tables. +\begin{codeexample}[] +% requires \usepackage{booktabs} +\pgfplotstableset{ + every head row/.style={before row=\toprule,after row=\midrule}, + every last row/.style={after row=\bottomrule}} + +\pgfplotstabletypesetfile[string type]{pgfplotstable.example2.dat} + +\pgfplotstabletypesetfile[ + display columns/0/.style={select equal part entry of={0}{2},string type}, + display columns/1/.style={select equal part entry of={0}{2},string type}, + display columns/2/.style={select equal part entry of={1}{2},string type}, + display columns/3/.style={select equal part entry of={1}{2},string type}, + columns={A,B,A,B} +] + {pgfplotstable.example2.dat} +\end{codeexample} + The example above shows the original file as-is on the left side. The right side shows columns A,B,A,B~-- but only half of the elements are shown, selected by indices \#0 or \#1 of \#2. The parts are equally large, up to a remainder. - The resulting output needs to be written to |/pgfplots/table/@cell content|. + If the available number of rows is not dividable by \marg{part count}, the remaining entries are distributed equally among the first parts. +\end{stylekey} + + +\subsection{Postprocessing cells} +\begin{codekey}{/pgfplots/table/postproc cell content} + Allows to \emph{modify} assigned cell content \emph{after} it has been assigned, possibly content-dependend. Ideas could be to draw negative numbers in red, typeset single entries in bold face or insert replacement text. + + This key is evaluated \emph{after} |assign cell content|. Its semantics is to modify an existing |@cell content| value. + + There may be more than one |postproc cell content| command, if you use |.append code| or |.append style| to define them: +\begin{codeexample}[] +% requires \usepackage{eurosym} +\pgfplotstabletypesetfile[ + column type=r, + columns={dof,info}, + columns/info/.style={ + postproc cell content/.append style={ + /pgfplots/table/@cell content/.add={$\bf}{$}}, + postproc cell content/.append style={ + /pgfplots/table/@cell content/.add={}{\EUR{}}} + }] + {pgfplotstable.example1.dat} +\end{codeexample} + The code above modifies |@cell content| successively. First, ``|$\bf ... $|'' is inserted, then ``|...\EUR|''. It should be noted that |pgfkeys| handles |.style| and |.code| in (quasi) the same way -- both are simple code keys and can be used as such. You can combine both with |.append style| and |.append code|. Please refer to~\cite[section about pgfkeys]{tikz} for details. + + As in |assign cell contents|, the code can evaluate helper macros like |\pgfplotstablerow| to change only particular entries. Furthermore, the argument ``|#1|'' expands to the \emph{unformatted} input argument which was found in the input table. This allows complete context based formatting options. Please remember that empty strings may appear due to column balancing -- introduce special treatment if necessary. + + There is one special case which occurs if |@cell content| itsself contains the cell separation character `|&|'. In this case, |postproc cell contents| is invoked \emph{separately} for each part before and after the ampersand and the ampersand is inserted afterwards. This allows compatibility with special styles which create artificial columns in the output (which is allowed, see |dec sep align|). To allow separate treatment of each part, you can use the macro |\pgfplotstablepartno|. It is defined only during the evaluation of |postproc cell content| and it evaluates to the current part index (starting with~$0$). If there is no ampersand in your text, the value will always be~$0$. + + This key is evaluated inside of a local \TeX\ group, so any local macro assignments will be cleared afterwards. + + The following example can be used to insert a dash, $-$, in a slope column: +\begin{codeexample}[] +\pgfplotstableset{ + create on use/slopes1/.style= + {create col/gradient loglog={dof}{error1}}} + +\pgfplotstabletypesetfile[ + columns={dof,error1,slopes1}, + columns/error1/.style={sci,sci zerofill}, + columns/slopes1/.style={ + postproc cell content/.append code={% + \ifnum\pgfplotstablerow=0 + \pgfkeyssetvalue{/pgfplots/table/@cell content}{\ensuremath{-}}% + \fi + }% + }] + {pgfplotstable.example1.dat} +\end{codeexample} +Since this may be useful in a more general context, it is available as |empty cells with| style. \end{codekey} -\begin{key}{/pgfplots/table/assign cell content as number} - This here is the default implementation of |assign cell contents|. -\end{key} +\begin{stylekey}{/pgfplots/table/empty cells with=\marg{replacement}} + Appends code to |postproc cell content| which replaces any empty cell with \marg{replacement}. -\begin{stylekey}{/pgfplots/table/string type} - A style which redefines |assign cell contents| to allow (a) text column(s). + If |dec sep align| is active, the replacement will be inserted only for the part before the decimal separator. \end{stylekey} +\begin{stylekey}{/pgfplots/table/set content=\marg{content}} + A style which redefines |postproc cell contents| to always return the value \marg{content}. +\end{stylekey} + + + \section{Customizing and getting the tabular code} -\begin{stylekey}{/pgfplots/table/every table} +\begin{stylekey}{/pgfplots/table/every table=\marg{file name}} A style which is installed at the beginning of every |\pgfplotstabletypeset| command. + + The table file name is given as first argument. \end{stylekey} \begin{key}{/pgfplots/table/font=\marg{font name} (initially empty)} @@ -510,13 +715,13 @@ The command |\pgfutilensuremath| checks whether math mode is active and switches \end{key} \begin{key}{/pgfplots/table/debug=\marg{boolean} (initially false)} - If enabled, will write every final tabular code to you log file. + If enabled, will write every final tabular code to your log file. \end{key} \section{Defining column types for \texttt{tabular}} Besides input of text files, it is sometimes desireable to define column types for existing \texttt{tabular} environments. -\begin{command}{\newcolumntype\marg{letter}[\meta{number of arguments}]$>$\marg{before column}\meta{column type}$<$\marg{after column}} +\begin{command}{\newcolumntype\marg{letter}\oarg{number of arguments}$>$\marg{before column}\meta{column type}$<$\marg{after column}} The command |\newcolumntype| is part of the |array| package and it defines a new column type \marg{letter} for use in \LaTeX\ tabular environments. \begin{codeexample}[code only] \usepackage{array} @@ -532,6 +737,7 @@ c & d \\ Now, the environment |pgfplotstablecoltype| can be used in \marg{before column} and \marg{after column} to define numerical columns: \begin{codeexample}[] +% requires \usepackage{array} \newcolumntype{L}[1] {>{\begin{pgfplotstablecoltype}[#1]}r<{\end{pgfplotstablecoltype}}} @@ -551,6 +757,7 @@ Now, the environment |pgfplotstablecoltype| can be used in \marg{before column} \noindent The environment |pgfplotstablecoltype| accepts an optional argument which may contain any number formatting options. It is an error if numerical columns contain non-numerical data, so it may be necessary to use |\multicolumn| for column names. \begin{codeexample}[] +% requires \usepackage{array} \newcolumntype{L}[1] {>{\begin{pgfplotstablecoltype}[#1]}r<{\end{pgfplotstablecoltype}}} @@ -788,7 +995,7 @@ A predefined style which installs commas `\texttt{,}' as decimal separators and \end{codeexample} \end{key} -\begin{key}{/pgf/number format/skip 0.=\marg{boolean}} +\begin{key}{/pgf/number format/skip 0.=\marg{boolean} (initially false)} Configures whether numbers like $0.1$ shall be typeset as $.1$ or not. \begin{codeexample}[] \pgfkeys{/pgf/number format/.cd, @@ -806,6 +1013,28 @@ A predefined style which installs commas `\texttt{,}' as decimal separators and \end{codeexample} \end{key} +\begin{key}{/pgf/number format/showpos=\marg{boolean} (initially false)} + Enables or disables display of plus signs for non-negative numbers. +\begin{codeexample}[] +\pgfkeys{/pgf/number format/showpos} +\pgfmathprintnumber{12.345} +\end{codeexample} + +\begin{codeexample}[] +\pgfkeys{/pgf/number format/showpos=false} +\pgfmathprintnumber{12.345} +\end{codeexample} + +\begin{codeexample}[] +\pgfkeys{/pgf/number format/.cd,showpos,sci} +\pgfmathprintnumber{12.345} +\end{codeexample} +\end{key} + +\begin{stylekey}{/pgf/number format/print sign=\marg{boolean}} + A style which is simply an alias for |showpos=|\marg{boolean}. +\end{stylekey} + \begin{key}{/pgf/number format/sci 10e} Uses $m \cdot 10^e$ for any number displayed in scientific format. @@ -846,17 +1075,247 @@ Typesets the exponent as subscript for any number displayed in scientific format \end{codeexample} \end{key} +\begin{key}{/pgf/number format/@dec sep mark=\marg{text}} + Will be placed right before the place where a decimal separator belongs to. However, \marg{text} will be inserted even if there is no decimal separator. It is intented as place-holder for auxiliary routines to find alignment positions. + + This key should never be used to change the decimal separator! Use |dec sep| instead. +\end{key} + +\begin{key}{/pgf/number format/@sci exponent mark=\marg{text}} + Will be placed right before exponents in scientific notation. It is intented as place-holder for auxiliary routines to find alignment positions. + + This key should never be used to change the exponent! +\end{key} + \begin{key}{/pgf/number format/assume math mode=\marg{boolean} (default true)} Set this to |true| if you don't want any checks for math mode. The initial setting installs a |\pgfutilensuremath| around each final number to change to math mode if necessary. Use |assume math mode=true| if you know that math mode is active and you don't want |\pgfutilensuremath|. \end{key} +\section{Changing tables} +After tables have been loaded from disk, it is possible to change their contents. The methods are limited up to now and they reflect mostly what I needed in my applications. Nevertheless, the approaches are quite general and can be customized. + +\begin{command}{\pgfplotstablecreatecol\oarg{options}\marg{new col name}\marg{\textbackslash table}} +Creates a new column named \marg{new col name} and appends it to table \marg{\textbackslash table}. + +This command is technical, but it offers a flexible framework to generate new columns. It has been designed to create new columns using the already existing values -- for example using logical or numerical methods to combine existing values. It provides fast access to a row's value and the next row's value. + +The following documentation is for all who want to \emph{write} specialised columns. It is not particularly difficult; it is just technical and it requires some knowledge of |pgfkeys|. If you don't like it, you can resort to some predefined column generation styles - and enable those styles in \marg{options}. + +The column entries will be created using the command key |create col/assign|. It will be invoked for every row of the table. +It is supposed to assign contents to |create col/next content|. +During the evaluation, the macro |\thisrow|\marg{col name} +expands to the current row's value of the column identified by \marg{col name}. +Furthermore, |\nextrow|\marg{col name} expands to the \emph{next} row's +value of the designated column and |\prevrow|\marg{col name} expands to the value of the \emph{previous} row. + +Two special |assign| routines are available for the first and last row: The contents for the \emph{last} row is computed with |create col/assign last|. Its semantics is the same. The contents for the \emph{first} row is computed with |create col/assign first| to simplify special cases here. These first and last commands are optional, their default is to invoke the normal |assign| routine. + +The evaluation of the |assign| keys is done in local \TeX\ groups. + +The following macros are useful during cell assignments: +\begin{enumerate} + \item |\prevrow|\marg{col name} / |\getprevrow|\marg{col name}\marg{\textbackslash macro} + + These two routines return the value stored in the \emph{previous} row of the designated column \marg{col name}. The |get| routine stores it into \meta{\textbackslash macro}. + + \item |\thisrow|\marg{col name} / |\getthisrow|\marg{col name}\marg{\textbackslash macro} + + These two routines return the \emph{current} row's value stored in the designated column. The |get| routine stores it into \meta{\textbackslash macro}. + + \item |\nextrow|\marg{col name} / |\getnextrow|\marg{col name}\marg{\textbackslash macro} + + These two routines return the \emph{next} row's value. + + \item |\pgfplotstablerow| and |\pgfplotstablerows| which contain the current row's index and the total number of rows, respectively. See page~\pageref{pgfplotstable:page:tablerow} for details. + \item |\pgfmathaccuma| and |\pgfmathaccumb| can be used to transport intermediate results. + Both maintain their value from one call to the next. All other local variables will be deleted after leaving the assignment routines. +\end{enumerate} +The \marg{col name} is expected to be a \emph{physical} column name, no alias or column index is allowed (unless column indices and column names are the same). + +The following example takes our well-known input table and creates a copy of the |level| column. Furthermore, it produces a lot of output to show the available macros. Finally, it uses |\pgfkeyslet| to assign the contents of the resulting |\entry| to |next content|. +\begin{codeexample}[] +\pgfplotstableread{pgfplotstable.example1.dat}\table +\pgfplotstablecreatecol[ + create col/assign/.code={% + \getthisrow{level}\entry + \getnextrow{level}\nextentry + \edef\entry{thisrow=\entry; nextrow=\nextentry. + (\#\pgfplotstablerow/\pgfplotstablerows)}% + \pgfkeyslet{/pgfplots/table/create col/next content}\entry + }] + {new}\table + +\pgfplotstabletypeset[ + column type=l, + columns={level,new}, + columns/new/.style={string type} +]\table +\end{codeexample} + +There is one more speciality: you can use |columns=|\marg{column list} to reduce the runtime complexity of this command. This works only if the |columns| key is provided directly into \marg{options}. In this case |\thisrow| and its variants are only defined for those columns listed in the |columns| value. + +\paragraph{Limitations.} Currently, you can only access three values of one column at a time: the current row, the previous row and the next row. Access to arbitrary indices is not (yet) supported. Furthermore, this command has been designed to modify an existing table. You can't create a table from scratch with this command. + +The default implementation of |assign| is to produce empty strings. The default implementation of |assign last| is to invoke |assign|, so in case you never really use the next row's value, you won't need to touch |assign last|. The same holds for |assign first|. +\end{command} + +\begin{pgfplotstablecreateonusekey} + Allows ``lazy creation'' of the column \meta{col name}. Whenever the column \meta{col name} is queried by name, for example in an |\pgfplotstabletypeset| command, and such a column does not exist already, it is created on-the-fly. + +\begin{codeexample}[narrow] +% requires \usepackage{array} +\pgfplotstableset{ + create on use/quot1/.style= + {create col/quotient={error1}}} + +\pgfplotstabletypesetfile[ + columns={error1,quot1}, + columns/error1/.style={sci,sci zerofill}, + columns/quot1/.style={dec sep align}] +{pgfplotstable.example1.dat} +\end{codeexample} + The example above queries |quot1| which does not yet exist in the input file. Therefor, it is checked whether a |create on use| style for |quot1| exists. This is the case, so it is used to create the missing column. + + A |create on use| specification is translated into + + |\pgfplotstablecreatecol[|\meta{create options}|]|\marg{col name}\marg{the table}. + + This feature allows some lazyness, because you can omit the lengthy table modifications. However, lazyness may cost something: in the example above, the generated column will be \emph{lost} after returning from |\pgfplotstabletypesetfile|. + + The |create on use| has higher priority than |alias|. + + In case \meta{col name} contains characters which are required for key settings, you need to use braces around it: ``|create on use/{name=wi/th,special}/.style={...}|''. +\end{pgfplotstablecreateonusekey} + +\begin{stylekey}{/pgfplots/table/create col/expr=\marg{math expression}} + A style for use in |\pgfplotstablecreatecol| which uses \marg{math expression} to assign contents for the new column. + +\begin{codeexample}[] +\pgfplotstableread{pgfplotstable.example1.dat}\table +\pgfplotstablecreatecol[ + create col/expr={\thisrow{level}*2}] + {new}\table + +\pgfplotstabletypeset[ + columns={level,new}, + columns/new/.style={column name=$2\cdot $level} +]\table +\end{codeexample} + The macros |\thisrow|\marg{col name} and |\nextrow|\marg{col name} can be used to use values of the existing table. + + Please see |\pgfplotstablecreatecol| for more information. + + \paragraph{Attention:} Currently, |expr| uses the math parser of \PGF. Although very powerful, it has never been intended for scientific applications. Its $5.5$ fixed point arithmetic has its limitations. + + There is limited support for float point arithmetics, but that is far from complete and every elementary operation needs to be programmed by hand. Some of the following numerical styles employ these routines, they have been included into the \PGF\ math library (but not into the parser). + + Furthermore, you can create new columns using the |fp| package which allows precise fixed point arithmetics. +\end{stylekey} + +\begin{stylekey}{/pgfplots/table/create col/quotient=\marg{column name}} + A style for use in |\pgfplotstablecreatecol| which computes the quotient $c_i := m_{i-1} / m_i$ for every entry $i = 1,\dotsc, (n-1)$ in the column identified with \marg{column name}. The first value $c_0$ is kept empty. + +\begin{codeexample}[] +% requires \usepackage{array} +\pgfplotstableread{pgfplotstable.example1.dat}\table +\pgfplotstablecreatecol[create col/quotient=error1] + {quot1}\table +\pgfplotstablecreatecol[create col/quotient=error2] + {quot2}\table + +\pgfplotstabletypeset[ + columns={error1,error2,quot1,quot2}, + columns/error1/.style={sci,sci zerofill}, + columns/error2/.style={sci,sci zerofill}, + columns/quot1/.style={dec sep align}, + columns/quot2/.style={dec sep align}] +\table +\end{codeexample} + This style employs methods of the floating point unit, that means it works with a relative precision of about $10^{-7}$ ($7$ significant digits in the mantisse). +\end{stylekey} + +\begin{stylekey}{/pgfplots/table/create col/iquotient=\marg{column name}} + Like |create col/quotient|, but the quotient is inverse. +\end{stylekey} + +\begin{stylekey}{/pgfplots/table/create col/dyadic refinement rate=\marg{column name}} + A style for use in |\pgfplotstablecreatecol| which computes the convergence rate $\alpha$ of the data in column \marg{column name}. The contents of \marg{column name} is assumed to be something like $e_i(h_i) = O(h_i^\alpha)$. Assuming a dyadic refinement relation from one row to the next, $h_i = h_{i-1}/2$, we have $h_{i-1}^\alpha / (h_{i-1}/2)^\alpha = 2^\alpha$, so we get $\alpha$ using + \[ c_i := \log_2\left( \frac{e_{i-1}}{e_i} \right). \] + The first value $c_0$ is kept empty. + +\begin{codeexample}[] +% requires \usepackage{array} +\pgfplotstabletypesetfile[ + create on use/rate1/.style={create col/dyadic refinement rate={error1}}, + create on use/rate2/.style={create col/dyadic refinement rate={error2}}, + columns={error1,error2,rate1,rate2}, + columns/error1/.style={sci,sci zerofill}, + columns/error2/.style={sci,sci zerofill}, + columns/rate1/.style={dec sep align}, + columns/rate2/.style={dec sep align}] + {pgfplotstable.example1.dat} +\end{codeexample} + This style employs methods of the floating point unit, that means it works with a relative precision of about $10^{-6}$ ($6$ significant digits in the mantisse). +\end{stylekey} + +\begin{stylekey}{/pgfplots/table/create col/idyadic refinement rate=\marg{column name}} + As |create col/dyadic refinement rate|, but the quotient is inverse. +\end{stylekey} + +\begin{keylist}{ + /pgfplots/table/create col/gradient=\marg{col x}\marg{col y}, + /pgfplots/table/create col/gradient loglog=\marg{col x}\marg{col y}, + /pgfplots/table/create col/gradient semilogx=\marg{col x}\marg{col y}, + /pgfplots/table/create col/gradient semilogy=\marg{col x}\marg{col y}} + A style for |\pgfplotstablecreatecol| which computes piecewise gradients $(y_{i+1} - y_i) / (x_{i+1} - x_i )$ for each row. The $y$ values are taken out of column \marg{col y} and the $x$ values are taken from \marg{col y}. + + The logarithmic variants apply the natural logarithm, $\log(\cdot)$, to its argument before starting to compute differences. More precisely, the |loglog| variant applies the logarithm to both, $x$ and $y$, the |semilogx| variant applies the logarithm only to~$x$ and the |semilogy| variant applies the logarithm only to~$y$. + +\begin{codeexample}[] +% requires \usepackage{array} +\pgfplotstableread{pgfplotstable.example1.dat}\table +\pgfplotstablecreatecol[create col/gradient loglog={dof}{error1}] + {slopes1}\table +\pgfplotstablecreatecol[create col/gradient loglog={dof}{error2}] + {slopes2}\table + +\pgfplotstabletypeset[ + columns={dof,error1,error2,slopes1,slopes2}, + columns/dof/.style={int detect}, + columns/error1/.style={sci,sci zerofill}, + columns/error2/.style={sci,sci zerofill}, + columns/slopes1/.style={dec sep align}, + columns/slopes2/.style={dec sep align}] +\table +\end{codeexample} + +\begin{codeexample}[] +% requires \usepackage{array} +\pgfplotstableread + {pgfplotstable.example1.dat}\table +\pgfplotstablecreatecol + [create col/gradient semilogy={level}{error1}] + {slopes1}\table + +\pgfplotstabletypeset[ + columns={level,error1,slopes1}, + columns/level/.style={int detect}, + columns/error1/.style= + {sci,sci zerofill,sci subscript}, + columns/slopes1/.style={dec sep align}] +\table +\end{codeexample} + This style employs methods of the floating point unit, that means it works with a relative precision of about $10^{-6}$ ($6$ significant digits in the mantisse). +\end{keylist} + \section{Plain \TeX\ and Con\TeX t support} +\label{sec:pgfplotstable:context} The table code generator is initialised to produce \LaTeX\ |tabular| environments. However, it only relies on~`|&|' being the column separator and~`|\\|' the row terminator. The |column type| feature is more or less specific to |tabular|, but you can disable it completely. Replace |begin table| and |end table| with appropriate \TeX- or Con\TeX t commands to change it. If you have useful default styles (or bug reports), let me know. \printindex \bibliographystyle{abbrv} %gerapali} %gerabbrv} %gerunsrt.bst} %gerabbrv}% gerplain} -\bibliography{pgfplots.bib} +\bibliography{pgfplots} \end{document} |