[Tex/LaTex] Tabularx or Tabulary populated by csvsimple

csvsimpletablestabularxtabulary

I am using csvsimple to import table data.

Some cell information is too long and I would like to dynamically manage the column width. I have tried combining the tabularx and tabulary environments with the csvsimple part, but I have been unsuccessful. I am using the memoir document class.

Here is my fixed-width code:

\documentclass[pdftex,10pt,letterpaper, openleft, twoside]{memoir}
\usepackage{array,booktabs,csvsimple,longtable}
...
%Type of column
\newcolumntype{P}[1]{>{#1\hspace{0pt}\arraybackslash}p{75pt}}
...
\begin{document}
...
\csvreader[tabular={P{\raggedright}P{\raggedright}P{\raggedright}P{\raggedright}P{\raggedright}}, %
head to column names=true, %
table head=\hline \textbf{Short header} & \textbf{Middle header} & %
\textbf{middle header} & \textbf{long text header} & \textbf{long text header}\\\hline, %
table foot=\hline, filter equal={\csvcoli}{Something}]{mycsv.csv}%
{}%
{\csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi}\\
\end{document}

Best Answer

It would be difficult to integrate tabularx into the option list of the macro \csvreader; for an environment, there would be ways.

But you can always put a tabularx environment around a \csvreader. The following example shows, how to do this:

\begin{filecontents*}{mycsv.csv}
This,is,my,example
Red,Green,This is some very very long text,This is also some very very long text
Blue,Yellow,This is some even longer text without real content,This is also some even longer text without real content
\end{filecontents*}

\documentclass[pdftex,10pt,letterpaper, openleft, twoside]{memoir}
\usepackage{array,booktabs,csvsimple,longtable,tabularx}

\begin{document}

\begin{tabularx}{\linewidth}{llXX}\toprule
\textbf{A} & \textbf{B} & \textbf{C} & \textbf{D}\\\midrule
\csvreader[late after line=\\\midrule,late after last line=\\\bottomrule]
  {mycsv.csv}
  {}
  {\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}
\end{tabularx}

\end{document}

enter image description here

Related Question