2

I've read through a bunch of questions on that topic but I don't understand why their solutions don't work here.

I have two minipages that cover 50 % of the width each. One contains text, the other contains an image. The text is of much smaller height than the image. I want the text and the image both aligned to the top. Other questions provide [t] as the solution, but I only get this:

enter image description here

Here is my code:

\documentclass{exam}
\usepackage{graphicx}

\begin{document}
\begin{minipage}[t]{0.5\linewidth}
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
\end{minipage}
\begin{minipage}[t]{0.5\linewidth}
\begin{center}
    \includegraphics[width=\linewidth]{Peter_the_Hermit.jpg}
\end{center}
\end{minipage}

\end{document}

(The image is a random image from Wikipedia, found here)

1
  • 2
    you need a % after \end{minipage} so you do not have a space between them and use \vspace*{-5pt} (choose an amount) before \includegraphics you have aligned on the top row of each minipage but the image is like a big letter so put a vspace above it so the first thing in the minipage is not the image Oct 21, 2023 at 10:01

2 Answers 2

3

Use a \vspace above the image to move the reference point of the minipage to the top. You can adjust the value for fine tuning.

\documentclass{exam}
\usepackage{graphicx}

\begin{document}
\begin{minipage}[t]{0.5\linewidth}
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
\end{minipage}
\begin{minipage}[t]{0.5\linewidth}
\centering \vspace{-0.7\baselineskip}
\includegraphics[width=\linewidth]{example-image}
\end{minipage}

\end{document}

enter image description here

2
  • How did you come up with the number 0.7? This works pretty well but to me it seems like a trail and error solution. Is there no "exact" way to align the image with the top of the first line? Oct 21, 2023 at 13:18
  • That is the default height of a \strut and so normally a good default for the height of a line (the part above the baseline). In the latex code you can find \setbox\strutbox\hbox{\vrule\@height.7\baselineskip \@depth.3\baselineskip\@width\z@}. Oct 21, 2023 at 13:39
1

You can use adjustbox.

\documentclass{article}% any class is allowed
\usepackage{graphicx}
\usepackage[export]{adjustbox}

\begin{document}

\noindent
\begin{minipage}[t]{0.495\linewidth}
  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
  nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
  erat, sed diam voluptua. At vero eos et accusam et justo duo
  dolores et ea rebum.
\end{minipage}\hfill
\includegraphics[width=0.495\linewidth,valign=t]{example-image}

\end{document}

enter image description here

Another possibility is to add \vspace{0pt} in the minipage and T alignment. This way, the top of the picture is aligned to the top of the “L”.

\documentclass{article}% any class is allowed
\usepackage{graphicx}
\usepackage[export]{adjustbox}

\begin{document}

\noindent
\begin{minipage}[t]{0.495\linewidth}
  \vspace{0pt}\par
  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
  nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
  erat, sed diam voluptua. At vero eos et accusam et justo duo
  dolores et ea rebum.
\end{minipage}\hfill
\includegraphics[width=0.495\linewidth,valign=T]{example-image}

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