我在环境中使用表格tabular
,单元格包含tikzpicture
。我想将单元格的内容垂直和水平居中,并在每个单元格的左上角放置小标签。据我所知,对齐问题可以通过一些调整来解决,例如在\setlength\extrarowheight{..}
表格之前和[..]
表格内的换行符之后。这种\def\arraystretch{..}
方法也可以提供更多的填充,但我并不完全理解这种方法,所以我没有使用这种方法。
这是我当前的图表代码:
\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}
结果是这样的:
我对此有几个疑问。
- 为什么最后一列的元素发生了位移?
[2pc]
我认为这可能与行末尾的 I有关,但我不确定。图表的总体位置也不居中,但我发现表格中的对齐是一个常见问题,并且水平和垂直对齐可能tikzpicture
已在某个地方得到解决(如果有指向此类地方的链接,我将不胜感激!),因此我不会关注这一点。
- 我怎样才能在每个单元格的左上角放置标签?
我希望每个标签都可以单独编辑,最终结果看起来像这样(当然,图表在垂直和水平方向上都居中):
我知道还有其他几个用于制作表格的软件包(而且制作表格tikz
似乎太麻烦了),但我无法决定哪一个最适合。我曾考虑过填充单元格而不是居中(我在这里使用了两者的混合),但同样,没有一种方法可以解决我的所有疑问。任何与这些问题相关的提示都值得赞赏!
答案1
这只是第一次尝试:
\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}
我使用了六列tabular
,奇数列用于tikzpicture
标记。另外,还进行了一些手动调整以进行垂直对齐。