[Tex/LaTex] Vertically align text to graphic

boxesgraphicsvertical alignment

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.This is what I want to prevent.

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?
The ultimate goal.

Best Answer

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:

MWE

\documentclass[a4paper]{article}
\usepackage[export]{adjustbox}%
\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:

MWE

\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}
Related Question