1

I found a solution to include PDF documents into my LaTeX document, different from using \usepackage{pdfpages}. \usepackage{pdfpages} does a fine job in just putting the pages from the PDF where I want them, as full pages. But today I wanted to include a PDF document, and keep my overall document style, including page numbering and headers and footers and so on. This can be done by using the \includegraphics[width=\textwidth,page=1]{Document.pdf} command. The specified page of the PDF is integrated into my LaTeX document like a figure. Therefore, it only covers the area that would otherwise be covered by text and footers and headers and everything remains intact. This is what I achieve with the following solution.

As a side note: The PDF document I wanted to integrate was in A5 format, my LaTeX Document in A4 (both portrait). To my surprise, A5 in portrait mode just fills the whole space available for text in my A4 LaTeX document. So I can easily include 1 PDF page on 1 LaTeX page and it looks fine.

2
  • 1
    Is this a question or just a comment? You can scale the PDF and keep your header and footer with \includepdf as well. Commented Oct 28, 2020 at 22:17
  • Welcome to TeX.SE. This would fit a bit better in our Q&A format if you specified your question that is addressed by your solution. (Also, it is possible (albeit annoying) to do for loops in TeX.)
    – Teepeemm
    Commented Oct 28, 2020 at 22:39

1 Answer 1

-1

To include multiple pages can be tedious, as the includegraphics command has to be adjusted for every page (page=1, page=2, page=3,...). Instead, I wrote a short program in C that creates the tex-code that can then be inserted into the LaTeX document:

#include <stdio.h>
int a=0, b=0, i=0;

int main()
{
    FILE *f = fopen("myfile.tex","w");
    for (i=1 ; i<b+1 ; i++)
    {
        fprintf(f,"\\includegraphics[width=\\textwidth,page=%d]{document.pdf}\n\n",i);
    }
    return 0;
}

This creates a file myfile.tex with the \includegraphics[width=\textwidth,page=1]{Document.pdf} followed by a double line break for the pages 1 to b (so b should be the number of the last page you want to include, just change it in the second line (int a=0, b=0, i=0;)). Also, change the part saying {document.pdf} to the name of the PDF you want to include (the name you would tell LaTeX).

Once you changed b to the last page you want to include, and {document.pdf} to the appropriate name/location for LaTeX, you can save this file, e.g. as write.c (the ending .c is important here, as it is a program written in C).

Then, open a terminal and go to the file location, e.g.

cd ./MyCodeFolder

make sure that you are in the right folder and that your write.c-file is there:

ls

lists all files in the current folder. If so, compile it, to get a executable program:

gcc write.c -o write.exe 

gcc is the compiler, write.c the source code we just created, and write.exe the program that will get created by the compiler after hitting enter.

Now, execute write.exe. This creates myfile.tex. Final step: Go to you LaTeX code, make sure Document.pdf is there as well, add myfile.tex and include it where you want your PDF pages in your LaTeX document using

\input{myfile.tex}

Profit.

4
  • you know that you can write loops in latex too? pdfpages manages to include all pages quite fine alone with latex code. Commented Oct 28, 2020 at 23:03
  • Would you mind sharing how? Sounds nice!
    – kalle
    Commented Oct 29, 2020 at 18:50
  • 1
    \ExplSyntaxOn \int_step_inline:nn{20}{\includegraphics[width=4cm,page=#1]{example-image-duck}\quad} \ExplSyntaxOff Commented Oct 29, 2020 at 18:54
  • Thank you very much! I didn't know about this possibility! If you write it as an answer I would accept it, if you'd like. ;)
    – kalle
    Commented Oct 29, 2020 at 21:30

You must log in to answer this question.

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