9

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}
CarLaTeX
  • 62,716
Nathan
  • 461

2 Answers2

6

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
  • 49,614
Ulrike Fischer
  • 327,261
  • 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
5

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}
Arash Esbati
  • 7,416