I'm brand new to Latex, and I'm currently using Overleaf to manage my Latex files. Here's my issue: I have multiple projects in Overleaf, each about a specific topic. I want to create another project which somehow reads all the other projects and gets the number of pages in each project (pages of the associated PDF file). This new project serves as a page counter, keeping track of how long all the other projects are. Is there a simple way to do this?

Also, if this is unfeasible in Overleaf, but simple in Texstudio or any local tex editor, please let me now

  • It depends. The lastpge package will save \thepage for the last page in the aux.file (see the xr packaage). However, \thepage is not 100% reliable. You can add your own counter using everypage to increment it, or use \number\Hy@pagecounter from hyperref Commented Mar 25, 2020 at 23:26

1 Answer 1


On any modern TeX system, assuming you are using the pdfTeX engine (which is the default on Overleaf), you can print the number of pages of an external PDF file with a macro like this:


where the argument is the relative path to the PDF file in question. There are versions for other engines at Get number of pages of external PDF. This part is not specific to Overleaf, you can use it on your own local TeX distribution if you want.

With documents spread across multiple Overleaf projects

The trick on Overleaf (Full disclosure: I'm on support staff at Overleaf.) then will be accessing the PDF file from your other project(s) within your summary project that's counting the pages (let's call it the summary project). This can be done by including the other projects' output files as new files in your summary project.

I would not recommend doing this, because it relies on remembering to keep all the external files updated. But if you absolutely need to do this, it can be done.

Open the summary project, then choose "New File" -> "From Another Project". Then select the project of which you want to count the pages. Under the "Select a File" dropdown, click on "select from output files" and then choose output.pdf.

enter image description here

Then choose a suitable name for the output PDF file within the summary project. (This will have to be unique if you want to count the pages of multiple external pages.)

Then define the \numberofpages macro listed above somewhere in the preamble of your summary project. Say I bring in an external PDF with a filename PaperA.pdf, then I can print its number of pages anywhere in the summary project with


The caveat is that as you edit your other project(s) and return to the summary project, you need to remember to "Refresh" each of your external PDF files with the button that will appear on each external PDF file you add to the summary project:

enter image description here

Because of the architecture of Overleaf, it's not possible for these external files to be updated automatically as the other project changes. This has to be done manually with the "Refresh" button.

With multiple documents within a single Overleaf project

If you have multiple documents within one single Overleaf project, it can be done more automatically with some combination of xr-like code and this \numberofpages macro or lastpage as John mentioned in a comment. But there are other trade-offs with this method on Overleaf too, depending on exactly what you have in each of these other documents. Refer to Can I choose which file is the main tex file in a project on Overleaf for more information about compiling multiple documents within a single Overleaf project.

If you end up consolidating multiple documents into one Overleaf project, this help article shows how to use a latexmkrc file on Overleaf to trigger other files to be compiled within a single Overleaf project: Cross referencing with the xr package in Overleaf. That article talks about it in terms of using the xr package, but the same type of approach could be used to get an updated PDF in the project's output files area, in order to use with the \numberofpages macro described above.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .