0

I have an image within a framed environment and would like to caption it. Using the figure environment crashes the compiler

\documentclass[a4paper]{article}
\usepackage[a4paper, total={170mm,257mm}, left=20mm, top=20mm]{geometry}

\usepackage{framed}
\usepackage{graphicx}

\begin{document}
\begin{framed}
    Lots and lots of text
    \begin{figure}
        \centering
        \includegraphics{Image.JPG}
        \caption{Caption}
        \label{fig:my_label}
    \end{figure}
    Lots more text
\end{framed}
\end{document}

I have found way to still include the image within my frame

\documentclass[a4paper]{article}
\usepackage[a4paper, total={170mm,257mm}, left=20mm, top=20mm]{geometry}

\usepackage{framed}
\usepackage{graphicx}

\begin{document}
\begin{framed}
    Lots and lots of text
    \begin{center}
        \includegraphics{Image.JPG}
    \end{center}
    Lots more text
\end{framed}
\end{document}

However, this method does not allow me to caption or label the image. How do I caption an image that is positioned within a framed environment.

2
  • Welcome to TeX.SE! Have you tried \captionof from package caption?
    – Mensch
    Nov 28, 2021 at 20:31
  • I hadn't. It solved my problem. Thank you so much!
    – Poseidaan
    Nov 28, 2021 at 21:40

1 Answer 1

0

I'm putting this in an answer so that it can be more easily seen by future visitors.

As Mensch commented, the \captionof command allows you to create a caption outside of a float environment. The following code achieves what I wanted

\documentclass[a4paper]{article}
\usepackage[a4paper, total={170mm,257mm}, left=20mm, top=20mm]{geometry}

\usepackage{framed}
\usepackage{caption}
\usepackage{graphicx}

\begin{document}
\begin{framed}
    Lots and lots of text
    \begin{center}
        \includegraphics{example-image-a}
        \captionof{figure}{Caption}
    \end{center}
    Lots more text
\end{framed}
\end{document}
3
  • You are right, it was supposed to import caption as well
    – Poseidaan
    Nov 30, 2021 at 13:36
  • You can accept your answer too :-)
    – Zarko
    Dec 30, 2021 at 15:36
  • Thanks for mentioning it, I accepted my answer
    – Poseidaan
    Dec 31, 2021 at 9:26

You must log in to answer this question.

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