0

I actually thought that this question has already been asked but I could not find anything similar to it. I would like to separate the row and column caption from the table content in the following way:

          +-------+-------+
          |       |       |
          | Col 1 | Col 2 |
          |       |       |
          +-------+-------+
+-------+ +-------+-------+
|       | |       |       |
| Row 1 | |   1   |   2   |
|       | |       |       |
+-------+ +-------+-------+
|       | |       |       |
| Row 2 | |   3   |   4   |
|       | |       |       |
+-------+ +-------+-------+

Note that the space between the row caption and the table content has just been inserted for aesthetical reasons and to make clear what I mean.

I already tried using something like the hhline package and experimented with it a bit, but I never came up with a result like this.


Edit: I have created an example with Libre Office in order to show what I mean.

enter image description here

  • So do you want white space there or not (asking for both the vertical and the horizontal one)? – Skillmon Oct 29 '18 at 14:48
  • Yes, I would like to have a little portion of white space between the captions and the content. – Nemgathos Oct 29 '18 at 14:49
  • If it is thin enough, yes. Take a look at other examples all over the place where, for example, the column captions are separated from the content by \hline\hline. – Nemgathos Oct 29 '18 at 14:58
  • Did any of the answers help you? Is there anything left which isn't solved? If the latter, please explain what's still missing/wrong. If the former, how about giving some feedback? – Skillmon Oct 30 '18 at 18:23
3

As requested, but also a booktabs version. The requested version uses two tabulars that are aligned at the [b]ottom.

enter image description here

\documentclass{article}

\newlength{\rowlabelsep}
\newlength{\collabelsep}

\setlength{\rowlabelsep}{5pt}
\setlength{\collabelsep}{5pt}

\usepackage{booktabs,array}

\begin{document}

\begin{tabular}[b]{ | c | }
  \hline
  \bfseries Row 1 \\
  \hline
  \bfseries Row 2 \\
  \hline
\end{tabular}%
\hspace{\rowlabelsep}%
\begin{tabular}[b]{ | c | c | }
  \hline
  \bfseries Col 1 & \bfseries Col 2 \\
  \hline
  \multicolumn{2}{c}{} \\[\dimexpr-\normalbaselineskip-.4pt+\collabelsep]
  \hline
  1 & 2 \\
  \hline
  3 & 4 \\
  \hline
\end{tabular}

\bigskip

\begin{tabular}{ l c c }
  \toprule
  & \bfseries Col 1 & \bfseries Col 2 \\
  \midrule
  \bfseries Row 1 & 1 & 2 \\
  \bfseries Row 2 & 3 & 4 \\
  \bottomrule
\end{tabular}

\end{document}

You can adjust the lengths \rowlabelsep and \collabelsep to change the gaps.

  • Great idea the two tabulars :) – CarLaTeX Oct 31 '18 at 6:28
  • Really a good idea, but one that only works for ordinary sized contents. – Skillmon Oct 31 '18 at 6:47
2

In the following I use code that I wrote for an unpublished package (unpublished meaning it is not on CTAN -- and might never be). I just copied the code and made it compatible with this MWE with the least possible effort, so it might not be an optimal implementation for the stuff you want to achieve.

I don't include a documentation of what is possible with \MYcline and what isn't here.

Also I personally think this doesn't look good and won't advise you to typeset a table like this!

\documentclass[border=2mm]{standalone}

\usepackage{xparse}
\usepackage[table]{xcolor}

