
是否可以將\begin{figure} ... \end{figure}
或\begin{table} ... \end{table}
環境插入命令中\AtBeginDocument{}
?
考慮下面的 MWE:
\documentclass{article}
\usepackage{blindtext, caption}
\AtBeginDocument{
\begin{figure}[h!]\label{fig:figurelabelexample}%.........Line [1]
\centering
\rule{1.5cm}{1.5cm}
\captionof{figure}{figurecaptionexample}
\end{figure}%.............................................Line [2]
\begin{table}[h!]\label{tab:tablelabelexample}%...........Line [3]
\centering
\begin{tabular}{ l c r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
\captionof{table}{tablecaptionexample}
\end{table}%..............................................Line [4]
}
\begin{document}
\blindtext
\end{document}
如果標記為 [1-4] 的行被註解掉,則 MWE 程式碼正確符合要求。然而,包含它們後,我的編譯器會抱怨並拋出錯誤! LaTeX Error: Missing \begin{document}.See the LaTeX manual or LaTeX Companion for explanation.Type H <return> for immediate help.... \begin{document}
。有什麼想法為什麼\begin{figure}
和\begin{table}
會發生衝突嗎\AtBeginDocument{}
?
答案1
我不明白你為什麼想要這樣做:-)但是你可以請執行下列操作。
\documentclass{article}
\usepackage{blindtext, caption}
\AtBeginDocument{\global\expandafter\let \csname @nodocument\endcsname \relax
\begin{figure}[htp!]%.........Line [1]
\centering
\rule{1.5cm}{1.5cm}
\captionof{figure}{figurecaptionexample}\label{fig:figurelabelexample}
\end{figure}%.............................................Line [2]
\begin{table}[htp!]%...........Line [3]
\centering
\begin{tabular}{ l c r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
\captionof{table}{tablecaptionexample}\label{tab:tablelabelexample}
\end{table}%..............................................Line [4]
}
\begin{document}
\blindtext
\end{document}
我還移動了你的標籤,以便它們可以工作(它們必須始終位於標題之後)。
(不相關,但我改變了你的[h]
選項h
永遠不應該單獨使用,LaTeX通常會警告並將其更改為ht
)