如何使用 pgfplotstable 修改長表的版面

如何使用 pgfplotstable 修改長表的版面

為了準備一系列包含複雜長表的工業報告,我結合了我在網站上找到的程式碼範例,這看起來很有希望。在我的領域,儘管通常建議做法,但單元格需要用垂直線和水平線分隔。在MWE(非常簡化與實際需要)中,我仍然面臨兩個問題:資料行1和2之間沒有水平線。我也歡迎任何改進程式碼的提示(我不清楚在哪裡最好地進行列的格式設置,例如在每行之間定義一條水平線)和/或視覺外觀。 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}

答案1

讓我們從問題的簡單方面開始:

  • 如果喜歡使用垂直線,則不要使用 中的規則\booktabs。相反,他們使用\hline
  • 對於垂直線,我建議執行以下操作:

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

  • 合我口味比較好說every nth row={1}{before row=\hline}。表格下的文字(下一頁繼續)沒有在其下劃線。

完整程式碼:

\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}

在此輸入影像描述

在此輸入影像描述

相關內容