5

I'm having a few issues with vertically aligning table elements. There seems to be too much space either above or below the text or image. Is this to do with the fbox environment? I want both the image and text to be centered vertically in each table cell.

\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}

enter image description here

Edit: Following Gonzalo Medina's suggestion, I get: enter image description here and there is no difference between center and centering.

2 Answers 2

6

You can use a m{length} column type for the first column:

\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}

enter image description here

I wasn't sure if some of the blank spaces at end of lines were intentional or not; I commented them out. Also take into account that center adds some vertical spacing so perhaps you could consider using \centering instead.

As an alternative, I'd like to propose the tcolorbox package to easily produce your box; you can even customize it even further:

\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}

enter image description here

3

You can use valign macro from the adjustbox package in

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

Code:

\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}

enter image description here

You must log in to answer this question.

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