2

In table, when I use \hline and \cline, line gets very close to the text which looks congested. For example, in following example, G is almost touching the line above it. This looks very congested. This does not happen with \toprule and \midrule. How can some space be given with \hline and \cline?

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\begin{table}[ht!]
    \begin{tabular}{
            >{\raggedright}p{0.2\textwidth}
            >{\raggedright}p{0.2\textwidth}
            >{\raggedright}p{0.2\textwidth}
            >{\raggedright}p{0.4\textwidth}
        }
        \toprule
        \textbf{Layer} & \textbf{Provider} & \textbf{Functionality} & \textbf{Uses}\tabularnewline
        \midrule
        A       &   B       &   C   &   D       \tabularnewline
                &           &       &   E       \tabularnewline
                &           &       &   F       \tabularnewline
        \hline
        G       &   H       &   I   &   J       \tabularnewline
                &           &       &   K       \tabularnewline
                \cline{2-4}
                &   L       &       &   M       \tabularnewline
                &           &       &           \tabularnewline
        \cline{2-4}
                &   N       &   O   &   P       \tabularnewline
        \cline{2-4}
                &   Q       &       &           \tabularnewline
        \hline
        R       &   S       &   T   &   U       \tabularnewline
        \cline{2-4}
                &   V       &   W   &   X       \tabularnewline
        \cline{2-4}
                &   Y       &   Z   &   1       \tabularnewline
        \hline
        2       &   3       &   4   &   5       \tabularnewline
        \hline      
    \end{tabular}
    \caption{India Stack APIs}
    \label{table:india_stack_1}
\end{table}     

\end{document}

enter image description here

3
  • 2
    If you insist on using \hline and \cline instead of \midrule (or \bottomrule) and \cmidrule, do take at look at the posting Inserting a small vertical space in a table and, in particular, this answer, which shows how to create and use (typographic) struts. (Shameless self-citation alert!)
    – Mico
    Commented Jan 2, 2021 at 7:53
  • Do you need line breaks in the cells?
    – egreg
    Commented Jan 2, 2021 at 14:02
  • \cmidrule saves my day. Thanks!
    – Catbuilts
    Commented May 17, 2023 at 4:53

3 Answers 3

3

You may want to replace \hline and \cline by \midrule and \cmidrule, respectively.

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\begin{table}[ht!]
    \begin{tabular}{
            >{\raggedright}p{0.2\textwidth}
            >{\raggedright}p{0.2\textwidth}
            >{\raggedright}p{0.2\textwidth}
            >{\raggedright}p{0.4\textwidth}
        }
        \toprule
        \textbf{Layer} & \textbf{Provider} & \textbf{Functionality} & \textbf{Uses}\tabularnewline
        \midrule
        A       &   B       &   C   &   D       \tabularnewline
                &           &       &   E       \tabularnewline
                &           &       &   F       \tabularnewline
        \midrule
        G       &   H       &   I   &   J       \tabularnewline
                &           &       &   K       \tabularnewline
                \cmidrule{2-4}
                &   L       &       &   M       \tabularnewline
                &           &       &           \tabularnewline
        \cmidrule{2-4}
                &   N       &   O   &   P       \tabularnewline
        \cmidrule{2-4}
                &   Q       &       &           \tabularnewline
        \midrule
        R       &   S       &   T   &   U       \tabularnewline
        \cmidrule{2-4}
                &   V       &   W   &   X       \tabularnewline
        \cmidrule{2-4}
                &   Y       &   Z   &   1       \tabularnewline
        \midrule
        2       &   3       &   4   &   5       \tabularnewline
        \midrule      
    \end{tabular}
    \caption{India Stack APIs}
    \label{table:india_stack_1}
\end{table}     
\end{document}

enter image description here

1
  • 1
    +1. One extra issue to fix in the OP's code: The total width of the tabular object isn't \textwidth but, instead, \textwidth+8\tabcolsep. Thus, do change the 3 instances of 0.2\textwidth to \dimexpr0.2\textwidth-2\tabcolsep\relax and the lone instance of 0.4\textwidth to \dimexpr0.4\textwidth-2\tabcolsep\relax.
    – Mico
    Commented Jan 2, 2021 at 7:19
1

I'd say that the main issue with your table isn't the (lack of adequate) spacing around some of the horizontal lines. Instead, it's the fact that the table's width currently exceeds \textwidth by a considerable bit: Because LaTeX inserts whitespace padding, in the amount of \tabcolsep on both edges of a column, the table's width is \textwidth+8\tabcolsep. Most document classes set the default value of \tabcolsep to 6pt; 8*6pt = 48pt = ca 2/3 of an inch, or ca 17 mm. Quite noticeable, wouldn't you say?

