![表格的精美标签](https://linux22.com/image/296132/%E8%A1%A8%E6%A0%BC%E7%9A%84%E7%B2%BE%E7%BE%8E%E6%A0%87%E7%AD%BE.png)
我正在尝试使用 绘制关系数据库表tabular
。我想在表格左上角为每个表格添加标签。我想到的两种解决方案都不是我想要的。
我想要一个具有以下属性的矩形标签:
- 边框宽度与表格宽度相同
- 左边框与表格左边框对齐
- 下边框与表格上边框重叠
- 宽度小于表格的列宽
在第一个解决方案中,我尝试Tikz
将标签绘制为矩形节点。我无法让标签的下边框与表格的上边框重叠。我也无法让左边框与表格的左边框对齐。
在第二个解决方案中,我使用了multicolumn
。这个非常接近,唯一剩下的问题就是宽度。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}
\begin{document}
\begin{tikzpicture}
\node (A) {
\begin{tabular}{|l|l|l|||l|}
\hline
$\texttt{o\_orderkey}$ & $\texttt{o\_orderdate}$ & $\texttt{o\_custkey}$ & rest \\
\hline
0 & 1992-01-01 & 100 & A \\
1 & 1992-01-15 & 3 & B \\
200 & 1993-01-01 & 26 & C \\
201 & 1993-01-15 & 42 & D \\
300 & 1994-01-01 & 88 & E \\
301 & 1994-01-01 & 74 & F \\
\hline
\end{tabular}
};
\node[draw, anchor=south west, inner sep=2pt] at (A.north west) {Table};
\end{tikzpicture}
\begin{tabular}{|l|l|l|||l|}
\cline{1-1} \multicolumn{1}{|l|}{Label} \\
\hline
$\texttt{o\_orderkey}$ & $\texttt{o\_orderdate}$ & $\texttt{o\_custkey}$ & rest \\
\hline
0 & 1992-01-01 & 100 & A \\
1 & 1992-01-15 & 3 & B \\
200 & 1993-01-01 & 26 & C \\
201 & 1993-01-15 & 42 & D \\
300 & 1994-01-01 & 88 & E \\
301 & 1994-01-01 & 74 & F \\
\hline
\end{tabular}
\end{document}
答案1
您可以将标签装箱并进行调整,以便底部规则与第一个规则重叠\hline
。
\documentclass[a4paper]{article}
\newcommand{\tablelabel}[1]{%
\multicolumn{1}{@{}l@{}}{%
\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{\arrayrulewidth}%
\hspace{-.5\arrayrulewidth}%
\fbox{\hspace{\tabcolsep}\strut#1\hspace{\tabcolsep}}%
}\\[-\arrayrulewidth]
}
\begin{document}
\begin{tabular}{|l|l|l|l|}
\tablelabel{Label}
\hline
\texttt{o\_orderkey} & \texttt{o\_orderdate} & \texttt{o\_custkey} & rest \\
\hline
0 & 1992-01-01 & 100 & A \\
1 & 1992-01-15 & 3 & B \\
200 & 1993-01-01 & 26 & C \\
201 & 1993-01-15 & 42 & D \\
300 & 1994-01-01 & 88 & E \\
301 & 1994-01-01 & 74 & F \\
\hline
\end{tabular}
\end{document}
请注意,您不需要$...$
around \texttt
。
您可能需要一个稍微不同的版本,允许指定要跨越的列数,以防标签比第一列宽。
\documentclass[a4paper]{article}
\usepackage{xparse}
\DeclareExpandableDocumentCommand{\tablelabel}{O{1}m}{%
\multicolumn{#1}{@{}l@{}}{%
\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{\arrayrulewidth}%
\hspace{-.5\arrayrulewidth}%
\fbox{\hspace{\tabcolsep}\strut#2\hspace{\tabcolsep}}%
}\\[-\arrayrulewidth]
}
\begin{document}
\begin{tabular}{|l|l|l|l|}
\tablelabel[4]{Label}
\hline
\texttt{o\_orderkey} & \texttt{o\_orderdate} & \texttt{o\_custkey} & rest \\
\hline
0 & 1992-01-01 & 100 & A \\
1 & 1992-01-15 & 3 & B \\
200 & 1993-01-01 & 26 & C \\
201 & 1993-01-15 & 42 & D \\
300 & 1994-01-01 & 88 & E \\
301 & 1994-01-01 & 74 & F \\
\hline
\end{tabular}
\end{document}
示例中的可选参数[4]
实际上并不是必要的,而只是用来显示语法。
如果您决定使用booktabs
并避免使用垂直规则(我建议),则需要进行简单的修改(没有一半的退格键\arrayrulewidth
)。
\documentclass[a4paper]{article}
\usepackage{booktabs,xparse}
\DeclareExpandableDocumentCommand{\tablelabel}{O{1}m}{%
\multicolumn{#1}{@{}l@{}}{%
\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{\arrayrulewidth}%
\fbox{\hspace{\tabcolsep}\strut#2\hspace{\tabcolsep}}%
}\\[-\arrayrulewidth]
}
\begin{document}
\begin{tabular}{llll}
\tablelabel[4]{Label}
\toprule
\texttt{o\_orderkey} & \texttt{o\_orderdate} & \texttt{o\_custkey} & rest \\
\midrule
0 & 1992-01-01 & 100 & A \\
1 & 1992-01-15 & 3 & B \\
200 & 1993-01-01 & 26 & C \\
201 & 1993-01-15 & 42 & D \\
300 & 1994-01-01 & 88 & E \\
301 & 1994-01-01 & 74 & F \\
\bottomrule
\end{tabular}
\end{document}
答案2
是這樣嗎?
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}
\begin{document}
\begin{tikzpicture}
\node[anchor=north west,inner sep=0pt,outer sep=0pt] (A) {
\begin{tabular}{|l|l|l|||l|}
\hline
$\texttt{o\_orderkey}$ & $\texttt{o\_orderdate}$ & $\texttt{o\_custkey}$ & rest \\
\hline
0 & 1992-01-01 & 100 & A \\
1 & 1992-01-15 & 3 & B \\
200 & 1993-01-01 & 26 & C \\
201 & 1993-01-15 & 42 & D \\
300 & 1994-01-01 & 88 & E \\
301 & 1994-01-01 & 74 & F \\
\hline
\end{tabular}
};
\node[draw, anchor=south west,text depth=0.5ex, inner ysep=2pt,inner xsep=\tabcolsep,outer sep=0pt] at ([yshift=-0.5\pgflinewidth]A.north west) {Table};
\end{tikzpicture}
\begin{tabular}{|l|l|l|l|||l|}
\cline{1-1} Label \\
\hline
\multicolumn{2}{|l|}{$\texttt{o\_orderkey}$} & $\texttt{o\_orderdate}$ & $\texttt{o\_custkey}$ & rest \\
\hline
\multicolumn{2}{|l|}{0} & 1992-01-01 & 100 & A \\
\multicolumn{2}{|l|}{1} & 1992-01-15 & 3 & B \\
\multicolumn{2}{|l|}{200} & 1993-01-01 & 26 & C \\
\multicolumn{2}{|l|}{201} & 1993-01-15 & 42 & D \\
\multicolumn{2}{|l|}{300} & 1994-01-01 & 88 & E \\
\multicolumn{2}{|l|}{301} & 1994-01-01 & 74 & F \\
\hline
\end{tabular}
\end{document}
答案3
一个非常简单的解决方案,使用caption
包:
\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{caption}
\DeclareCaptionFormat{mine}{\setlength\fboxsep{0pt}\hskip-0.5\arrayrulewidth\fbox{\space#3\space}}
\begin{document}
\begin{table}
\captionsetup{singlelinecheck = false, format = mine, skip =-\arrayrulewidth}
\caption{Label}
\begin{tabular}{|l|l|l|||l|}
\hline
\texttt{o\_orderkey} & \texttt{o\_orderdate} & \texttt{o\_custkey} & rest \\
\hline
0 & 1992-01-01 & 100 & A \\
1 & 1992-01-15 & 3 & B \\
200 & 1993-01-01 & 26 & C \\
201 & 1993-01-15 & 42 & D \\
300 & 1994-01-01 & 88 & E \\
301 & 1994-01-01 & 74 & F \\
\hline
\end{tabular}
\end{table}
\end{document}
答案4
这是一个{NiceTabular}
使用 的解决方案nicematrix
。
{tabular}
该环境与经典环境(包的)类似,array
但在单元格、行和列下创建 PGF/Tikz 节点。您可以将这些节点与 Tikz 一起使用来绘制您想要的任何规则。
\documentclass[a4paper]{article}
\usepackage{nicematrix,tikz}
\begin{document}
\begin{NiceTabular}{llll}
table \\
\hline
\Block[vlines]{*-*}{}
\verb|o_orderkey| & \verb|o_orderdate| & \verb|o_custkey| & rest \\
\hline
0 & 1992-01-01 & 100 & A \\
1 & 1992-01-15 & 3 & B \\
200 & 1993-01-01 & 26 & C \\
201 & 1993-01-15 & 42 & D \\
300 & 1994-01-01 & 88 & E \\
301 & 1994-01-01 & 74 & F \\
\hline
\CodeAfter
\tikz \draw (1-|1) -| ([xshift=\tabcolsep]1-1.east) |- (2-|1) -- cycle ;
\end{NiceTabular}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。