This question is building off the solution found here. This solution given on that post, begins the table on the next page. How to begin the table on the same page as the \section or \chapter beginning?

Also, from the code given below, although the data is complete, a header with no data is printed on a new page. How to overcome this issue?

Here is the MWE:

 \documentclass{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry}
\usepackage{pgfplotstable, booktabs}
\pgfplotsset{compat=newest}
                \usepackage{supertabular}
                \usepackage{amsmath}
                \usepackage{array}
                \usepackage{fancyhdr}
                \pagestyle{fancy}
                \fancyhf{}

                \usepackage{filecontents}
                \begin{filecontents}{verylong.txt}
                c   G(c)GTOT    GhatGTOT    MODELPERFORM
                0.10106383  1   0.971896956 0.971896956
                0.095744681 1   0.974238876 0.974238876
                0.090425532 1   0.974238876 0.974238876
                0.085106383 1   0.974238876 0.974238876
                0.079787234 1   0.974238876 0.974238876
                0.074468085 1   0.976580796 0.976580796
                0.069148936 1   0.978922717 0.978922717
                0.063829787 1   0.978922717 0.978922717
                0.058510638 1   0.981264637 0.981264637
                0.053191489 1   0.981264637 0.981264637
                0.04787234  1   0.981264637 0.981264637
                0.042553191 1   0.990632319 0.990632319
                0.037234043 1   0.990632319 0.990632319
                0.031914894 1   0.992974239 0.992974239
                0.026595745 1   0.992974239 0.992974239
                0.021276596 1   0.992974239 0.992974239
                0.015957447 1   1   1
                0.010638298 1   1   1
                0.005319149 1   1   1
                0   1   1   1
                0.893617021 0.629976581 0.585480094 0.92936803
                0.888297872 0.644028103 0.592505855 0.92
                0.882978723 0.658079625 0.613583138 0.932384342
                0.877659574 0.672131148 0.62295082  0.926829268
                0.872340426 0.683840749 0.634660422 0.928082192
                0.867021277 0.695550351 0.641686183 0.922558923
                0.861702128 0.707259953 0.641686183 0.907284768
                0.856382979 0.718969555 0.644028103 0.895765472
                0.85106383  0.730679157 0.655737705 0.897435897
                0.845744681 0.742388759 0.669789227 0.902208202
                0.840425532 0.754098361 0.669789227 0.888198758
                0.835106383 0.763466042 0.669789227 0.877300613
                0.829787234 0.772833724 0.683840749 0.884848485
                0.824468085 0.782201405 0.68852459  0.880239521
                0.819148936 0.791569087 0.700234192 0.884615385
                0.813829787 0.800936768 0.711943794 0.888888889
                0.808510638 0.81030445  0.716627635 0.884393064
                0.803191489 0.817330211 0.723653396 0.885386819
                0.10106383  1   0.971896956 0.971896956
                0.095744681 1   0.974238876 0.974238876
                0.090425532 1   0.974238876 0.974238876
                0.085106383 1   0.974238876 0.974238876
                0.079787234 1   0.974238876 0.974238876
                0.074468085 1   0.976580796 0.976580796
                0.069148936 1   0.978922717 0.978922717
                0.063829787 1   0.978922717 0.978922717
                0.058510638 1   0.981264637 0.981264637
                0.053191489 1   0.981264637 0.981264637
                0.04787234  1   0.981264637 0.981264637
                0.042553191 1   0.990632319 0.990632319
                0.037234043 1   0.990632319 0.990632319
                0.031914894 1   0.992974239 0.992974239
                0.026595745 1   0.992974239 0.992974239
                0.021276596 1   0.992974239 0.992974239
                0.015957447 1   1   1
                0.010638298 1   1   1
                0.005319149 1   1   1
                0   1   1   1
                0.638297872 0.948477752 0.880562061 0.928395062
                0.632978723 0.950819672 0.880562061 0.926108374
                0.627659574 0.953161593 0.880562061 0.923832924
                0.622340426 0.955503513 0.880562061 0.921568627
                0.617021277 0.957845433 0.880562061 0.919315403
                0.611702128 0.960187354 0.880562061 0.917073171
                0.606382979 0.962529274 0.880562061 0.914841849
                0.10106383  1   0.971896956 0.971896956
                0.095744681 1   0.974238876 0.974238876
                0.090425532 1   0.974238876 0.974238876
                0.085106383 1   0.974238876 0.974238876
                0.079787234 1   0.974238876 0.974238876
                0.074468085 1   0.976580796 0.976580796
                0.069148936 1   0.978922717 0.978922717
                0.063829787 1   0.978922717 0.978922717
                0.058510638 1   0.981264637 0.981264637
                0.053191489 1   0.981264637 0.981264637
                0.04787234  1   0.981264637 0.981264637
                0.042553191 1   0.990632319 0.990632319
                0.037234043 1   0.990632319 0.990632319
                0.031914894 1   0.992974239 0.992974239
                0.026595745 1   0.992974239 0.992974239
                0.021276596 1   0.992974239 0.992974239
                0.015957447 1   1   1
                0.010638298 1   1   1
                0.005319149 1   1   1
                0   1   1   1
                0.521276596 1   0.915690867 0.915690867
                0.515957447 1   0.915690867 0.915690867
                0.510638298 1   0.915690867 0.915690867
                0.505319149 1   0.915690867 0.915690867
                0.5 1   0.915690867 0.915690867
                0.494680851 1   0.918032787 0.918032787
                0.489361702 1   0.918032787 0.918032787
                0.484042553 1   0.920374707 0.920374707
                0.478723404 1   0.920374707 0.920374707
                0.473404255 1   0.920374707 0.920374707
                0.468085106 1   0.920374707 0.920374707
                0.462765957 1   0.922716628 0.922716628
                0.457446809 1   0.922716628 0.922716628
                0.45212766  1   0.925058548 0.925058548
                0.446808511 1   0.925058548 0.925058548
                0.441489362 1   0.925058548 0.925058548
                0.436170213 1   0.925058548 0.925058548
                0.10106383  1   0.971896956 0.971896956
                0.095744681 1   0.974238876 0.974238876
                0.090425532 1   0.974238876 0.974238876
                0.085106383 1   0.974238876 0.974238876
                0.079787234 1   0.974238876 0.974238876
                0.074468085 1   0.976580796 0.976580796
                0.069148936 1   0.978922717 0.978922717
                0.063829787 1   0.978922717 0.978922717
                0.058510638 1   0.981264637 0.981264637
                0.053191489 1   0.981264637 0.981264637
                0.04787234  1   0.981264637 0.981264637
                0.042553191 1   0.990632319 0.990632319
                0.037234043 1   0.990632319 0.990632319
                0.031914894 1   0.992974239 0.992974239
                0.026595745 1   0.992974239 0.992974239
                0.021276596 1   0.992974239 0.992974239
                0.015957447 1   1   1
                0.010638298 1   1   1
                0.005319149 1   1   1
                0   1   1   1
                0.367021277 1   0.948477752 0.948477752
                0.361702128 1   0.948477752 0.948477752
                0.356382979 1   0.950819672 0.950819672
                0.35106383  1   0.950819672 0.950819672
                0.345744681 1   0.950819672 0.950819672
                0.340425532 1   0.950819672 0.950819672
                0.335106383 1   0.950819672 0.950819672
                0.329787234 1   0.950819672 0.950819672
                0.324468085 1   0.950819672 0.950819672
                0.319148936 1   0.950819672 0.950819672
                0.313829787 1   0.950819672 0.950819672
                0.10106383  1   0.971896956 0.971896956
                0.095744681 1   0.974238876 0.974238876
                0.090425532 1   0.974238876 0.974238876
                0.085106383 1   0.974238876 0.974238876
                \end{filecontents}


                \begin{document}
                \section{This is a VERY LONG TABLE}
                \twocolumn

                %%% Code from Dr. Christian ------ for not using headers.----------------------
                \pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{
                    % upcoming releases offer this more convenient option:
                    \pgfplotstableset{
                        empty header/.style={
                          every head row/.style={output empty row},
                          outfile=pgfplotstable.example1.out.tex,
                        }
                    }
                }{
                    % versions up to and including 1.5.1 need this:
                    \pgfplotstableset{
                        empty header/.style={
                            typeset cell/.append code={%
                                \ifnum\pgfplotstablerow=-1 %
                                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
                                \fi
                            }
                        }
                    }
                }

                \noindent
                {\scriptsize
                % define tab first header
                \tablefirsthead{\multicolumn{2}{c}{\emph{c}} & \multicolumn{2}{c}{$\dfrac{G(c)}{G_{TOT}}$} & \multicolumn{2}{c}{$\dfrac{\hat{G}(c)}{G_{TOT}}$} & \multicolumn{2}{c}{$\phi(c)$} \\[7pt] \toprule\\}
                %define subsequent headers
                \tablehead{\multicolumn{8}{c}%
                    {{\bfseries Table \thetable\ \textit{(Continued)}}}\\
                    \toprule
                    %
                    \multicolumn{2}{c}{\emph{c}} & \multicolumn{2}{c}{$\dfrac{G(c)}{G_{TOT}}$} & \multicolumn{2}{c}{$\dfrac{\hat{G}(c)}{G_{TOT}}$} & \multicolumn{2}{c}{$\phi(c)$} \\[7pt] \toprule\\}
                %define tails
                \tabletail{ \midrule \multicolumn{8}{r}{{Continued on next column}} \\ \bottomrule\\}
                %define last tail
                \tablelasttail{\midrule\multicolumn{8}{r}{{End of Table}} \\ \bottomrule\\}
                %define caption
                \tablecaption{This is a Table with Data}
                \pgfplotstabletypeset[col sep=space,empty header,
                columns={c,G(c)GTOT,GhatGTOT,MODELPERFORM},      % display specified columns
                begin table=\begin{supertabular},
                end table=\end{supertabular},
                columns/c/.style={column type={c},dec sep align,precision=4,fixed,fixed zerofill},
                columns/G(c)GTOT/.style={column type={c},dec sep align,precision=4,fixed,fixed zerofill},
                columns/GhatGTOT/.style={column type={c},dec sep align,precision=4,fixed,fixed zerofill},
                columns/MODELPERFORM/.style={column type={c},dec sep align,precision=4,fixed,fixed zerofill},
                every head row/.append style={before row={%
                    \label{tab:DataTable}
                    }},
                ]{verylong.txt}
                \onecolumn %restores one-column output
                \end{document} 
  • 1
    Your MWE does not compile… – Moriambar Apr 10 '17 at 18:29
  • I was able to get it to compile on my machine...I checked it by copying and pasting the code into my compiler and it compiles. What error do you get? Thanks. – Joe Apr 10 '17 at 20:01
  • 1
    sorry, had to clean aux files :) works now. – Moriambar Apr 10 '17 at 20:07
