# How to align text and picture with parbox?

I would like to align the top of the picture with the top of the text using parbox. When I compile the following code with

LuaTeX, Version 1.10.0 TeX Live 2019/W32TeX format=lualatex 2020.2.26

the picture is vertically centered.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\noindent
\lipsum[1]

\parbox{0.5\linewidth}
{\lipsum[2]}
\parbox{0.5\linewidth}
{\begin{tikzpicture}
\fill (0,0) rectangle (2,2);
\end{tikzpicture}}

\lipsum[3]
\end{document}

• Don't you want to use wrapfig? – egreg Feb 28 '20 at 20:30
• @egreg Using wrapfig is fine with me. My question was definitively about the efficient usage of parbox. – ePaulard Feb 29 '20 at 9:27

Note that \ht\strutbox is the distance from the baseline to the top of a line of text (technically, the top of a \strut).

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]

\parbox[t]{0.5\linewidth}
{\lipsum[2]}\hfil
\raisebox{\dimexpr \ht\strutbox-\height}%
{\begin{tikzpicture}
\fill (0,0) rectangle (3,3);
\end{tikzpicture}}

\lipsum[3]
\end{document}


\parbox provides an optional first argument that sets the alignment anchor; make the second \parbox be aligned at the [t]op:

\documentclass{article}

\usepackage{tikz,lipsum}

\begin{document}

\lipsum[1]

\noindent
\parbox{0.5\linewidth}
{\strut\lipsum[2]\strut}%
\parbox[t]{0.5\linewidth}
{\begin{tikzpicture}
\fill (0,0) rectangle (3,3);
\end{tikzpicture}}

\lipsum[3]

\end{document}

• In my initial tentative I used the [t] argument on both parboxes and the result was not the one expected. Now it works ! – ePaulard Feb 28 '20 at 17:39
• When I tried to compile this the box had a lower alignment with yours (closer aligned to the auc- than the so-). One way to remedy this would be to use a \raisebox, as the top box is vertically aligned to the baseline(?) – oliversm Feb 28 '20 at 17:41
• Note that the [t] option refers to the first baseline, which is at the bottom of the tikzpicture. In this case you are aligning the bottom of the tikzpicture with the center of the text. \raisebox is a better approach for images. – John Kormylo Feb 29 '20 at 3:45
• @Werner I've just updated the code with a smaller picture. As mentioned above, the top of the picture is not aligned with the top of the text. – ePaulard Feb 29 '20 at 14:08
• @ePaulard: How about John's solution, using \raisebox{\dimexpr\ht\strutbox-\height}{...}? – Werner Feb 29 '20 at 18:15