0

I'm trying to do a vertical centering in a table, but centered on a specific component. I'm hoping to do this in a general way because I generate the LaTeX algorithmically.

Here's a MWE:

\documentclass[a4paper, 10pt]{scrartcl}

\usepackage{soul}
\usepackage[cm]{fullpage}
\usepackage{array}
\usepackage{tikz}


\begin{document}
\thispagestyle{empty}
\begin{center}

% Table for row 1, which has 2 columns.
%\begin{table}[h]
\begin{tabular}{>{\centering\arraybackslash}m{7cm}l>{\centering\arraybackslash}m{4cm}}
% SHAPE: rect
\parbox[c][4.4cm][c]{7cm}{\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 1 *******
\draw (0cm,0cm) -- (0cm,4cm) -- (7cm,4cm) -- (7cm,0cm) -- cycle;
\end{tikzpicture}
\center first}
 & \hspace{1cm} & % SHAPE: rect
\parbox[c][3.4cm][c]{4cm}{\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 2 *******
\draw (0cm,0cm) -- (0cm,3cm) -- (4cm,3cm) -- (4cm,0cm) -- cycle;
\end{tikzpicture}
\center second}
\\
\end{tabular}


\end{center}
\end{document}

which generates this:

output

The intention of this was to have it centered vertically (which doesn't look like it's worked perfectly I assume because of a bounding box issue).

However, what I really want is to have the tikz boxes aligned vertically on their centers, but with the text still hanging directly below the box as it is in the diagram above. To be clear, I want the boxes aligned by their vertical middles like this (the dotted line is just to guide the eye):

Adjusted boxes

1 Answer 1

2

Like this:

\documentclass[a4paper, 10pt]{scrartcl}
\usepackage{soul}
\usepackage[cm]{fullpage}
\usepackage{array}
\usepackage{tikz}

\begin{document}
\thispagestyle{empty}
\begin{center}
% Table for row 1, which has 2 columns.
\begin{tabular}{>{\centering\arraybackslash}m{7cm}l>{\centering\arraybackslash}m{4cm}}
% SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 1 *******
\draw (0,0) rectangle (7,4);
\draw [red,dotted] (0,2) -- (7,2);  
\end{tikzpicture}\begin{center}
first
\end{center}
 & \hspace{1cm} & % SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 2 *******
\draw (0,0) rectangle (4,3);
\draw [red,dotted] (0,1.5) -- (4,1.5); 
\end{tikzpicture}\begin{center}
second
\end{center}\\
\end{tabular}
\end{center}
\end{document}

enter image description here

Update:

I should mention this before. You actually don't need the >{\centering\arraybackslash} before the m column specifier. The m column specifier by default will center each entry vertically and horizontally inside the cell proportion to the rest of the line. So if you only have the tikz pictures, they will always aligned like centered in the cell (both horizontally and vertically). However, the text under picture may cause problem. If the text took different lines, because the whole contents are centered vertically, then the picture will be off-set. In order to make the picture center aligned again, you need make the underneath text occupy same lines (e.g., make empty line). Here is an example:

\documentclass[a4paper, 10pt]{scrartcl}
\usepackage{soul}
\usepackage[cm]{fullpage}
\usepackage{array}
\usepackage{tikz}

\begin{document}
\thispagestyle{empty}
\begin{center}
% Table for row 1, which has 2 columns.
\begin{tabular}{|m{7cm}|l|m{4cm}|}
% SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 1 *******
\draw (0,0) rectangle (7,4);
\draw [red,dotted] (0,2) -- (7,2);  
\end{tikzpicture}\begin{center}
first
\end{center}
 & \hspace{1cm} & % SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 2 *******
\draw (0,0) rectangle (4,3);
\draw [red,dotted] (0,1.5) -- (4,1.5); 
\end{tikzpicture}\begin{center}
second picture have long text under the picture
\end{center}\\
\end{tabular}
\end{center}
The second example centered the picure again by putting a empty line below first.
\begin{center}
%Table for row 1, which has 2 columns.
\begin{tabular}{|m{7cm}|l|m{4cm}|}
% SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 1 *******
\draw (0,0) rectangle (7,4);
\draw [red,dotted] (0,2) -- (7,2);  
\end{tikzpicture}\begin{center}
first\\\null%put a manually line break and empty contents for the new line
\end{center}
 & \hspace{1cm} & % SHAPE: rect
\begin{tikzpicture}
% *** RECTANGLE FOR ROW 1, COLUMN 2 *******
\draw (0,0) rectangle (4,3);
\draw [red,dotted] (0,1.5) -- (4,1.5); 
\end{tikzpicture}\begin{center}
second picture have long text under the picture
\end{center}\\
\end{tabular}
\end{center}
\end{document}

Output:

enter image description here

2
  • This is great. Thank you. This works. But how does it work? Where do we tell it to center on the tikz? And thanks for cleaning up the ugly LaTeX that I had! Commented Feb 1, 2024 at 0:05
  • 1
    @DrXorile I edit the answer to add some explanations. Commented Feb 1, 2024 at 1:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.