# How do I center my drawing and matrices horizontally and vertically inside a table in Latex?

I have some drawings and matrices in written in Latex and I want to write them in tabular form. The problem is I am unable to center them.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{angles, quotes}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

%PLEASE IGNORE IT. THIS IS FOR DRAWING
\newcommand{\AxisRotator}[1][rotate=0]{%
\tikz [x=0.25cm,y=0.60cm,line width=.2ex,-stealth,#1] \draw (0,0) arc (-150:150:1 and 1);%
}

%DRAWING IN LATEX
\def\x{\begin{tikzpicture}[x=0.5cm,y=0.5cm,z=0.3cm,>=stealth]
\draw[->] (xyz cs:x=0) -- (xyz cs:x=5) node[above] {$z$};
\draw[rotate=15][->] (xyz cs:x=0) -- (xyz cs:x=5) node[above] {$z1$};

\draw[->] (xyz cs:y=0) --(xyz cs:y=5) node[right] {$x$};

\draw[->] (xyz cs:z=0) --(xyz cs:z=-5) node[above] {$y$};
\draw[rotate=15][->] (xyz cs:z=0) --(xyz cs:z=-5) node[above] {$y1$};
\draw (0,0)  -- (0,3)  node [midway] {\AxisRotator[rotate=-90]};
\draw (1,3) node {$$\phi$$};
\end{tikzpicture}
}

\newpage

%MY TABLE STARTS FROM HERE
\begin{table}
\begin{center}

\caption{Euler Elemental Rotations}
\begin{tabular}{|p{2cm}|p{5cm}|p{6cm}|}
\hline
Axis & Euler Elemental Rotation & Rotation Matrix\\
\hline
x axis & \x & $$R_x(\phi) = \begin{bmatrix} 1 & 0 & 0\\ 0 &cos\phi& -sin\phi \\ 0 &sin\phi &cos\phi \end{bmatrix}$$ \\
\hline

\hline
\end{tabular}
\end{center}
\end{table}
\end{document}


How do I center everything in my table vertically and horizontally?

• Unrelated to the alignment of elements inside of table cells, but probably nevertheless something to consider: Using the center environment inside of a table environment adds additional white space, which you might want to avoid. To nevertheless horizontally center the table within the textwidth, you may want to remove the center environment and use \centering instead. For an improved spacing between caption and table, you may want to use the caption package. Jul 3, 2021 at 7:44

The p column type aligns cell contents vertically along the topmost common baseline across all rows. In the case of a tikzpicture object, though, the one and only "baseline" by default is placed at the bottom of the object. Hence, in your example, it looks like the three cells are actually aligned at the bottom, and not at either the top or middle.

I can think of two ways of achieving your formatting objective.

• You could use the m column type instead of p. E.g., replace

\begin{tabular}{|p{2cm}|p{5cm}|p{6cm}|}


with

\begin{tabular}{|m{2cm}|m{5cm}|m{6cm}|}


Note: In order to use the m column type, the array package must be loaded. Happily, that's is the case in your test document.

• You could keep the p column type but modify the setup of the tikzpicture environment, so that the object's notional baseline is placed at the center rather than at the bottom of the object. One way to do this is to add the option baseline=(current bounding box.center) to the optional argument of the tikzpicture environment. E.g., change

 \begin{tikzpicture}[x=0.5cm,y=0.5cm,z=0.3cm,>=stealth]


to

 \begin{tikzpicture}[x=0.5cm,y=0.5cm,z=0.3cm,>=stealth,
baseline=(current bounding box.center)]