2

While trying to solve another issue of mine, I encountered the following issue:

In a tabularx cell there is unwanted space getting inserted at the bottom when using the following code:

\documentclass[border=1cm]{standalone}

\usepackage{tikz}
\usepackage{tabularx}

\renewcommand{\tabcolsep}{0mm}
\tikzset{inner sep=0mm}

\begin{document}
\begin{tabularx}{10cm}{|X|X|}
    \hline
    \rule{2cm}{2cm} & \rule{2cm}{2cm} \\
    \hline
    \tikz{\node[fill,minimum width=2cm,minimum height=2cm]{};} & \tikz{\node[fill,minimum width=2cm,minimum height=2cm]{};} \\
    \hline
    Also get it & with text \\
    \hline
\end{tabularx}
\end{document}

Picture:

picture

I suppose that space is for the stuff that belongs below the baseline of a text. Since I only want to put tikzpictures into the cells, how can I disable this behaviour?

Thanks in advance!

4
  • Why not just omit the unnecessary \hline directives?
    – Mico
    Jan 29, 2021 at 18:15
  • What do you mean by unnecessary? If I remove the \hlines the spacing is still there.
    – shilch
    Jan 29, 2021 at 18:17
  • I mean unnecessary as in, "all that's needed to parse the table visually can be achieved without the horizontal lines". For that matter, the vertical lines are needed either.
    – Mico
    Jan 29, 2021 at 18:19
  • The black boxes are just for demonstration purposes. It practice, the cells contain tikz pictures.
    – shilch
    Jan 29, 2021 at 18:21

2 Answers 2

2

The space is the depth of the strut.

Use baseline:

\documentclass[border=1cm]{standalone}

\usepackage{tikz}
\usepackage{tabularx}

\renewcommand{\tabcolsep}{0mm}
\tikzset{inner sep=0mm}

\begin{document}
\begin{tabularx}{10cm}{|X|X|}
    \hline
    \rule{2cm}{2cm} & \rule{2cm}{2cm} \\
    \hline
    \tikz[baseline]{\node[fill,minimum width=2cm,minimum height=2cm]{};} &
    \tikz[baseline]{\node[fill,minimum width=2cm,minimum height=2cm]{};} \\
    \hline
    Also get it & with text \\
    \hline
\end{tabularx}
\end{document}

enter image description here

1
  • That did the job. Thanks!
    – shilch
    Jan 29, 2021 at 18:39
1

There's absolutely no need in the tabular environment for either the vertical lines or the interior horizontal lines. I suggest you load the booktabs package and use its \toprule and \bottomrule macros to create well-spaced exterior, i.e., top and bottom, horizontal lines.

enter image description here

\documentclass{article}
\usepackage{tikz,tabularx,booktabs}
\renewcommand{\tabcolsep}{0mm}
\tikzset{inner sep=0mm}

\begin{document}
\begin{center}
\begin{tabularx}{10cm}{XX}
    \toprule
    \rule{2cm}{2cm} & 
    \rule{2cm}{2cm} \\
    %\hline
    \tikz{\node[fill,minimum width=2cm,minimum height=2cm]{};} &
    \tikz{\node[fill,minimum width=2cm,minimum height=2cm]{};} \\
    %\hline
    Also get it & with text \\
    \bottomrule
\end{tabularx}
\end{center}
\end{document}

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .