# Vertically align text to graphic

At first this might look like a duplicate, but I couldn't find any solution, although I tried various (combinations of) vspace, minipage and box-commands.

Situation: I have some pictures (all have the same size, about 0.4 textwidth) and text explaining them. I want to place the text using wrapfigure next to and around them. (Ideally the pictures would always be aligned left on uneven pages and right on even ones, but thats another problem.)

I tried minipages, but they do overlap like the text. \hfill didnt do anything either.

Problem: Sometimes the height of the explanations is less than the height of the picture, in which cases the following text is next to wrong picture.

\documentclass[a4paper]{article}
\usepackage{a4wide}
\usepackage{blindtext}
\usepackage{wrapfig}

\begin{document}
\begin{wrapfigure}{l}{0.2\linewidth}
\rule{\linewidth}{5 cm}
\end{wrapfigure}
\blindtext

\begin{wrapfigure}{l}{0.2\linewidth}
\rule{\linewidth}{5 cm}
\end{wrapfigure}
\textbf{Text that belongs to the second box. Please align me there.}\\
\blindtext

\end{document}


Question: How do I get the bold text to align to the second box (automatically), without vspace? Remember that my explanations are unequal in length, not like the two blindtexts, so a vspace{x} will not always work.

EDIT: (Clarification)

I don't want to use captions, because there will be other (much smaller) pictures to wrap text around, too. It is not the problem to align the first line of each explanation, but to prevent the overlapping of pictures if the text is shorter. I've included an example.

I tried the following, but I can't get the minipage to have minimal height equal or higher than the height of the image.

\documentclass[a4paper]{article}
\usepackage{a4wide}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{blindtext}
\usepackage{ifthen}
\usepackage{wrapfig}
\usepackage{graphicx}

\begin{document}
\begin{minipage}{\linewidth}
\begin{wrapfigure}{l}{0.4\linewidth}
\vspace{-\baselineskip}
\ifthenelse{\isodd{\thepage}}{\hspace*{-0\textwidth}}{\hspace*{1\textwidth}}
\includegraphics[width=1\linewidth]{pic.jpg}
\end{wrapfigure}
\blindtext
\end{minipage}
%   \vspace{2 cm} % Now they are no longer overlapping. I don't want to have to do this. As might have to adjust the next command.
\vspace{1 cm} % This is some standard space to get the text above away from the image below.

\begin{minipage}{\linewidth}
\begin{wrapfigure}{l}{0.4\linewidth}
\vspace{-\baselineskip}
\ifthenelse{\isodd{\thepage}}{\hspace*{-0\textwidth}}{\hspace*{1\textwidth}}
\includegraphics[width=1\linewidth]{pic.jpg}
\end{wrapfigure}
\blindtext
\end{minipage}
\vspace{0.1 cm}
\end{document}


The follow up problem is that in minipages the \ifthenelse{\isodd{\thepage}} doesn't seem to work. How do I get the information into the minipage?

I included a picture of where I want to get for further clarification. I know LaTeX is neither designed nor intended for something like this, but OpenOffice stops to work over 100 MB file size, so I have to look for alternatives. Is this even possible?

• Welcome to TeX.SX! – user31729 Apr 12 '15 at 20:14
• Have you tried something like \capbeside as in the example in Caption on the side of a figure? – Fato39 Apr 12 '15 at 20:54
• From the manual: It is possible that a non-floating wrapfigure will be forced to float when an earlier one is still being processed. A warning will be written in that case. – John Kormylo Apr 12 '15 at 22:23

I am not sure if you are searching really for long side captions, but otherwise, this is a way using minipages for the main text and adjustbox package to have top aligned images:

\documentclass[a4paper]{article}
\usepackage{lipsum,graphicx}
\parskip1em

\def\capR#1#2{\includegraphics[width=.3\linewidth,valign=t]{#1}%
\hfill\begin{minipage}[t]{.6\textwidth}#2\end{minipage}}

