# How to get the cellwidth when inside a tabularx environment

When I start a tabularx environment like this:

\begin{tabularx}{\textwidth}{lXX}


Can I, from inside a cell, get the width of the cell (to be used with includegraphics, minipage or something similar)?

Like this:

\begin{tabularx}{\textwidth}{lXX}
bla &
\includegraphics[width=\cellwidth]{...} &
\begin{minipage}{\cellwidth}...\end{minipage}
\end{tabularx}


The width of a X-column is saved in \TX@col@width. Define a user command and then you can access it in your table. But it is much easier to use \linewidth. Code:

\documentclass{article}

\usepackage{tabularx}

\makeatletter
\newcommand\mytxcellwidth{\TX@col@width}
\makeatother

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{l|X|X|}
\hline
bla & \rule{\linewidth}{1cm} & \\ \hline
bla & \rule{\mytxcellwidth}{1cm} &
\begin{minipage}{\mytxcellwidth}...\end{minipage}
\end{tabularx}

\end{document}

• usually it is easier to use \hsize or \linewidth – David Carlisle Nov 27 '16 at 17:38
• @DavidCarlisle - Thanks for the reminder. Indeed easier and I forgot that somehow. – Arash Esbati Nov 28 '16 at 7:55

You can normally use \linewidth (but the minipage doesn't make much sense here):

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tabularx,graphicx,lipsum}

\begin{document}
\noindent
\begin{tabularx}{\textwidth}{lXX}
bla &
\includegraphics[width=\linewidth]{example-image-a}
&
\begin{minipage}{\linewidth}\lipsum[1]\end{minipage}%
\end{tabularx}
\end{document}

• @gernot: Thanks. I will probably never learn it ... – Ulrike Fischer Nov 27 '16 at 16:30
• You can easily learn it: just switch to Linux, MacOS or any other operating system where case makes a difference in file names ;-) – gernot Nov 27 '16 at 16:32