\AtBeginDocument{} コマンドに \begin{figure} および \begin{table} 環境を挿入する

\AtBeginDocument{} コマンドに \begin{figure} および \begin{table} 環境を挿入する

コマンドに\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)

関連情報