8

I am facing some problem in aligning (top-aligned) image and text in a table.

\documentclass[11pt,table,a4paper]{article}
\usepackage{array,ragged2e}
\usepackage{graphicx}
\usepackage[top=2cm, bottom=3cm,right=1cm, left=1cm, headsep=26pt]{geometry}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[table]{xcolor}
\usepackage{sidecap}
\usepackage{longtable}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}                          % End of preamble and beginning of  text.
\arrayrulecolor{gray}
\begin{center}
\newcolumntype{M}[1]{>{\arraybackslash}p{#1}}
 \renewcommand{\arraystretch}{2.0}\begin{longtable}{M{5cm}|M{6cm}|M{6cm}}
\hline
\rowcolor{gray}   
\textbf{ Col1} &  \textbf{Col2} &  \textbf{Col3} \\ \hline
 \vspace{0pt} \large{ABC} & \includegraphics[width=0.15\textheight, width=0.15\textwidth]{Europe_regions.png} \hspace{.1cm}    \includegraphics[width=0.15\textwidth]{male-female.png} &  \vspace{0pt} XYZ.  
\\ \hline   
\end{longtable}
\end{center}
\end{document}             % End of document.

enter image description here

How can i top-align image and text?

7

The valign option provided by adjustbox is what you're looking for:

\documentclass[11pt,a4paper]{article}
\usepackage[margin=1cm]{geometry}
\usepackage[demo]{graphicx} % the demo option is just for the example
\usepackage{array}
\usepackage[table]{xcolor}
\usepackage[export]{adjustbox}

\begin{document}
\begin{center}
\arrayrulecolor{gray}
\begin{tabular}{p{5cm}|p{6cm}|p{6cm}}
\hline
\rowcolor{gray}
\textbf{ Col1} &  \textbf{Col2} &  \textbf{Col3} \\ \hline
\vspace{0pt}\large ABC
  & \large
  \includegraphics[width=2.5cm,valign=T]{Europe_regions.png}%
  \hspace{.1cm}%
  \includegraphics[width=2.5cm,valign=T]{male-female.png}
  &\vspace{0pt}   XYZ.
\\
\hline
\end{tabular}
\end{center}
\end{document}

I've simplified the input to a minimum (longtable is not needed to show the effect, but can be used; the M column type was useless).

enter image description here

3

The \belowbaseline command of the stackengine package changes the baseline of stuff, including images. In this case, setting the optional length argument to a negative number [0pt-\heightof{X}] moves the top edge of the main argument (the image) above the baseline (in this case by the height of the letter "X"). Recall, in the other columns, the baseline is at the bottom edge of the "ABC" and "XYZ". Therefore, if I had set the pictures 0pt below the baseline, the tops of the images would align with the bottom of the "ABC" and "XYZ".

Note that my above comments apply to what are denoted as "short" stacks, which are the default stacktype for stackengine. "Long" stacks will behave differently (see package documentation for details).

\documentclass[11pt,table,a4paper]{article}
\usepackage{array,ragged2e}
\usepackage[demo]{graphicx}
\usepackage[top=2cm, bottom=3cm,right=1cm, left=1cm, headsep=26pt]{geometry}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[table]{xcolor}
\usepackage{sidecap}
\usepackage{longtable}
\usepackage{helvet}
\usepackage{stackengine}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}                          % End of preamble and beginning of  text.
\arrayrulecolor{gray}
\begin{center}
\newcolumntype{M}[1]{>{\arraybackslash}p{#1}}
 \renewcommand{\arraystretch}{2.0}\begin{longtable}{M{5cm}|M{6cm}|M{6cm}}
\hline
\rowcolor{gray}   
\textbf{ Col1} &  \textbf{Col2} &  \textbf{Col3} \\ \hline
 \vspace{0pt} \large{ABC} & 
 \belowbaseline[0pt-\heightof{X}]{%
   \includegraphics[width=0.15\textwidth]{Europe_regions.png}%
 }
 \hspace{.1cm}%
 \belowbaseline[0pt-\heightof{X}]{%
   \includegraphics[width=0.15\textwidth]{male-female.png}
 } &
 \vspace{0pt} XYZ.  
\\ \hline   
\end{longtable}
\end{center}
\end{document}             % End of document.

enter image description here

3

As an alternative, you can use the cals-tables:

Top-align

\documentclass[11pt,table,a4paper]{article}
\usepackage{array,ragged2e}
\usepackage[draft]{graphicx}
\usepackage[top=2cm, bottom=3cm,right=1cm, left=1cm, headsep=26pt]{geometry}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[table]{xcolor}
\usepackage{sidecap}
\usepackage{cals}
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\begin{calstable}
\makeatletter
\colwidths{{5cm}{65mm}{6cm}}
\brow
\def\cals@bgcolor{gray}\bfseries
\cell{Col1}
\cell{Col2}
\cell{Col3}
\def\cals@bgcolor{}\mdseries
\erow
\brow
\cell{\large{ABC}}
\cell{\includegraphics[width=0.15\textheight, width=0.15\textwidth]{Europe_regions.png} \hspace{.1cm}    \includegraphics[width=0.15\textwidth]{male-female.png}}
\cell{XYZ.}
\erow
\end{calstable}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.