Rather than reduce the values of the usable column widths by hand (say, by 2\tabcolsep) in order to get the table to fit inside the text block, you could employ the tabularx package and use the eponymous tabularx environment and its X column type to typeset the tabular material. The code below shows how to preserve the desired relative width ratio of 2:1 between column 4 and the other 3 columns while using a tabularx-based setup.

Next, since you load the booktabs package anyway, by all means replace the remaining \hline and \cline instructions with \midrule, \bottomrule, and \cmidrule instructions. Actually, if this were my table, I'd use \addlinespace instead of \cmidrule -- whitespace can be every bit as effective at providing visual prominence as a (straight) line is.

Well, as noted in a comment below your query, there is a way of making the table look better even when using \cline and \hline, viz., by inserting typographic struts where needed. For the table at hand, though, and especially in view of the fact that you're employing the booktabs package anyway, I simply can't see a good reason for solving the spacing issue by inserting typographic struts.

enter image description here

\documentclass{article}

\usepackage{tabularx} % tabularx loads array automatically
\usepackage{ragged2e} % for '\RaggedRight' directive
\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}\hsize=#1\hsize}X} % permit hyphenation

\usepackage{booktabs}

\begin{document}

\begin{table}[ht!]
    \begin{tabularx}{\textwidth}{@{} *{3}{L{0.8}} L{1.6} @{} } % 3*0.8+1.6 = 4 = # of cols.
        \toprule
        \textbf{Layer} & \textbf{Provider} & \textbf{Functionality} & \textbf{Uses}\\
        \midrule
        A       &   B       &   C   &   D       \\
                &           &       &   E       \\
                &           &       &   F       \\
        \midrule
        G       &   H       &   I   &   J       \\
                &           &       &   K       \\
        \addlinespace  % \cmidrule(l){2-4}
                &   L       &       &   M       \\
                %%% &           &       &           \\  % why this all-blank line?
        \addlinespace  % \cmidrule(l){2-4}
                &   N       &   O   &   P       \\
        \addlinespace  % \cmidrule(l){2-4}
                &   Q       &       &           \\
        \midrule
        R       &   S       &   T   &   U       \\
        \addlinespace  % \cmidrule(l){2-4}
                &   V       &   W   &   X       \\
        \addlinespace  % \cmidrule(l){2-4}
                &   Y       &   Z   &   1       \\
        \midrule
        2       &   3       &   4   &   5       \\
        \bottomrule      
    \end{tabularx}
    \caption{India Stack APIs} \label{table:india_stack_1}
\end{table}     

\end{document}
0

Looking at the question I thought that while the MWE provided by the OP was appropriate for the subject of the question (space between the text and the horizontal lines), in practice the table will contain useful information, not only the alphabet, since there was already horizontal space allocated for the content of the columns.

I wondered how the answers provided will fare using real data. So I made a simulation assuming that the field Provider will hold some name and contact (3 lines), the Functionality will be a larger description, and so the Uses.

Now the table challenges us to show the information in various combinations of Providers, Funtionality and Uses.

I assumed that several providers might offer the same functionality and in turn a functionality might have several uses. This is not obvious from the question, but I think it's a sensible real life assumption and worthy of a test.

If such were the case then for the first layer it will be 1-1-3, in the second 1-1-2 and 3-1-2, and in the last two layers a simple 1-1-1.

I defined several \newcommand: \A, \B, \C, etc. to hold dummy text (words in English, taken from kantlipsum) to replace A, B, C ... in order to use he table as a template.

The first test was using Mico's answer. Here is the result, showing the first two layers. Using

\documentclass[10pt]{article}
\usepackage[left=1.5cm, right=1.5cm, top=2cm]{geometry}

mico

In the first layer there is an undesirable gap between Uses 1 and 2, due to the length of Functionality. The same happens in the second layer, and also within the third and fourth supplier.

The get around of this inconveniences I prepared a solution based in nicematrix that offer the command \Block. It is the only package needed to design the table.

The blocks allows the use of \\ inside, so it is possible to choose were to break the line (for example between the name and the email of a provider).

It can be seen that, even without using more horizontal lines (although \cline{2-4} could be an option in some places), now the visual grouping makes the association between providers, functionalities and uses easier to discern. nice3

\documentclass[10pt]{article}

\usepackage[left=1.5cm, right=1.5cm, top=2cm]{geometry}

\usepackage{nicematrix}

