TransWikia.com

How to resize an image within a cell in longtable environment while keeping its aspect?

TeX - LaTeX Asked by Moreblue on December 1, 2020

Problem

I am currently writing a table with longtable environment, and would like to insert a picture within a cell and vertically center-align it. However, my try ends up as follows:

enter image description here

So what I would like to do is as follows:

  • If the boy was taller than the cell, then reducing his height to the cell while keeping aspect.
  • If the boy was fatter than the cell, then reducing his width to the cell while keeping aspect.

Try

The above table was generated using the following code:

documentclass[fontsize=12bp]{article}
usepackage[table,xcdraw]{xcolor}
usepackage{multirow}
usepackage{lipsum}
usepackage{longtable}
usepackage{graphicx}
usepackage{geometry}
usepackage{float}
usepackage{helvet}
renewcommand{familydefault}{sfdefault}

begin{document}
begin{longtable}{|ccc|}
hline
rowcolor[HTML]{C0C0C0} 
multicolumn{1}{c}{large textbf{Column1} } & multicolumn{1}{c}{large textbf{Column2}} & multicolumn{1}{c}{large textbf{Column3}}  hline endhead 

multicolumn{1}{|c|}{} & multicolumn{1}{l|}{begin{minipage}{0.5textwidth} medskip scriptsize lipsum[4] medskip end{minipage}} &  cline{2-2} 

multicolumn{1}{|c|}{multirow{-4}{*}{Large Name1}} &

multicolumn{1}{l|}{begin{minipage}{0.5textwidth} medskip scriptsize lipsum[2] medskip end{minipage}} & 
multirow{-10}{*}{begin{minipage}{0.4textwidth} 
begin{figure}[H]{center includegraphics[keepaspectratio=true, width=linewidth]{figures/human.png}} end{figure} end{minipage}}  hline
end{longtable}

end{document}

Question

How should I put this boy within the cell while keeping his aspect? Any help will be appreciated.

One Answer

Something like this?

enter image description here

  • float figure had not to be enclosed in minipage or similar box environment
  • image, included by includegraphics[width=<length>]{<image-file>} is keeps own aspect ratio
  • instead of use minipage in cells is better to define column type p{<width>}
  • I didn't change your code for columns headers
  • for multirow in the last column you can use option defined in recent multirow package: {=}
documentclass[fontsize=12bp]{article}
usepackage{geometry}
usepackage{helvet}
renewcommand{familydefault}{sfdefault}

usepackage[table,xcdraw]{xcolor}
usepackage{array, multirow, longtable}
usepackage{lipsum}
usepackage{graphicx}

begin{document}
begin{longtable}{|c 
                  |>{scriptsize}p{0.4textwidth}
                  |p{0.5textwidth}|}
    hline
rowcolor[HTML]{C0C0C0}
multicolumn{1}{c}{large textbf{Column1} } 
    & multicolumn{1}{c}{large textbf{Column2}} 
        & multicolumn{1}{c}{large textbf{Column3}}  
    hline 
endhead
multicolumn{1}{|c|}{} 
    & lipsum[4] 
        &    
    cline{2-2}
multirow{-1}{*}{Large Name1}
    &   scriptsize lipsum[2]
        &   multirow{-6.2}{=}{includegraphics[width=linewidth]{example-image-duck}}  
    hline
end{longtable}

end{document}

Correct answer by Zarko on December 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP