11

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.

0

3 Answers 3

14

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.

5
  • I've chosen this as best answer because it seem not be an scale issue, I've noticed what could be different sizes of images themselves, and your second option seems to be closer of get what I want, still I would like control vertical alignment. Could I switch -\height option to any other vertical measurement? To push down A-pic, I mean.
    – jfernandz
    Commented Aug 21, 2016 at 21:41
  • 1
    @JFernan Yes, any length can be used; \height refers to the height of the box to be raised (or lowered).
    – egreg
    Commented Aug 21, 2016 at 21:42
  • Other issue what I've got with second way is to use captions.
    – jfernandz
    Commented Aug 21, 2016 at 21:52
  • Adding \vspace{0pt} looks like a hack. Is this really intended in latex or rather a bug? Thanks for the advice.
    – Nikodem
    Commented Mar 17, 2023 at 16:00
  • 1
    @Nikodem It's a feature: a top aligned minipage has its reference point at the first item inside. If it's \vspace{0pt} the precise top left; with text it's at the baseline of the first line.
    – egreg
    Commented Mar 17, 2023 at 16:57
2

You can take advantage of graphbox package which load graphicx and add key align to modify vertical alignment of image, in your case you need align=t for both.

\documentclass[8pt,a4paper,dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphbox}
\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[width=3cm,align=t]{example-image-a}
\end{minipage}
{\hfill}
\begin{minipage}[t]{0.55\textwidth}
\centering
\includegraphics[width=5cm,align=t]{example-image-b}
\end{minipage}
{\hfill}
\end{figure} 

\end{document} 
1

You could load the adjustbox package which provides many facilities for adjusting content:

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

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


\end{document}

Here I used the \adjincludegraphics macro instead of \includegraphics, with the valign key set to t. The result is as follows:

enter image description here

You must log in to answer this question.

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