# Center tikz vertically inside tabular cell

The black tikz boxes/nodes are not vertically aligned inside the cells. Does anyone know how to do it? Thanks in advance, and please ignore irrelevant code, I know it's not very clean or straightforward!

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usepackage{multirow}
\usepackage{arydshln}

\begin{document}

text width=0.25\columnwidth, text depth=0mm, text centered, text height=0.30cm, rounded corners]
\begin{tikzpicture}[node distance = 1cm, auto]
\node [block] (init) {\textcolor{white}{\textbf{#1}}};
\end{tikzpicture}}

\begin{table}[h!]
\makebox[\linewidth]{
{\renewcommand{\arraystretch}{2}
\setlength{\arrayrulewidth}{0.5mm}
\setlength\tabcolsep{1mm}
\begin{tabular}{
p{0.22\linewidth} p{0\linewidth};{0.5mm/2pt} @{\linethickness{0.2mm}\framebox(10,10){}\hspace{3mm}}
p{0.23\linewidth} p{0\linewidth};{0.5mm/2pt} @{\linethickness{0.2mm}\framebox(10,10){}\hspace{3mm}}
p{0.23\linewidth} p{0\linewidth};{0.5mm/2pt} @{\linethickness{0.2mm}\framebox(10,10){}\hspace{3mm}}
p{0.24\linewidth} p{0\linewidth} @{\linethickness{0.2mm}\framebox(10,10){}\hspace{3mm}}}

AA (111) &&
Aaaa-\textit{Aaaa} (111)& \\\hline

Aaaaaaa (111) &&\hspace{1mm}
Aaaaaaa (111) &&\hspace{1mm}
AA aaaaa (111) &&\hspace{1mm}
Aaaa-\textit{aaa} (111)& \\
\end{tabular}
}
}
\end{table}
\end{document}

• Welcome! tabu cannot be recommended. The package author has promised backwards-incompatible changes and will not fix serious bugs in the meantime. This means that documents using it will break at some point and there will be no 'compatibility' option to support compilation. – cfr Aug 23 '15 at 0:58
• You can use the m{} alternative to the p{} supplied by the array package for centre vertical alignment. – cfr Aug 23 '15 at 1:02
• Thanks, but that will only align the text, the boxes remain in the same place. – user84854 Aug 23 '15 at 1:04

The array package provides a m{} column specifier which is just like p{} except that, whereas p{} puts the cell contents in a top-aligned \parbox[t]{}{}, \m{} puts it in a centre-aligned \parbox[c]{}{}. For example:

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{p{.25\linewidth}cm{.25\linewidth}}
this is a very long line of text in a top-aligned parbox & this is horizontally centred & this is a very long line of text in a centre-aligned parbox\\
\end{tabular}
\end{document}


Please do not rely on tabu. If you wish to use it even though it may break unexpectedly on you, fine. But understand that there are bugs which will be fixed only when the new version breaks your documents anyway.

# EDIT

In this case, you have complications caused by oversized content which isn't regular text. For the empty boxes, you can use \raisebox, I've used -2.5pt to move the box a bit down. Adjust as desired. For the TikZ pictures, you can adjust the picture relative to the baseline using baseline. Here, I've used baseline=(init.base). Again, adjust as desired. I've also updated the code to use the new syntax \tikzset{} rather than the deprecated \tikzstyle{}.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usepackage{multirow}
\usepackage{arydshln}

\begin{document}

\tikzset{
block/.style = {
rectangle,
fill=black,
text width=0.25\columnwidth,
text depth=0mm,
text centered,
text height=0.30cm,
rounded corners
}
}%
\begin{tikzpicture}[node distance = 1cm, auto, baseline=(init.base)]
\node [block] (init) {\textcolor{white}{\textbf{#1}}};
\end{tikzpicture}}

\newsavebox{\mybox}
\sbox\mybox{%
\raisebox{-2.5pt}{\linethickness{0.2mm}\framebox(10,10){}\hspace{3mm}}}

\begin{table}[h!]
\makebox[\linewidth]{
{\renewcommand{\arraystretch}{2}
\setlength{\arrayrulewidth}{0.5mm}
\setlength\tabcolsep{1mm}
\begin{tabular}{
m{0.22\linewidth} m{0\linewidth};{0.5mm/2pt} @{\usebox{\mybox}}
m{0.23\linewidth} m{0\linewidth};{0.5mm/2pt} @{\usebox{\mybox}}
m{0.23\linewidth} m{0\linewidth};{0.5mm/2pt} @{\usebox{\mybox}}
m{0.24\linewidth} m{0\linewidth} @{\usebox{\mybox}}}

AA (111) &&
Aaaa-\textit{Aaaa} (111)& \\\hline

Aaaaaaa (111) &&\hspace{1mm}
Aaaaaaa (111) &&\hspace{1mm}
AA aaaaa (111) &&\hspace{1mm}
Aaaa-\textit{aaa} (111)& \\
\end{tabular}
}
}
\end{table}
\end{document}


But the basic idea of using m{} and adjusting to the baseline is the same as in the simpler example above.

• Thanks, but the tikz boxes remain unaffected – user84854 Aug 23 '15 at 1:11
• @user84854 See edit. You can adjust them as you wish. I've set them to a particular value here but you can alter -2.5pt and or init.base as required. – cfr Aug 23 '15 at 1:30

Try to use:

\newcommand{\especialidade}[1]{\tikzstyle{block} = [rectangle, fill=black,
text width=0.2\columnwidth, text depth=0mm, text centered, text height=0.30cm, rounded corners]
\begin{tikzpicture}[baseline=(init.base)]
\node [block] (init) {\textcolor{white}{\textbf{#1}}};
\end{tikzpicture}}


instead of your newcomand.

Edit: The slightly modified your command (it gives the same result):

\newcommand{\especialidade}[1]{
\begin{tikzpicture}[baseline=(init.base),%this determine where is baseline
block/.style = {rectangle, fill=black, rounded corners,
text width=0.22\columnwidth, text depth=0mm, text height=0.30cm,
font=\bfseries, align=center}]
\node [block] (init) {\textcolor{white}{#1}};
\end{tikzpicture}}