Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I found in How to use figure inside a exercise environment? that I can't use begin{figure} ... \end{figure} in an exercise environment. I also found situations where avoiding the use of such floats can cause other trouble (see: In memoir environment, the chapter heading placement gets messed up if a too big figure gets placed on next page).

The logical thing to do would be to have a programatically check for an Exercise or Answer environment, perhaps doing something like the following:

\newcommand{\imageFigure}[4]{%
  IF I AM IN A FLOAT-UNSUPPORTED environment % ie: Answer or Exercise
    \begin{center}%
       \captionsetup{type=figure}%
       \includegraphics[totalheight=#4\textheight]{#1}%
       \caption{#2}%
       \label{#3}%
    \end{center}%
  ELSE
    \begin{figure}[htp]%
       \centering%
       \includegraphics[totalheight=#4\textheight]{#1}%
       \caption{#2}%
       \label{#3}%
    \end{figure}%
  ENDIF
}

The question How can I detect if I'm inside or outside of a float environment? does something similar, and using that idea I thought I might be able to use the \theExercise and \theAnswer counters, but it appears that their values can be non-zero even when not in these environments.

I imagine that it would be possible to set and clear a flag before and after using either of these environments respectively. Perhaps there is already such a flag already internally in the exercise.sty implementation.

Presuming this is possible, how would the pseudocode predication in the figure generation command above be modified to actually work?

share|improve this question
    
You can use \@currenvir to check for the current environment. See Command behavior depending on current environment. –  Werner May 14 '12 at 23:00
add comment

2 Answers

up vote 12 down vote accepted

figure and other floating environments give an error if they are inside any kind of box (as it stops them floating) so if \ifinner is true you don't want to float.

  \ifinner
      non float version
   \else

      float version

   \fi
share|improve this answer
    
wow, that was fast! –  Peeter Joot May 14 '12 at 23:08
add comment

You could use etoolbox's \AtBeginEnvironment to set a boolean to be true inside the Exercise and the Answer environment and then test inside your command for that boolean:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{exercise}

\newbool{InExerciseOrAnswer}
\AtBeginEnvironment{Exercise}{\booltrue{InExerciseOrAnswer}}
\AtBeginEnvironment{Answer}{\booltrue{InExerciseOrAnswer}}

\newcommand*\test{\ifbool{InExerciseOrAnswer}{I'm in :)}{I'm not :(}}

\begin{document}
\begin{Exercise}
 \test
\end{Exercise}
\test

\begin{Answer}
 \test
\end{Answer}
\test

\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.