up vote 2 down vote accepted

Borrowing from this question, I suggest you introduce the following command:

\makeatletter
\let\mcnewpage=\newpage
\newcommand{\TrickSupertabularIntoMulticols}{%
\renewcommand\newpage{%
    \if@firstcolumn%
        \hrule width\linewidth height0pt%
            \columnbreak%
        \else%
          \mcnewpage%
        \fi%
}%
}
\makeatother

which basically tricks supertabular into breaking into a new column instead of a new page while in the multicols(*) environment

So this is the solution:

\documentclass{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry}
\usepackage{multicol}
\usepackage{pgfplotstable, booktabs}
\pgfplotsset{compat=newest}
                \usepackage{supertabular}
                \makeatletter
\let\mcnewpage=\newpage
\newcommand{\TrickSupertabularIntoMulticols}{%
\renewcommand\newpage{%
    \if@firstcolumn%
        \hrule width\linewidth height0pt%
            \columnbreak%
        \else%
          \mcnewpage%
        \fi%
}%
}
\makeatother



       \usepackage{amsmath}
            \usepackage{array}
            \usepackage{fancyhdr}
            \pagestyle{fancy}
            \fancyhf{}

            \usepackage{filecontents}
            \begin{filecontents}{verylong.txt}
            c   G(c)GTOT    GhatGTOT    MODELPERFORM
            0.10106383  1   0.971896956 0.971896956
            0.095744681 1   0.974238876 0.974238876
            0.090425532 1   0.974238876 0.974238876
            0.085106383 1   0.974238876 0.974238876
            0.079787234 1   0.974238876 0.974238876
            0.074468085 1   0.976580796 0.976580796
            0.069148936 1   0.978922717 0.978922717
            0.063829787 1   0.978922717 0.978922717
            0.058510638 1   0.981264637 0.981264637
            0.053191489 1   0.981264637 0.981264637
            0.04787234  1   0.981264637 0.981264637
            0.042553191 1   0.990632319 0.990632319
            0.037234043 1   0.990632319 0.990632319
            0.031914894 1   0.992974239 0.992974239
            0.026595745 1   0.992974239 0.992974239
            0.021276596 1   0.992974239 0.992974239
            0.015957447 1   1   1
            0.010638298 1   1   1
            0.005319149 1   1   1
            0   1   1   1
            0.893617021 0.629976581 0.585480094 0.92936803
            0.888297872 0.644028103 0.592505855 0.92
            0.882978723 0.658079625 0.613583138 0.932384342
            0.877659574 0.672131148 0.62295082  0.926829268
            0.872340426 0.683840749 0.634660422 0.928082192
            0.867021277 0.695550351 0.641686183 0.922558923
            0.861702128 0.707259953 0.641686183 0.907284768
            0.856382979 0.718969555 0.644028103 0.895765472
            0.85106383  0.730679157 0.655737705 0.897435897
            0.845744681 0.742388759 0.669789227 0.902208202
            0.840425532 0.754098361 0.669789227 0.888198758
            0.835106383 0.763466042 0.669789227 0.877300613
            0.829787234 0.772833724 0.683840749 0.884848485
            0.824468085 0.782201405 0.68852459  0.880239521
            0.819148936 0.791569087 0.700234192 0.884615385
            0.813829787 0.800936768 0.711943794 0.888888889
            0.808510638 0.81030445  0.716627635 0.884393064
            0.803191489 0.817330211 0.723653396 0.885386819
            0.10106383  1   0.971896956 0.971896956
            0.095744681 1   0.974238876 0.974238876
            0.090425532 1   0.974238876 0.974238876
            0.085106383 1   0.974238876 0.974238876
            0.079787234 1   0.974238876 0.974238876
            0.074468085 1   0.976580796 0.976580796
            0.069148936 1   0.978922717 0.978922717
            0.063829787 1   0.978922717 0.978922717
            0.058510638 1   0.981264637 0.981264637
            0.053191489 1   0.981264637 0.981264637
            0.04787234  1   0.981264637 0.981264637
            0.042553191 1   0.990632319 0.990632319
            0.037234043 1   0.990632319 0.990632319
            0.031914894 1   0.992974239 0.992974239
            0.026595745 1   0.992974239 0.992974239
            0.021276596 1   0.992974239 0.992974239
            0.015957447 1   1   1
            0.010638298 1   1   1
            0.005319149 1   1   1
            0   1   1   1
            0.638297872 0.948477752 0.880562061 0.928395062
            0.632978723 0.950819672 0.880562061 0.926108374
            0.627659574 0.953161593 0.880562061 0.923832924
            0.622340426 0.955503513 0.880562061 0.921568627
            0.617021277 0.957845433 0.880562061 0.919315403
            0.611702128 0.960187354 0.880562061 0.917073171
            0.606382979 0.962529274 0.880562061 0.914841849
            0.10106383  1   0.971896956 0.971896956
            0.095744681 1   0.974238876 0.974238876
            0.090425532 1   0.974238876 0.974238876
            0.085106383 1   0.974238876 0.974238876
            0.079787234 1   0.974238876 0.974238876
            0.074468085 1   0.976580796 0.976580796
            0.069148936 1   0.978922717 0.978922717
            0.063829787 1   0.978922717 0.978922717
            0.058510638 1   0.981264637 0.981264637
            0.053191489 1   0.981264637 0.981264637
            0.04787234  1   0.981264637 0.981264637
            0.042553191 1   0.990632319 0.990632319
            0.037234043 1   0.990632319 0.990632319
            0.031914894 1   0.992974239 0.992974239
            0.026595745 1   0.992974239 0.992974239
            0.021276596 1   0.992974239 0.992974239
            0.015957447 1   1   1
            0.010638298 1   1   1
            0.005319149 1   1   1
            0   1   1   1
            0.521276596 1   0.915690867 0.915690867
            0.515957447 1   0.915690867 0.915690867
            0.510638298 1   0.915690867 0.915690867
            0.505319149 1   0.915690867 0.915690867
            0.5 1   0.915690867 0.915690867
            0.494680851 1   0.918032787 0.918032787
            0.489361702 1   0.918032787 0.918032787
            0.484042553 1   0.920374707 0.920374707
            0.478723404 1   0.920374707 0.920374707
            0.473404255 1   0.920374707 0.920374707
            0.468085106 1   0.920374707 0.920374707
            0.462765957 1   0.922716628 0.922716628
            0.457446809 1   0.922716628 0.922716628
            0.45212766  1   0.925058548 0.925058548
            0.446808511 1   0.925058548 0.925058548
            0.441489362 1   0.925058548 0.925058548
            0.436170213 1   0.925058548 0.925058548
            0.10106383  1   0.971896956 0.971896956
            0.095744681 1   0.974238876 0.974238876
            0.090425532 1   0.974238876 0.974238876
            0.085106383 1   0.974238876 0.974238876
            0.079787234 1   0.974238876 0.974238876
            0.074468085 1   0.976580796 0.976580796
            0.069148936 1   0.978922717 0.978922717
            0.063829787 1   0.978922717 0.978922717
            0.058510638 1   0.981264637 0.981264637
            0.053191489 1   0.981264637 0.981264637
            0.04787234  1   0.981264637 0.981264637
            0.042553191 1   0.990632319 0.990632319
            0.037234043 1   0.990632319 0.990632319
            0.031914894 1   0.992974239 0.992974239
            0.026595745 1   0.992974239 0.992974239
            0.021276596 1   0.992974239 0.992974239
            0.015957447 1   1   1
            0.010638298 1   1   1
            0.005319149 1   1   1
            0   1   1   1
            0.367021277 1   0.948477752 0.948477752
            0.361702128 1   0.948477752 0.948477752
            0.356382979 1   0.950819672 0.950819672
            0.35106383  1   0.950819672 0.950819672
            0.345744681 1   0.950819672 0.950819672
            0.340425532 1   0.950819672 0.950819672
            0.335106383 1   0.950819672 0.950819672
            0.329787234 1   0.950819672 0.950819672
            0.324468085 1   0.950819672 0.950819672
            0.319148936 1   0.950819672 0.950819672
            0.313829787 1   0.950819672 0.950819672
            0.10106383  1   0.971896956 0.971896956
            0.095744681 1   0.974238876 0.974238876
            0.090425532 1   0.974238876 0.974238876
            0.085106383 1   0.974238876 0.974238876
            \end{filecontents}


            \begin{document}

            \section{This is a VERY LONG TABLE}
            \begin{multicols*}{2}
            \TrickSupertabularIntoMulticols
            %%% Code from Dr. Christian ------ for not using headers.----------------------
            \pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{
                % upcoming releases offer this more convenient option:
                \pgfplotstableset{
                    empty header/.style={
                      every head row/.style={output empty row},
                      outfile=pgfplotstable.example1.out.tex,
                    }
                }
            }{
                % versions up to and including 1.5.1 need this:
                \pgfplotstableset{
                    empty header/.style={
                        typeset cell/.append code={%
                            \ifnum\pgfplotstablerow=-1 %
                                \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
                            \fi
                        }
                    }
                }
            }

            \noindent
            {\scriptsize
            % define tab first header
            \tablefirsthead{\multicolumn{2}{c}{\emph{c}} & \multicolumn{2}{c}{$\dfrac{G(c)}{G_{TOT}}$} & \multicolumn{2}{c}{$\dfrac{\hat{G}(c)}{G_{TOT}}$} & \multicolumn{2}{c}{$\phi(c)$} \\[7pt] \toprule\\}
            %define subsequent headers
            \tablehead{\multicolumn{8}{c}%
                {{\bfseries Table \thetable\ \textit{(Continued)}}}\\
                \toprule
                %
                \multicolumn{2}{c}{\emph{c}} & \multicolumn{2}{c}{$\dfrac{G(c)}{G_{TOT}}$} & \multicolumn{2}{c}{$\dfrac{\hat{G}(c)}{G_{TOT}}$} & \multicolumn{2}{c}{$\phi(c)$} \\[7pt] \toprule\\}
            %define tails
            \tabletail{ \midrule \multicolumn{8}{r}{{Continued on next column}} \\ \bottomrule\\}
            %define last tail
            \tablelasttail{\midrule\multicolumn{8}{r}{{End of Table}} \\ \bottomrule\\}}
            %define caption
            \tablecaption{This is a Table with Data}
            \pgfplotstabletypeset[col sep=space,empty header,
            columns={c,G(c)GTOT,GhatGTOT,MODELPERFORM},      % display specified columns
            begin table=\begin{supertabular},
            end table=\end{supertabular},
            columns/c/.style={column type={c},dec sep align,precision=4,fixed,fixed zerofill},
            columns/G(c)GTOT/.style={column type={c},dec sep align,precision=4,fixed,fixed zerofill},
            columns/GhatGTOT/.style={column type={c},dec sep align,precision=4,fixed,fixed zerofill},
            columns/MODELPERFORM/.style={column type={c},dec sep align,precision=4,fixed,fixed zerofill},
            every head row/.append style={before row={%
                \label{tab:DataTable}
                }},
            ]{verylong.txt}

            \end{multicols*} %restores one-column output

            test
            \end{document} 

Yielding:

enter image description here


DETAILS

  • In your preamble include the code about \TrickSupertabularIntoMulticols and also \usepackage{multicol}

  • Get rid of \twocolumn and \onecolumn

  • Use \begin{multicols*}{2} (assuming you want two columns) before the table. This starred version simply does not want to obtain perfectly balanced columns in the last page (I would recommend to keep the starred version)

  • Immediately below that use \TrickSupertabularIntoMulticols

  • Put there the code (i.e. from \pgfkeysifdefined on)

  • At the end of the table put \end{multicols*}

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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