2

How do I draw my table with the column lines only in the first row? Here's a MWE of my code:

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage[table]{xcolor}

\begin{document}
    \begin{table}[H]
        \centering
        \caption{Data of subways in Fortal city}
        \rowcolors{1}{gray!20}{white}
        \begin{tabular}{c|c|c|c}
            \hline
            Line    & Extension & Stations  & Vehicle type \\
            \hline
            South   & 24,1 km   & 19        & Electric \\
            West    & 19,5 km   & 10        & VLT a diesel \\
            Parangaba-Mucuripe  & 13,2 km   & 10    & VLT a diesel \\
            East    & 7,3 km    & 11        & VLT a diesel \\
            \hline
        \end{tabular}
    \end{table}
\end{document}

What I'd like to achieve:

table with column lines only in the first row

  • 2
    \multicolumn{1}{c|}{Line} ... – John Kormylo Mar 19 at 15:46
  • 2
    Related: tex.stackexchange.com/a/36803/156344 – user156344 Mar 19 at 15:48
  • 1
    @JohnKormylo Would you like to add an answer? – CarLaTeX Mar 20 at 6:09
  • @CarLaTeX I'll post my answer with the help of them. – Levy Mar 20 at 15:39
2

It's an easy trick using the multicol package. I removed the columns in my tabular agument but I drew them back, only in the header, using multicolumn{1}{c|}{text}

Here's the full working code:

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage[table]{xcolor}
\usepackage{multicol}

\begin{document}
    \begin{table}[H]
        \centering
        \caption{Data of subways in Fortal city}
        \rowcolors{1}{gray!20}{white}
        \begin{tabular}{cccc}
            \hline
            \multicolumn{1}{c|}{Line}    & \multicolumn{1}{c|}{Extension} & \multicolumn{1}{c|}{Stations}  & Vehicle type \\
            \hline
            South   & 24,1 km   & 19        & Electric \\
            West    & 19,5 km   & 10        & VLT a diesel \\
            Parangaba-Mucuripe  & 13,2 km   & 10    & VLT a diesel \\
            East    & 7,3 km    & 11        & VLT a diesel \\
            \hline
        \end{tabular}
    \end{table}
\end{document}

Notice that for this particular problem I don't need to draw a column c| in the last column. Otherwise it'd turned into a border, which I don't want to happen.

