1

Firstly, please check the answer's LaTeX code:

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{array}
\begin{document}
\begin{tabular}{|m|m|m|}
\hline
 & Size & Figure \\  
\hline
One & 1cm*1cm & \includegraphics[width=1cm, height=1cm]{owl.jpg} \\ 
\hline
 Two & 2cm*2cm & \includegraphics[width=2cm, height=2cm]{owl.jpg} \\ 
\hline   
\end{tabular}
\end{document}

What commands should I need to use so that the table cells get auto-adjusted with the given picture size?
And how to center align (vertically and horizontally) every table cell content including the pictures?

TIA

5
  • Could you please make your question self-comtained? Links to external sites might expire.
    – leandriis
    Mar 8, 2020 at 17:19
  • The other site might be inaccessible (deleted, moved,...) in the future making a question rely on something that is not there any more.
    – leandriis
    Mar 8, 2020 at 17:26
  • Regaring the code: m type columns need a width argument in order to work properly. Do you want allo columns to be equally wide?
    – leandriis
    Mar 8, 2020 at 17:27
  • I get it. I have added the LaTeX code.
    – raf
    Mar 8, 2020 at 17:28
  • No, I want the table to be autofit to the contents or to the window. @leandriis
    – raf
    Mar 8, 2020 at 17:34

2 Answers 2

2

Probably this is what you're looking for:

enter image description here

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{cellspace}
\setlength\cellspacetoplimit{\tabcolsep}
\setlength\cellspacebottomlimit{\tabcolsep}
\begin{document}
\begin{tabular}{|Sc|Sc|Sc|}
\hline
 & Size & Figure \\  
\hline
One & 1cm*1cm & \includegraphics[width=1cm, height=1cm, valign=c]{example-image} \\ 
\hline
 Two & 2cm*2cm & \includegraphics[width=2cm, height=2cm, valign=c]{example-image} \\ 
\hline   
\end{tabular}
\end{document}
1
  • Thank you for your answer!
    – raf
    Mar 9, 2020 at 8:09
2

The m columns should have some length associated, so you should use some like m{2cm} or use another type of column.

The l, c and r columns adjust automatically to the content, so it is all you need. You can use also @{} before and after the column to avoid the default padding defined by \tabcolsep.

On the other hand, note that by default the image will be aligned at bottom with "cm" of the left cell (is placed just as character), but you can center with the help of adjustbox package as showed in the MWE, and then will fit perfectly the biggest image.

mwe

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{array}
\begin{document}
\tabcolsep10pt
\begin{tabular}{|l|c|@{}c@{}|}\hline
 & Size & Figure \\\hline
One & 1cm*1cm & \includegraphics[valign=c,width=1cm]{example-image-1x1.jpg} \\\hline
 Two & 2cm*2cm & \includegraphics[valign=c,width=2cm]{example-image-1x1.jpg} \\\hline   
\end{tabular}
\end{document}

If you want some padding around the image, a trick without additional package is put the image in a \fbox{} but previously set \fboxrule to 0pt and \fboxsep to more that 0pt, obviously. (e.g. \fboxrule0pt\fboxsep1em\fbox{}. That allow even different paddings in the same column. For instance:

mwe

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{array}
\begin{document}
\tabcolsep10pt\fboxrule0pt\fboxsep\tabcolsep
\begin{tabular}{|l|c|@{}c@{}|}\hline
 & Size & Figure \\\hline
 One & 1cm*1cm & \fbox{\includegraphics[valign=c,width=1cm]{example-image-1x1.jpg}} \\\hline
 Two & 2cm*2cm & \fboxsep2pt\fbox{\includegraphics[valign=c,width=2cm]{example-image-1x1.jpg}} \\\hline
\end{tabular}
\end{document}
1
  • Thank you for your answer!
    – raf
    Mar 9, 2020 at 8:09

You must log in to answer this question.

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