6

I want two text boxes to appear next to a photo which is 5cm wide. The combined height of the boxes may be greater than or less than the height of the photo.

If the combined height is less than the photo, I want the text boxes aligned vertically with the top and the bottom of the photo thus:

+---------------------------------------+  +------------+
| Text1 text1 text1 text1 text1 text1   |  |            |
| text1 text1 text1 text1 text1.        |  |            |
+---------------------------------------+  |            |
                                           |   Photo    |
                                           |            |
+---------------------------------------+  |            |
| Text2 text2 text2 text2 text2 text2   |  |            |
| text2 text2 text2 text2 text2.        |  |            |
+---------------------------------------+  +------------+

If the combined height is greater than the photo, I want the two boxes below each other thus:

+---------------------------------------+  +------------+
| Text1 text1 text1 text1 text1 text1   |  |            |
| text1 text1 text1 text1 text1 text1   |  |            |
| text1 text1 text1 text1 text1 text1   |  |            |
| text1 text1 text1 text1 text1 text1   |  |   Photo    |
+---------------------------------------+  |            |
+---------------------------------------+  |            |
| Text2 text2 text2 text2 text2 text2   |  |            |
| text2 text2 text2 text2 text2 text2   |  |            |
| text2 text2 text2 text2 text2 text2   |  +------------+
| text2 text2 text2 text2 text2.        |
+---------------------------------------+

Is there any way to achieve this automatically? And if not, how can I best achieve what is shown in the first illustration? (I've experimented with both tabular/multirow setups and with floats, but I just can't get it right.)

Edit:

This is one of the things I've tried to no avail (replace 1988.jpg with an appropriate filename):

\documentclass{report}
\usepackage{graphicx}
\usepackage{multirow}

\begin{document}

\begin{tabular}{l r}
\fbox{\parbox[t]{8cm}{Text1 text1 text1 text1 text1 text1 text1 text1 text1 text1
text1 text1 text1 text1 text1.}}
 & \multirow{3}{*}{\parbox[t]{5cm}{\includegraphics[width=5cm,height=10cm]{1988.jpg}
}} \\
\parbox{8cm}{\vfill} & \\
\fbox{\parbox[b]{8cm}{Text2 text2 text2 text2 text2 text2 text2 text2 text2 text2
text2 text2 text2 text2 text2.}} & \\
\end{tabular}

\end{document}
2
  • Could you add a minimal working example of what you've tried so far?
    – Joseph Wright
    May 5, 2014 at 15:09
  • I've added some Latex code to the question. You'll have to set an appropriate filename for the photo.
    – oz1cz
    May 5, 2014 at 15:31

4 Answers 4

6

You can box the text and the image, measure their heights and then act accordingly:

\documentclass{article}
\usepackage{graphicx}
\usepackage{adjustbox}

\newlength\PhotoWd
\setlength\PhotoWd{5cm}
\newlength\PhotoGap
\setlength\PhotoGap{20pt}
\newlength\TextWd
\setlength\TextWd{\dimexpr\textwidth-\PhotoWd-\PhotoGap\relax}

\newlength\TextHt
\newlength\ImageHt
\newlength\MaxHt

\newsavebox\TextBox
\newsavebox\ImageBox

