6

I have Table 1. Repeating values in consecutive cells of a column should be replaced with \textquotedbl as in Table 2. Also \textquotedbl should be aligned centered according to column width although numbers are aligned right.

Here is MWE:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{pgfplotstable}
\pgfplotsset{width=10cm,compat=1.6}
\usepackage{filecontents}
\usepackage[T1]{fontenc} 

\begin{filecontents}{testdataExisting.dat}
id val
1  5634
2  78945
3  78945
4  689623
5  14514
6  14514
7  14514
\end{filecontents}

\begin{filecontents}{testdataGenerated.dat}
id val
1  5634
2  78945
3  \textquotedbl
4  689623
5  14514
6  \textquotedbl
7  \textquotedbl
\end{filecontents}

 \begin{document}
Table 1

 \pgfplotstabletypeset[col sep=space,columns={id,val},
     columns/val/.style={fixed,precision=0,column type=r},
 ]{testdataExisting.dat}

Table 2

 \pgfplotstabletypeset[col sep=space,columns={id,val},
   %  columns/val/.style={fixed,precision=0,column type=r},
    columns/val/.style={string type,column type=r},
 ]{testdataGenerated.dat}
 \end{document}

Output:

In the solution, testdataGenerated.dat should not exist. I need a solution that postprocesses the table of testdataExisting.dat so that it replaces repeating values with \textquotedbl and align these \textquotedbls.

6

You can create a new column on the fly with the duplicate values replaced by quotes as follows:

Sample output

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{pgfplotstable}
\pgfplotsset{width=10cm,compat=1.6}
\usepackage{filecontents}
\usepackage[T1]{fontenc} 

\begin{filecontents}{testdataExisting.dat}
id val
1  5634
2  78945
3  78945
4  689623
5  14514
6  14514
7  14514
\end{filecontents}

\pgfplotstableread{testdataExisting.dat}\origtable

 \begin{document}
 \begin{minipage}{0.4\linewidth}
   Table 1
   \medskip

   \pgfplotstabletypeset[col sep=space,columns={id,val},
   columns/val/.style={fixed,precision=0,column type=r}, ]\origtable
 \end{minipage}
\qquad
\begin{minipage}{0.4\linewidth}
  Table 2
  \medskip

  \pgfplotstableset{create on use/newval/.style={create
  col/assign/.code={
  \getthisrow{val}\curval\getprevrow{val}\pval\ifx\curval\pval\def\curval{\textquotedbl}\fi
  \pgfkeyslet{/pgfplots/table/create col/next content}\curval}}}
  \pgfplotstabletypeset[col sep=space,columns={id,newval},
  columns/newval/.style={string type,column type=r,column name={Value}}, ]\origtable
\end{minipage}
\end{document}

The original data is stored in \origtable. We print this essentially as it stands in the first instance. In the second, we provide create on use code that takes the current value in \curval and the previous value in \pval, compares them and passes the required output to next content.

One way to specify the formatting of the numbers in the new string column is to format these numbers when defining the entries in an \else clause of the comparison with the previous line:

\pgfplotstableset{create on use/newval/.style={create
  col/assign/.code={
  \getthisrow{val}\curval\getprevrow{val}\pval
  \ifx\curval\pval\def\curval{\textquotedbl}\else
  \pgfkeys{/pgf/number format/.cd,fixed,precision=0}%
  \pgfmathprintnumberto{\curval}\curval\fi
  \pgfkeyslet{/pgfplots/table/create col/next content}\curval}}}
  \pgfplotstabletypeset[col sep=space,columns={id,newval},
  columns/newval/.style={string type,column type=r,column name={Value}}, ]\origtable

Sample output

  • Dear @AndrewSwann, is it possible to preserve the format of the numbers in the new table? The thousands separator in numbers has great importance since other columns in my original table must have thousands separators also. – Kadir May 5 '13 at 20:51
  • I have now added code for this. – Andrew Swann May 6 '13 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.