TransWikia.com

move the text to a new line inside a tabular of text and figure

TeX - LaTeX Asked on December 27, 2021

hi I have a piece of code that looks like this in tabular:

usepackage{mwe} 

begin{tabular}

(1) & includegraphics[width=0.2textwidth]{images/some_image.png} & He thinks:
   $``$Maybe, the food     is in the yellow pot.$"$
  newline
   But, the yellow pot is empty.\
end{tabular}

I have used newline to move the sentence to the next line, but it does not work. I have tried p{some cm} but the problem is when I add this, two horizontal line tabke lines appear that I do not want, and the text moves under the image.

I would like to keep it in a way that there is an image on the left with it’s corresponding text on the right, but if the text is long, it can be moved to the new line similar to the image in : figures on the left with corresponding texts on the right

2 Answers

enter image description here

The serial number 1 in the begining I have removed just for simplicity sake-- the image is no problem and can be adjusted with the package usepackage[export]{adjustbox} and by adding valign=c as answered by @leandriis

The right hand side length can be set by taking the longest length and setting it as the column width so that anything longer will flow to the second line automatically

So

settowidthcolwidth{He thinks: $``$Maybe, the food is in the yellow pot.$"$}

becomes the column width of the second column

Taken from --

https://tex.stackexchange.com/a/512217/197451

newlengthcolwidth
settowidthcolwidth{He thinks: $``$Maybe, the food is in the yellow pot.$"$}
begin{tabular}{lm{colwidth}} 
        includegraphics[width=0.2textwidth, valign=c]{example-image} & 
            He thinks: $``$Maybe, the food is in the yellow pot.$"$
    But, the yellow pot is empty.\
end{tabular}   

Answered by js bibra on December 27, 2021

With the help of tabularx in order to make sure the table is as wide as the textwidth and valign=c from adjustbox in order to vertically center the elements:

enter image description here

documentclass{article}
usepackage[demo]{graphicx}
usepackage{tabularx}
renewcommand{tabularxcolumn}[1]{m{#1}}
usepackage[export]{adjustbox}
begin{document}

noindent
begin{tabularx}{textwidth}{llX}
(1) & includegraphics[width=0.2textwidth, valign=c]{images/some_image.png} & He thinks:
   ``Maybe, the food     is in the yellow pot.''  But, the yellow pot is empty.\
end{tabularx}

end{document}

If your table will be longer than a single page, you can use the xltabular package instead. For this, just replace tabularx with xltabular in the above MWE.


For fun, here are two more versions in which each set of image and text is automatically numbered. Version 1 makes use of magicrownumber from Automatic table row numbers while version 2 uses side-by-side minipages inside of an enumerate environment:

documentclass{article}
usepackage[demo]{graphicx}
usepackage{tabularx}
renewcommand{tabularxcolumn}[1]{m{#1}}
usepackage[export]{adjustbox}

%%% used in example 1 %%%%%
usepackage{etoolbox}
pretotabular{setcounter{magicrownumbers}{0}}
newcounter{magicrownumbers}
newcommandrownumber{stepcounter{magicrownumbers}arabic{magicrownumbers}}

%%% used in example 2 %%%%%
usepackage{enumitem}

begin{document}

noindent
begin{tabularx}{textwidth}{@{makebox[3em][r]{(rownumber)quad}} lX}
includegraphics[width=0.2textwidth, valign=c]{images/some_image.png} & He thinks:
   ``Maybe, the food     is in the yellow pot.''  But, the yellow pot is empty.\
end{tabularx}

begin{enumerate}[label={(arabic*)}]
item begin{minipage}{0.2textwidth} 
        includegraphics[width=textwidth, valign=c]{images/some_image.png}
      end{minipage} 
      hfill 
      begin{minipage}{0.7textwidth} 
        He thinks: ``Maybe, the food is in the yellow pot.'' But, the yellow pot is empty.
      end{minipage}
end{enumerate}

end{document}

Answered by leandriis on December 27, 2021

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