What I know
I know that it is possible to set the width for specific columns when using the tabularx
environment. See minimal working example below. The preamble argument for the tabularx
environment shown below was retrieved from the documentation of the tabularx
package.
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tabularx}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{
|>{\hsize=.2\hsize\linewidth=\hsize}X
|>{\hsize=1.8\hsize\linewidth=\hsize}X|}
\hline
foo & foo \\ \hline
foo & foo \\ \hline
foo & foo \\ \hline
\end{tabularx}
\end{document}
I also know that it is possible to display images inside cells. In the minimal example below, I changed the width of the image to 2cm
, because the default size exceeds the cell dimensions.
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tabularx}
\usepackage{graphicx}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{
|>{\hsize=0.2\hsize}X
|>{\hsize=0.8\hsize}X
|
}
\hline
\includegraphics[width=2cm]{example-image} & foo \\ \hline
foo & foo \\ \hline
foo & foo \\ \hline
\end{tabularx}
\end{document}
I am now trying to make an image that is being displayed in a cell to occupy the entire width of the cell in which it is shown regardless of the width of the column. This means that if I change the width of the column, the image needs to adjust itself so that the image occupies the entire width.
Through trial and error, I was able to make the image occupy as much space as possible without exceeding the right limit. See code and screenshot below. However, this doesn't fullfil what I'm trying to accomplish because there's still space on the left side of the cell that is left blank.
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tabularx}
\usepackage{graphicx}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{
|>{\hsize=0.2\hsize}X
|>{\hsize=0.8\hsize}X
|
}
\hline
\includegraphics[width=3.07cm]{example-image} & foo \\ \hline
foo & foo \\ \hline
foo & foo \\ \hline
\end{tabularx}
\end{document}
So my question is: How to make an image that is being displayed in a cell to occupy the entire width of the cell in which it is shown regardless of the width of the column?
What I've tried
From this answer, I was able to get the dimensions of the current cell. However, the width doesn't consider the padding of cells (e.g. the space between the content and the borders of the cell.) See minimal working example below.
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tabularx}
\makeatletter
\newcommand\mycellwidth{\TX@col@width}
\makeatother
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|X|X|}
\hline
\rule{\mycellwidth}{.1cm} & foo \\ \hline
foo & foo \\ \hline
foo & foo \\ \hline
\end{tabularx}
\end{document}
I also found this answer in a question on the similar topic which recommends using \setkeys{Gin}{width = \linewidth}
. I tried it, but I noticed that there's space between the image and the left cell border and the right cell border
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tabularx}
\usepackage{graphicx}
\setkeys{Gin}{width=\linewidth}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|X|X|}
\hline
\includegraphics{example-image} & foo \\ \hline
foo & foo \\ \hline
foo & foo \\ \hline
\end{tabularx}
\end{document}
Additional information
I understand that sometimes an image might not have the same dimension ratio (e.g. 16:9 or 4:3) than the ratio of the current cell. In such case, when the image occupies the entire width, it is possible that there's blank space at the top and the bottom between the image and the cell borders.
0.8\hsize
and1.8\hsize
for the second column specification. Please fix.