3

In my ME below, I am having a problem to get the header align properly with the text. I have looked at the documentation for the pgfplotstable, also from other posts from this forum like that posted here, here and here, but still cannot get the table to show up properly. I humbly request to get the alignment correct. Here is my code:

\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 Answer 1

3

There is some black magic going on here. With dec sep align key, `pgfplotstable is out putting double the number of columns to align around the decimal point. The columns will be like

rlrlrlrlrl

for a 5 column table. All r column type hold the integers while the l type columns hold the decimal fractions. One can see this by adding

outfile=pgfplotstable.example1.out.tex

to the options of \pgfplotstabletypeset. The file pgfplotstable.example1.out.tex looks like this:

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

So eventually you are dealing with 10 columns not 5. You have to change the head rows accordingly:

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

I have modified every column suitably like

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

as in the code.

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

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .