Inserindo ambientes \begin{figure} e \begin{table} no comando \AtBeginDocument{}

Inserindo ambientes \begin{figure} e \begin{table} no comando \AtBeginDocument{}

É possível inserir o \begin{figure} ... \end{figure}ou os \begin{table} ... \end{table}ambientes no \AtBeginDocument{}comando?

Considere o MWE abaixo:

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

Se as linhas marcadas como [1-4] estiverem comentadas, o código MWE está em conformidade. Porém, com eles incluídos, meu complier reclama e lança o erro ! LaTeX Error: Missing \begin{document}.See the LaTeX manual or LaTeX Companion for explanation.Type H <return> for immediate help.... \begin{document}. Alguma idéia de por que \begin{figure}e \begin{table}estaria em conflito \AtBeginDocument{}?

Responder1

Não consigo entender por que você iria querer fazer isso :-) mas vocêpoderiafaça o seguinte.

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

Também movi seus rótulos para que funcionem (têm que ficar depois da legenda, sempre).

(Não relacionado, mas eu mudei sua [h]opção hnunca deve ser usada sozinha, o LaTeX geralmente avisa e muda para ht)

informação relacionada