\def\capL#1#2{\begin{minipage}[t]{.6\textwidth}#2\end{minipage}
\hfill\includegraphics[width=.3\linewidth,valign=t]{#1}}

\begin{document}

\capR{example-image-9x16}{\lipsum[2]}

\capR{example-image-a}{\lipsum[3]}

\capL{example-image-a}{\lipsum[4]}

\end{document}


For the secondary question, you can use an \ifoddpage conditional of the changepage or ifoddpage packages (here a MWE with the former).

Edit: If wrapping text is needed, another approach is a wrapfigure inside a minipage. This approach also solve messing you with \ifoddpage testings using the I position and twoside. The problem then is when the text is too short. Rhen the height of the minipage environment will be less that the height of the image. But fortunately you can force the height of the minipages. Example:

\documentclass[a4paper,twoside]{article}
\usepackage{lipsum,graphicx,calc}
\usepackage{wrapfig}
\parskip1em\parindent0pt
\intextsep0pt

\newlength\gh \newlength\gw

\def\ImgNW#1#2{
\def\mygraphic{\includegraphics[width=.25\linewidth]{#1}}
\setlength\gh{\heightof{\mygraphic}}
\setlength\gw{\widthof{\mygraphic}}
\begin{minipage}[t][\gh+1em]{\textwidth}
\begin{wrapfigure}{I}[0pt]{0.25\textwidth}
\includegraphics[width=\linewidth]{#1}
\end{wrapfigure}#2
\end{minipage}\par}

\def\ImgW#1#2{
\def\mygraphic{\includegraphics[width=.25\linewidth]{#1}}
\setlength\gh{\heightof{\mygraphic}}
\setlength\gw{\widthof{\mygraphic}}
\begin{minipage}[t]{\textwidth}
\begin{wrapfigure}{I}[0pt]{0.25\textwidth}
\includegraphics[width=\linewidth]{#1}
\end{wrapfigure}#2
\end{minipage}\par}

\begin{document}

\ImgNW{example-image-9x16}{\lipsum[2]}
\ImgW{example-image-a}{\parskip1em\lipsum[3-4]}
\ImgW{example-image-b}{\lipsum[4]}
\newpage
\ImgW{example-image-c}{\lipsum[5]}
\ImgNW{example-image-9x16}{\lipsum[6]}
\ImgW{example-image-1x1}{\lipsum[7]}

\end{document}

• This looks very nice. Can you make the text wrap around the images A? – Sebastian Apr 14 '15 at 19:02
• @Sebastian Not with this approach, but see another in the edited answer. – Fran Apr 14 '15 at 21:25

Normally the figure is lower than the start of the text, although obviously the start of the document is an exception. So to align the top of the figure to the top of the text you will need to either raise the figure or lower the text by \baselineskip.

One can force the wrapfigure to terminate using \WFclear, but you sill need to space down far enough to not overlap the figure.

\documentclass[a4paper]{article}
\usepackage{a4wide}
\usepackage{blindtext}
\usepackage{wrapfig}

\newlength{\oldparindent}

\begin{document}
\noindent\rule{\linewidth}{1pt}

\begin{wrapfigure}{l}{0.2\linewidth}
\vspace{-\baselineskip}%
\rule{\linewidth}{5 cm}
\end{wrapfigure}
\noindent\parbox[t][\dimexpr 5cm+\baselineskip][t]{\linewidth}%
{\parindent=15pt \blindtext}
\WFclear

\begin{wrapfigure}{l}{0.2\linewidth}
\vspace{-\baselineskip}%
\rule{\linewidth}{5 cm}
\end{wrapfigure}
\textbf{Text that belongs to the second box. Please aligne me there.}\\
\blindtext

\end{document}


Actually, if you don't plan on wrapping the text around the figure, you'd be better off NOT using wrapfigure.

\documentclass[a4paper]{article}
\usepackage{a4wide}
\usepackage{blindtext}
\usepackage{wrapfig}

\newlength{\oldparindent}

\begin{document}
\noindent\rule{\linewidth}{1pt}

\noindent\raisebox{\dimexpr 0.6\baselineskip-\height}{\rule{0.2\linewidth}{5 cm}}\hfill
\begin{minipage}[t]{\dimexpr 0.8\linewidth - \columnsep}
\parindent=15pt
\blindtext
\end{minipage}

\bigskip
\begin{wrapfigure}{l}{0.2\linewidth}
\vspace{-\baselineskip}%
\rule{\linewidth}{5 cm}
\end{wrapfigure}
\textbf{Text that belongs to the second box. Please aligne me there.}\\
\blindtext

\end{document}

• Thanks for your answer, but trial and error would mean I have to align more than 150 pictures, not counting changing the length of the explanations afterwards. This is exact the type of work I want to let LaTeX do for me. – Sebastian Apr 14 '15 at 19:06
• If you know how big the figure is, you can put the text into a \parbox the same size (plus a margin). But if you have more than one paragraph, then you're just SOL. – John Kormylo Apr 14 '15 at 20:52