How to generate a table of trigonometric functions that can be broken across pages?

Packages

  • xcolor with the option table for \rowcolor
  • pgfplotstable (and internally pgfmath) that is used to build the entire table. The pgfmath package helps us to create the trigonometric values (which are all built-in)
  • longtable to allow page-breaks in one table (needs multiple passes)
  • siunitx to typeset numbers in tables (pgfmath does a good job on number printing already, but that is not very well suited for tables)
  • booktabs for nice rules.

\pgfplotstableset

Auxiliary styles

Styles prepended with an @ are new styles created by me. Neither is the @ needed (just to separate pgfplotstable’s styles from mine) nor are the names set in stone. These styles are used with the .list handler to ease the creation of the columns.

The line

@create function/.list={sin,cos,tan,cot,cosec,sec},

builds the main columns, note that the csc function is named cosec in PGF. The column is named cosec but I later change the column name to csc. (One may also note that one should use proper column names, e.g. $\phi$ and $\sin \phi$ instead of empty column names and non-math-mode but math-function names.)

Note the string type key, this deactivates PGF printing number features (but not the PGF math calculation of the columns).

longtable setup

The basic keys begin table and end table are used to set the internal table environment from tabular to longtable.

The every head row style is (mis-)used to set up the special rows (these are longtable features). See the longtable manual for more.

\pgfplotstablenew

Let's create 91 rows (+ header (e.g. longtable preamble)):

\pgfplotstablenew[
    columns={left,sin,cos,tan,cot,sec,cosec,right},
    ]{91}\myTable

\sisetup

Certain siunitx settings are made before the actual typesetting (this could have been done in the S[…] column specifications, too). These settings are needed so that siunitx doesn’t try to setup numbers in scientific notation as PGF math may give an output in the form of 1.746e-2 (pretty much everywhere).

\pgfplotstabletypeset

Finally!

Here the auxiliary styles for setting up the column types are used.

The @secure header uses the \multicolumn macro to hide the content from siunitx’s parsing. The usual approach by putting the content in braces, e.g. {sin} doesn’t appear to be working here.

The left and the right column only uses r columns (we could use S here, too) with an appended \si{\degree} via array’s <{…} syntax. This is also the reason the header needs an empty \multicolumn{1}{c}{} entry.

Code

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{siunitx}
\usepackage{booktabs}
\pgfplotstableset{
    % helpers
    @create function/.style={
        create on use/#1/.style={
            create col/expr=#1(\thisrow{left})}},
    @secure header/.style={
        columns/#1/.append style={
            column name=\multicolumn{1}{c}{#1}}},
    @set columns to siunitx type 1/.style={
        columns/#1/.append style={
            string type,
            column type={S[table-format=1.4]}}},
    @set columns to siunitx type 2/.style={
        columns/#1/.append style={
            string type,
            string replace={inf}{\multicolumn{1}{c}{$\infty$}},
            column type={S[table-format=2.4]}}},
    @set columns to siunitx type 3/.style={
        columns/#1/.append style={
            string type,
            string replace={inf}{\multicolumn{1}{c}{$\infty$}},
            column type={S[table-format=2.3]}}},
    @set columns to basic style/.style={
        columns/#1/.append style={
            column type={r<{\si{\degree}}}}},
    @set empty header/.style={
        columns/#1/.append style={
            column name={\multicolumn{1}{c}{}}}},
    %
    % the left and right columns
    create on use/left/.style={
        create col/expr=\pgfplotstablerow},
    create on use/right/.style={
        create col/expr={90-\thisrow{left}}},
    %
    % Let's start: the functions
    @create function/.list={sin,cos,tan,cot,cosec,sec},
    % The longtable setup
    begin table=\begin{longtable},
    end table=\end{longtable},
    every head row/.append style={
        before row=\toprule,
        after row=%
        \midrule \endhead
        \midrule
        \multicolumn{1}{c}{} & {cos} & {sin} & {cot} & {tan} & {csc} & {sec} & \multicolumn{1}{r}{\dots}\\ \bottomrule
        \endfoot
        \midrule
        \multicolumn{1}{c}{} & {cos} & {sin} & {cot} & {tan} & {csc} & {sec} & \multicolumn{1}{r}{} \\ \bottomrule
        \endlastfoot},
    every odd row/.style={before row={\rowcolor[gray]{.9}}},
}
\pgfplotstablenew[
    columns={left,sin,cos,tan,cot,sec,cosec,right},
    ]{91}\myTable
\begin{document}
\sisetup{scientific-notation = fixed, fixed-exponent = 0, table-auto-round=true}
\pgfplotstabletypeset[
    % the column types
    @set columns to siunitx type 1/.list={sin,cos},
    @set columns to siunitx type 2=tan,
    @set columns to siunitx type 3/.list={cot,sec,cosec},
    @set columns to basic style/.list={left,right},
    % setup for the headers
    @secure header/.list={sin,cos,tan,cot,sec},
    @set empty header/.list={left,right},
    columns/cosec/.append style={column name={\multicolumn{1}{c}{csc}}},
]\myTable
\end{document}

Output

enter image description here

enter image description here

enter image description here


More easily, you can use the calculator package which will allow you to evaluate the trigonometric functions. Also the supertabular package will allow you to make the repeated heading/footer on each table.

Here is a simpler example:

Code

\documentclass{article}
\usepackage[nomessages]{fp}
\usepackage[table]{xcolor}
\usepackage{calculator}
\usepackage{forloop}
\usepackage{supertabular}
\usepackage{longtable}
\usepackage{fullpage}

\begin{document}

\newcounter{theangle}

\tablefirsthead{
    \cline{2-4}
    \rowcolor{white}
    \multicolumn{1}{c|}{ } &
    \cos \theta & \sin \theta & \tan \theta &
    \multicolumn{1}{c}{ } \\
    \hline
      }
\tablehead{
    \rowcolor{white}
    \multicolumn{5}{c}{Table continued \ldots}\\
    \cline{2-4}
    \rowcolor{white}
    \multicolumn{1}{c|}{ } &
    \cos \theta & \sin \theta & \tan \theta &
    \multicolumn{1}{c}{ } \\
    \hline
      }
\tablelasttail{
    \rowcolor{white}
    \multicolumn{1}{c|}{ } &
    \cos \theta & \sin \theta & \tan \theta &
    \multicolumn{1}{c}{ }\\
    \cline{2-4}
      }
\tabletail{
    \hline
    \rowcolor{white}
    \multicolumn{1}{c|}{ } &
    \cos \theta & \sin \theta & \tan \theta &
    \multicolumn{1}{c}{  } \\
    \cline{2-4}
    \rowcolor{white}
    \multicolumn{5}{c}{Continued on next page \ldots}\\
      }

\rowcolors{2}{gray!50}{white}
\begin{center}
\begin{supertabular}{*5{|>{$}c<{$}}|}
\forloop{theangle}{0}{\value{theangle} < 360}{
    \arabic{theangle}^\circ &
    \DEGREESCOS{\value{theangle}}{\solx} \solx &
    \DEGREESSIN{\value{theangle}}{\solx} \solx &
    \DEGREESTAN{\value{theangle}}{\solx} \solx &
    \arabic{theangle}^\circ\\
    \addtocounter{theangle}{4} 
      }
\end{supertabular}

\end{center}
\end{document}

Output Table Start Table Bottom Table Continued