pgfplots 表中居中标题的问题

pgfplots 表中居中标题的问题

我关注了一些与 pgfplots 表中标题居中有关的帖子,发现这里但是,我似乎仍然无法将标题置于列数字的中央。您能帮我将标题与数字对齐吗?谢谢。

这是我的代码:

\documentclass{book}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.12}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{filecontents}

\begin{filecontents}{dataa.dat}
k      z   xbar   Pkp1   Kkp1
1 12.0000 10.0000 0.2222 0.1111
2 17.7735 15.7735 1.3846 0.6923
3 21.1068 19.1068 0.5306 0.2653
4 23.0313 21.0313 0.9703 0.4851
5 24.1424 22.1424 0.6801 0.3401
6 24.7839 22.7839 0.8474 0.4237
7 25.1543 23.1543 0.7422 0.3711
8 25.3682 23.3682 0.8050 0.4025
\end{filecontents}%

\begin{document}

%%% 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},
        }
    }
}{
    % 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
            }
        }
    }
}

\pgfplotstableset{
begin table=\bgroup\small\setlength{\tabcolsep}{10pt}\begin{longtable},
end table=\end{longtable}\egroup,
}

\pgfplotstableset{
    % #1 = row index
    % #2 = row style keys
    row style/.style 2 args={
        every row #1 column 0/.style={#2},
        every row #1 column 1/.style={#2},
        every row #1 column 2/.style={#2},
        every row #1 column 3/.style={#2},
        every row #1 column 4/.style={#2},
        every row #1 column 5/.style={#2},
        every row #1 column 6/.style={#2},
        every row #1 column 7/.style={#2},
        every row #1 column 8/.style={#2},
        every row #1 column 9/.style={#2},
        every row #1 column 10/.style={#2},
    }
}

\pgfplotstabletypeset[col sep=space,
empty header,
columns/k/.style={column type={c},fixed zerofill,precision=0,dec sep align},
columns/z/.style={column type={c},fixed zerofill,precision=4,dec sep align},
columns/xbar/.style={column type={c},fixed zerofill,precision=4,dec sep align},
columns/Pkp1/.style={column type={c},fixed zerofill,precision=4,dec sep align},
columns/Kkp1/.style={column type={c},fixed zerofill,precision=4,dec sep align},
every first row/.append style={before row={%
    \caption{Tabulated Data for Problem 19-6(a)}%
    \label{tab:DataTable}\\\toprule
    &\multicolumn{1}{c} {$k$} &&  
    \multicolumn{1}{c} {$z$} &&
    \multicolumn{1}{c} {$\hat{x}_{k+1}$} &&  
    \multicolumn{1}{c} {$P_{k+1}$} &&
    \multicolumn{1}{c} {$K_{k+1}$} \\ \toprule
    \endfirsthead
    %
    \multicolumn{10}{r}%
    {{\bfseries Table \thetable\ Continued from previous page}} \\
    \toprule
    %
    &\multicolumn{1}{c} {$k$} &&
    \multicolumn{1}{c} {$z$} &&
    \multicolumn{1}{c} {$\hat{x}_{k+1}$} &&
    \multicolumn{1}{c} {$P_{k+1}$} &&
    \multicolumn{1}{c} {$K_{k+1}$} \\ \toprule
    \endhead
    %
    \midrule \multicolumn{10}{c}{{Continued on next page}} \\ \bottomrule
    \endfoot
    %
    \midrule
    \multicolumn{10}{r}{{End of Table}} \\ \bottomrule
    \endlastfoot
    }},%
    %
]{dataa.dat}

\end{document} 

虽然我使用了\multicolumn{1}{c}代码,但标题仍然没有与数据正确对齐。

答案1

这是你想要的?

\documentclass{book}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.12}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{filecontents}

\begin{filecontents}{dataa.dat}
k      z   xbar   Pkp1   Kkp1
1 12.0000 10.0000 0.2222 0.1111
2 17.7735 15.7735 1.3846 0.6923
3 21.1068 19.1068 0.5306 0.2653
4 23.0313 21.0313 0.9703 0.4851
5 24.1424 22.1424 0.6801 0.3401
6 24.7839 22.7839 0.8474 0.4237
7 25.1543 23.1543 0.7422 0.3711
8 25.3682 23.3682 0.8050 0.4025
\end{filecontents}%

\begin{document}

%%% 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},
        }
    }
}{
    % 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
            }
        }
    }
}

\pgfplotstableset{
begin table=\bgroup\small\setlength{\tabcolsep}{10pt}\begin{longtable},
end table=\end{longtable}\egroup,
}

\pgfplotstableset{
    % #1 = row index
    % #2 = row style keys
    row style/.style 2 args={
        every row #1 column 0/.style={#2},
        every row #1 column 1/.style={#2},
        every row #1 column 2/.style={#2},
        every row #1 column 3/.style={#2},
        every row #1 column 4/.style={#2},
        every row #1 column 5/.style={#2},
        every row #1 column 6/.style={#2},
        every row #1 column 7/.style={#2},
        every row #1 column 8/.style={#2},
        every row #1 column 9/.style={#2},
        every row #1 column 10/.style={#2},
    }
}

\pgfplotstabletypeset[col sep=space,
empty header,
columns/k/.style={column type={c},fixed zerofill,precision=0,dec sep align},
columns/z/.style={column type={c},fixed zerofill,precision=4,dec sep align},
columns/xbar/.style={column type={c},fixed zerofill,precision=4,dec sep align},
columns/Pkp1/.style={column type={c},fixed zerofill,precision=4,dec sep align},
columns/Kkp1/.style={column type={c},fixed zerofill,precision=4,dec sep align},
every first row/.append style={before row={%
    \caption{Tabulated Data for Problem 19-6(a)}%
    \label{tab:DataTable}\\\toprule
    \multicolumn{2}{c} {$k$}&
    \multicolumn{2}{c} {$z$} &
    \multicolumn{2}{c} {$\hat{x}_{k+1}$} &
    \multicolumn{2}{c} {$P_{k+1}$} &
    \multicolumn{2}{c} {$K_{k+1}$} \\ \toprule
    \endfirsthead
    %
    \multicolumn{10}{r}%
    {{\bfseries Table \thetable\ Continued from previous page}} \\
    \toprule
    %
    \multicolumn{2}{c} {$k$}&
    \multicolumn{2}{c} {$z$} &
    \multicolumn{2}{c} {$\hat{x}_{k+1}$} &
    \multicolumn{2}{c} {$P_{k+1}$} &
    \multicolumn{2}{c} {$K_{k+1}$} \\ \toprule
    \endhead
    %
    \midrule \multicolumn{10}{c}{{Continued on next page}} \\ \bottomrule
    \endfoot
    %
    \midrule
    \multicolumn{10}{r}{{End of Table}} \\ \bottomrule
    \endlastfoot
    }},%
    %
]{dataa.dat}

\end{document}

在此处输入图片描述

相关内容