Furthermore, to avoid always writing multicolumn{1}{c|}{text} I defined a new command \topr{} as \newcommand{\topr}[1]{\multicolumn{1}{c|}{#1}} which spare me to write fixed values. So the improved code turned out like this:

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage[table]{xcolor}
\usepackage{multicol}
\newcommand{\topr}[1]{\multicolumn{1}{c|}{#1}}

\begin{document}
    \begin{table}[H]
        \centering
        \caption{Data of subways in Fortal city}
        \rowcolors{1}{gray!20}{white}
        \begin{tabular}{cccc}
            \hline
            \topr{Line}    & \topr{Extension} & \topr{Stations}  & Vehicle type \\
            \hline
            South   & 24,1 km   & 19        & Electric \\
            West    & 19,5 km   & 10        & VLT a diesel \\
            Parangaba-Mucuripe  & 13,2 km   & 10    & VLT a diesel \\
            East    & 7,3 km    & 11        & VLT a diesel \\
            \hline
        \end{tabular}
    \end{table}
\end{document}
1

calstable

Here is your table type setted using the package cals. As you will find, all rules are visible, and there are no white lines between rules and colours. However, I wold recommend typesetting a classic booktable tabular, without vertical rules, and without any gray shading, see below.

I have defined some shortcuts to reduce coding.

Compared to your original, I have removed side bearing (the left padding in first column and the right padding in the last column, which his customary for academic tables. In addition, I have right aligned the column with figures, but moved the figures to the centre of the column by 1.25em. I also moved the "km" to the column heading. If you prefer one line headings, just increase the column with by some pt.

\documentclass[12pt, a4paper]{article}
\usepackage{cals, url}
\usepackage{xcolor, caption}

\newcommand{\rl}{\hspace*{1.25em}}

\begin{document}

\begin{table*}
\caption{Data of subways in Fortal city}
%\footnotesize % Not necessary with too tiny font. Eventually, use `small` if you prefer smaller font in the tables. 
\small\sffamily
\begin{calstable}[c]
% Defining columns relative to each other and relative to the margins
\colwidths{{\dimexpr(\columnwidth)/3\relax}
            {\dimexpr(\columnwidth)/6\relax}
            {\dimexpr(\columnwidth)/6\relax}
            {\dimexpr(\columnwidth)/4\relax}
            }
% The tabular fills the text area if the divisors for all columns were 4

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.8pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0pt}
\def\cals@bodyrs@width{0.6pt}
\def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}
\def\cals@bgcolor{}

\def\lb{\ifx\cals@borderL\relax     % Left border switch (off-on)
    \def\cals@borderL{0pt}
\else \let\cals@borderL\relax\fi}

\def\rb{\ifx\cals@borderR\relax     % Right border switch (off-on)
    \def\cals@borderR{0pt}
\else \let\cals@borderR\relax\fi}

\def\bb{\ifx\cals@borderB\relax     % Bottom border switch (off-on)
    \def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}

\def\lp{\ifdim\cals@paddingL=0.0pt\relax    % Left padding switch (off-on)
    \cals@setpadding{Ag}
\else \setlength{\cals@paddingL}{0pt}\fi}

\def\rp{\ifdim\cals@paddingR=0.0pt\relax    % Right padding switch (off-on)
    \cals@setpadding{Ag}
\else \setlength{\cals@paddingR}{0pt}\fi}

\def\gray{\ifx\cals@bgcolor\empty     % "Switch" to turn on and off colour
    \def\cals@bgcolor{gray!20}
\else \def\cals@bgcolor{} \fi}


% R1H1
\thead{\bfseries%
\brow
     \gray\lp\alignL\cell{\vfil Line}\lp
    \alignC\cell{\vfil Extension (km)}
    \alignC\cell{\vfil Stations}
    \rp\alignR\cell{\vfil Vehicle type}\rp \gray
\erow
\mdseries%
}
\tfoot{\lastrule\strut}
%R2H1
\brow
   \lb\lp\bb\alignL\cell{South}\lp
    \alignR\cell{24,1\rl}
    \alignR\cell{19\rl}
    \rp\alignR\cell{Electric}\rp
\erow
%R3H2
\brow
    \gray\lp\alignL\cell{West}\lp
    \alignR\cell{19,5\rl}
    \alignR\cell{10\rl}
    \rp\alignR\cell{VLT a diesel}\rp\gray
\erow
%R4B3
\brow
    \lp\alignL\cell{Parangaba-Mucuripe}\lp
    \alignR\cell{13,2\rl}
    \alignR\cell{10\rl}
    \rp\alignR\cell{VLT a diesel}\rp\bb
\erow
%R7N1
\brow
    \gray\lp\cell{ East}\lp
    \alignR\cell{7,3\rl}
    \alignR\cell{11\rl}
    \rp\alignR\cell{VLT a diesel}\gray\rp
\erow
\makeatletter
\end{calstable}\par
\end{table*}

\end{document}

enter image description here

Classic booktable style tabular

Here I have modified @Levy's code, but made several changes:

  1. Using tabulary for best fitting column with
  2. Used booktabs for correctly spaced rules
  3. Removed side bearing (@{})
  4. Gray shading removed
  5. Columns and headers similar alignment
  6. Small caps header

\documentclass[12pt,a4paper]{article}
\usepackage{booktabs, caption, tabulary}

\begin{document}
    \begin{table}[!tb]
        \small\sffamily\centering
        \caption{Data of subways in Fortal city}
        \begin{tabulary}{\linewidth}{@{}LRRC@{}}
            \toprule
            \textsc{line}    & \textsc{extension\linebreak(km)} & \textsc{stations}  & \textsc{vehicle type}\\
            \midrule
            South   & 24,1   & 19        & Electric \\
            West    & 19,5  & 10        & VLT a diesel \\
            Parangaba-Mucuripe  & 13,2 & 10    & VLT a diesel \\
            East    & 7,3   & 11        & VLT a diesel \\
            \bottomrule
        \end{tabulary}
    \end{table}
\end{document}

enter image description here

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.