It appears a ?? when I put \label after \clearpage

The last page of your document is effectively the one containing this is main file. \pageref{LastPage} as it is shipped out with a call to \clearpage. Since \label doesn't cause any printable content to be set, there are no other pages to be shipped out. And, since \labels are only "processed" during page shipout (in order to obtain the appropriate values of the page for \pageref - an asynchronous process to the content), your final \label is never processed, leaving \pageref to search for an unknown (page) reference LastPage.

The lastpage package does some work to circumvent a similar setup to yours, allow the following minimal example to work as expected, even though it matches your setup:

\documentclass{article}

\usepackage{lastpage}

\begin{document}
this is main file. \pageref{LastPage}
\clearpage

\end{document}

Other options include the use of atveryend for placing \labels at the end of a document.


I think this is probably because that no new page was actually created after the last \clearpage, since there's no content. Adding a \null or \hbox{} resolves this.

\documentclass{article}
% \usepackage{lastpage}
\begin{document}
    this is main file. \pageref{LastPage}
    \clearpage
    \null
    \label{LastPage}
\end{document}