
我試圖在文檔類別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}