8

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:

MWE behavior

Desired behavior:

enter image description here

amoodie
  • 183

3 Answers3

8

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}
\usepackage[export]{adjustbox}
\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}

enter image description here

CarLaTeX
  • 62,716
5

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

Output

\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}
M. Al Jumaily
  • 4,035
  • 2
  • 11
  • 26
  • 4
    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
0

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}
amoodie
  • 183