[pstricks] Generate Table via \readdata
Michael Sharpe
msharpe at ucsd.edu
Wed Apr 22 03:54:34 CEST 2009
On Apr 20, 2009, at 11:58 PM, Herbert Voss wrote:
> Buddy Ledger schrieb:
>> Thanks Herbert, works great. How would I include a \hline command to
>> produce the row separation lines? the code below gives a misplaced
>> \noalign error.
>
> I am a bit lost here ... :-( don't know why the last column
> appears. Maybe others can have a look, what'S going wrong.
>
> HEbrert
>
>
> \documentclass[]{article}
> \usepackage[T1]{fontenc}
> \usepackage{filecontents}% allows overwriting
> \begin{filecontents*}{doterrorbar.dat}
> 1.00E+001 2.00E+001 3.00E+001 4.00E+001
> 2.00E+001 3.00E+001 4.00E+001 5.00E+001
> 3.00E+001 4.00E+001 5.00E+001 6.00E+001
> 4.00E+001 5.00E+001 6.00E+001 7.00E+001
> \end{filecontents*}
>
> \usepackage{pstricks-add}
> \makeatletter
> \def\checkEntries#1 #2#3\@nil{%
> \def\@tempA{#3}
> \ifx\@tempA\@empty\else
> \global\advance\pst at cnta\@ne%
> \expandafter\checkEntries#2#3\@nil
> \fi}
> \def\readLine#1#2{%
> \openin1=#1
> \read1 to \pst at tempa
> \global\pst at cnta=0
> \expandafter\checkEntries\pst at tempa\space\@nil
> \advance\pst at cnta\@ne
> \gdef#2{\the\pst at cnta}\ignorespaces}
>
> \def\fillTab at i#1{\fillTab at ii#1}
> \def\fillTab at ii D #1 {%
> #1
> \global\advance\pst at cntm by \@ne
> \ifnum\pst at cntm>\dataNo
> \global\pst at cntm=\@ne
> \\\hline \else & \fi
> \@ifnextchar D\fillTab at ii{}}
> \def\fillTab#1{%
> \global\pst at cntm=\@ne
> \expandafter\fillTab at i#1}
> \makeatother
> \parindent=0pt
> \begin{document}
> \readLine{doterrorbar.dat}{\dataNo}
> \readdata{\data}{doterrorbar.dat}
>
> the data:\\
> \ttfamily|\data|
>
>
> \bigskip
> \begin{tabular}{|*{\dataNo}{r|}}\hline
> \fillTab{\data}
> \end{tabular}
>
> \end{document}
>
I don't know if this is progress, but I have an glimmer of an idea of
what may be going on with the extra row in the table. The table this
produces is the same as the output from
\begin{tabular}{|*{4}{r|}}\hline
1.00E+001 & 2.00E+001 & 3.00E+001 & 4.00E+001 \\\hline
2.00E+001 & 3.00E+001 & 4.00E+001 & 5.00E+001 \\\hline
3.00E+001 & 4.00E+001 & 5.00E+001 & 6.00E+001 \\\hline
4.00E+001 & 5.00E+001 & 6.00E+001 & 7.00E+001 \\\hline
\\
\end{tabular}
So, perhaps, with the table body given by a macro rather than actual
data, the tabular code believes there is no end of line, and adds it.
So, we could omit the \\\hline from the last line of the macro and add
it explicitly at the end. As you will see, this leads to a new
problem---the first column is too wide. Note too that is requires
knowing the line number of the last line---there should be a macro to
determine this from the data, like Herbert's \readLine.
\documentclass[]{article}
\usepackage{filecontents}% allows overwriting
\begin{filecontents*}{doterrorbar.dat}
1.00E+001 2.00E+001 3.00E+001 4.00E+001
2.00E+001 3.00E+001 4.00E+001 5.00E+001
3.00E+001 4.00E+001 5.00E+001 6.00E+001
4.00E+001 5.00E+001 6.00E+001 7.00E+001
\end{filecontents*}
\usepackage{pstricks-add}
\makeatletter
\def\checkEntries#1 #2#3\@nil{%
\def\@tempA{#3}
\ifx\@tempA\@empty\else
\global\advance\pst at cnta\@ne%
\expandafter\checkEntries#2#3\@nil
\fi}
\def\readLine#1#2{%
\openin1=#1
\read1 to \pst at tempa
\global\pst at cnta=0
\expandafter\checkEntries\pst at tempa\space\@nil
\advance\pst at cnta\@ne
\gdef#2{\the\pst at cnta}\ignorespaces}
\def\fillTab at i#1{\fillTab at ii#1}
\def\fillTab at ii D #1 {%
\xdef\myline{\myline\space #1}
\global\advance\pst at cntm by \@ne
\ifnum\pst at cntm>\dataNo %start new line
\global\advance\pst at cntn by \@ne
\global\pst at cntm=\@ne
\ifnum\pst at cntn<4 %omit \\\hline from last line
\xdef\myline{\myline\space\noexpand\\\noexpand\hline}
\fi
\show\myline
\myline\xdef\myline{}
\else
\xdef\myline{\myline\space &}
\fi
\@ifnextchar D\fillTab at ii{}}
\def\fillTab#1{%
\edef\cmd{\noexpand\tabular{|*{\dataNo}{r|}}\noexpand\hline}
\cmd
\global\pst at cntm=\@ne
\global\pst at cntn=0 %line counter
\gdef\myline{} %current line
\expandafter\fillTab at i#1
\\\hline
\endtabular}
\makeatother
\parindent=0pt
\begin{document}
\readLine{doterrorbar.dat}{\dataNo}
\readdata{\data}{doterrorbar.dat}
the data:\\
\ttfamily|\data|
\bigskip
\fillTab{\data}
\end{document}
More information about the PSTricks
mailing list