2

I have been trying to fix the column width of my table so that the text would automatically get warped to the next line and not shoot outside the page. After searching for a few days, being a total newbie to Latex, I came across the first answer for this question: Automatic line breaks in csvautobooklongtable Although it does solve the problem, I'm not able to get the separating lines to appear in the table. I tried using late after line=\\\hline for example but it is not working. I'm kinda going crazy here. Here is my current code:

\usepackage{array,booktabs,longtable,csvsimple,tabularx}
\makeatletter
\csvset{
  my column width/.style={after head=\csv@pretable\begin{longtable}{*{\csv@columncount}{p{#1}}}\csv@tablehead},
}
\makeatother
\begin{document}
\csvautobooklongtable[separator=comma, respect all,
  my column width=3cm, ]{NewTextDocument.csv}
\end{document}

If there is an alternative using \csvreader I'm also open to that. And just to be clear on that I did try this code also:

\csvreader[longtable=|c|c|c|l|,my column width=2cm,
  no head, column count=4,
  table head=\hline,
  late after line=\\\hline,
   late after last line=\\\hline]
  {NewTextDocument.csv}{}
{\csvlinetotablerow}

But my last column has a lot of text and is shooting outside the page instead of extending the row for another line. The same happens if I use tabular instead of longtable in the same code. PS: I am trying to avoid stuff like tabularx as it didn't compile at all when I tried it using this code:

\begin{tabularx}{\linewidth}{llXX}\toprule
\csvreader[no head,late after line=\\\midrule,late after last line=\\\bottomrule]
  {NewTextDocument.csv}

  {\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}
\end{tabularx}
1
  • 1
    Can you include a small subset of lines of NewTextDocument.csv so we can reproduce your issue? EDIT: you can modify it, but please test the issue still appears. Commented Aug 28, 2017 at 16:29

1 Answer 1

1
+100

There are two possibilities, which are both provided for in the following code:

\documentclass{article}
\usepackage{array,booktabs,longtable,csvsimple}

\usepackage{filecontents}

\begin{filecontents*}{NewTextDocument.csv}
name,job,age,profile
John,student,21,John has always been a very diligent student his marks always being among the best
Frederik,student,18,Frederik has not been a very diligent student
Johnson,professor,49,Johnson is just a professor ...
\end{filecontents*}

\makeatletter
\csvset{
 my first column width/.style={after head=\csv@pretable\begin{longtable}{*{\csv@columncount}{p{#1}}}\csv@tablehead},
 my second column width/.style={after head=\csv@pretable\begin{longtable}{|*{\csv@columncount}{p{#1}|}}\csv@tablehead},
}
\makeatother

\begin{document}

\csvautobooklongtable[
 separator=comma, 
 my first column width=3cm, 
 late after line={\\\midrule},
 late after last line={\end{longtable}}
]{NewTextDocument.csv}

\csvautobooklongtable[
 separator=comma, 
 my second column width=3cm, 
 table head={\hline\csvlinetotablerow\\\hline},
 late after line={\\\hline},
 late after last line={\\\hline\end{longtable}}
]{NewTextDocument.csv}

\end{document}

If you use the booktabs package, consider using \midrule instead of \hline. It is typically discouraged to use vertical rules in tables. But after all, it is your decision.

Using the above code, the tables will look like this:

enter image description here

However, I am not sure what you mean with text “shooting outside the page”. Maybe, you have text which is considerably longer than in my example … but without knowing about this, it is hard to come up with another solution.


Edit

To typeset bold and centered column headers, use the following entry in \csvautobooklongtable (this only works for four columns):

table head={\hline\multicolumn{1}{c}{\bfseries\csvcoli} & \multicolumn{1}{c}{\bfseries\csvcolii} & \multicolumn{1}{c}{\bfseries\csvcoliii} & \multicolumn{1}{c}{\bfseries\csvcoliv}\\\hline},
6
  • That worked wonderfully, thx a lot :) Yet still I have a question. Why is it typically discouraged to use vertical rules in tables? To clarify: The text in my table is actually a little bit shorter than yours. However what it did with the code I posted in my question (the one using csvreader) is that it wrote the whole content of the last column on one line. So the text kept going till the page "ended" and the rest of the text was simply lost (the last column did not end on the page so no table closing vertical line either). Commented Sep 1, 2017 at 9:39
  • Well, the code is not the problem. But from a typographic/design point of view, using as few rules as possible is less interrupting the eye movement. So, as the alignment of the table data already makes it clear which data belongs to the same column, vertical rules are not necessarily needed (see tex.stackexchange.com/questions/40542/…) But of course, it is up to you how to format your table. Technically, both is ok. :) Commented Sep 1, 2017 at 9:46
  • Well, honestly after seeing how my table looked like using both of the methods I have to say that I like the one with the horizontal lines only.. It does look better and doesn't tire the eyes. Without any lines though is kinda the other way around :P Thanks @Jasper Commented Sep 1, 2017 at 11:31
  • Hello @Jasper, once more I need your help. I've been trying for a few hours now to make a tiny little change to both of the variants you suggested but unfortunately had no luck so far. I want to make the text in a whole row, say the first one, bold and centered. Can you please tell me how I could achieve this? Commented Oct 7, 2017 at 16:48
  • I hope you got the poke :) Commented Oct 8, 2017 at 2:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .