在考試文件類別的解決方案環境中使用表格

在考試文件類別的解決方案環境中使用表格

我試圖在文檔類別solution的環境中使用表格環境exam。不幸的是,這並不像中所述那樣工作問題因為table也是一個浮動。

解決方案環境內部使用 \vbox,因此在該環境中不允許浮動。您可以使用中心環境,而不是使用浮動環境表

由於它們在環境之外工作,solution我正在尋找一種修改方法,\begin{table} ... \end{table}以使其在解決方案之外並且內部將其自身替換為\begin{center}...\end{center}.

幸運的是貼文顯示,很容易確定您是否在某個solution環境中。我測試了這個並且它有效。
然而,由於我必須適應exam文檔類的文檔很多,並且也在其他文檔類中使用,所以理想情況下我不會創建新環境,而是適應table與發生的情況類似的環境這裡figure這裡到表格環境。到目前為止,這是我的程式碼:
更新:我不知道為什麼,但是用\center而不是\begin{center}錯誤消失了,並且還允許一種方式來處理可選參數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}

但我擔心我沒有處理正確的參數table,因為這會導致錯誤:未知的浮動選項“[”。 \開始{表}[

該解決方案在“內部”之外工作,並且可以處理 的可選參數table,但是如果\ifinner為 true 我收到錯誤

額外的 },或忘記了 \endgroup。 \茶几}

table有人對如何根據環境進行適應提出建議嗎?

非常感謝

答案1

如果其他人面臨這個問題,我找到了一個解決方案並將其發佈在這裡作為答案。 (事後看來很明顯)問題是,它\ifinner在開始時有效,因為它是一個浮動環境,但一旦\table開始它就不再是了。因此\endcenter無法到達。我嘗試了幾種替代方法,發現最簡單的方法是定義一個自訂布林值,檢查是否達到ifinner修​​改的值,並使用它來設定布林值。然後可以使用此佈林table\endtable來正確結束它。

\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}

相關內容