6

Im trying to use pgfplotstable package. And my task is to create new column in a table using this package.

Here is my minimal working example:

\documentclass{article}
\usepackage[]{filecontents}
\usepackage[]{pgfplotstable}

\begin{document}

\begin{filecontents*}{data.txt}
id, Text
10, aaa
20, bbb
30, ccc
40, ddd
\end{filecontents*}

\pgfplotstableread[
  col sep=comma
]{data.txt}\loadedtable

\pgfplotstablecreatecol[
  create col/assign/.code={%
 }]{new}\loadedtable

\begin{table}[H]
\centering
\pgfplotstabletypeset[
  string type
]\loadedtable
\end{table}

\end{document}

And in the result table my row have to look like

id | Text
10, \href{http://example.com/\idcolumn}{\textcolumn}
5

You can also gather the contents in another column and then change the header name

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage[colorlinks=true]{hyperref}

\begin{document}
\begin{table}[H]
\centering
\pgfplotstabletypeset[col sep=comma,string type,
columns={id,link},% Declare the column order
create on use/link/.style={create col/assign/.code={%
\edef\temp{\noexpand\href{http://www.example.com/\thisrow{id}/}{\thisrow{Text}}}%
  \pgfkeyslet{/pgfplots/table/create col/next content}\temp%
  }
},
columns/link/.style={column name=Text}
]{data.csv}
\end{table}
\end{document}

enter image description here

|improve this answer|||||
  • this is exactly what I was looking for. Thank you very much!!! – RusAlex Mar 15 '15 at 10:06
  • @RusAlex My pleasure. – percusse Mar 15 '15 at 14:13
3

After many experiments, I gave up on \pgfplotstablemodifyforeachcolumnelement, nor could I run \pgfplotstablegetelem inside a tabular, so I went old school and used \csname.

Also, I wasn't sure if you want to see \href or use \href, so I did both.

table of hrefs

\documentclass{article}
\usepackage[colorlinks=true]{hyperref}
\usepackage[]{filecontents}
\usepackage[]{pgfplotstable}

\newcounter{row}
\newcount{\total}

\begin{document}

\begin{filecontents*}{data.txt}
id, Text
10, aaa
20, bbb
30, ccc
40, ddd
\end{filecontents*}

\pgfplotstableread[col sep=comma,string type]{data.txt}\loadedtable

\pgfplotstablegetrowsof{\loadedtable}
\total=\pgfplotsretval\relax
\loop
 \pgfplotstablegetelem{\therow}{id}\of\loadedtable
 \expandafter\let\csname id\therow\endcsname=\pgfplotsretval
 \pgfplotstablegetelem{\therow}{Text}\of\loadedtable
 \expandafter\let\csname Text\therow\endcsname=\pgfplotsretval
 \stepcounter{row}
 \ifnum\value{row}<\total\repeat

\begin{table}[th]
\centering
\begin{tabular}{cc}
id & Text \\
\csname id0\endcsname & \verb$\href{http://example.com/$\csname id0\endcsname\verb$}{$\csname Text0\endcsname\verb$}$ \\
\csname id1\endcsname & \verb$\href{http://example.com/$\csname id1\endcsname\verb$}{$\csname Text1\endcsname\verb$}$ \\
\csname id2\endcsname & \verb$\href{http://example.com/$\csname id2\endcsname\verb$}{$\csname Text2\endcsname\verb$}$ \\
\csname id3\endcsname & \verb$\href{http://example.com/$\csname id3\endcsname\verb$}{$\csname Text3\endcsname\verb$}$ 
\end{tabular}
\end{table}

\begin{table}[th]
\centering
\begin{tabular}{cc}
id & Text \\
\csname id0\endcsname & \href{http://example.com/\csname id0\endcsname}{\csname Text0\endcsname} \\
\csname id1\endcsname & \href{http://example.com/\csname id1\endcsname}{\csname Text1\endcsname} \\
\csname id2\endcsname & \href{http://example.com/\csname id2\endcsname}{\csname Text2\endcsname} \\
\csname id3\endcsname & \href{http://example.com/\csname id3\endcsname}{\csname Text3\endcsname}
\end{tabular}
\end{table}

\end{document}

I revised the solution so as not to use tabular. All needed information is passed by macro args or from the table itself.

\documentclass{article}

\usepackage[colorlinks=true]{hyperref}
\usepackage[]{filecontents}
\usepackage[]{pgfplotstable}

\newcommand{\hreftable}[3]% #1 = id column name, #2 =  text column name. #3 = table cname
{\bgroup% allocate registers
  \countdef\row=1
  \countdef\total=2
  \dimendef\idwidth=0
  \dimendef\twidth=1
  \dimendef\temp=2
  \def\sep{\hspace{\arraycolsep}}% distance between columns
%
  \settowidth{\idwidth}{#1}% get column widths
  \settowidth{\twidth}{#2}%
  \pgfplotstablegetrowsof{#3}%
  \total=\pgfplotsretval\relax
%
  \row=0
  \loop
    \pgfplotstablegetelem{\the\row}{#1}\of{#3}%
    \settowidth{\temp}{\pgfplotsretval}%
    \ifdim\temp>\idwidth\relax\idwidth=\temp\fi
    \pgfplotstablegetelem{\the\row}{#2}\of{#3}%
    \settowidth{\temp}{\pgfplotsretval}%
    \ifdim\temp>\twidth\relax\twidth=\temp\fi
    \advance\row by 1
    \ifnum\row<\total\repeat
%
  \parbox{\dimexpr \idwidth+\arraycolsep+\twidth}%
  {\makebox[\idwidth]{#1}\sep\makebox[\twidth]{#2}
  \row=0
  \loop
    \pgfplotstablegetelem{\the\row}{#1}\of{#3}%
    \let\id=\pgfplotsretval%
    \pgfplotstablegetelem{\the\row}{#2}\of{#3}%
    \let\text=\pgfplotsretval%
    \makebox[\idwidth]{\id}\sep\makebox[\twidth]{\href{http://example.com/\id}{\text}}
    \advance\row by 1
    \ifnum\row<\total\repeat
  }
\egroup}

\begin{document}

\begin{filecontents*}{data.txt}
id, Text
10, aaa
20, bbb
30, ccc
40, ddd
\end{filecontents*}

\pgfplotstableread[col sep=comma,string type]{data.txt}\loadedtable

\hreftable{id}{Text}{\loadedtable}

\end{document}

BTW, I could NOT figure out how to get the column names from the table itself. I even tried using \pgfplotstabletranspose.

|improve this answer|||||
  • Thanks for your answer. And yep, I wanted to process columns as an href linkable value. Your second answer is too complicated for me yet =) Still looking for solution via create column pgfplotstable – RusAlex Mar 11 '15 at 6:35
  • 1
    Okay, but just remember, packages are supposed to make things easier, not harder. pgfplotstable is very good at reading in data from files for use with things like pgfplots, but otherwise is a pain to use. – John Kormylo Mar 11 '15 at 14:50

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.