4

Within the tabularx environment, X is a particular width which allows the table to fit the width of the specified horizontal space. I want to create a table which has square cells.

  • How can I set the height of a particular row within a table to be the same length as the value of X?
1
  • 2
    Do you really have to use for X here? If you want the cells to be square and you know the table width and how many columns you have you could save TeX an awful lot of work and just tell it the width (or not use a table at all and just make a row of square boxes Jul 20, 2012 at 9:35

1 Answer 1

6

If you can't, for some reason, follow David Carlisle's excellent advice, you can use a strut:

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|X|X|X|X|}
\hline
\rule{0.0pt}{\linewidth}
& \rule{0.0pt}{\linewidth}
& \rule{0.0pt}{\linewidth}
& \rule{0.0pt}{\linewidth}\\
\hline
\rule{0.0pt}{\linewidth}
& \rule{0.0pt}{\linewidth}
& \rule{0.0pt}{\linewidth}
& \rule{0.0pt}{\linewidth}\\
\hline
\end{tabularx}
\end{document}

with this result:

pdf output image

Note that this doesn't solve any problems you may encounter trying to position text in these boxes. TikZ would probably give you a better answer for that.


Alternatively, following Stephan Lemke's much better suggestion, you can use a parbox:

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|X|X|X|}
\hline
\parbox[b][\linewidth][t]{\linewidth}{Top Top Top Top Top Top Top Top}
& \parbox[b][\linewidth][c]{\linewidth}{Middle Middle Middle Middle }
& \parbox[b][\linewidth][b]{\linewidth}{Bottom Bottom Bottom Bottom }\\
\hline
\parbox[b][\linewidth][t]{\linewidth}{Top}
& \parbox[b][\linewidth][c]{\linewidth}{Middle}
& \parbox[b][\linewidth][b]{\linewidth}{Bottom}\\
\hline
\end{tabularx}
\end{document}

with this result:

image of pdf output

2
  • 1
    Well replacing the rules by parboxes (which allow to set the box height) would probably solve the text positioning issue. Jul 20, 2012 at 11:07
  • 1
    @StephanLehmke: Thanks for that suggestion; much better than my original. I've taken the liberty of incorporating it into an alternative solution. Jul 20, 2012 at 11:44

You must log in to answer this question.

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