Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to draw relational database tables with tabular. I would like to label each table with a label at the top left of the table. Neither solution that comes to my mind is exactly what I want.

I would like a rectangular label with the following properties:

  • Border width is same as that of table
  • Left border aligns with left border of table
  • Bottom border overlaps top border of table
  • Width is less than column width of table

In the first solution, I use Tikz and try to draw the label as a rectangular node. I can never quite get the bottom border of the label to overlap the top border of the table. Nor can I get the left border to align with that of the table.

In the second solution, I use multicolumn. This comes very close, the only problem remaining is the width.

\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}

Tables

share|improve this question
    
What do you mean with "Width is less than column width of table"? In your second example if you change the line with your multicolumn to ` \hline \multicolumn{4}{|l|}{Label} \\ ` you get a rectangle where the borders are the same as the borders of your table. –  Gilean0709 Aug 14 at 9:07
    
For instance, if in the second example, the right border of the label would come before the right border of the first column. Right now, the right border of the label aligns with the right border of the first column. –  jpcooper Aug 14 at 9:10
    
What's wrong with using booktabs and a simple Table that is left justified just as the first column: \documentclass{article} \usepackage{booktabs} \begin{document} \begin{tabular}{*5l} Table \\\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} ? –  Marc van Dongen Aug 14 at 9:29

3 Answers 3

up vote 5 down vote accepted

You can box the label and adjust it so that the bottom rule overlaps the first \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}

Note that you don't need $...$ around \texttt.

enter image description here

You may want a slightly different version that allows for specifying the number of columns to be spanned, in case the label is wider than the first column.

\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}

The [4] optional argument in the example is not really necessary, but is used just to show the syntax.

In case you decide to use booktabs and avoid vertical rules, which I recommend, a simple modification is needed (no backspace of half the \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}

enter image description here

share|improve this answer
    
Brilliant. This solves it. Thanks. –  jpcooper Aug 14 at 9:30
    
Update: there needs to be a % after the \setlength{\fboxrule}{\arrayrulewidth} line. Otherwise the horizontal alignment of the label is not correct. I could not rectify this as an edit, as the edit would be too short. –  jpcooper Aug 15 at 8:54
    
@jpcooper Thanks for noting, fixed. –  egreg Aug 15 at 9:20

Is it like this?

\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}

enter image description here

share|improve this answer
    
Thanks a lot. The second solution seems to solve it, but the first is more elegant. Are you sure that in the first solution, the border of the label has the same as the border width of the table? When zooming in, they still don't seem to overlap fully. –  jpcooper Aug 14 at 9:27
1  
@MarcvanDongen Fair enough. I have changed. :) –  Harish Kumar Aug 14 at 10:09

A very simple solution, using the caption package:

\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} 

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.