TransWikia.com

Ordering and aligning graphics and text in tables

TeX - LaTeX Asked by QuagTeX on June 26, 2021

I have a very urgent matter and I’ve read so many threads and tried to combine the answers but nothing works or it gets much uglier… I just want to show up my figure and the text in a table in a nice way. The images cover the lines and aren’t vertical aligned…

Sorry for the german language but i got no time to make a simple example in english out of it

I have the following code

begin{table}[h]
    centering
    begin{tabular}{l|l|l|}
    cline{2-3}
        & textbf{Konflikttyp}
        & textbf{Kollisionsarten}  
        hline
    multicolumn{1}{|l|}{
    parbox[c]{1em}{includegraphics[width=50mm]{./Abbildungen/Auffahren.jpg}}
    label{fig:Auffahren}}  
    & Auffahren                                                           
    & tabitem Auffahrunfall
    hline
    multicolumn{1}{|l|}{
    includegraphics[width=50mm]{./Abbildungen/Fahrstreifenwechsel.jpg}
    label{fig:Fahrstreifenwechsel}} 
    & Fahrstreifenwechsel                                                 
    & begin{tabular}[c]{@{}l@{}}
        tabitem Auffahrunfall
        tabitem Seitenkollision (wenn die Seite ei-
         quad thinspace nes Fahrzeuges im Längsverkehr
         quad thinspace mit der Seite eines anderen 
         quad thinspace Fahrzeugs kollidieren würde)
    end{tabular}  
    hline
    end{tabular}
    caption[Konfliktszenarien mit zugehörigen Kollisionsarten]{Konfliktszenarien mit zugehörigen Kollisionsarten nach textsc{Wang} und textsc{Stamatiadis} cite{Wang.2013}  label{tab:Konfliktszenarios}}
end{table}

It provides the following output:
generated table

2 Answers

The following should serve as a point to start from:

enter image description here

documentclass{article}
usepackage{geometry}
usepackage[demo]{graphicx} % remove demo option in actual document
usepackage{tabularx}
usepackage[column=0]{cellspace}
usepackage[export]{adjustbox}
usepackage{enumitem}

setlength{cellspacetoplimit}{tabcolsep}
setlength{cellspacebottomlimit}{cellspacetoplimit}
addparagraphcolumntypes{X}

newlist{tabitemize}{itemize}{1}
setlist[tabitemize]{label=textbullet,nosep,after=strut,align=parleft,leftmargin=*,before={begin{minipage}[t]{hsize}}, after={end{minipage}}}


begin{document}

begin{table}[h]
    centering
    begin{tabularx}{textwidth}{|0l|0l|0{X}|}
    cline{2-3}
       multicolumn{1}{c|}{} & textbf{Konflikttyp}
        & textbf{Kollisionsarten}  
        hline
    includegraphics[width=50mm,valign=t]{./Abbildungen/Auffahren.jpg}
%    label{fig:Auffahren}  % this label does not make sense since the image does nt have its own figure number and caption.
    & Auffahren                                                           
    & begin{tabitemize} item Auffahrunfall end{tabitemize} 
    hline
    includegraphics[width=50mm,valign=t]{./Abbildungen/Fahrstreifenwechsel.jpg}
%    label{fig:Fahrstreifenwechsel} % this label does not make sense since the image does nt have its own figure number and caption.
    & Fahrstreifenwechsel                                                 
    & begin{tabitemize} 
          item  Auffahrunfall
          item Seitenkollision (wenn die Seite eines Fahrzeuges im Längsverkehr mit der Seite eines anderen Fahrzeugs kollidieren würde)
      end{tabitemize}
    hline
    end{tabularx}
    caption[Konfliktszenarien mit zugehörigen Kollisionsarten]{Konfliktszenarien mit zugehörigen Kollisionsarten nach textsc{Wang} und textsc{Stamatiadis} cite{Wang.2013}  label{tab:Konfliktszenarios}}
end{table}


end{document}

Answered by leandriis on June 26, 2021

A small variation of nice @leandriis answer (+1).

Since you not provide any information about your document design (used documentclass, page layout, relevant package in your document preamble), both answer are faced by guessing about this, we both used a documentclass and preamble according to our taste, what a beautiful table:

In MWE below:

  • the table position option [h] is changed to the more correct [ht] (now table can migrate to the top of the next page)
  • for table is used tabularx because it enables to prescribe table width automatic determination of X column widths
  • for better formatting of text in the last columns, the X column type is modified to >{RaggedRight}X (RaggedRight is defined in the ragged2e package), which enable "smart" aligning of cells' contents to the left
  • for column headers is used thead command defined in the makecell command
  • is exploited features of the adjustbox package for moving image baseline to top of images, adding vertical spaces around images and inserting images by use of the adjustimage command
  • for lists in the third column is used the itemize package and its ability for their customization
  • for list customization is employed AtBeginEnvironment{...}{...} environment from the etoolbox package
documentclass{article}
usepackage{geometry}
usepackage{ragged2e}
usepackage{makecell, tabularx}
renewcommandtheadfont{normalsizebfseries}
usepackage[demo,               % remove demo option in actual document
            export]{adjustbox}  % it load graphicx too
usepackage{enumitem}
usepackage{etoolbox}
AtBeginEnvironment{table}%
{%
setlist[itemize]{nosep,
                 leftmargin=*,
                 label=textbullet,
                 after=end{minipage},                  % <---
                 before=begin{minipage}[t]{linewidth} % <---
                 }
}

begin{document}
    begin{table}[ht] % <---
    centering
    adjustboxset{width=50mm,valign=t, margin=0pt 6pt 0pt 6pt} % <---
begin{tabularx}{textwidth}{|l|l|>{RaggedRight}X|}
    cline{2-3}
multicolumn{1}{c|}{} 
    & thead[l]{Konflikttyp}
        & thead[l]{Kollisionsarten} 
    hline
adjustimage{}{./Abbildungen/Auffahren}
    & Auffahren
        &   begin{itemize} 
            item Auffahrunfall 
            end{itemize} 
    hline
adjustimage{}{./Abbildungen/Fahrstreifenwechsel}
%    label{fig:Fahrstreifenwechsel} % this label does not make sense since the image does nt have its own figure number and caption.
    & Fahrstreifenwechsel
        &   begin{itemize}
              item  Auffahrunfall
              item Seitenkollision (wenn die Seite eines Fahrzeuges im Längsverkehr mit der Seite eines anderen Fahrzeugs kollidieren würde)
            end{itemize}
    hline
end{tabularx}
caption[Konfliktszenarien mit zugehörigen Kollisionsarten]{Konfliktszenarien mit zugehörigen Kollisionsarten nach textsc{Wang} und textsc{Stamatiadis} cite{Wang.2013}  
label{tab:Konfliktszenarios}}
    end{table}
end{document}

enter image description here

Answered by Zarko on June 26, 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