不能在 \newenvironment 內包含“禁忌”,且後導碼中有儲存格

不能在 \newenvironment 內包含“禁忌”,且後導碼中有儲存格

我試圖tabu在環境的新定義中包含 a ,但如果enddef新環境的後置碼 ( ) 包含禁忌的內容,那麼它會失敗,因此 cells.與tabulartabular*作品。錯誤是:

! Missing number, treated as zero. \[email protected]

氣象局:

\documentclass{article}
\usepackage{tabu}
\newenvironment{testt}
{\begin{tabu} to \textwidth{cccc}
 11 & 12 & 13 & 14 \\
 21 & 22 & 23 & 24 \\}
{
 41 & 42 & 43 & 44
\end{tabu}
}

\begin{document}

\begin{testt}
 31 & 32 & 33 & 34 \\
\end{testt}
\end{document}

怎麼解決這個問題呢?

答案1

嘗試一下這個包environ。此套件用於\BODY標記參數的位置,從而避免出現大括號不匹配以及本問題中發生的情況等問題。

見其文件

\documentclass{article}
\usepackage{tabu}
\usepackage{environ}
\NewEnviron{testt}
{\begin{tabu} to \textwidth{cccc}
 11 & 12 & 13 & 14 \\
 21 & 22 & 23 & 24 \\
 \BODY
 41 & 42 & 43 & 44
\end{tabu}
}

\begin{document}

\begin{testt}
 31 & 32 & 33 & 34 \\
\end{testt}

\end{document}

答案2

tabu包內部做了

\tabu@collectbody#1#2\end

在排版之前收集表格的所有內容。如果您「隱藏」任何表格,這將無法正常運作。如果您僅隱藏\end{tabu}例如,它確實有效

\documentclass{article}
\usepackage{tabu}
\newenvironment{testt}
{\begin{tabu} to \textwidth{cccc}
 11 & 12 & 13 & 14 \\
 21 & 22 & 23 & 24 \\}
{
\end{tabu}
}

\begin{document}

\begin{testt}
 31 & 32 & 33 & 34 \\
 41 & 42 & 43 & 44
\end{testt}
\end{document}

仍然正確地抓取所有表格。

如果您在儲存格內部使用,您會發現有些問題\showthe\currentgrouplevel:所有「工作」案例的群組層級為 14,而「錯誤」案例的群組層級為 8。

正如egreg所說,這可以說是一個錯誤tabu,或至少是一個應該記錄在案的「功能」。為了提供解決方法的建議,我想我們需要知道您想要什麼效果,然後可以提出替代方法:我認為這最好是作為一個新問題。

答案3

您可以嘗試使用該collect套件。注意:我既沒有使用過collect,也tabu肯定無法專業地評估這是否是一種可行且可靠的方法。

\documentclass{article}
\usepackage{tabu}
\usepackage{collect}

\definecollection{rescuetabu}

\makeatletter
\newenvironment{testt}
{\@nameuse{collect*}{rescuetabu}
 {\begin{tabu} to \textwidth{cccc}
 11 & 12 & 13 & 14 \\
 21 & 22 & 23 & 24 \\ }
 {41 & 42 & 43 & 44 \\ \end{tabu}}{}{}}
{\@nameuse{endcollect*}}
\makeatother

\begin{document}

\texttt{normal tabu}

\begin{tabu} to \textwidth{cccc}
 11 & 12 & 13 & 14 \\
 21 & 22 & 23 & 24 \\ 
 31 & 32 & 33 & 34 \\
\end{tabu}

\hrule\medskip
\texttt{rescued tabus}

\begin{testt}
 31 & 32 & 33 & 34 \\
\end{testt}

\bigskip

\begin{testt}
 51 & 52 & 53 & 54 \\
 61 & 62 & 63 & 64 \\
\end{testt}
\hrule

\end{document}

獲救禁忌

相關內容