1

I know there are multiple questions and their answers on the topic but I can't make it work. I tried multiple codes to make a table, the one I have now works as long as I don't use images. And I would like very much to use them.

here is my MWE :

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz,esvect}
\usepackage{tabularray}
\usepackage{multirow}


\begin{document}

\setlength\arraycolsep{1pt}

\begin{tblr}{
  colspec = {Q[c,m]Q[c,m]Q[c,m]Q[c,m]},
  stretch = 0,
  rowsep = 6pt,
  hlines = {1.2pt},
  vlines = {1.2pt},
}

        Famille
    & 
        Nom
    & 
        Schéma 
    &
        Torseur Cinématique
 \\   
    \SetCell[r=2]{} Liaison à direction
     &
     Glissière
     & 
     \includegraphics[height=2cm]{glissiere.png}
     & 
        $VVV = \displaystyle \frac{zz}{\displaystyle \int dx}$
\\  
        
     &
        Plane
     & 
          \includegraphics[height=2cm]{plane.png}
     &
         $VVV = \displaystyle \frac{zz}{\displaystyle \int dx}$
\\   
\end{tblr}


\end{document}

Here is the result, despite Q[c,m] : TableOfMWE

You can see that the content of the second and fourth column are lower than expected and in the third column it's higher : as if the bottom of the images were vertically aligned with the text in the other columns

If you like overleaf here is a link to a directory of my MWE : https://www.overleaf.com/read/wmbbxqyssxvd

I know that using images complicates my MWE but it is at the heart of my problem. so here they are : LiaisonGlisiere LiaisonPlane

Follow up question : Why are the tables in LaTeX so awful to use ?

1
  • "as if the bottom of the images were vertically aligned with the text in the other column" not "as if" that is exactly what you have specfied, each table cell is placed with its reference point on the baseline and by default the reference point of an image is on it bottom edge. Mar 20 at 14:17

2 Answers 2

4

You need to move images baseline to images vertical center. This can be done on several vays, but seems (to my opinion) that valign of adjustbox is the most handy. Doing this you not need option m in columns specifications:

\documentclass[border=5pt]{standalone}

\usepackage[export]{adjustbox}   % <---
\usepackage{tikz,esvect}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
\newcommand\ds{\displaystyle}    % <---


\begin{document}
    \begin{tblr}{colspec = {X[c] Q[c]Q[c] Q[c, mode=dmath]}, % <---
                 colsep  = 2pt,
                 row{1}  = {mode=text},                      % <---
                 hlines  = {1.2pt},  vlines = {1.2pt},
                }
Famille & Nom       & Schéma    & Torseur Cinématique               \\
\SetCell[r=2]{c}    Liaison à direction
        & Glissière & \includegraphics[height=2cm, valign=m]{example-image-duck}%{glissiere.png}
                                & VVV = \frac{zz}{\ds \int dx}      \\
        & Plane     & \includegraphics[height=2cm, valign=m]{example-image-duck}%{plane.png}
                                & VVV = \frac{zz}{\ds \int dx}
                \\
    \end{tblr}
\end{document}

Off-topic: In above MWE are

  • for shorter code for \displaystyle is defined new command \ds
  • last column in is display math style (do you really need integral in fraction as display style?)
  • used is only tabularray syntax (\colsep in table preamble instead \setlength\arraycolsep{1pt} before table)

enter image description here

  • In cases, when for images you insert tikz code, than for moving image baseline to it vertical center, add baseline=(current bounding box.center) to tikzpicture options.

Edit:

In case, that texts in some cells have few lines, that for their vertical centering you should replace X[c] with X[c, ]`:

    \begin{tblr}{colspec = {X[c,m] Q[c,m]Q[c] Q[c, mode=dmath]}, % <---
                 colsep  = 2pt,
                 row{1}  = {mode=text},                      % <---
                 hlines  = {1.2pt},  vlines = {1.2pt},
                }
Famille & Nom       & Schéma    & Torseur Cinématique               \\
\SetCell[r=2]{c}    Liaison à direction
        & Glissière & \includegraphics[height=2cm, valign=m]{example-image-duck}%{glissiere.png}
                                & VVV = \frac{zz}{\ds \int dx}      \\
        & {Plane\\ Plane\\ Plane}    
                    & \includegraphics[height=2cm, valign=m]{example-image-duck}%{plane.png}
                                & VVV = \frac{zz}{\ds \int dx}
                \\
    \end{tblr}

enter image description here

3
  • Thank you for your answer, I have marked it as accepted but could you help me further : I want to use several lines like so : { Plane \\ Plane \\ Plane } in a cell and it's not centered anymore
    – LMT-PhD
    Mar 23 at 17:01
  • 1
    @LMT-PhD, re< to add m to column specifier options: X[c,m]. I will return to your question ASAP.
    – Zarko
    Mar 23 at 18:05
  • Thank you for your help.
    – LMT-PhD
    Mar 25 at 9:24
3

It seems to me that:

  • for the vertical alignment of the images, we need h and not m;
  • the text of column 2 is aligned vertically with the = of column 4, if you want to align vertically with the image, I haven't found anything other than a manual adjustment with {\\[-1ex]Plane} or use adjustbox

the code

    \documentclass[border=5pt]{standalone}
    %https://tex.stackexchange.com/questions/680131/table-vertical-centering-what-am-i-doing-wrong
    \usepackage{tabularray}
    \usepackage{graphicx}
    \usepackage{adjustbox}% <-- added
    \begin{document}
    \setlength\arraycolsep{1pt}
    \begin{tblr}{
    %colspec = {Q[c]Q[c]Q[c,h]Q[c]},
    colspec = {*{4}{Q[c]}},% with adjsutbox
    stretch = 0,
    rowsep = 6pt,
    hlines = {1.2pt},
    vlines = {1.2pt},
    }
            Famille
        & 
            Nom
        & 
            Schéma 
        &
            Torseur Cinématique
    \\   
        \SetCell[r=2]{} Liaison à direction
        &
        Glissière
        & 
        %\includegraphics[height=2cm]{example-image-a}
        \adjustbox{valign=M}{\includegraphics[height=2cm]{example-image-a}}
        & 
                $VVV = \displaystyle \frac{zz}{\displaystyle \int dx}$
    \\        
        &
        %{\\[-1ex]Plane}
        %& 
        %    \includegraphics[height=2cm]{example-image-b}
        Plane
        & 
        \adjustbox{valign=M}{\includegraphics[height=2cm]  {example-image-b}}
        &
            $VVV = \displaystyle \frac{zz}{\displaystyle \int dx}$
    \\   
    \end{tblr}
    \end{document}

enter image description here

Another possibility with nicematrix and adjustbox

    \documentclass[border=5pt]{standalone}
    \usepackage{nicematrix}
    \usepackage{graphicx}
    \usepackage{adjustbox}
    \begin{document}
    \begin{NiceTabular}{c c c}[hvlines,cell-space-limits=1mm]
    %\Body
    Glissière & 
    \adjustbox{valign=M}{\includegraphics[height=3cm]{example-image-b}}
    & $VVV = \displaystyle \frac{zz}{\displaystyle \int dx}$ \\
    \end{NiceTabular}
    \end{document}

enter image description here

1
  • 1
    You can also use \adjustbox{valign=m}, adjustbox documentation, section 4.6 Vertical Alignment
    – pascal974
    Mar 21 at 6:26

You must log in to answer this question.

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