我实际上以为这个问题已经被问过了,但我找不到类似的东西。我想通过以下方式将行和列标题与表格内容分开:
+-------+-------+
| | |
| Col 1 | Col 2 |
| | |
+-------+-------+
+-------+ +-------+-------+
| | | | |
| Row 1 | | 1 | 2 |
| | | | |
+-------+ +-------+-------+
| | | | |
| Row 2 | | 3 | 4 |
| | | | |
+-------+ +-------+-------+
请注意,出于美观的原因,也为了清楚地表达我的意思,行标题和表格内容之间的空格刚好被插入。
我已经尝试使用类似hhline
包的东西并进行了一些实验,但我从未得到这样的结果。
编辑:我使用 Libre Office 创建了一个示例来说明我的意思。
答案1
根据要求,而且booktabs
版本。所请求的版本使用两个tabular
在底部对齐的 s [b]
。
\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}
您可以调整长度\rowlabelsep
并\collabelsep
改变间隙。
答案2
下面我使用我为未发布的包编写的代码(未发布意味着它不在 CTAN 上——并且可能永远不会在)。我只是复制了代码并以最少的努力使其与此 MWE 兼容,因此它可能不是您想要实现的东西的最佳实现。
我没有\MYcline
在这里提供关于什么是可能的、什么是不可能的文档。
此外,我个人认为这看起来不太好,并且不建议您排版这样的表格!
\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}
答案3
肮脏的伎俩:
\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}
正如 Skillmon 指出的那样,“Col 1”单元格和“ 3 ”单元格的左下角顶点之间存在一个小间隙,tikz matrix
您可以使用它来避开它:
\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}