1

What I want: to make a chart from a csv file (exp1.csv).

Problem:

  1. I don't get all the data visible in the chart
  2. The header of my table is not consistent (see \hline) Could you help me to solve those two problems?
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[left=3cm,top=3cm,right=3cm]{geometry}
\usepackage{tabularx}
\usepackage{xfrac}
\usepackage{float}
\newcolumntype{Y}{>{\small\raggedright\arraybackslash}X}
\usepackage{csvsimple}

\begin{filecontents*}{exp1.csv}
Ultrapure,0.406,0.384,0.362
Deionized,2.78,2.769,2.603
\end{filecontents*}

\begin{document}

\subsection*{Characterisation of chemicals}
\begin{table}[ht]{
\scalebox{1}{
\begin{tabularx}{\textwidth}{|Y||Y|Y|}
\bfseries Water type & \bfseries value 1 & \bfseries value 2\\
\csvreader[head to column names]{exp1.csv}{}
{\\\hline\csvcoli&\csvcolii& \csvcoliii}\\
\hline
\end{tabularx}}}
\end{table}

\end{document}
2
  • Your csv file has four columns, hence I guess your tabular also need four columns.
  • Your csv contains no table head, hence the use of option no head.
\documentclass[a4paper, 11pt]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[left=3cm,top=3cm,right=3cm]{geometry}
\usepackage{tabularx}
\usepackage{xfrac}
\usepackage{float}
\newcolumntype{Y}{>{\small\raggedright\arraybackslash}X}
\usepackage{csvsimple}

\begin{filecontents*}{exp1.csv}
Ultrapure,0.406,0.384,0.362
Deionized,2.78,2.769,2.603
\end{filecontents*}

\begin{document}

\subsection*{Characterisation of chemicals}
\begin{table}[ht]
  \begin{tabularx}{\textwidth}{*3{|Y}|Y|}
    \hline
    \bfseries Water type & \bfseries value 1 & \bfseries value 2 & \bfseries value 3
    \csvreader[no head]{exp1.csv}{}{\\ \hline \csvcoli & \csvcolii & \csvcoliii & \csvcoliv}
    \\ \hline
  \end{tabularx}
\end{table}

\end{document}

enter image description here

Follow-up: Multi-page tables with X column type

  • Load ltablex and uses \keepXColumns to activate the effect of X column type.
  • Move tabularx environment output of table floating environment, and use tabularx like longtable environment. For example, use \caption{...} \\ in tabularx to put an caption.
\documentclass[a4paper, 11pt]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[left=3cm,top=3cm,right=3cm]{geometry}
\usepackage{tabularx}
\usepackage{ltablex}
  \keepXColumns

\usepackage{xfrac}
\usepackage{float}
\newcolumntype{Y}{>{\small\raggedright\arraybackslash}X}

\usepackage{csvsimple}

\begin{filecontents*}{exp1.csv}
Ultrapure,0.406,0.384,0.362
Deionized,2.78,2.769,2.603
\end{filecontents*}

\begin{document}

\subsection*{Characterisation of chemicals}
  \begin{tabularx}{\textwidth}{XXXX}
    \caption{text} \\
    \hline
    \bfseries Water type & \bfseries value 1 & \bfseries value 2 & \bfseries value 3
    \csvreader[no head]{exp1.csv}{}{\\ \hline \csvcoli & \csvcolii & \csvcoliii & \csvcoliv}
    \\ \hline
  \end{tabularx}

\end{document}
7
  • thank you! amazing :D – Elia May 22 '20 at 4:59
  • Do you know how to make this table span multiple pages because with longpages instead tabularx it changes the whole layout – Elia May 22 '20 at 5:43
  • 2
    @Elia: xltabular combines the features of tabularx (X type columns) and longtable(mid-table page breaks). Why would you need tabularx in the first place for this specific table? The only thing it does here is artificially making the table a lot wider than it needs to be, making it less readable. – leandriis May 22 '20 at 6:53
  • 1
    @Elia I've appended a multi-page table with X column type to my answer. – muzimuzhi Z May 22 '20 at 6:56
  • @muzimuzhiZ thank you! your answer is very helpful – Elia May 22 '20 at 8:40

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.