# Automate importing pages from PDF file and add text to some of these

I'm trying to use LaTeX for lecture notes, but am having trouble implementing it the way I want to. For each lecture I will have access to a PDF file with presentation slides, where one slide corresponds to one page. I would like to be able to import these slides and put them at the top of my document's pages, and then have writing associated with that slide displayed below. During the lecture, I would write something like \slide{23}{Example text}, to have "Example text" displayed below slide 23. I won't have something to write for every slide, so they will need to be added to the top of every page without my having to write \slide{24}{} for instance, if I don't have anything to say about slide 24.

I'm a novice when it comes to LaTeX, so I don't know where to start for something like this, and would greatly appreciate some pointers or useful links. My general idea for how to proceed is this:

Get the number of pages of the pdf file to be imported with

\pdfximage{lecture_slides.pdf}
\the\pdflastximagepages


Note: I will be using pdflatex, so this should work.

Define a command \slide{}{} that takes a number and a string as mandatory arguments. The number is saved in a list that contains the number of all slides that have had text added to them.

Define a command \importslides{path}, that counts the pages of the pdf file as mentioned above, then importing slides with \includegraphics[page number]{path}. Presumably, I would use a for loop from 1 to the number of pages in the file for this. For every page, the command would have to check the list of slides with text, and if that particular slide is present in the list, add the string associated with it below the page.

Is this a good way to approach this problem? Does anyone have any recommendations on how to proceed, or where I can find information on how to do this kind of programming in LaTeX?

• pdfpages package and its \includepdf command should be able to this, in a wrapper macro, preferably.
– user31729
Sep 9, 2015 at 17:38

Update Improved version (older version at the end)

The spacings etc. can't be automated straightforwardly. Some manual adaption is needed for each slide file.

\documentclass[a5paper]{article}

\usepackage{pdfpages}
\usepackage{pgffor}
\usepackage{xparse}
\usepackage{blindtext}

\newlength\slidetextdistance

\setlength{\slidetextdistance}{0.48\textheight}
\NewDocumentCommand{\mypagecommand}{+m}{%
\vspace*{\slidetextdistance}

#1
}

\newtoks\localtok% To grab the possible \par in the third argument.

\NewDocumentCommand{\importsheet}{O{}m+o}{%
\IfFileExists{#2.pdf}{%
\IfValueTF{#3}{%
\localtok={#3}
\includepdf[offset=0 0.4\textheight,frame,column,pagecommand={\mypagecommand{\the\localtok}},scale=0.9,#1]{#2.pdf}
\localtok={}
}{%
\includepdf[offset=0 0.4\textheight,frame,scale=0.9,#1]{#2.pdf}
}%
}{%
Sorry, not available
}%
}
\newcommand{\myslidetext}[1]{\noindent\textbf{Slide #1}\par \blindtext}

\begin{document}
\pagestyle{empty}
% Use some in a loop, just for demonstration
\foreach \x in {1,...,10} {%
\importsheet[pages=\x]{gravitation_slides}[\noindent\textbf{Slide \x}\par \blindtext]
}
% Import some special one, without comments
\importsheet[pages=15]{gravitation_slides} % No slide text
\end{document}


Here is an example, not perfect, especially I've no idea how to count the number of slides in an external file at the moment.

\documentclass[a4paper]{article}

\usepackage[lmargin=1.5cm,rmargin=1.5cm,tmargin=1.5cm,bottom=1.5cm]{geometry}
\usepackage{pdfpages}
\usepackage{pgffor}
\usepackage{xparse}
\usepackage{blindtext}

\newcommand{\mypagecommand}[1]{%
\vspace*{0.9\textheight}
#1
}
\NewDocumentCommand{\importsheet}{O{}m+o}{%
\IfFileExists{#2.pdf}{%
\IfValueTF{#3}{%
\includepdf[frame,column,pagecommand={\mypagecommand{#3}},noautoscale=true,scale=0.9,#1]{#2.pdf}
}{%
\includepdf[noautoscale=true,scale=0.9,#1]{#2.pdf}
}%
}{%
Sorry, not available
}%
}

\begin{document}
\foreach \x in {1,...,10} {%
\importsheet[pages=\x]{mysource}[Slide \x \blindtext]
}
\end{document}


• I'll try to improve this
– user31729
Sep 9, 2015 at 18:11
• This is excellent! I combined this with the arrayjob package to get something like the \slide{}{} command that I wanted. With arrayjob, one can create arrays, and then edit the contents of the array at a given index (or slide number) using \array{myArray}(index)={text}. Your example loop can be used to go through any PDF file by first using \pdfximage{external_pdf_file.pdf}, and continuing the loop to \the\pdflastximagepages. \importsheet[pages=\x]{external_pdf_file}[\array{myArray}(\x)] then includes all slides, with text added where appropriate. Thanks a lot for your help! Sep 9, 2015 at 22:20
• @MartinBoström: Thanks for information, I did not use arrayjob so far.
– user31729
Sep 10, 2015 at 13:54