2

I am trying to use CSV reader (from the csvsimple package) to print out some data to a table. However, I am observing a weird offset in the first two cells of the first column:

\documentclass[12pt,a4paper]{report}
\usepackage{csvsimple}
\usepackage{filecontents}

\begin{document}

\begin{filecontents}[overwrite]{data.csv} x,y,z 42,43,44 42,43,44 42,43,44 42,43,44 42,43,44 \end{filecontents}

\begin{table}[h] \centering \begin{tabular}{|l|l|l|} \hline $x$ & $y$ & $z$ \tabularnewline \hline\hline 42 & 43 & 44 \tabularnewline\hline \csvreader[ head to column names, late after line=\tabularnewline\hline] {data.csv}{}{ \x & \y & \z } \end{tabular} \end{table}

\end{document}

offsets

This issue clearly comes from the \csvreader macro (removing the first line of hardcoded data does not affect the offset); using p{0.5cm} instead of l solves the issue for me, but I would ideally want to stick with l. What am I missing here?

My TeX is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian) (preloaded format=pdflatex 2020.10.20) My csvsimple is Package: csvsimple 2019/04/09 version 1.21 LaTeX CSV file processing

UPD: ideally, I would like to retain the separation of the body of the table and the header, since this is a small piece of a bigger puzzle and I would like to be able to design a complex table header)

3 Answers3

3

enter image description here

\documentclass{article}
\usepackage{csvsimple}

\begin{document}

\begin{filecontents}[overwrite]{test.csv} x,y,z 42,43,44 42,43,44 42,43,44 42,43,44 42,43,44 \end{filecontents}

\csvautotabular{test.csv} \csvreader[ tabular = |l|l|l|, table head = \hline $X$&$Y$ &$Z$\\hline\hline, late after line = \\hline ]{test.csv}{}{% \csvcoli&\csvcolii & \csvcoliii } \end{document}

Edit as per OP requirement

\documentclass{article}
\usepackage{csvsimple}

\begin{document}

\begin{filecontents}[overwrite]{test.csv} x,y,z 42,43,44 42,43,44 42,43,44 42,43,44 42,43,44 \end{filecontents}

\csvautotabular{test.csv} % \begin{tabular}{|l|l|l|}\hline% $x$ & $y$ & $z$ \\hline\hline \csvreader[ late after line = \\hline ] {test.csv}{}{% \csvcoli & \csvcolii & \csvcoliii }% \end{tabular} \end{document}

![enter image description here

js bibra
  • 21,280
  • Thank you for the answer! Would it be possible to retain the markup where \csvreader is only tasked with filling up the body of the table (i.e. I am free to configure the header to be as complex as I need)? – Big Monday Oct 16 '21 at 01:03
  • an example of usage would be helpful to decide-- in case the above answer met your requirement and removed the offset please consider accepting the answer and upvoting – js bibra Oct 16 '21 at 01:21
  • @BigMonday please see the edit above ---is this what you wanted – js bibra Oct 16 '21 at 12:42
  • 1
    Thank you again. Looks like its the % character before the newline in the last parameter to \csvreader that fixes the issue. – Big Monday Oct 16 '21 at 22:58
  • @BigMonday would you like to accept the answer – js bibra Oct 17 '21 at 00:23
  • Upon scrutinizing your answer, I posted my own, clarifying the original problem and preserving the initial markup as much as possible. Thank you once again for your help. – Big Monday Oct 17 '21 at 00:36
2

Here is a way with readarray. Make sure you use v3.1 2021-09-17.

\begin{filecontents*}[overwrite]{data.csv}
  x,y,z
  42,43,44
  42,43,44
  42,43,44
  42,43,44
  42,43,44
\end{filecontents*}

\documentclass[12pt,a4paper]{report} \usepackage{readarray} \def\firstrow{\hline} \renewcommand\typesetrowsepchar{\\firstrow\hline\gdef\firstrow{}} \renewcommand\typesetcolsepchar{&} \renewcommand\typesetcell[1]{$#1$}

\begin{document} \begin{table}[h] \centering \readarraysepchar{,} \readdef{data.csv}\tabdata \readarray*\tabdata\tabarray[-,\ncols] \begin{tabular}{|l|l|l|} \hline \typesetarray\tabarray \\hline \end{tabular} \end{table} \end{document}

enter image description here

If you eliminate the \hline from the definition of \typesetrowsepchar, one obtains

enter image description here

1

While @js bibra's answer technically solves the problem, it turns out that the extra spacing is generated due to the newline in the row declaration (i.e. the last parameter passed to \csvreader). Simply changing

\csvreader[
  head to column names,
  late after line=\tabularnewline\hline]
{data.csv}{}{
  \x  & \y  & \z
}

to

\csvreader[
  head to column names,
  late after line=\tabularnewline\hline]
{data.csv}{}{%
  \x  & \y  & \z
}

solves the problem without any further modifications.

fix

More info: What is the use of percent signs (%) at the end of lines?