\newcommand\PlaceTextImage[4][]{%
  \begin{lrbox}{\TextBox}
    \begin{minipage}{\linewidth}
    \fbox{\parbox{\dimexpr\TextWd-2\fboxsep-2\fboxrule\relax}{#2}}\par\medskip
    \fbox{\parbox{\dimexpr\TextWd-2\fboxsep-2\fboxrule\relax}{#3}}
    \end{minipage}
  \end{lrbox}%
  \savebox\ImageBox{\includegraphics[#1]{#4}}%
  \setlength\TextHt{\dimexpr\ht\TextBox+\dp\TextBox\relax}%  
  \setlength\ImageHt{\dimexpr\ht\ImageBox+\dp\ImageBox\relax}%  
  \setlength\MaxHt{\TextHt}
  \ifdim\ImageHt>\TextHt\relax
    \setlength\MaxHt{\ImageHt}
  \fi%
  \par\medskip\noindent
  \begin{minipage}[t][\MaxHt][t]{\TextWd}
  \fbox{\parbox[t]{\dimexpr\TextWd-2\fboxsep-2\fboxrule\relax}{#2}}%
  \ifdim\ImageHt>\TextHt\relax
    \par\vfill
  \else
    \par\medskip
  \fi%  
  \fbox{\parbox[t]{\dimexpr\TextWd-2\fboxsep-2\fboxrule\relax}{#3}}
  \end{minipage}\hspace*{\PhotoGap}%
  \begin{minipage}[t][\MaxHt][t]{\PhotoWd}
  \adjustbox{valign=t}{\usebox\ImageBox}
  \end{minipage}\par\medskip%
}

\begin{document}

Text before

\PlaceTextImage[width=\PhotoWd,height=200pt]{some test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text}{some other text  test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text}{example-image-b}

Text after and before 

\PlaceTextImage[width=\PhotoWd,height=40pt]{some test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text}{some other text  test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text test text}{example-image-b}

Text after

\end{document}

enter image description here

\PlaceTextImage has three mandatory arguments:

\PlaceTextImage{<upper text>}{<lower text>}{<image file>}

and an optional argument allowing you to specify options for \includegraphics.

Using the lengths \PhotoWd and \PhotoGap you can change the width for the photo and the gap between text and photo; the width for the text boxes is calculated automatically.

2
  • Thank you. That is very useful. However, there is a problem with text following \PlaceTextImage. If you add a line containing "Bla bla bla" just before \end{document} in your example, you'll see what I mean.
    – oz1cz
    May 6, 2014 at 7:44
  • @oz1cz Yes, you're right. I've fixed the code in my updated answer. May 6, 2014 at 12:25
4

This can be done using a number of minipages to wrap and align the text appropriately.

\documentclass{report}
\usepackage[demo]{graphicx} % Remove [demo] for final version
\usepackage[export]{adjustbox}
\usepackage{calc,etoolbox}
\usepackage{fullpage} % Increase margins for these specific widths

\begin{document}

\newcommand{\imagebox}{\includegraphics[width=5cm,height=10cm,valign=t]{1988.jpg}}

\newcommand{\textboxes}{
     \noindent\begin{minipage}[t]{\linewidth}
     \fbox{\parbox[t]{8cm}{
     Text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
    text1 text1 text1 text1 text1.}}
    \par\vspace{1ex}\vfill
    \fbox{\parbox[b]{8cm}{
    Text2 text2 text2 text2 text2 text2 text2 text2 text2
    text2 text2 text2 text2 text2 text2 text2 text2 text2
    text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
    text2 text2 text2 text2 text2 text2 text2 text2 text2
    text2 text2 text2 text2 text2.}}
    \end{minipage}
}

% Calculate height of image and text boxes
\newlength{\imageboxheight}
\settototalheight{\imageboxheight}{\imagebox}
\newlength{\textboxesheight}
\settototalheight{\textboxesheight}{\textboxes}
\newlength{\maxTextBoxes}
\setlength{\maxTextBoxes}{\ifdimgreater{\textboxesheight}{\imageboxheight}{\textboxesheight}{\imageboxheight}}

% Typeset content
\noindent\begin{minipage}[t][\maxTextBoxes]{\textwidth}
    \noindent\begin{minipage}[t][\imageboxheight]{9cm} 
    \textboxes\end{minipage}\begin{minipage}[t]{6cm}
    \imagebox
    \end{minipage}
\end{minipage}\vspace{\baselineskip} % A bit of extra space

% Following text starts after both image and text
Next paragraph starts here.

\end{document}

case1

case2

2
  • It doesn't work for me. In the first example I get the two text blocks directly under each other. I'm using your code without any modifications.
    – oz1cz
    May 6, 2014 at 7:46
  • I've posted a slight modification to your answer.
    – oz1cz
    May 6, 2014 at 8:19
1

Here's an interface for such a construction.

\documentclass{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{environ}
\usepackage{calc}

\newsavebox{\photobox}
\newsavebox{\upperbox}
\newsavebox{\lowerbox}
\newsavebox{\tempbox}
\newlength{\photodepth}
\newlength{\boxesdepth}
\newlength{\photowidth}

\newenvironment{boxesandphoto}[2][]
 {\vfuzz=2pt
  \par\addvspace{\topsep}
  \sbox{\photobox}{\includegraphics[#1,valign=t]{#2}}%
  \setlength{\photowidth}{\wd\photobox}%
  \setlength{\photodepth}{\dp\photobox}%
 }
 {%
  \maketextboxes\centering
  \ifdim\photodepth<\boxesdepth \photodepth=\boxesdepth \fi
  \addtolength\photodepth{\ht\photobox}%
  \addtolength\photodepth{\fboxrule+\fboxsep}
  \leavevmode
  \vtop to\photodepth{
    \vspace*{0pt}
    \unvbox\tempbox
  }\hfill
  \vtop to\photodepth{
    \vspace*{0pt}
    \box\photobox
    \vss
  }
  \par\addvspace{\dimexpr\topsep+\medskipamount}
 }

\newcommand{\maketextbox}[2]{%
  \sbox{\tempbox}{%
    \fbox{%
      \begin{minipage}[#1]{\textwidth-\photowidth-2\fboxsep-2\fboxrule-1em}
      \BODY\par
      \vspace{0pt}
      \end{minipage}%
    }%
  }
  \global\setbox#2=\box\tempbox
}

\NewEnviron{toptext}{\maketextbox{t}{\upperbox}}
\NewEnviron{bottomtext}{\maketextbox{b}{\lowerbox}}

\newcommand{\maketextboxes}{%
  \setbox\tempbox=\vtop{
    \box\upperbox
    \vspace{2ex plus 1fill}
    \box\lowerbox
    \kern\fboxrule\kern\fboxsep
  }%
  \setlength{\boxesdepth}{\dp\tempbox}%
  \addtolength{\boxesdepth}{-\fboxsep-\fboxrule}
}

\begin{document}

something above

\begin{boxesandphoto}[height=3cm,width=3cm]{example-image-b}
\begin{toptext}
some test text test text test text test text test text test text 
test text test text test text test text test text test text test 
text test text test text
\end{toptext}
\begin{bottomtext}
some other text test text test text test text test text test text 
test text test text test text test text test text test text test text 
test text test text test text test text test text test text test text 
test text test text test text test text test text
\end{bottomtext}
\end{boxesandphoto}

something in the middle

\begin{boxesandphoto}[height=5cm,width=3cm]{example-image-b}
\begin{toptext}
some test text test text test text test text test text test text 
test text test text test text test text test text test text test 
text test text test text
\end{toptext}
\begin{bottomtext}
some other text test text test text test text test text test text 
test text test text test text test text test text test text test text 
test text test text test text test text test text test text test text 
test text test text test text test text test text
\end{bottomtext}
\end{boxesandphoto}

something at the bottom

\end{document}

enter image description here

0

Here's a slight modification of cslstr's solution. Here I avoid the minipage environment in the \textboxes command. This works for me.

\documentclass{report}
\usepackage[demo]{graphicx} % Remove [demo] for final version
\usepackage[export]{adjustbox}
\usepackage{calc,etoolbox}
\usepackage{fullpage} % Increase margins for these specific widths

\begin{document}

\newcommand{\imagebox}{\includegraphics[width=5cm,height=10cm,valign=t]{1988.jpg}}

\newcommand{\textboxes}{
     \fbox{\parbox[t]{8cm}{
     Text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
     text1 text1 text1 text1 text1 text1 text1 text1 text1
    text1 text1 text1 text1 text1.}}
    \par\vspace{1ex}\vfill
    \fbox{\parbox[b]{8cm}{
    Text2 text2 text2 text2 text2 text2 text2 text2 text2
    text2 text2 text2 text2 text2 text2 text2 text2 text2
    text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
%   text2 text2 text2 text2 text2 text2 text2 text2 text2
    text2 text2 text2 text2 text2 text2 text2 text2 text2
    text2 text2 text2 text2 text2.}}
}

\newcommand{\textboxespaged}{
     \noindent\begin{minipage}[t]{\linewidth}
     \textboxes%
     \end{minipage}
}

% Calculate height of image and text boxes
\newlength{\imageboxheight}
\settototalheight{\imageboxheight}{\imagebox}
\newlength{\textboxesheight}
\settototalheight{\textboxesheight}{\textboxespaged}
\newlength{\maxTextBoxes}
\setlength{\maxTextBoxes}{\ifdimgreater{\textboxesheight}{\imageboxheight}{\textboxesheight}{\imageboxheight}}

% Typeset content
\noindent\begin{minipage}[t][\maxTextBoxes]{\textwidth}
    \noindent\begin{minipage}[t][\imageboxheight]{9cm} 
    \textboxes\end{minipage}\begin{minipage}[t]{6cm}
    \imagebox
    \end{minipage}
\end{minipage}\vspace{\baselineskip} % A bit of extra space

% Following text starts after both image and text
Next paragraph starts here.

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .