2

I've created a new float element to define examples:

\usepackage{float}
\floatstyle{plain}
\newfloat{example}{thp}{lop}
\floatname{example}{\textsc{Example}}

I want to set my example in an frame. Usually I use the mdframed or framed environment to set figures in frames.

The current problem is that I can't wrap my example with the caption. I can only wrap the example.

\begin{example}
    \begin{mdframed}{
        My example
        \caption{content of the caption}
    }
    \end{mdframed}
\end{example}

Is there a way to set the example and the caption in a frame like with a figure or a table ?

With a minimal example to see the difference between a figure and my example :

\documentclass[a4paper,twoside,12pt]{article}

\usepackage[francais]{babel} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[labelsep=endash]{caption}
\usepackage{mdframed}

\usepackage{float}
\floatstyle{plain}
\newfloat{example}{thp}{lop}
\floatname{example}{\textsc{Example}}

\begin{document}

% The figure
\begin{figure}
    \begin{mdframed}{
        My figure
        \caption{content of the caption}
    }
    \end{mdframed}
\end{figure}

%The example
\begin{example}
    \begin{mdframed}{
        My example
        \caption{content of the caption}
    }
    \end{mdframed}
\end{example}

\end{document}
1
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Jun 3, 2015 at 13:12

2 Answers 2

1

Using my own MWE, rather than building on yours. I create the figure content, with a \captionof caption, and place it in a temporary \vbox. Then I place a frame around the recalled \vbox. I need the \makebox[\textwidth]{...} construct, since the framed box is now wider than \textwidth.

\documentclass{article}
\usepackage{caption,xcolor,graphicx,lipsum}
\begin{document}
\lipsum[1]

\setbox0=\vbox{
\centering
\includegraphics[width=2in]{example-image}
\captionof{figure}{xyz}
}
\begin{figure}[ht]
\makebox[\textwidth]{\fboxrule=2pt\textcolor{red}{\fbox{\box0}}}
\end{figure}
\end{document}

enter image description here

2

Another solution with tcolorbox.

A new kind of float (example) is declared with float and also a framed environment for it (texample). This one is made with a floating tcolorbox (float option). It's floating position is controlled with floatplacement option, which by default is htb. The caption is added with \captionof{example}{...} command.

\documentclass[a4paper,twoside,12pt]{article}

\usepackage[francais]{babel} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[labelsep=endash]{caption}

\usepackage[most]{tcolorbox}

\usepackage{lipsum}

\newtcolorbox{texample}[1][]{float, notitle, #1}

\usepackage{float}
\floatstyle{plain}
\newfloat{example}{thp}{lop}
\floatname{example}{\textsc{Example}}

\begin{document}

\lipsum[2]


\begin{figure}
\centering
\includegraphics[width=.25\linewidth]{example-image}
        \caption{content of the caption}
\end{figure}


\begin{texample}[floatplacement=t]
\centering
\includegraphics[width=.25\linewidth]{example-image}
        \captionof{example}{content of the caption}
\end{texample}

\begin{texample}
\centering
\includegraphics[width=.25\linewidth]{example-image}
        \captionof{example}{content of the caption}
\end{texample}
\end{document}

enter image description here

3
  • The aim is also to have the Example 1 instead of Figure 1. I want to differentiate the two elements. Jun 3, 2015 at 15:18
  • @Klapmox I've updated the answer to distinguish between both environments.
    – Ignasi
    Jun 3, 2015 at 16:27
  • @thibaut-guirimand this may help : tex.stackexchange.com/a/581645/28926
    – 3isenHeim
    Feb 3, 2021 at 7:30

You must log in to answer this question.

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