\newcolumntype{P}[1]{>{\raggedright\arraybackslash}m{#1\textwidth}}

\newcommand{\A}{Logic} %L1
\newcommand{\B}{1. The Ideal of \\ practical  reason  is a\\ representation} % P1
\newcommand{\C}{The manifold, it teaches us\\ nothing whatsoever regarding  the \\content of hypothetical principles} %F1
\newcommand{\D}{1. As is shown in the writings of Aristotle, the things in themselves are a representation of time} %U1

\newcommand{\E}{2. Natural causes  constitute the whole content for the paralogisms.} %U1
\newcommand{\F}{3. Our  experience is just as necessary as  philosophy} %U1
\newcommand{\G}{Philosophy} %L2
\newcommand{\Hi}{1. This expounds the\\ contradictory rules\\ of metaphysics} % P2

\newcommand{\I}{The never-ending regress in the\\ series of empirical conditions has\\ lying before it the  paralogisms\\ of human reason; with the sole \\exception of the architectonic of \\pure reason } %F2
\newcommand{\J}{1. The transcendental unity of apperception constitutes the whole content for the noumena}  %U2
\newcommand{\K}{2. The  transcendental aesthetic can not take account of the objects in space and time}  %U2
\newcommand{\Li}{2. Knowledge of the \\  Categories is a \\ posteriori} % P2

\newcommand{\M}{1. To explain that our ideas are \\ the clue to  the discovery of   our\\ understanding}  %U2
\newcommand{\N}{3. Our understanding stands in need of our judgements} % P2
\newcommand{\Oi}{To avoid all misapprehension,\\ it is necessary to explain\\ that the pure employment of the\\ architectonic of practical reason} %F2
\newcommand{\Pii}{2. It remains amystery why the\\ discipline of natural reason is a \\body of demonstrated science}  %U2

\newcommand{\Q}{4. The practical employment of the objects in space } % P2
\newcommand{\R}{Knowledge} %L3
\newcommand{\Si}{1. Antinomies exclude the possibility of natural causes} % P3
\newcommand{\T}{The reader should be careful to observe that the objects in space and time  are the mere results of the power of time} %F3

\newcommand{\U}{1. Irrespective of all empirical conditions, our concepts   are what first give rise to our experience} %U3
\newcommand{\V}{2. Because of our   necessary ignorance of the conditions}  % P3
\newcommand{\W}{The architectonic of natural reason is the key to understanding} %F3
\newcommand{\X}{1. Metaphysics excludes the possibility of the manifold}   %U3

\newcommand{\Y}{3. It is necessary to explain that our ideas}  % P3
\newcommand{\Z}{A representation of the never-ending regress in the series of empirical conditions} %F3

\newcommand{\Ai}{1. The paralogisms constitute a body of demonstrated doctrine} %U3
\newcommand{\Aii}{Science} %L4
\newcommand{\Aiii}{1. The Ideal proves the validity of the objects in space and time.} %P4
\newcommand{\Aiv}{Is it true that metaphysics can not take  account of the paralogisms of human reason} %F4
\newcommand{\Av}{1. The power of pure logic, a blind but indispensable function of the soul} %U4

\begin{document}

\begin{table}[ht!]
        
\begin{NiceTabular}{>{\bfseries}l P{0.2}  P{0.3}  P{0.3}}[% 
                cell-space-top-limit = 5pt,
                cell-space-bottom-limit = 6pt
                ]   
    \hline
    Layer               & \bfseries Provider & \bfseries Functionality & \centering\bfseries\arraybackslash Uses \\
    \hline
    \Block{3-1}{\A}     & \Block[l]{3-1}{\B} & \Block[l]{3-1}{\C}       &  \D   \\
                        &                    &                          &  \E   \\
                        &                    &                          &  \F   \\ 
    \hline
    \Block{5-1}{\G}     & \Block[l]{2-1}{\Hi}&  \Block[l]{2-1}{\I}      &  \J   \\
                        &                    &                          &  \K   \\[4em] % add vertical separation
                        & \Block[l]{}{\Li}   & \Block[l]{3-1}{\Oi}      &  \Block[l]{3-1}{\M \\ \\ \Pii}  \\ 
    %%%                 &                    &                          &   \\    
                        & \N                 &                          &       \\
                        & \Q                 &                          &       \\  
    \hline
    \Block{3-1}{\R}     & \Si                & \T                       & \U    \\
                        & \V                 & \W                       & \X    \\
                        & \Y                 & \Z                       & \Ai   \\
    \hline
                \Aii    &  \Aiii             & \Aiv                     & \Av   \\
    \hline
\end{NiceTabular}
\caption{India Stack APIs}
\label{table:india_stack_2}
\end{table} 

\end{document}

Indeed the other approaches can be improved! There are many tools available and my assumptions were outside of the question asked.

You must log in to answer this question.

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