3

I got a table table.txtcontaining:

 $nTracks$ | $PT(\Lambda_b)$ | $\eta(\Lambda_b)$ | $Dira(\Lambda_b)$ |
     0     |      0.039      |       0.045       |       0.017       |
     1     |      0.042      |       0.041       |       0.026       |
     2     |      0.048      |       0.027       |       0.021       |
     3     |      0.039      |       0.024       |       0.023       |
     4     |      0.022      |       0.041       |       0.041       |

with help from https://tex.stackexchange.com/a/643076/273534

I made a standard tabular like this:

Standard tabular

I'd like to add a vertical separator after the 1st column with below script:

\documentclass{article}
\usepackage{csvsimple-l3}
\usepackage{booktabs}

\begin{filecontents*}[overwrite]{table.txt}
 \textit{nTracks} | $PT(\Lambda_b)$ | $\eta(\Lambda_b)$ | \textit{Dira}$(\Lambda_b)$ |
     0     |      0.039      |       0.045       |       0.017       |
     1     |      0.042      |       0.041       |       0.026       |
     2     |      0.048      |       0.027       |       0.021       |
     3     |      0.039      |       0.024       |       0.023       |
     4     |      0.022      |       0.041       |       0.041       |
\end{filecontents*}

\begin{document}
    \begin{table}[h]
                \csvreader[
                  tabular=r|rrr,
                  separator=pipe,
                  no head,
                  before first line=\\\toprule,
                  late after first line=\\\midrule,
                  late after last line=\\\bottomrule
                ]{table.txt}{}{\csvcoli & \csvcolii &\csvcoliii & \csvcoliv}
    \end{table}
\end{document}

However, this went wrong that an extra vertical separator above toprule:

enter image description here

I read the official document on csvsimple-13 package only to be more confused.

How can I get a tabular with a vertical separator after the 1st column?

5
  • 1
    never use vertical rules with booktabs tables. the package makes vertical rules unusable by design, see its documentation. Commented May 27 at 11:56
  • your extra line at the top is presumably from before first line=\\\toprule as you have an \\ at the start. Commented May 27 at 11:58
  • Instead of using a \midrule, you could use two \cmidrule. This would give you a much nicer separation between rules than a vertical line. Commented May 27 at 11:58
  • Please add a short, but compilable test file which allows us to reproduce the problem. It should include a class, the necessary packages and a document environment. Commented May 27 at 11:58
  • Thanks for your timely help. I modified it to be compliable. Btw, I understand that the extra line comes from before first line=\\\toprule but don't know how to solve it. Simply remove the toprule, I got a two-horizontal-line table that looks good. But I still wonder if there is a better way to get exactly what I need.
    – RedQuark
    Commented May 27 at 12:13

2 Answers 2

2

With vertical lines it works when you use table head = \toprule instead of before first line. However, it does not look good with booktabs, so if you want a vertical line (which, contrary to some opinions, is perfectly valid) it is better to use a regular table with \hline.

\documentclass{article}
\usepackage{csvsimple-l3}
\usepackage{booktabs}

\begin{document}
    \begin{table}[h]
        \centering
        \csvreader[
          tabular=r|rrr,
          separator=pipe,
          no head,
          table head = \toprule,
          late after first line=\\\midrule,
          late after last line=\\\bottomrule
        ]{tableout.txt}{}{\csvcoli & \csvcolii & \csvcoliii &\csvcoliv}
        \caption{Table with booktabs}
    \end{table}
    
    \begin{table}[h]
    \centering
     \csvreader[
       tabular=r|rrr,
       separator=pipe,
       no head,
       table head = \hline,
       late after first line=\\\hline,
       late after last line=\\\hline
     ]{tableout.txt}{}{\csvcoli & \csvcolii & \csvcoliii &\csvcoliv}
     \caption{Regular table}
    \end{table}
    
\end{document}

enter image description here


If you like the increased line width of \toprule and \bottomrule then you can use them combined with a value of 0 for the rulesep and a bit of array stretch, as in https://tex.stackexchange.com/a/399741/:

\begin{table}[h]
        \aboverulesep=0ex
        \belowrulesep=0ex
        \renewcommand{\arraystretch}{1.1}
 
        \centering
        \csvreader[
          tabular=r|rrr,
          separator=pipe,
          no head,
          table head = \toprule,
          late after first line=\\\midrule,
          late after last line=\\\bottomrule
        ]{tableout.txt}{}{\csvcoli & \csvcolii & \csvcoliii &\csvcoliv}
        \caption{Booktabs with reduced line height}
    \end{table}

enter image description here

2
  • Exactly what I need! Thanks a lot. When dealing with this problem myself, I tried to set table head = \toprule but remove no head and this went wrong. It's interesting that this function can assign table head and no head at the same time.
    – RedQuark
    Commented May 27 at 12:40
  • @RedQuark no head just means that the header line does not come from the csv, i.e., that the first line is a data line. It doesn't mean that there is no header - indeed the idea is to provide your own header in the options because the csv does not have the information. In your case it is a bit of a hybrid situation, as there is a header in the csv but you treat it as a data row.
    – Marijn
    Commented May 27 at 12:46
3

Instead of a vertical line, you could use \cmidrules to separate the first column from the rest:

\documentclass{article}
\usepackage{csvsimple-l3}
\usepackage{booktabs}

\begin{filecontents*}[overwrite]{table.txt}
 \textit{nTracks} | $PT(\Lambda_b)$ | $\eta(\Lambda_b)$ | \textit{Dira}$(\Lambda_b)$ |
     0     |      0.039      |       0.045       |       0.017       |
     1     |      0.042      |       0.041       |       0.026       |
     2     |      0.048      |       0.027       |       0.021       |
     3     |      0.039      |       0.024       |       0.023       |
     4     |      0.022      |       0.041       |       0.041       |
\end{filecontents*}


\begin{document}
    \begin{table}[h]
                \csvreader[
                  tabular=rrrr,
                  separator=pipe,
                  no head,
                  before first line=\\\toprule,
                  late after first line=\\\cmidrule(r){1-1}\cmidrule(l){2-4},
                  late after last line=\\\bottomrule
                ]{table.txt}{}{\csvcoli & \csvcolii &\csvcoliii & \csvcoliv}
    \end{table}
\end{document}

enter image description here

2

You must log in to answer this question.

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