pgfplot 中的“dec sep align”与标题不正确对齐

pgfplot 中的“dec sep align”与标题不正确对齐

在下面的 ME 中,我无法让标题与文本正确对齐。我查看了 pgfplotstable 的文档,也查看了本论坛的其他帖子,例如这里这里这里,但仍然无法让表格正确显示。我谦虚地请求正确对齐。这是我的代码:

\documentclass{book}
\usepackage{pgfplots, pgfplotstable, booktabs, colortbl, siunitx, array}
\pgfplotsset{compat=1.8}
\usepackage{longtable}
\usepackage{array}

\usepackage{filecontents}
\begin{filecontents}{measurements.dat}
theta1    r2v    r3v      x      y
73.32513 0.08109 0.32216 1.07136 1.58646
81.52127 0.39714 0.18930 1.12726 1.54498
11.42881 0.15561 0.40579 2.00083 -0.25418
82.20383 0.26427 0.26641 0.98759 1.65272
56.91233 0.08282 0.17536 1.39728 1.09515
8.77864 0.30099 0.46950 2.07209 -0.48038
25.06484 0.13149 0.43797 2.02329 0.26017
49.21934 0.32704 0.27508 1.78136 0.81424
86.17562 0.34461 0.31124 0.95389 1.75653
86.83997 0.37408 0.29352 0.96191 1.74816
14.18518 0.22527 0.10387 1.73510 -0.29920
87.35335 0.04191 0.15062 0.60779 1.62930
86.14503 0.11449 0.23546 0.72013 1.69588
43.68381 0.45667 0.11524 1.82554 0.43447
72.02524 0.07619 0.42215 1.13327 1.65837
12.76977 0.41291 0.09738 1.76232 -0.52640
\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
            }
        }
    }
}
\pgfkeys{
    /pgfplots/table/string type in dec sep align/.style={
        string type,
        postproc cell content/.code={%
            \ifnum\pgfplotstablepartno=0%
                \pgfkeys{/pgfplots/table/@cell content/.add={}{&}}
            \fi
        }%
    }
}

\pgfplotstableset{
begin table=\begin{longtable},
end table=\end{longtable},
}

% https://tex.stackexchange.com/questions/40411/import-files-with-pgfplotstable-and-split-tables-across-multiple-pages

\pgfplotstabletypeset[col sep=space,empty header,precision=5,fixed,fixed zerofill,
%columns={theta1,r2v,r3v,x,y},      % display specified columns
columns/theta1/.style={dec sep align={c|}, column type/.add={}{|}},
columns/r2v/.style={dec sep align={c|}, column type/.add={}{|}},
columns/r3v/.style={dec sep align={c|}, column type/.add={}{|}},
columns/x/.style={dec sep align={c|}, column type/.add={}{|}},
columns/y/.style={dec sep align={c}},
every first row/.append style={before row={%
    \caption{This is a Table with Data}%
    \label{tab:DataTable}\\\toprule
    $\theta_{1}$ & $r_{2v}$ & $r_{3v}$ & $x$ & $y$ \\ \toprule
    \endfirsthead
    %
    \multicolumn{5}{c}%
    {{\bfseries Table \thetable\ Continued from previous page}} \\
    \toprule
    %
    $\theta_{1}$ & $r_{2v}$ & $r_{3v}$ & $x$ & $y$ \\ \toprule
    \endhead
    %
    \midrule \multicolumn{5}{r}{{Continued on next page}} \\ \bottomrule
    \endfoot
    %
    \midrule
    \multicolumn{5}{r}{{End of Table}} \\ \bottomrule
    \endlastfoot
    }},%
    %
]{measurements.dat}

\end{document}

答案1

这里有一些黑魔法。使用dec sep alignkey,`pgfplotstable 会将列数增加一倍以对齐小数点。列将像

rlrlrlrlrl

对于 5 列表格。所有r列类型都保存整数,而l类型列保存小数。可以通过添加

outfile=pgfplotstable.example1.out.tex

到 的选项\pgfplotstabletypeset。该文件pgfplotstable.example1.out.tex如下所示:

\begin {longtable}{r<{\pgfplotstableresetcolortbloverhangright }@{}l<{\pgfplotstableresetcolortbloverhangleft }r<{\pgfplotstableresetcolortbloverhangright }@{}l<{\pgfplotstableresetcolortbloverhangleft }r<{\pgfplotstableresetcolortbloverhangright }@{}l<{\pgfplotstableresetcolortbloverhangleft }r<{\pgfplotstableresetcolortbloverhangright }@{}l<{\pgfplotstableresetcolortbloverhangleft }r<{\pgfplotstableresetcolortbloverhangright }@{}l<{\pgfplotstableresetcolortbloverhangleft }}%
\caption {This is a Table with Data}\label {tab:DataTable}\\\toprule \multicolumn {2}{c}{$\theta _{1}$} & \multicolumn {2}{c}{$r_{2v}$} & \multicolumn {2}{c}{$r_{3v}$} & \multicolumn {2}{c}{$x$} & \multicolumn {2}{c}{$y$} \\ \toprule \endfirsthead \multicolumn {10}{c}{{\bfseries Table \thetable \ Continued from previous page}} \\ \toprule \multicolumn {2}{c}{$\theta _{1}$} & \multicolumn {2}{c}{$r_{2v}$} & \multicolumn {2}{c}{$r_{3v}$} & \multicolumn {2}{c}{$x$} & \multicolumn {2}{c}{$y$} \\ \toprule \endhead \midrule \multicolumn {10}{r}{{Continued on next page}} \\ \bottomrule \endfoot \midrule \multicolumn {5}{r}{{End of Table}} \\ \bottomrule \endlastfoot %
$73$&$.32513$&$0$&$.08109$&$0$&$.32216$&$1$&$.07136$&$1$&$.58646$\\%
$81$&$.52127$&$0$&$.39714$&$0$&$.18930$&$1$&$.12726$&$1$&$.54498$\\%
$11$&$.42881$&$0$&$.15561$&$0$&$.40579$&$2$&$.00083$&$-0$&$.25418$\\%
$82$&$.20383$&$0$&$.26427$&$0$&$.26641$&$0$&$.98759$&$1$&$.65272$\\%
$56$&$.91233$&$0$&$.08282$&$0$&$.17536$&$1$&$.39728$&$1$&$.09515$\\%
$8$&$.77864$&$0$&$.30099$&$0$&$.46950$&$2$&$.07209$&$-0$&$.48038$\\%
$25$&$.06484$&$0$&$.13149$&$0$&$.43797$&$2$&$.02329$&$0$&$.26017$\\%
$49$&$.21934$&$0$&$.32704$&$0$&$.27508$&$1$&$.78136$&$0$&$.81424$\\%
$86$&$.17562$&$0$&$.34461$&$0$&$.31124$&$0$&$.95389$&$1$&$.75653$\\%
$86$&$.83997$&$0$&$.37408$&$0$&$.29352$&$0$&$.96191$&$1$&$.74816$\\%
$14$&$.18518$&$0$&$.22527$&$0$&$.10387$&$1$&$.73510$&$-0$&$.29920$\\%
$87$&$.35335$&$0$&$.04191$&$0$&$.15062$&$0$&$.60779$&$1$&$.62930$\\%
$86$&$.14503$&$0$&$.11449$&$0$&$.23546$&$0$&$.72013$&$1$&$.69588$\\%
$43$&$.68381$&$0$&$.45667$&$0$&$.11524$&$1$&$.82554$&$0$&$.43447$\\%
$72$&$.02524$&$0$&$.07619$&$0$&$.42215$&$1$&$.13327$&$1$&$.65837$\\%
$12$&$.76977$&$0$&$.41291$&$0$&$.09738$&$1$&$.76232$&$-0$&$.52640$\\%
\end {longtable}%

因此,最终您要处理的是 10 列而不是 5 列。您必须相应地更改头行:

every head row/.append style={before row={%
    \caption{This is a Table with Data}%
    \label{tab:DataTable}\\\toprule
    \multicolumn{2}{c}{$\theta_{1}$} & \multicolumn{2}{c}{$r_{2v}$} & \multicolumn{2}{c}{$r_{3v}$} & \multicolumn{2}{c}{$x$} & \multicolumn{2}{c}{$y$} \\ \toprule
    \endfirsthead
    %
    \multicolumn{10}{c}%
    {{\bfseries Table \thetable\ Continued from previous page}} \\
    \toprule
    %
    \multicolumn{2}{c}{$\theta_{1}$} & \multicolumn{2}{c}{$r_{2v}$} & \multicolumn{2}{c}{$r_{3v}$} & \multicolumn{2}{c}{$x$} & \multicolumn{2}{c}{$y$} \\ \toprule
    \endhead
    %
    \midrule \multicolumn{10}{r}{{Continued on next page}} \\ \bottomrule
    \endfoot
    %
    \midrule
    \multicolumn{10}{r}{{End of Table}} \\ \bottomrule
    \endlastfoot
    }},%

我已经适当地修改了每一列

 column type={c},dec sep align,precision=5,fixed,fixed zerofill

如代码所示。

\documentclass{book}
\usepackage{pgfplotstable, booktabs}
\pgfplotsset{compat=1.8}
\usepackage{longtable}
\usepackage{array}

\usepackage{filecontents}
\begin{filecontents}{measurements.dat}
theta1    r2v    r3v      x      y
73.32513 0.08109 0.32216 1.07136 1.58646
81.52127 0.39714 0.18930 1.12726 1.54498
11.42881 0.15561 0.40579 2.00083 -0.25418
82.20383 0.26427 0.26641 0.98759 1.65272
56.91233 0.08282 0.17536 1.39728 1.09515
8.77864 0.30099 0.46950 2.07209 -0.48038
25.06484 0.13149 0.43797 2.02329 0.26017
49.21934 0.32704 0.27508 1.78136 0.81424
86.17562 0.34461 0.31124 0.95389 1.75653
86.83997 0.37408 0.29352 0.96191 1.74816
14.18518 0.22527 0.10387 1.73510 -0.29920
87.35335 0.04191 0.15062 0.60779 1.62930
86.14503 0.11449 0.23546 0.72013 1.69588
43.68381 0.45667 0.11524 1.82554 0.43447
72.02524 0.07619 0.42215 1.13327 1.65837
12.76977 0.41291 0.09738 1.76232 -0.52640
\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
            }
        }
    }
}
%\pgfkeys{
%    /pgfplots/table/string type in dec sep align/.style={
%        string type,
%        postproc cell content/.code={%
%            \ifnum\pgfplotstablepartno=0%
%                \pgfkeys{/pgfplots/table/@cell content/.add={}{&}}
%            \fi
%        }%
%    }
%}

%\pgfplotstableset{
%begin table=\begin{longtable},
%end table=\end{longtable},
%}

% http://tex.stackexchange.com/questions/40411/import-files-with-pgfplotstable-and-split-tables-across-multiple-pages

\pgfplotstabletypeset[col sep=space,empty header,
columns={theta1,r2v,r3v,x,y},      % display specified columns
begin table=\begin{longtable},
end table=\end{longtable},
columns/theta1/.style={column type={c},dec sep align,precision=5,fixed,fixed zerofill},
columns/r2v/.style={column type={c},dec sep align,precision=5,fixed,fixed zerofill},
columns/r3v/.style={column type={c},dec sep align,precision=5,fixed,fixed zerofill},
columns/x/.style={column type={c},dec sep align,precision=5,fixed,fixed zerofill},
columns/y/.style={column type={c},dec sep align,precision=5,fixed,fixed zerofill},
every head row/.append style={before row={%
    \caption{This is a Table with Data}%
    \label{tab:DataTable}\\\toprule
    \multicolumn{2}{c}{$\theta_{1}$} & \multicolumn{2}{c}{$r_{2v}$} & \multicolumn{2}{c}{$r_{3v}$} & \multicolumn{2}{c}{$x$} & \multicolumn{2}{c}{$y$} \\ \toprule
    \endfirsthead
    %
    \multicolumn{10}{c}%
    {{\bfseries Table \thetable\ Continued from previous page}} \\
    \toprule
    %
    \multicolumn{2}{c}{$\theta_{1}$} & \multicolumn{2}{c}{$r_{2v}$} & \multicolumn{2}{c}{$r_{3v}$} & \multicolumn{2}{c}{$x$} & \multicolumn{2}{c}{$y$} \\ \toprule
    \endhead
    %
    \midrule \multicolumn{10}{r}{{Continued on next page}} \\ \bottomrule
    \endfoot
    %
    \midrule
    \multicolumn{10}{r}{{End of Table}} \\ \bottomrule
    \endlastfoot
    }},%
    %outfile=pgfplotstable.example1.out.tex
    %
]{measurements.dat}

\end{document}

在此处输入图片描述

相关内容