2

In order to prepare a long series of industrial reports containing complicated long tables, I am combining samples of code I have found on the site, and this looks promising. In my field, cells need to be separated by vertical and horizontal lines, in spite of the usually recommended practices. In the MWE (very simplified vs the actual neeed), I am still facing two problems: there is no horizontal line between data row 1 and 2. Also, I could not place a vertical line between the two columns (and others to be added). I would welcome also any tip to improve the code (It is unclear to me where to best do the formatting of columns and say, define a horizontal line between eauch row) and/or the visual appearance. The vertical lines are not continuous in the MWE.

\documentclass[a5paper]{article}    
\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{array}
\usepackage[top=8 cm,bottom=8cm]{geometry} %For demo purposes
\usepackage{filecontents}

\begin{filecontents}{data1.csv}
column1,column2
5001,102
5002,75
5003,115
5004,45
5005,97
5036,110
5037,77
5038,147
5039,89
5040,62
5041,160
5042,102
\end{filecontents}%

\begin{document}

%%% Code from Dr. Christian Feuersanger ------ 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},
        }
    }
}{
    % 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
            }
        }
    }
}
%%%-----------------------------------------------


\newcommand\MyHead[2]{%
  \multicolumn{1}{|> {\centering}m{#1}|}{#2}
}

\pgfplotstabletypeset[
    empty header,
    outfile = test.dat,
    begin table=\begin{longtable},
     every first column/.style ={%
     column type/.add={|}{}
     },
         every last column/.style ={%
     column type/.add={}{|}
    },
          every head row/.style={output empty row},
    every nth row={1}{after row=\midrule},
 every first row/.append style={before row={%
    \caption{This is  a long table spreading over several pages.}%
    \label{tab:DataTable}\\\toprule
\MyHead{4cm} {column with long header} & \MyHead{4cm} {Other column with very long header}  \\ 
\hline    
    \endfirsthead
    %
    \multicolumn{2}{c}%
    {{\bfseries Table \thetable\ Continued from previous page}} \\
    \toprule 
    %
\MyHead{4cm} {column with long header} & \MyHead{4cm} {Other column with very long header}  \\ 
\hline    
    \endhead
    %
    \midrule \multicolumn{2}{r}{{Continued on next page}} \\ \bottomrule
    \endfoot
    %
    \midrule
    \multicolumn{2}{r}{{End of table}} \\ \bottomrule
    \endlastfoot
    }},%
    %
    end table=\end{longtable},
   col sep=comma,
    string type,
    ]{data1.csv}


\end{document}
  • you should delete those empty header styles they are too old today – percusse Feb 13 '16 at 13:38
1

Lets start from easy side of your question(s):

  • if like to use vertical lines, than don't use rules from \booktabs. Instead them use \hline
  • for vertical lines I sugest to do the following:

    every odd column/.style ={% column type/.add={|}{} }, every even column/.style ={% column type/.add={|}{} }, every last column/.style ={% column type/.add={}{|} },

  • to my taste is better to say every nth row={1}{before row=\hline}. With this text under table (Continued on next page) hasn't line under it.

Complete code:

\documentclass[a5paper]{article}
\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{array}
\usepackage[top=8 cm,bottom=8cm]{geometry} %For demo purposes
\usepackage{filecontents}

\begin{filecontents}{data1.csv}
column1,column2
5001,102
5002,75
5003,115
5004,45
5005,97
5036,110
5037,77
5038,147
5039,89
5040,62
5041,160
5042,102
\end{filecontents}%

\begin{document}

%%% Code from Dr. Christian Feuersanger ------ 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},
        }
    }
}{
    % 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
            }
        }
    }
}
%%%-----------------------------------------------
\newcommand\MyHead[2]{%
  \multicolumn{1}{|> {\centering}m{#1}|}{#2}
}    
\pgfplotstabletypeset[
    empty header,
    outfile = test.dat,
    begin table=\begin{longtable},
     every odd column/.style ={%
     column type/.add={|}{}
     },
     every even column/.style ={%
     column type/.add={|}{}
     },
         every last column/.style ={%
     column type/.add={}{|}
    },
          every head row/.style={output empty row},
    every nth row={1}{before row=\hline},
 every first row/.append style={before row={%
    \caption{This is  a long table spreading over several pages.}%
    \label{tab:DataTable}\\\hline
\MyHead{4cm} {column with long header} & \MyHead{4cm} {Other column with very long header}  \\
    \hline
    \endfirsthead
    %
    \multicolumn{2}{c}%
    {Table \thetable\ Continued from previous page} \\
    \hline
    %
\MyHead{4cm} {column with long header} & \MyHead{4cm} {Other column with very long header}  \\
    \endhead
    %
    \multicolumn{2}{r}{{Continued on next page}} \\
    \endfoot
    %
    \hline
    \multicolumn{2}{r}{{End of table}} \\ 
    \endlastfoot
    }},%
    %
    end table=\end{longtable},
   col sep=comma,
    string type,
    ]{data1.csv}
\end{document}

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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