在表格中插入图像并将文本垂直居中对齐

在表格中插入图像并将文本垂直居中对齐

我想将表格中的文本与图片对齐。期望的结果如下表格中的对齐文本

我试过了,得到了这个代码

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage[demo]{graphicx}

\begin{document}
\begin{table*}[]
    \centering
    \renewcommand{\arraystretch}{1.5}       
    \begin{tabular}{p{2cm} p{2cm} p{2cm} p{2cm} c}
        \hline
        Nome                                   & Dim. & Range           & Ótimo & Sketchup em 2D \\ \hline
        %
        Sphere                                 & 30   & $[-100, 100]^p$ & -1400 & \raisebox{-\totalheight}{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        %
        Rotated Bent Cigar                     & 30   & $[-100, 100]^p$ & -1200 & \raisebox{-\totalheight}{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Rotated Discus                         & 30   & $[-100, 100]^p$ & -1100 & \raisebox{-\totalheight}{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Schwefel                               & 30   & $[-100, 100]^p$ & -100  & \raisebox{-\totalheight}{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Rotated Schwefel                       & 30   & $[-100, 100]^p$ & 100   & \raisebox{-\totalheight}{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Rotated Katsuura                       & 30   & $[-100, 100]^p$ & 200   & \raisebox{-\totalheight}{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Composition Function 2 (n=3,Unrotated) & 30   & $[-100, 100]^p$ & 800   & \raisebox{-\totalheight}{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Composition Function 5                 & 30   & $[-100, 100]^p$ & 1100  & \raisebox{-\totalheight}{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Composition Function 8 (n=5,Rotated)   & 30   & $[-100, 100]^p$ & 1400  & \raisebox{-\totalheight}{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\ \hline
    \end{tabular}
    \caption{My caption}
    \label{my-label}
\end{table*}

\end{document}

此代码产生 在此处输入图片描述

答案1

唯一的问题是,你将图像移动得太远,移动了整个高度,而你想要的高度却不到一半,但我也做了一些不太重要的改变

在此处输入图片描述

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage[demo]{graphicx}
\usepackage{array}
\begin{document}
\begin{table*}[htp]
    \centering
    \renewcommand{\arraystretch}{2}  
    \setlength\extrarowheight{5pt}    
    \begin{tabular}{lllll}
        \hline
        Nome                                   & Dim. & Range           & Ótimo & Sketchup em 2D \\ \hline
        %
        Sphere                                 & 30   & $[-100, 100]^p$ & -1400 & \raisebox{-.4\totalheight}{\includegraphics[width=2cm,height=1cm]{sphere.png}}               \\
        %
        Rotated Bent Cigar                     & 30   & $[-100, 100]^p$ & -1200 & \raisebox{-.4\totalheight}{\includegraphics[width=2cm,height=1cm]{sphere.png}}               \\
        Rotated Discus                         & 30   & $[-100, 100]^p$ & -1100 & \raisebox{-.4\totalheight}{\includegraphics[width=2cm,height=1cm]{sphere.png}}               \\
        Schwefel                               & 30   & $[-100, 100]^p$ & -100  & \raisebox{-.4\totalheight}{\includegraphics[width=2cm,height=1cm]{sphere.png}}               \\
        Rotated Schwefel                       & 30   & $[-100, 100]^p$ & 100   & \raisebox{-.4\totalheight}{\includegraphics[width=2cm,height=1cm]{sphere.png}}               \\
        Rotated Katsuura                       & 30   & $[-100, 100]^p$ & 200   & \raisebox{-.4\totalheight}{\includegraphics[width=2cm,height=1cm]{sphere.png}}               \\
        Composition Function 2 (n=3,Unrotated) & 30   & $[-100, 100]^p$ & 800   & \raisebox{-.4\totalheight}{\includegraphics[width=2cm,height=1cm]{sphere.png}}               \\
        Composition Function 5                 & 30   & $[-100, 100]^p$ & 1100  & \raisebox{-.4\totalheight}{\includegraphics[width=2cm,height=1cm]{sphere.png}}               \\
        Composition Function 8 (n=5,Rotated)   & 30   & $[-100, 100]^p$ & 1400  & \raisebox{-.4\totalheight}{\includegraphics[width=2cm,height=1cm]{sphere.png}}               \\[1cm] \hline
    \end{tabular}

\bigskip

    \caption{My caption}
    \label{my-label}
\end{table*}

\end{document}

答案2

图像自然地在基线上方、略低于减号水平的位置构建-。这就是为什么将图像提高 永远不准确的原因。另一种更简单的方法(无需反复试验)是将图像封装在表格中,因为表格自然是垂直居中的。可以使用以下-\totalheight新命令来实现此目的:\tabcell

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage[demo]{graphicx}
\usepackage{array}
\begin{document}

\newcommand{\tabcell}[1]{\begin{tabular}{@{}c@{}}#1\end{tabular}}

\begin{table*}
    \centering
    \caption{My caption}\label{my-label}
    \renewcommand{\arraystretch}{1.2}       
    \begin{tabular}{>{\raggedright}p{2cm} p{2cm} p{2cm} p{2cm} c}
        \hline
        Nome                                   & Dim. & Range           & Ótimo & Sketchup em 2D \\ \hline \noalign{\vskip2pt}
        Sphere                                 & 30   & $[-100, 100]^p$ & -1400 & \tabcell{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        %
        Rotated Bent Cigar                     & 30   & $[-100, 100]^p$ & -1200 & \tabcell{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Rotated Discus                         & 30   & $[-100, 100]^p$ & -1100 & \tabcell{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Schwefel                               & 30   & $[-100, 100]^p$ & -100  & \tabcell{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Rotated Schwefel                       & 30   & $[-100, 100]^p$ & 100   & \tabcell{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Rotated Katsuura                       & 30   & $[-100, 100]^p$ & 200   & \tabcell{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Composition Function 2 (n=3,Unrotated) & 30   & $[-100, 100]^p$ & 800   & \tabcell{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Composition Function 5                 & 30   & $[-100, 100]^p$ & 1100  & \tabcell{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\
        Composition Function 8 (n=5,Rotated)   & 30   & $[-100, 100]^p$ & 1400  & \tabcell{\includegraphics[width=0.1\textwidth, height=20mm]{sphere.png}}               \\ \hline
    \end{tabular}
\end{table*}

\end{document}

在此处输入图片描述

相关内容