Graphics, Figures & TablesPlace Figure in Solution

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
correaraul
Posts: 2
Joined: Sun May 05, 2013 12:15 am

Place Figure in Solution

Post by correaraul »

I'm using the exam document class. How can I insert a figure in the solution of my document? The code is as follows.

Code: Select all

\begin{figure}[!h]
  \centering
  \includegraphics{figure.jpg}
\end{figure}
This doesn't work.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Place Figure in Solution

Post by localghost »

Simply saying "doesn't work" does not provide any useful information with which anyone can provide you with any help. Hence prepare a self-contained and minimal example in order to give an adequate problem description.

Just one tip. Don't let the figure float. Look at the \captionof command provided by the caption package.


Best regards and welcome to the board
Thorsten
correaraul
Posts: 2
Joined: Sun May 05, 2013 12:15 am

Place Figure in Solution

Post by correaraul »

Sorry, I'm new in the forum and LaTeX.. Let me try to explain better:

I use the TeXShop of MacTeX..
I think the most important parts of my document are:

Code: Select all

\renewcommand{\solutiontitle}{}
\qformat{Question #\thequestion\quad\hrulefill}
\shadedsolutions

...

\begin{questions}
\question The question..
\begin{solution}
The solution..
\begin{figure}
\centering
\includegraphics{Figure.jpg}
\end{figure}
\end{solution}
\end{questions}
If I put the figure in the solution environment, like the code above, the error "LaTeX Error: float(s) lost" appears.. but if I put the figure out of the solution environment, the pdf is created without any error..
It's possible to put a figure in the solution?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Place Figure in Solution

Post by cgnieder »

The {figure} environment like the {table} environment is a floating environment. Their purpose is not to have the user be bothered with looking for the best place to place a figure or a table but let LaTeX decide. In order to be able to float a floating environment cannot be placed inside boxes, lists or similar constructs. That's why you get the error: you placed the float somewhere where it can't float and the consequence is that the float is lost.

In your case, however, you don't want the figure to float around for a possible place but want it as a part of the solution. In this case you don't need the {figure} environment. Replace

Code: Select all

\begin{figure}
\centering
\includegraphics{Figure.jpg}
\end{figure}
with

Code: Select all

\begin{center}
\includegraphics{Figure.jpg}
\end{center}
Regards
site moderator & package author
slrommel
Posts: 1
Joined: Sun Jun 22, 2014 12:19 pm

Re: Place Figure in Solution

Post by slrommel »

I understand the comment posted above about figures or tables as floats, but is there any way to have a caption field or reference to the image? I am asking because I have exam or homework problems in a semiconductor device physics class where reading data from a figure is a major part of a problem. It would be nice to be able to have an inline reference to the figure in the text of the solution beyond "see the image below". Are there any tricks you might be able to suggest?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Place Figure in Solution

Post by cgnieder »

Hi slrommel,

Welcome to the LaTeX community!

Instead of »hijacking« an already solved thread it would have been better to open a new one...

Anyway: if you're using a KOMA-Script class such as scrartcl then there is a command \captionof{<float>}{<caption>} that you could use. If you're using a different class then have a look at the caption packages which also provides this command.

Regards
site moderator & package author
Post Reply