2

I am trying to use the table environment inside the solution environment of the exam documentclass. Unfortunately this doesnt work as stated in this question since table is also a float.

The solution environment internally uses a \vbox and this has as a consequence that floats are not allowed inside this environment. Instead of using the floating environment table you can use a center environment

Since they work outside of the solution environment i am looking for a way to modify \begin{table} ... \end{table} to act as it is outside the solution and inside replaces itself with \begin{center}...\end{center}.

Fortunately as this post shows its easy to find out whether or not you are inside a solution environment. I tested this and it works.
However since the documents i have to adapt to the exam documentclass are numerous and are also used in other documentclasses I would ideally not create a new environment but adapt the table environments similar to what happend here to the figure or here to tabular environment. This is my code so far:
UPDATE: I dont know why, but with \center instead of \begin{center} the error is gone and also allows a way to tread the optional argument of table

\documentclass{exam}

\let\oldtable\table
\let\endoldtable\endtable

\renewenvironment{table}[1][h]
{\ifinner \center    \else  \oldtable[#1] \fi}
{\ifinner \endcenter \else  \endoldtable  \fi}

\begin{document}

\begin{questions} %error is no \question 
\printanswers

\question{Question featuring a table environment}

\begin{table}[h]
    \begin{tabular}{|ll|}
        Question & in  \\
        Question & part \\
    \end{tabular}
\end{table}


\begin{solution}
Solution featuring a table environment

    \begin{table}[h]
        \begin{tabular}{|ll|}
            Solution & in  \\
            Solution & part \\
        \end{tabular}
    \end{table}

\end{solution}

\end{questions}

\end{document}

But I fear I am not handling the paramter of table correct, since this leads to the error: Unknown float option `['. \begin{table}[

This solution works outside "inner" and can handle the optional argument of table, however if \ifinner is true i get the error

Extra }, or forgotten \endgroup. \end{table}

Does anyone has a suggestion on how to adapt table based on the environment?

Thank you very much

3
  • 4
    As long as you don't use \caption, you can use the center environment. If you need a caption, use \captionof{table}{some caption}.
    – Bernard
    Commented Mar 4, 2019 at 16:35
  • Thanks! This is a solution i can use for future documents, yet i still need a way to handle the existing ones without changeging the table parts in them
    – Finn
    Commented Mar 5, 2019 at 7:49
  • In this case, another possibility is to use the ``[H]` placement specifier (defined by the float package), which can easily be done with Search and Replace in your editor.
    – Bernard
    Commented Mar 5, 2019 at 10:15

2 Answers 2

1

I found a solution and posting it here as answer if someoneelse is facing this issue. The (in hindsight obvious) problem was that \ifinner works in the beginning, because it is a floating environment, but as soon as \table begins it is not anymore. Therefor the \endcenter could no be reached. I have tried a few alternatives and found the easiest way is to define a custom boolean that checks if the ifinner of the modified table is reached and use this to set a boolean. On the \endtablethis boolean can then be used to take care of ending it correctly.

\let\oldtable\table
\let\endoldtable\endtable
\newif\ifinsidefloatingenv  %set boolean for 
\insidefloatingenvfalse     %not necessary but to be sure its false

\renewenvironment{table}[1][h]
{\ifinner               \center \insidefloatingenvtrue     \else    \oldtable[#1]   \fi}
{\ifinsidefloatingenv   \endcenter \insidefloatingenvfalse \else    \endoldtable    \fi}
0

Do not forget like me that table is not needed at all if you do not need captions, labels for cross-referencing, and the extra whitespace added for those even when they don't exist. A plain tabular environment works out-of-the-box in a solution environment and is much simpler:

\documentclass{exam}

\begin{document}
\begin{questions} %error is no \question 
\printanswers

\question{Question featuring a table environment}

\begin{tabular}{|ll|}
    Question & in  \\
    Question & part \\
\end{tabular}
    
\begin{solution}
Solution featuring a table environment

    \centering
    \begin{tabular}{|ll|}
        Solution & in  \\
        Solution & part
    \end{tabular}

\end{solution}
\end{questions}
\end{document}

To add more left-aligned content in the solution box after \centering, add braces with a newline:

\begin{solution}
Solution featuring a table environment

    {\centering
    \begin{tabular}{|ll|}
        Solution & in  \\
        Solution & part
    \end{tabular}
    
    %newline before brace is required
    }
    
    More left-aligned text at end of the solution.

\end{solution}

You must log in to answer this question.

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