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!
mdframedfor years as I findtcolorboxmuch more powerful and convenient. But it looks very strange to patch the macro related toexampleenvironment withAtBeginEnvironmentand sibling before to define them! \ By the way, at least withtcolorboxthe 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:11https://tex.stackexchange.com/questions/86825/define-a-new-example-environment
– kid_a Aug 29 '23 at 23:20figurethat can not be hosted in such a context. If you need to insertva picture, you can use a bare\includegrophics, or wrap it In aminipage. I could privide a full example withtcolorbox, 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\documemtclassand 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\begin{figure}. That will never work --figureis 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\includegraphicsand\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:20MWEfrom\documentclassto\end{document}– MadyYuvi Aug 30 '23 at 05:04I added the figure as suggested, without the "figure" environment and using:
– kid_a Aug 31 '23 at 00:02\begin{center} \includegraphics[width=0.25\linewidth]{imgs/Image.pdf} \captionof{figure}{Caption for figure.} \end{center}Thanks!