Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?
share|improve this question
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 –  David Carlisle Jul 20 '12 at 9:35
add comment

1 Answer

up vote 5 down vote accepted

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

share|improve this answer
1  
Well replacing the rules by parboxes (which allow to set the box height) would probably solve the text positioning issue. –  Stephan Lehmke Jul 20 '12 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. –  Brent.Longborough Jul 20 '12 at 11:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.