Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider the following screenshot.

enter image description here

The features I want to have are:

  • I can shrink the page size and the table is allowed to break across pages.
  • The background of odd rows must be different from that of the even rows to ease reading.
  • The values must be calculated automatically rather than by hand-made data entry.

MWE

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage[nomessages]{fp}
\usepackage[table]{xcolor}
\usepackage{longtable}
\usepackage{pgffor}
\begin{document}
\topskip=0pt
\begin{longtable}{*2{|>{$\displaystyle}c<{$}}|}\hline
\theta & \sin \theta\\
\foreach \x in {0,10,...,360}{\x & \FPeval\temp{round(\x:3)}\temp\\}
\end{longtable}
\end{document}
share|improve this question
    
Your solution should combine (1) longtable, (2) xcolor with the table option, (3) either fp or pgfmath. –  Werner Apr 13 '13 at 5:22
    
Related tex.stackexchange.com/a/33771/1410 for zebra stripes. –  morbusg Apr 13 '13 at 8:46

2 Answers 2

up vote 14 down vote accepted

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

share|improve this answer
3  
May be with example to benefit wider audience. –  texenthusiast Apr 13 '13 at 5:55

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.