Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am a beginner in LaTeX, experimenting with industrial documents. I cannot find the suitable code to solve some of the problems below:

  1. I would like to be able to sort the table, but excluding the final row, supposed to be a summary.
  2. I could not find how to apply the fixed, zerofill, dec sep align, precision=2 parameters to all columns but the first.
  3. I could not make the code for bold fonts in the last row to work.
\documentclass{article}
\usepackage{colortbl,hhline} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{booktabs}

\begin{filecontents}{data.csv} 
{Items},{Rating},{Design 1},{Design 2},{Design 3}
Design complexity, 6.3,3.5, 8.5, 8.12
Cost,9.2, 3.8, 5.77, 5.9
Energy consumption,6.62,4.8, 8.1,9.5
Average weighted normalized,, 3.7, 7.92,7.35
\end{filecontents}

\begin{document}
\pgfplotstableread[col sep=comma]{data.csv}{\datatable}

\centering
\pgfplotstabletypeset[font=\small,
every even row/.style={
before row={\rowcolor[gray]{0.9}}},
every head row/.style={
before row=\toprule,after row=\midrule},
every last row/.style={
after row=\bottomrule},
%every first column/.style={string type},%does not work
columns/Items/.style={string type}, %works
column 2/.style={fixed, zerofill, dec sep align, precision=2}, %does not work
every last column/.style={fixed, zerofill, dec sep align, precision=1}, 
%sort, sort key={Design 2}, %Works but should not include last row
%sort cmp={float >}, %Works but should not include last row
every first column/.style={column type={l}}, %Does not work
every head row/.style={
before row={\toprule},
after row=\midrule
},
every last row/.style={before row={\bottomrule}, 
after row={\bottomrule}, 
postproc cell content/.style={@cell content=\textbf{##1}}}, %does not work
]{\datatable}

\end{document}
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted
  1. The task to sort all except for one can be done by means of a temporary column in which the sort weight of the "special" row has an extreme value.
  2. column styles need to be applied to individual columns. You can either assign styles by column name or by (output) column index. In your case, you may want to create a shared style numeric column which is referenced by all others.
  3. This is a usability issue of pgfplotstable; see also PGFplotstable: Is it possible to set a number format for a *row* instead of a column?

Here is your modified example. I added %% CF wherever I modified stuff. I also addressed your "does not work" markers:

\documentclass{article}
\usepackage{colortbl,hhline} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\usepackage{filecontents} \usepackage{booktabs}

\begin{filecontents}{data.csv} 
{Items},{Rating},{Design 1},{Design 2},{Design 3}
Design complexity, 6.3,3.5, 8.5, 8.12
Cost,9.2, 3.8, 5.77, 5.9
Energy consumption,6.62,4.8, 8.1,9.5
Average weighted normalized,, 3.7, 7.92,7.35
\end{filecontents}

\begin{document}
\pgfplotstableread[col sep=comma]{data.csv}{\datatable}

\centering
\pgfplotstabletypeset[
% CF:
numeric column/.style={fixed, zerofill, dec sep align, precision=2},
display columns/1/.style={numeric column},
display columns/2/.style={numeric column},
display columns/3/.style={numeric column},
display columns/4/.style={numeric column},
display columns/5/.style={numeric column},
display columns/6/.style={numeric column},
%%%
font=\small,
every even row/.style={
before row={\rowcolor[gray]{0.9}}},
every head row/.style={
before row=\toprule,after row=\midrule},
every last row/.style={
after row=\bottomrule},
%every first column/.style={string type},%does not work
%% CF: yes, it does: but you overwrite it a couple of lines below
%%
columns/Items/.style={string type}, %works
column 2/.style={fixed, zerofill, dec sep align, precision=2}, %does not work
%% CF: this is the wrong style  'display columns/2/.style='
%%
every last column/.style={fixed, zerofill, dec sep align, precision=1}, 
%sort, sort key={Design 2}, %Works but should not include last row
%sort cmp={float >}, %Works but should not include last row
%% CF:
sort, sort key={sort key},
sort cmp={float >},
create on use/sort key/.style={
    create col/assign/.code={
        \begingroup
            \global\edef\entry{\thisrow{Design 2}}
            \count0=\pgfplotstablerow
            \advance \count0 by1
            \ifnum\count0=\pgfplotstablerows
                \global\def\entry{-1e20}%
            \fi
        \endgroup
        %\message{sort key entry no \pgfplotstablerow / \pgfplotstablerows: \entry^^J}%
        \pgfkeyslet{/pgfplots/table/create col/next content}\entry
    },
},
every first column/.style={column type={l}}, %Does not work
%% CF: see above
every head row/.style={
before row={\toprule},
after row=\midrule
},
every last row/.style={before row={\bottomrule}, 
after row={\bottomrule}, 
postproc cell content/.style={@cell content=\textbf{##1}}}, %does not work
%% CF: confirmed... the style every last row does not support this.
%% Sorry; not properly documented or reported.
%% see http://tex.stackexchange.com/questions/65546/pgfplotstable-is-it-possible-to-set-a-number-format-for-a-row-instead-of-a-co
]{\datatable}

\end{document}

The create on use/sort key declaration defines how to create the sort key column which is referenced in the value of sort key. I used a temporary integer register to add "+1" to the row index, compared that with the number of rows and assigned some special extreme value for the last row. It is awkward due to TeX's ugly programming language...

Does this help you?

share|improve this answer
    
Great lessons. Thanks. As a matter of fact I am now inclined to take another route: ie sorting a table without the last row, calculating separately the contents of the last row, and consolidating the two tables in a single one at the very end, as I have seen possible in the manual, and in some examples on this site. This approach would make the code more flexible. PS Greetings from Paris.. It is amazing to find out that in spite of your new activities, you can still find time to be here helping beginners like me –  Yves Aug 4 '12 at 6:27
    
For some reason, there does not seem to be a strict equivalence between "every first column/.style={string type}," and "columns/Items/.style={string type}," The first command seems to do nothing, since the error "Could not parse..[the column] as a floating point number" is generated, while the second option works normally. In all cases I prefer generic calls rather than calling the exact names of columns, because using the exact names if far less flexible. –  Yves Aug 4 '12 at 7:37
    
@Ives I'm glad the answer proved to be useful. Thanks for the greetings :) Was a good time. Concerning the every first column: it worked for me (except that you declare it twice which essentially overwrites the first declaration). And it is evaluated in the very same place as columns/Items/.style, i.e. there should be no difference. –  Christian Feuersänger Aug 4 '12 at 9:41
    
You are of course correct. In fact "every first column/.style={string type}," worked if I commented "every first column/.style={column type={l}}," I probably failed to understand that the two calls do not work in parallel. I have finally found that "every first column/.style={string type, column type={l}}," works and is certainly the best way... –  Yves Aug 4 '12 at 10:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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