将表格内的图像和文本居中

将表格内的图像和文本居中

我正在努力制作一个包含一些图片的表格,并希望它们垂直和水平居中(图片和文本都居中)。我尝试了很多这里建议的示例,但都没有用。你能帮助我吗?

在此处输入图片描述

这是表格代码:

\begin{table}
\centering

\caption{text centered table}
\label{fGO_table}
\begin{tabular}{ m{0.20\textwidth}  m{0.1\textwidth}  p{0.55\textwidth} }      % centered columns (3 columns) 
    \hline\hline                                      %inserts double horizontal lines 
    ref  & code & structure \\ [0.5ex] % inserts table heading 
    \hline
    
    ref1    & GO2 &\begin{center} \includegraphics[scale=0.8]{immagini/MM/AMMON.jpg} 
    \end{center}                    
    
    \\ 
    \hline  
    ref2 
    & GO3 &\begin{center}
        \includegraphics[scale=0.8]{immagini/MM/ED.jpg}\end{center} \\
    \hline  
    ref3
    & GO3&
     \begin{center}
        \includegraphics[scale=0.8]{immagini/MM/TEPA.jpg}\end{center}\\
    \hline  
    ref4
    & GO5&
     \begin{center}
        \includegraphics[scale=0.8]{immagini/MM/pN.jpg}\end{center} \\
    \hline  
    ref5
    &  GO7&
    \begin{center}
        \includegraphics[scale=0.8]{immagini/MM/MEL.jpg}\end{center}\\
    \hline  
\end{tabular}
\end{table}

答案1

假设您希望图像在固定宽度的列内水平和垂直居中,我建议如下:

在此处输入图片描述

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{array}
\usepackage[export]{adjustbox}

\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{0.5\tabcolsep}
\setlength{\cellspacebottomlimit}{\cellspacetoplimit}
\begin{document}

\begin{table}
\centering

\caption{text centered table}
\label{fGO_table}
\begin{tabular}{ 0{p{0.20\textwidth}}  0{p{0.1\textwidth}}  0{wc{0.55\textwidth}} }      % centered columns (3 columns) 
    \hline\hline                                      %inserts double horizontal lines 
    ref  & code & structure \\ [0.5ex] % inserts table heading 
    \hline
    ref1 & GO2 & \includegraphics[scale=0.8,valign=c]{immagini/MM/AMMON.jpg} \\ 
    \hline  
    ref2 
    & GO3 &\includegraphics[scale=0.8,valign=c]{immagini/MM/ED.jpg} \\
    \hline  
    ref3
    & GO3& \includegraphics[scale=0.8,valign=c]{immagini/MM/TEPA.jpg}\\
    \hline  
    ref4
    & GO5& \includegraphics[scale=0.8,valign=c]{immagini/MM/pN.jpg} \\
    \hline  
    ref5
    &  GO7& \includegraphics[scale=0.8,valign=c]{immagini/MM/MEL.jpg}\\
    \hline  
\end{tabular}
\end{table}

\end{document}

答案2

通过采用booktabs,使用包\adjustimage中定义的宏adjustbox和键,Gin表的代码变得更短更清晰:

\documentclass{article}
\usepackage{array, booktabs}
\usepackage[demo,
            export]{adjustbox}  % it also load graphicx
\usepackage[skip=1ex]{caption}

\begin{document}
    \begin{table}[ht]
    \centering
\caption{text centered table}
\label{fGO_table}
    \setkeys{Gin}{}
    \adjustboxset{scale=0.8, valign=c}
\begin{tabular}{w{c}{0.20\textwidth} 
                w{c}{0.10\textwidth} 
                c}      
    \toprule                                   
ref     & code  & structure                             \\
    \midrule
ref1    & GO2   & \adjustimage{}{immagini/MM/AMMON.jpg} \\
    \midrule
ref2    & GO3   & \adjustimage{}{immagini/MM/ED.jpg}    \\
    \midrule
ref3    & GO3   & \adjustimage{}{immagini/MM/TEPA.jpg}  \\
    \midrule
ref4    & GO5   & \adjustimage{}{immagini/MM/pN.jpg}    \\
    \midrule
ref5    & GO7   & \adjustimage{}{immagini/MM/MEL.jpg}   \\
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

在此处输入图片描述

相关内容