2

I am using pgfplotstable to import .csv file to LaTeX.

I successfully wrapped column texts using p{width} command in each column type which also prevented me from centering the columns and which didn't wrap the headers (the header overlaps with the next column header).

Could you please help me wrap the header and the texts being able to center them?

Here is some of the code:

\renewcommand{\arraystretch}{1.5}
\afterpage{
\pgfplotstableset{
begin table=\begin{longtable},
end table=\end{longtable},
%outfile={testtable.tex} % to check the resulting table code
}
\pgfplotstabletypeset[
    col sep=comma,
    string type,
    columns/No/.style={column name=No, column type={p{.05\textwidth}}},
    columns/Abbreviation/.style={column name={Abbreviation}, column type={p{.05\textwidth}}},
    columns/Trait/.style={column name={Trait}, column type={p{.2\textwidth}}},
    columns/Unit/.style={column name={Unit}, column type={p{.2\textwidth}}},
    columns/Trait description/.style={column name={Trait description}, column type={p{.4\textwidth}}},
    every head row/.style={before row=\hline, after row=\hline},
    every last row/.style={after row=\hline},
    every head row/.append style={before row={% <==================
    \caption{List of the data to be collected from different traits, their abbreviation, units and description}\label{datalist}\\[0.5cm]
    \hline
    \endfirsthead
    \multicolumn{5}{c}{{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
    \hline
    \multicolumn{1}{p{.05\textwidth}}{\textbf{No}} & \multicolumn{1}{p{.05\textwidth}}{\textbf{Abbreviation}} & \multicolumn{1}{p{.2\textwidth}}    {\textbf{Trait}} & \multicolumn{1}{p{.2\textwidth}}{\textbf{Unit}} & \multicolumn{1}{p{.4\textwidth}}{\textbf{Trait description}} \\ 
    \hline
    \endhead
    \hline
    \multicolumn{5}{r}{{Continued on next page}} \\ %\bottomrule
    \endfoot % <============================================================
    \endlastfoot % <========================================================
    }
    }
        ]{Datalist.csv}
}

In this code the header "Abbreviation" and "Trait" overlap rather than wrapping the header "Abbreviation".

CarLaTeX
  • 62,716
Laly
  • 21
  • Welcome at tex.sx! Could you extend you code snippet such that we can compile it? Optimally, it is a minimal working example which is short and shows just the problem at hand. – Christian Feuersänger Jan 15 '16 at 18:44
  • Aside from my previous comment, the question sounds as if it is more "how can I configure longtable to wrap columns, and how can I use longtable from within pgfplotstable. Note that pgfplotstable on its own does not apply any line wrapping – Christian Feuersänger Jan 15 '16 at 18:46
  • @ChristianFeuersänger Here are some more lines from the preamble: – Laly Jan 30 '16 at 08:29
  • \documentclass[12pt,a4paper]{report}\usepackage{pgfplotstable} \pgfplotsset{compat=1.12} \usepackage{booktabs} – Laly Jan 30 '16 at 08:31

1 Answers1

1

The problem doesn't concern pgfplotstable but it's simply that "Abbreviation" is longer than .05\textwidth, you should modify the widths of your columns (as I've done in the following code) or/and change the font size of your table or write "Abbr." instead of "Abbreviation" (to have a column header with

Abbre-
viation

seems not very beautiful to me). See also here: My table doesn't fit; what are my options?

Since you didn't post a complete MWE, and we don't know your actual data, we can't say which solution is better.

As for centering the columns, I've created a new column type C, using array package, which has the column width as a parameter.

\documentclass[12pt,a4paper]{report}
\usepackage{pgfplotstable} 
\pgfplotsset{compat=1.15}% current version
%\usepackage{showframe}% for testing purpose only
\usepackage{afterpage}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{caption}
\captionsetup{labelfont=bf, textfont=bf, labelsep=endash, 
    justification=centering}
\usepackage{filecontents}
\begin{filecontents*}{Datalist.csv}
    No,Abbreviation,Trait,Unit,Trait description
    1,$U_{g}$,Good users,\TeX\ Stack Exchange,They always add a complete MWE (these definition are taken from the first DuckBoat - Herr Professor Paulinho van Duck's article)
    2,$U_{b}$,Bad users,\TeX\ Stack Exchange,They do not add a complete MWE
    2a,$U_{k}$,Uninformed users,\TeX\ Stack Exchange,They do not know what an MWE is
    2b,$U_{h}$,Disoriented users,\TeX\ Stack Exchange,They vaguely know what an MWE is but do not know how to create it in a correct way
    2c,$U_{l}$,Free riders,\TeX\ Stack Exchange,They do know what an MWE and how to create it but are too lazy to add it
    1,$U_{g}$,Good users,\TeX\ Stack Exchange,They always add a complete MWE (these definition are taken from the first DuckBoat - Herr Professor Paulinho van Duck's article)
    2,$U_{b}$,Bad users,\TeX\ Stack Exchange,They do not add a complete MWE
    2a,$U_{k}$,Uninformed users,\TeX\ Stack Exchange,They do not know what an MWE is
    2b,$U_{h}$,Disoriented users,\TeX\ Stack Exchange,They vaguely know what an MWE is but do not know how to create it in a correct way
    2c,$U_{l}$,Free riders,\TeX\ Stack Exchange,They do know what an MWE and how to create it but are too lazy to add it
\end{filecontents*}

\begin{document}
\renewcommand{\arraystretch}{1.5}
\afterpage{
    \pgfplotstableset{
        begin table=\begin{longtable},
            end table=\end{longtable},
        %outfile={testtable.tex} % to check the resulting table code
    }
\pgfplotstabletypeset[
    col sep=comma,
    string type,
    columns/No/.style={column name=\textbf{No}, 
        column type={C{.05\textwidth}}},
    columns/Abbreviation/.style={column name={\textbf{Abbreviation}}, 
        column type={C{.2\textwidth}}},
    columns/Trait/.style={column name={\textbf{Trait}}, 
        column type={C{.16\textwidth}}},
    columns/Unit/.style={column name={\textbf{Unit}}, 
        column type={C{.15\textwidth}}},
    columns/Trait description/.style={
        column name={\textbf{Trait description}}, 
        column type={C{.28\textwidth}}},
    every head row/.style={before row=\hline, after row=\hline},
    every last row/.style={after row=\hline},
    every head row/.append style={before row={% <==================
            \caption{List of the data to be collected from different traits, their abbreviation, units and description}\label{datalist}\\[0.5cm]
            \hline
            \endfirsthead
            \multicolumn{5}{c}{{\bfseries \tablename\ \thetable{} -- continued from previous page}}\\[7pt]
            \hline
            \multicolumn{1}{C{.05\textwidth}}{\textbf{No}} & \multicolumn{1}{C{.2\textwidth}}{\textbf{Abbreviation}} & \multicolumn{1}{C{.16\textwidth}}{\textbf{Trait}} & \multicolumn{1}{C{.15\textwidth}}{\textbf{Unit}} & \multicolumn{1}{C{.28\textwidth}}{\textbf{Trait description}} \\ 
            \hline
            \endhead
            \hline
            \multicolumn{5}{r}{{Continued on next page}} \\ %\bottomrule
            \endfoot % <============================================================
            \endlastfoot % <========================================================
        }
    }
    ]{Datalist.csv}
}
\end{document}

enter image description here

CarLaTeX
  • 62,716