0

I copy pasted an environment I found around here to create an "Example" section as defined below:

\usepackage[framemethod=tikz]{mdframed}
\usepackage{amssymb}
\usepackage{etoolbox}

\newlength\mylen

\newcommand{\xxqed}[1]{% \settowidth\mylen{$#1$}% \vskip-2\baselineskip\hspace*{-\mylen}\llap{\hbox{\ensuremath{#1}}}% }

\newcommand{\xqed}[1]{% \leavevmode\unskip\penalty9999 \hbox{}\nobreak\hfill \quad\hbox{\ensuremath{#1}}}

\AtBeginEnvironment{example}{\stepcounter{example}} \AtEndEnvironment{example}{\xqed{\textcolor{black}{\blacksquare}}}

\AtBeginEnvironment{lexample}{\stepcounter{example}} \AtEndEnvironment{lexample}{\xxqed{\textcolor{black}{\blacksquare}}}

\colorlet{myred}{black}

\newmdenv[ hidealllines=true, frametitle={Example~\theexample}, frametitlerule=true, frametitlerulecolor=black, frametitlerulewidth=2pt, frametitlefont=\bfseries, innerleftmargin=0pt, innerrightmargin=0pt, skipabove=.6\topskip, skipbelow=.6\topskip ]{lexample}

\newmdenv[ hidealllines=true, frametitle={Example~\theexample}, frametitlerule=true, frametitlerulecolor=black, frametitlerulewidth=2pt, frametitlefont=\bfseries, innerleftmargin=0pt, innerrightmargin=0pt, skipabove=.6\topskip, skipbelow=.6\topskip ]{example}

\newcounter{example}[chapter] \renewcommand\theexample{\thesection.\arabic{example}}

But, when I added a picture (with the normal \begin{figure} and \includegraphics) to this environment it gives an error compiling and it does not add the picture.

What am I doing wrong?

Thanks in advance!

cfr
  • 198,882
kid_a
  • 1
  • Welcome on TeX.SX ! \ I didn't used mdframed for years as I find tcolorbox much more powerful and convenient. But it looks very strange to patch the macro related to example environment with AtBeginEnvironment and sibling before to define them! \ By the way, at least with tcolorbox the numbering could be directily setup in the environment definition itself...\ Finally, it is not really consistent to try to add floats in this boxed stuff. – Jhor Aug 29 '23 at 23:11
  • Sorry, but I don't reallt understand what you say and how to implement changes in order for this to work. I took this code from this post:

    https://tex.stackexchange.com/questions/86825/define-a-new-example-environment

    – kid_a Aug 29 '23 at 23:20
  • I don't see how the quoted example can even work. But If it works, your ms8n pronlem is with the floating figure that can not be hosted in such a context. If you need to insertva picture, you can use a bare \includegrophics, or wrap it In a minipage. I could privide a full example with tcolorbox, but it's night here, and of nobody does thatvin the interval, you will have to wait a few hours... – Jhor Aug 29 '23 at 23:37
  • 1
    In any cases please help us to help you by editing your question to provide a full minimal (non-) working example (aka MWE) starting with \documemtclass and closing with \end{documemt}. And also add to it the reference of the TSx answer that you used. – Jhor Aug 29 '23 at 23:44
  • What you show in the code block is irrelevant. The relevant bit here is that you say you use \begin{figure}. That will never work -- figure is a floating environment, and it will be placed where LaTeX thinks it should go, not necessarily where you input it. What you should do instead is use just \includegraphics and \captionof, which will stay where you specify them. *Never* try to nest a float (figuremtable`, some others) inside another environment, – barbara beeton Aug 30 '23 at 01:20
  • 1
    Please provide your MWE from \documentclass to \end{document} – MadyYuvi Aug 30 '23 at 05:04
  • You are right guys, figure does not work as a float. I removed this environment; how would you suggest something similar to the example shown here: https://tex.stackexchange.com/questions/86825/define-a-new-example-environment – kid_a Aug 30 '23 at 17:52
  • well, I have solved the issue with figure as you have suggested:

    I added the figure as suggested, without the "figure" environment and using:

    \begin{center} \includegraphics[width=0.25\linewidth]{imgs/Image.pdf} \captionof{figure}{Caption for figure.} \end{center} Thanks!

    – kid_a Aug 31 '23 at 00:02

1 Answers1

0

well, I have solved the issue with figure as you have suggested. I added the figure as suggested, without the "figure" environment and using:

\begin{center}
\includegraphics[width=0.25\linewidth]{imgs/Image.pdf}
\captionof{figure}{Caption for figure.}
\end{center}
kid_a
  • 1