Insertar entornos \begin{figure} y \begin{table} en el comando \AtBeginDocument{}

Insertar entornos \begin{figure} y \begin{table} en el comando \AtBeginDocument{}

¿Es posible insertar el \begin{figure} ... \end{figure}o los \begin{table} ... \end{table}entornos en el \AtBeginDocument{}comando?

Considere el MWE a continuación:

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

Si las líneas marcadas como [1-4] están comentadas, el código MWE cumple correctamente. Sin embargo, con ellos incluidos, mi compilador se queja y arroja el error ! LaTeX Error: Missing \begin{document}.See the LaTeX manual or LaTeX Companion for explanation.Type H <return> for immediate help.... \begin{document}. ¿Alguna idea de por qué \begin{figure}y \begin{table}estaría en conflicto \AtBeginDocument{}?

Respuesta1

No veo por qué querrías hacer esto :-) peropodríaHaz lo siguiente.

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

También moví tus etiquetas para que funcionen (tienen que estar después del título, siempre).

(No está relacionado, pero cambié, su [h]opción hnunca debe usarse sola, LaTeX generalmente advierte y la cambia a ht)

información relacionada