0

I would like to have a framed box that contains some (justified) text and, on the side, a figure with its label and caption, something like this:

Exemple of what I want

I think/I read on other threads that some starting points may be the "wrapfigure" package, parbox(es) and/or minipages, but I cannot find an example where these features are used the way I want, and I cannot understand it by myself (tried the whole morning).

Anyone has any idea?

Thanks in advance, Jacorem

EDIT: what I tried so far:

  • wrap text around a figure inside a floating box In this case, the float is the whole box and the caption/label is given to it + the image is at the left of the text. I would like to give the caption to only the image and place it under the image itself. I think that one can adapt a little bit the code of this example, but
    1. since I do not want the whole box to be treated as a figure (as I said, I want the caption to be put under the figure, not somewhere else), I changed the figure environment to a minipage one, and I put the figure environment inside the sbox0 ==> compilation error
    2. cannot really understand how to use the \hangafter/\hangindent command to switch left and right
    3. I'm not sure that simple moving the caption in the \sbox0 may work
  • Floating "Tip" box using wrapfigure No figure
  • Making a boxed minipage environment No figure
  • tried mixing this and several other possibilities (including creating a single cell table) starting with code-less suggestions found online, resulting in error or very bad layout (like text overlapping itself and/or the reduced textwith perduring in the whole section)
5
  • 2
    Can you post what you've tried so far? Commented 2 days ago
  • 3
    This question is similar to: wrap text around a figure inside a floating box. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented yesterday
  • 5
    check the tcolorbox documentation, chapter 6 "Side by Side" Commented yesterday
  • 1
    @UlrikeFischer your suggestion really do it! Can you make it an answer, so that I can aprove it? Commented yesterday
  • 1
    you can self-answer if you found something useful there. Commented yesterday

4 Answers 4

5

This should be a floating object. The only problem is to set up a convenient way of coding it.

Here's a possibility: framedfigure has a standard positioning optional argument (default [!htp]) and first argument consisting of key-value pairs:

  • ratio for setting the amount of the available width for the width of the text;
  • sep for the inner margins between the object and the frame (default \fboxsep);
  • gutter for the separation between the two parts (default \columnsep);
  • align for the reciprocal alignment, can be c (default), t or b;
  • file for the name of the graphic file
  • options for additional options to \includegraphics, perhaps overriding the default width=\linewidth;
  • shortcaption for the possible table of contents short caption;
  • caption for the caption text.
\documentclass{article}
\usepackage{graphicx}

\usepackage{lipsum}% filler text

\ExplSyntaxOn

