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'm using tables with the tabular environment, with cells containing tikzpicture. I would like to center the contents of the cells both vertically and horizontally, and have little labels in the top left corner of each cell. As far as I've looked, the alignment problem is fixable with some tweaks, such as \setlength\extrarowheight{..} before the table, and [..] after line breaks inside the table. The \def\arraystretch{..} approach also works to give more padding, but in a way that I don't fully understand, so I'm not using that.

This is my current code of a table of graphs:

\documentclass{article}
\usepackage{array,tikz}

\tikzset{nnode/.style={circle,draw=black,inner sep=1.4pt,fill}}

\begin{document}

\setlength\extrarowheight{3pc}
\begin{tabular}{|>{\centering\arraybackslash}m{7pc}
                |>{\centering\arraybackslash}m{7pc}
                |>{\centering\arraybackslash}m{7pc}|}
\hline\begin{tikzpicture}
\node[nnode] (a) at (0,0){};
\node[nnode] (b) at (1,0){};
\draw (-.5,0) circle (.5);
\draw (1.5,0) circle (.5);
\draw (a) to (b);
\end{tikzpicture}
&
\begin{tikzpicture}
\node[nnode] (a) at (0,0){};
\node[nnode] (b) at (1,0){};
\draw (-.5,0) circle (.5);
\draw (1.5,0) circle (.5);
\draw (a) to (b);
\end{tikzpicture}
&
\begin{tikzpicture}
\node[nnode] (a) at (0,0){};
\node[nnode] (b) at (1,0){};
\draw (-.5,0) circle (.5);
\draw (1.5,0) circle (.5);
\draw (a) to (b);
\end{tikzpicture}
\\[2pc]\hline
\begin{tikzpicture}
\node[nnode] (a) at (0,0){};
\node[nnode] (b) at (1.5,0){}; 
\draw (.75,0) circle (.75);
\draw (a) to (b);
\end{tikzpicture}
&
\begin{tikzpicture}
\node[nnode] (a) at (0,0){}; 
\node[nnode] (b) at (1.5,0){}; 
\draw (.75,0) circle (.75);
\draw (a) to (b);
\end{tikzpicture}
&
\begin{tikzpicture}
\node[nnode] (a) at (0,0){}; 
\node[nnode] (b) at (1.5,0){}; 
\draw (.75,0) circle (.75);
\draw (a) to (b);
\end{tikzpicture}
\\[2pc]\hline
\end{tabular}

\end{document}

The result is this:

enter image description here

I have several issues with this.

  • Why are the elements of the last column displaced?

I think this might have to do with the [2pc] I have at the end of the rows, but I'm not sure. The general placement of the graphs isn't centered either, but I've found that alignment in tables is a common issue, and probably the horizontal and vertical alignment of a tikzpicture has been resolved somewhere (a link to such a place would be appreciated!), so I won't focus on that.

  • How could I put in labels in each top left corner of each cell?

I would want each of the labels to be individually editable, with the end result looking something like this (of course, with the graphs centered both vertically and horizontally):

enter image description here

I know that there are several other packages for making tables (and making tables with tikz seems too cumbersome), but I couldn't decide which one was suited best for this. I've thought of padding the cells instead of centering (a mix of both which I've used here), but again, no one approach solves all my queries. Any and all tips pertaining to these problems are appreciated!

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

Here is just a first attempt:

enter image description here

\documentclass{article}
\usepackage{array,tikz}% http://ctan.org/pkg/{array,tikz}

\tikzset{nnode/.style={circle,draw=black,inner sep=1.4pt,fill}}
\newcolumntype{M}{m{7.5pc}}
\newcommand{\mylabel}[3]{\hline #1 & & #2 & & #3 & \\ \cline{1-1} \cline{3-3} \cline{5-5}}
\newcommand{\emptyline}{\multicolumn{2}{|c|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{c|}{}}
\begin{document}

\noindent
\begin{tabular}{|*{3}{c|l|}}
  \mylabel{$G_1$}{$G_2$}{$G_3$}
  \emptyline \\[\dimexpr-2\normalbaselineskip+2.2pc]
  \multicolumn{2}{|M|}{\centering\begin{tikzpicture}
      \node[nnode] (a) at (0,0){};
      \node[nnode] (b) at (1,0){};
      \draw (-.5,0) circle (.5);
      \draw (1.5,0) circle (.5);
      \draw (a) to (b);
    \end{tikzpicture}}
  &
  \multicolumn{2}{M|}{\centering\begin{tikzpicture}
      \node[nnode] (a) at (0,0){};
      \node[nnode] (b) at (1,0){};
      \draw (-.5,0) circle (.5);
      \draw (1.5,0) circle (.5);
      \draw (a) to (b);
    \end{tikzpicture}}
  &
  \multicolumn{2}{M|}{\centering\begin{tikzpicture}
      \node[nnode] (a) at (0,0){};
      \node[nnode] (b) at (1,0){};
      \draw (-.5,0) circle (.5);
      \draw (1.5,0) circle (.5);
      \draw (a) to (b);
    \end{tikzpicture}}
  \\ \emptyline \\[\dimexpr-\normalbaselineskip+2pc]
  \mylabel{$G_4$}{$G_5$}{$G_6$}
  \emptyline \\
  \multicolumn{2}{|M|}{\centering\begin{tikzpicture}
      \node[nnode] (a) at (0,0){};
      \node[nnode] (b) at (1.5,0){}; 
      \draw (.75,0) circle (.75);
      \draw (a) to (b);
    \end{tikzpicture}}
  &
  \multicolumn{2}{M|}{\centering\begin{tikzpicture}
      \node[nnode] (a) at (0,0){}; 
      \node[nnode] (b) at (1.5,0){}; 
      \draw (.75,0) circle (.75);
      \draw (a) to (b);
    \end{tikzpicture}}
  &
  \multicolumn{2}{M|}{\centering\begin{tikzpicture}
      \node[nnode] (a) at (0,0){}; 
      \node[nnode] (b) at (1.5,0){}; 
      \draw (.75,0) circle (.75);
      \draw (a) to (b);
    \end{tikzpicture}}
  \\ \emptyline \\ \hline
\end{tabular}

\end{document}

I used a six-column tabular with the odd columns used for the tikzpicture labelling. Plus, some manual adjustment for vertical alignment.

share|improve this answer
    
Nice, I didn't know about \dimexpr, that fixes a lot. Your definitions works well in the example, but I have other tables like this, with different column and row numbers, not necessarily 3 and 2. I'm not too proficient at creating functions in LaTeX, but could those commands be generalized with loops in the definition like \foreach? I suppose in that case the labels would have to follow a certain order, but that's okay, it would just be numerical in the subscript of G, with an input first value. –  jlv Oct 17 '12 at 11:28
    
Seems this is the best I'll get! I create just a bunch of definitions, for tables of certain column numbers, and it works fine. Thanks again. –  jlv Oct 23 '12 at 1:38
add comment

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.