[Tex/LaTex] Align figures to top using minipage environment

floatsminipagevertical alignment

I would like to align 2 figures to top using minipage environment, I've seen post like this link but answers doesn't use minipage environment. I've got this code:

\documentclass[8pt,a4paper,dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[left=2cm,right=2cm,top=1.5cm,bottom=1.5cm]{geometry}


\begin{document}
Test text
\begin{figure}[htp]
{\hfill}
\begin{minipage}[t]{0.35\textwidth}
\centering
\includegraphics[scale=0.25]{example-image-a}
\end{minipage}
{\hfill}
\begin{minipage}[t]{0.55\textwidth}
\centering
\includegraphics[scale=0.55]{example-image-b}
\end{minipage}
{\hfill}
\end{figure} 


\end{document}

which produces this:

enter image description here

I think if I've got figures align to bottom, I could align them to top. Thank you so much and excuse me if this is a duplicated.

Best Answer

You have two ways: one with minipages, one without them.

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[left=2cm,right=2cm,top=1.5cm,bottom=1.5cm]{geometry}

\begin{document}

\noindent X\dotfill X % for seeing the line width

\begin{figure}[htp]

\hspace*{\fill}%
\begin{minipage}[t]{0.25\textwidth}
\centering
\vspace{0pt}
\includegraphics[width=\textwidth]{example-image-a}
\end{minipage}%
\hfill
\begin{minipage}[t]{0.45\textwidth}
\centering
\vspace{0pt}
\includegraphics[width=\textwidth]{example-image-b}
\end{minipage}%
\hspace*{\fill}

\end{figure} 

\begin{figure}[htp]

\hspace*{\fill}%
\raisebox{-\height}{\includegraphics[width=0.25\textwidth]{example-image-a}}%
\hfill
\raisebox{-\height}{\includegraphics[width=0.45\textwidth]{example-image-b}}%
\hspace*{\fill}

\end{figure} 

\end{document}

enter image description here

The addition of \vspace{0pt} sets an invisible item at the top of the minipage, which becomes the the reference point.

The second solution exploits the fact that the reference point of an image is the bottom left, so raising it by -\height pushes it down so the reference point is the upper left.

Note the % to avoid spurious spaces. You're adding too many of them.