\NewDocumentEnvironment{framedfigure}{O{!htp} m +b}
 {% #1 = pos options, #2 = data, #3 = text
  \jacopo_framefigure:nnn {#1} {#2} {#3}
 }
 {}

\keys_define:nn { jacopo/framefigure }
 {
  ratio .fp_set:N = \l__jacopo_framefigure_ratio_fp,
  ratio .initial:n = 0.5,
  sep .dim_set:N = \l__jacopo_framefigure_sep_dim,
  sep .initial:n = \fboxsep,
  gutter .dim_set:N = \l__jacopo_framefigure_gutter_dim,
  gutter .initial:n = \columnsep,
  align .tl_set:N = \l__jacopo_framefigure_align_tl,
  align .initial:n = c,
  file .tl_set:N = \l__jacopo_framefigure_file_tl,
  options .tl_set:N = \l__jacopo_framefigure_options_tl,
  shortcaption .tl_set:N = \l__jacopo_framefigure_shortcaption_tl,
  caption .tl_set:N = \l__jacopo_framefigure_caption_tl,
 }

\dim_new:N \l__jacopo_framefigure_wd_dim

\cs_new_protected:Nn \jacopo_framefigure:nnn
 {
  \keys_set:nn { jacopo/framefigure } {#2}
  \dim_set:Nn \fboxsep { \l__jacopo_framefigure_sep_dim }
  \dim_set:Nn \l__jacopo_framefigure_wd_dim
   {
    \columnwidth - 2\fboxrule - 2\fboxsep - \l__jacopo_framefigure_gutter_dim
   }
  \begin{figure}[#1]
  \framebox[\columnwidth][s]
   {
    \begin{minipage}
     [\l__jacopo_framefigure_align_tl] % alignment
     {\fp_eval:n {\l__jacopo_framefigure_ratio_fp}\l__jacopo_framefigure_wd_dim} % width
    \vspace{0pt}
    #3 % the text
    \par\vspace{0pt}
    \end{minipage}
    \hfill % the gutter
    \begin{minipage}
     [\l__jacopo_framefigure_align_tl] % alignment
     {\fp_eval:n {1-\l__jacopo_framefigure_ratio_fp}\l__jacopo_framefigure_wd_dim} % width
    \centering
    \vspace{0pt}
    % the image
    \use:e
     {
      \exp_not:N \includegraphics[width=\linewidth,\l__jacopo_framefigure_options_tl]
      {\l__jacopo_framefigure_file_tl}
     }
    % the caption
    \tl_if_blank:VTF \l__jacopo_framefigure_shortcaption_tl
     {
      \caption{\l__jacopo_framefigure_caption_tl}
     }
     {
      \caption[\l__jacopo_framefigure_shortcaption_tl]{\l__jacopo_framefigure_caption_tl}
     }
    \par\vspace{0pt}
    \end{minipage}
  }% end of \framebox
  \end{figure}
}{}

\ExplSyntaxOff

\begin{document}

\begin{framedfigure}{
  ratio=0.5,
  file=example-image,
  caption={This is a caption},
}
\lipsum[1][1-8]
\end{framedfigure}

\begin{framedfigure}{
  ratio=0.5,
  gutter=0.5em,
  sep=1em,
  options={width=0.8\linewidth},
  file=example-image,
  caption={This is a caption},
}
\lipsum[1][1-8]
\end{framedfigure}

\begin{framedfigure}[!b]{
  sep=1em,
  ratio=0.6,
  align=t,
  file=example-image,
  caption={This is a caption},
}
\lipsum[1][1-8]
\end{framedfigure}

\end{document}

Getting a pleasant result requires experiments with the various parameters.

output

3

One option is to use a tabular:

\documentclass{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{capt-of}
\usepackage{lipsum}
\begin{document}
\begin{tabular}{|p{7cm}p{7cm}|}
\hline
\lipsum[1] & {\vskip2mm\centering\includegraphics[width=6.5cm,valign=t]{example-image}\captionof{figure}{this is a representation of an atom}}\\
\hline
\end{tabular}
\end{document}

Result:

text and image side by side

The tabular has two p (paragraph) columns which allow multiple lines. You do need to set the width manually. The image is top-aligned using valign=t from the adjustbox package and slightly lowered using \vskip before the image. The caption is added using \captionof from the package capt-of. The image and caption are centered in the second column of the tabular using \centering in a {} group.

2
0

@UlrikeFischer comment to the original question solved my problem:

the tcolorbox package allow one to create top/down or side-by-side framed text and floats. The visual options allow one also to change the shape and color of the frame.

One of the limit of this solution (at least, I think that this is not possible) is that one cannot really create "wrapping" figures/frames, where text start on the whole line, then appear only to the side and finally on the whole line again.

1
  • 3
    @Jacopo-Redemondina, please share the code for your solution Commented yesterday
0

It is not so complicated to adapt the answer to "wrap text around a figure inside a floating box" move the caption below the image:

\documentclass{article}
\usepackage{mwe}
\newlength{\imagewidth}
\begin{document}

\begin{figure}
\fbox{\begin{minipage}{\dimexpr \textwidth-2\fboxsep-2\fboxrule}
\settowidth{\imagewidth}{\includegraphics[scale=0.25]{example-image}}% get
% width
\sbox0{\parbox[b]{\imagewidth}{%
    \includegraphics[scale=0.25]{example-image}
    \caption{Floating Box}\label{fig:example}
  }}
\hangafter \numexpr -\ht0/\baselineskip -1\relax% adds one blank lines at bottom
\hangindent \dimexpr \wd0 + 1em\relax% adds 1em margin on right
\vbox{\raisebox{-\height}[0pt][0pt]{\box0}}\vspace{-1ex}% overlap image
\lipsum[1]
\end{minipage}}
\end{figure}

\end{document}

box with figure and wrapping text

And if you don't want it to flow, you either can use package float and \begin{figure}[H]…\end{figure} or remove the figure environment and use \captionof{figure}{…} (needs either a package like caption or tocbasic or a class like scrartcl) instead of \caption:

\documentclass{article}
\usepackage{tocbasic}% or caption or capt-of
\usepackage{mwe}
\newlength{\imagewidth}
\begin{document}

\noindent\fbox{\begin{minipage}{\dimexpr \textwidth-2\fboxsep-2\fboxrule}
\settowidth{\imagewidth}{\includegraphics[scale=0.25]{example-image}}% get
% width
\sbox0{\parbox[b]{\imagewidth}{%
    \includegraphics[scale=0.25]{example-image}
    \captionof{figure}{Floating Box}\label{fig:example}
  }}
\hangafter \numexpr -\ht0/\baselineskip -1\relax% adds one blank lines at bottom
\hangindent \dimexpr \wd0 + 1em\relax% adds 1em margin on right
\vbox{\raisebox{-\height}[0pt][0pt]{\box0}}\vspace{-1ex}% overlap image
\lipsum[1]
\end{minipage}}

\end{document}

box with figure and floating text

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.