表格环境中的垂直对齐问题

表格环境中的垂直对齐问题

我在垂直对齐表格元素时遇到了一些问题。文本或图像上方或下方似乎有太多空间。这与环境有关吗fbox?我希望图像和文本在每个表格单元格中垂直居中。

\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage{graphicx}   
\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage{array}

\newcommand{\mybox}[1]{
    \begin{center}
    \fbox{
    \parbox{0.8\linewidth}{
        \begin{center}
            \begin{tabular}{ c m{4.5in} }
            \includegraphics[height=0.3in]{./myfig} & {#1}
            \end{tabular} 
        \end{center}
        }
     }
    \end{center}
}


\begin{document}

\mybox{hello}

\mybox{hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello}


\end{document}

在此处输入图片描述

编辑:按照 Gonzalo Medina 的建议,我得到: 并且和在此处输入图片描述 之间没有区别。centercentering

答案1

您可以m{length}对第一列使用某种列类型:

\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage{graphicx}   
\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage{array}

\newcommand{\mybox}[1]{
    \begin{center}
    \fbox{%
    \parbox{0.8\linewidth}{%
        \begin{center}
            \begin{tabular}{m{0.3in}m{4.5in} }
            \includegraphics[height=0.3in]{example-image-a} & #1
            \end{tabular} 
        \end{center}
        }
     }
    \end{center}%
}


\begin{document}

\mybox{hello}

\mybox{hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello}


\end{document}

在此处输入图片描述

我不确定行尾的一些空格是否是故意的;我将它们注释掉了。还考虑到这center会增加一些垂直间距,因此也许您可以考虑使用它\centering

作为替代方案,我想建议tcolorbox包装以轻松生产您的盒子;您甚至可以进一步定制它:

\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage{graphicx}   
\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage[many]{tcolorbox}

\newcommand\mybox[1]{%
    \begin{tcolorbox}[
      sidebyside,
      lefthand width=.3in,
      colback=white,
      outer arc=0pt,
      arc=0pt.
      colframe=black,
      boxrule=0.4pt,
      segmentation engine=empty
    ]
    \includegraphics[width=.3in]{example-image-a}
    \tcblower
    #1
    \end{tcolorbox}%
}

\begin{document}

\mybox{hello}

\mybox{hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello}


\end{document}

在此处输入图片描述

答案2

您可以使用包中valign的宏adjustbox

\includegraphics[valign=c,height=0.3in]{example-image-a} 

代码:

\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage[export]{adjustbox}  %% export is needed
\usepackage[top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage{array}

\newcommand{\mybox}[1]{%
    \begin{center}
    \fbox{%
    \parbox{0.8\linewidth}{%
        \begin{center}
            \begin{tabular}{ c m{0.75\linewidth} }
            \includegraphics[valign=c,height=0.3in]{example-image-a} & #1
            \end{tabular}
        \end{center}
        }%
     }%
    \end{center}
}


\begin{document}

\mybox{hello}

\mybox{hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello}


\end{document}

在此处输入图片描述

相关内容