# Vertical align (top) graphic and top of text-line

I want to align the top of an image, and the top of a line of text. Below is a MWE with the current setup (using a placeholder "graphic" but the effect seems the same) and the desired behavior.

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\begin{minipage}[t]{0.5\textwidth}
%\includegraphics[height=0.4in]{theimage.png}
\rule{0.95\textwidth}{0.5in}
\end{minipage}%
\begin{minipage}[t]{0.5\textwidth}
sometext to fill the line
\end{minipage}

\end{document}


MWE behavior:

Desired behavior:

A simple way to fix your problem is by adding \vspace{0pt} as soon as a minipage is created:

\documentclass{article}

\usepackage{graphicx}

\begin{document}
\begin{minipage}[t]{0.5\textwidth}
\vspace{0pt}
%\includegraphics[height=0.4in]{theimage.png}
\rule{0.95\textwidth}{0.5in}
\end{minipage}%
\begin{minipage}[t]{0.5\textwidth}
\vspace{0pt}
sometext to fill the line
\end{minipage}
\end{document}

• This also feels a little hacky (similar to my answer below) but it works. Can you explain why adding the zero-height vspace works in this case? – amoodie Aug 22 '18 at 13:57

You could use adjustbox, as marmot suggested in his comment.

Moreover, instead of using minipages, you could use a tabularx.

\documentclass{article}
\usepackage{tabularx}
\usepackage{graphicx}
\begin{document}
\noindent\begin{tabularx}{\linewidth}{@{}X@{\hspace{2pt}}X@{}}
\includegraphics[height=0.4in, width=0.475\textwidth,valign=t]{example-image}
&
sometext to fill the line \\
\end{tabularx}
\end{document}


A potential workaround I found is that if I know the height of the image, I can add a negative vspace. This seems kind of hacky though and I'm wondering if there is a cleaner solution

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\begin{minipage}[t]{0.5\textwidth}
% \includegraphics[height=0.4in]{theimage.png}%
\rule{0.95\textwidth}{4\baselineskip}
\end{minipage}%
\begin{minipage}[t]{0.5\textwidth}
\vspace*{-4\baselineskip}
sometext to fill the line
\end{minipage}

\end{document}