%% MYcline >>>
%%=============================================================================
\ExplSyntaxOn
\makeatletter
\int_new:N \l_MYtab_tmpa_int
\int_new:N \l_MYtab_tmpb_int
\tl_new:N \l__MYtab_cline_output_tl
\cs_new:Nn \MYtab_cline_output_single:nNnnnn% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    % #4 right skip
    % #5 start column
    % #6 end column
    \tl_put_right:Nn \l__MYtab_cline_output_tl
      {
        \@multicnt#5
        \advance\@multispan\m@ne
        \ifnum\@multicnt=\@ne
          \@firstofone{&\omit}
        \fi
        \@multicnt#6
        \advance\@multicnt -#5
        \advance\@multispan\@ne
        \group_begin:
          \color{#1}\rule{#3}\arrayrulewidth
        \group_end:
        \group_begin:
          \bool_if:NTF #2 { \color { #1 } } { \CT@arc@ }
          \leaders\hrule\@height\arrayrulewidth\hfill
        \group_end:
        \group_begin:
          \color{#1}\rule{#4}\arrayrulewidth
        \group_end:
        \cr
        \noalign{\vskip-\arrayrulewidth}
      }
  }% <<<
\cs_generate_variant:Nn \MYtab_cline_output_single:nNnnnn { xNxxxx }
\cs_new:Nn \MYtab_cline_output_strip:nNnnnn% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    % #4 right skip
    % #5 start column
    % #6 end column
    \MYtab_cline_output_single:xNxxxx
      { \tl_trim_spaces:n { #1 } }
      #2
      { \tl_trim_spaces:n { #3 } }
      { \tl_trim_spaces:n { #4 } }
      { \tl_trim_spaces:n { #5 } }
      { \tl_trim_spaces:n { #6 } }
  }% <<<
\cs_new:Npn \MYtab_cline_split_columns:nNnnw #1 #2 #3 #4 #5 - #6 \q_stop% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    % #4 right skip
    % #5 start column
    % #6 end column
    \MYtab_cline_output_strip:nNnnnn { #1 } #2 { #3 } { #4 } { #5 } { #6 }
  }% <<<
\cs_new:Npn \MYtab_cline_get_columns:nNnnw #1 #2 #3 #4 #5 \q_stop% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    % #4 right skip
    % #5 columns
    %\tl_analysis_show:n { #5 }
    \tl_if_in:nnTF { #5 } { , }
      { \MYtab_cline_column_group:nNnnn { #1 } #2 { #3 } { #4 } { #5 } }
      {
        \tl_if_in:nnTF { #5 } { - }
          {
            \MYtab_cline_split_columns:nNnnw { #1 } #2 { #3 } { #4 } #5 \q_stop
          }
          {
            \MYtab_cline_output_strip:nNnnnn { #1 } #2 { #3 } { #4 } { #5 }
            { #5 }
          }
      }
  }% <<<
\cs_new:Nn \MYtab_cline_column_group:nNnnn% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    % #4 right skip
    % #5 column group
    %\tl_analysis_show:n { #5 }
    %\tl_analysis_show:n { #6 }
    \int_set:Nn \l_MYtab_tmpb_int { \clist_count:n { #5 } }
    \clist_map_inline:nn { #5 }
      {
        \int_decr:N \l_MYtab_tmpb_int
        \MYtab_cline_get_columns:nNnnw { #1 } #2 { #3 } { #4 } ##1 \q_stop
        \int_compare:nNnT \l_MYtab_tmpb_int > 0
          { \tl_put_right:Nn \l__MYtab_cline_output_tl { \omit } }
      }
  }% <<<
\cs_new:Nn \MYtab_cline_peek_column_group:nNnn% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    % #4 right skip
    \peek_meaning_ignore_spaces:NTF \c_group_begin_token
      { \MYtab_cline_column_group:nNnnw { #1 } #2 { #3 } { #4 } }
      { \MYtab_cline_get_columns:nNnnw { #1 } #2 { #3 } { #4 } }
  }% <<<
\cs_new:Npn \MYtab_cline_get_right_skip:nNnw #1 #2 #3 [ #4 ]% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    % #4 right skip
    %\MYtab_cline_peek_column_group:nNnn { #1 } #2 { #3 } { #4 }
    \MYtab_cline_get_columns:nNnnw { #1 } #2 { #3 } { #4 }
  }% <<<
\cs_new:Nn \MYtab_cline_peek_right_skip:nNn% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    \peek_meaning_ignore_spaces:NTF [%]
      { \MYtab_cline_get_right_skip:nNnw { #1 } #2 { #3 } }
      { \MYtab_cline_get_columns:nNnnw { #1 } #2 { #3 } { .5\tabcolsep } }
  }% <<<
\cs_new:Nn \MYtab_cline_peek_right:nNn% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    \peek_meaning_remove_ignore_spaces:NTF >
      { \MYtab_cline_peek_right_skip:nNn { #1 } #2 { #3 } }
      { \MYtab_cline_get_columns:nNnnw { #1 } #2 { #3 } { 0pt } }
  }% <<<
\cs_new:Npn \MYtab_cline_get_left_skip:nNw #1 #2 [ #3 ]% >>>
  {
    % #1 fill color
    % #2 if star
    % #3 left skip
    \MYtab_cline_peek_right:nNn { #1 } #2 { #3 }
  }% <<<
\cs_new:Nn \MYtab_cline_peek_left_skip:nN% >>>
  {
    % #1 fill color
    % #2 if star
    \peek_meaning_ignore_spaces:NTF [%]
      { \MYtab_cline_get_left_skip:nNw { #1 } #2 }
      { \MYtab_cline_peek_right:nNn { #1 } #2 { .5\tabcolsep } }
  }% <<<
\cs_new:Nn \MYtab_cline_peek_left:nN% >>>
  {
    % #1 fill color
    % #2 if star
    \peek_meaning_remove_ignore_spaces:NTF <
      { \MYtab_cline_peek_left_skip:nN { #1 } #2 }
      { \MYtab_cline_peek_right:nNn { #1 } #2 { 0pt } }
  }% <<<
\cs_new:Npn \MYtab_cline_individual_color:Nw #1 [ #2 ]% >>>
  {
    % #1 if star
    % #2 fill color
    \MYtab_cline_peek_left:nN { #2 } #1
  }% <<<
\cs_new:Nn \MYtab_cline_peek_color:nN% >>>
  {
    % #1 fill color
    % #2 if star
    \peek_meaning_ignore_spaces:NTF [%]
      { \MYtab_cline_individual_color:Nw #2 }
      { \MYtab_cline_peek_left:nN { #1 } #2 }
  }% <<<
\cs_new:Nn \MYtab_cline_peek_star:n% >>>
  {
    % #1 fill color
    \peek_meaning_remove_ignore_spaces:NTF *
      { \MYtab_cline_peek_color:nN { #1 } \c_true_bool }
      { \MYtab_cline_peek_color:nN { #1 } \c_false_bool }
  }% <<<
\cs_new:Nn \MYtab_cline_arg_parser:nn% >>>
  {
    % #1 fill color
    \int_set:Nn \l_MYtab_tmpa_int { \clist_count:n { #2 } }
    \clist_map_inline:nn { #2 }
      {
        \int_decr:N \l_MYtab_tmpa_int
        \MYtab_cline_peek_star:n { #1 } ##1 \q_stop
        \int_compare:nNnT \l_MYtab_tmpa_int > 0
          { \tl_put_right:Nn \l__MYtab_cline_output_tl { \omit } }
      }
  }% <<<
\NewExpandableDocumentCommand \clineReveal { }% >>>
  {
    \noalign { \vskip \arrayrulewidth }
  }% <<<
\NewExpandableDocumentCommand \MYcline { }% >>>
  {
    \omit
    \MYtab@cline
  }% <<<
\NewDocumentCommand \MYtab@cline { O{white} m }% >>>
  {
    \tl_clear:N \l__MYtab_cline_output_tl
    \MYtab_cline_arg_parser:nn { #1 } { #2 }
    \l__MYtab_cline_output_tl
    \clineReveal
  }% <<<
\makeatother
\ExplSyntaxOff
%%==========================================================================<<<

\newcommand\MyLine
  {%
    \MYcline{>[\dimexpr\doublerulesep+\arrayrulewidth]1,<[-\arrayrulewidth]2-3}%
  }

\begin{document}
\begin{tabular}{|l||l|l|}
  \MYcline{<[-\arrayrulewidth]2-3}
  \multicolumn{1}{c|}{}& Col 1 & Col 2\\
  \MYcline{<[-\arrayrulewidth]2-3}
  \noalign{\vskip\doublerulesep}%
  \MyLine
  Row 1 & 1 & 2 \\
  \MyLine
  Row 2 & 3 & 4 \\
  \MyLine
\end{tabular}
\end{document}

enter image description here

  • I have created an example with Libre Office. – Nemgathos Oct 29 '18 at 15:24
2

Dirty trick:

\documentclass{article}
\usepackage{array}
\renewcommand{\arraystretch}{1.4}
\newcolumntype{C}{>{\centering\arraybackslash}p{5em}}
\newcommand{\mystr}{\rule{0pt}{3ex}}
\begin{document}
\begin{tabular}{|C|@{}p{5pt}@{}|C|C|}
  \cline{3-4}
  \multicolumn{1}{c}{} && \mystr\textbf{Col 1} & \textbf{Col 2}\\
  \cline{3-4}
  \noalign{\vskip5pt}
  \cline{1-1}\cline{3-4}
  \mystr\textbf{Row 1} && 1 & 2 \\
  \cline{1-1}\cline{3-4}
  \mystr\textbf{Row 2} && 3 & 4\\
  \cline{1-1}\cline{3-4}
\end{tabular}
\end{document}

enter image description here

As Skillmon pointed out, there is a small gap at the lower left vertices of the "Col 1" cell and the " 3 " cell, with a tikz matrix you can avoid it:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[
    matrix of nodes,
    column sep=-\pgflinewidth,
    row sep=-\pgflinewidth,
    nodes={text width=5em, draw, minimum height=4ex, text centered},
    row 1/.style={nodes={font=\bfseries}},
    column 1/.style={nodes={font=\bfseries}}
    ]{
    &[5pt] Col 1 & Col 2\\[5pt]
    Row 1 & 1 & 2\\
    Row 2 & 3 & 4\\
    };
\end{tikzpicture}
\end{document}

enter image description here

  • Why don't you use the \multicolumn only in the first row and change table definition? – Skillmon Oct 29 '18 at 16:15
  • @Skillmon I tried but I didn't manage to separate the horizontal line – CarLaTeX Oct 29 '18 at 16:17
  • I just tried and it does work if you use |C|@{}p{5pt}@{}|C|C| as your preamble. Then you can use \multicolumn{1}{c}{} in the first row to suppress the vertical rules for the first column. The rest doesn't need \multicolumn then (using \noalign{\vskip5pt} for the vertical gap). But you get small gaps at the lower left vertices of the "Col 1" cell and the " 3 " cell (and I don't know how to fix this without some code, e.g. with what I did in my answer). – Skillmon Oct 30 '18 at 8:33
  • @Skillmon Modified as you indicated, thanks! – CarLaTeX Oct 31 '18 at 6:05

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.