3

We are using the following setup to include a customized intro to our printed exams:

rmarkdown::render(input = "Intro.Rmd",output_file ="Intro.pdf")
quests <- unlist(Grupos[c(1:3,sample(c(4:7)))])
exams2nops(file=quests, n=1, nsamp = 1, intro = "\\includepdf[pages=-]{D:/ExameAEI_R/Intro.pdf}", ...)

We start by generating a intro.pdf file, and then use the intro argument to add it to the exams2nops.

We would like to do the same thing with exams2pdf:

exams2pdf(file=quests, n=1, nsamp = 1, intro = "\\includepdf[pages=-]{D:/ExameAEI_R/Intro.pdf}", language = "pt-PT", ...) 

There's no error returned, but the intro.pdf does not get appended to the exams2pdffile produced. Is there a way to add an intro file within exams2pdf?

user438383
  • 5,716
  • 8
  • 28
  • 43
JPMD
  • 644
  • 1
  • 7
  • 19

2 Answers2

2

There is no intro argument in exams2pdf(). The difference between exams2nops() and exams2pdf() is:

  • exams2pdf() assumes that the user writes the LaTeX templates. The templates included in the package are intended as illustrations or as starting points for new templates. But they should not be considered as templates for "production use".

  • exams2nops() creates its own template in a rather rigid format. You have quite a few options for smaller customizations but, by and large, the template is defined by the function.

Currently, there is nothing in between these two approaches: Either you have to write your own template and do all the work yourself. Or you have to live with the limitations of the NOPS format.

For more guidance on how to write a LaTeX template that has extra parameters or can be extended, see: How to have parameters in the LaTeX template?

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
  • Thanks @Achim. So if I add this "\includepdf[pages=-]{D:/ExameAEI_R/Intro.pdf}" to the pdf template, should do the trick no? – JPMD Jul 02 '21 at 15:05
  • or `exams2pdf(file=quests, n=1, nsamp = 1, header = "\\includepdf[pages=-]{D:/ExameAEI_R/Intro.pdf}", ...)`? – JPMD Jul 02 '21 at 15:21
  • So I think I am close to the solution. 1. In the ispa_solution.tex file used in the template I added the `\usepackage{pdfpages}` and moved the ` %% \exinput{header}` placeholder to the place I want the Intro.pdf to be inputed. 2. Then, in `exams2pdf(file=quests, n=1, nsamp = 1, header = "includepdf[pages=-]{D:/ExameAEI_R/Intro.pdf}",...)` The problem is that the tex file generated has `\{includepdf[pages=-]{D:/ExameAEI_R/Intro.pdf}}`. It does not compile. But if I remove the {} to `\includepdf[pages=-]{D:/ExameAEI_R/Intro.pdf}` it compiles ok. How do I get rid of the {} in exams2pdf? – JPMD Jul 02 '21 at 15:37
  • The `header` argument is intended for things like `header = list(Date = "2021-07-04", ID = "R/exams test")` which would then be turned into `\Date{2021-07-04}` and `\ID{R/exams test}` in the LaTeX file. Of course, the comands `\Date{}` and `\ID{}` would have to be defined for this. So the "clean" solution would be to include a small LaTeX command `\intropage{}`, say, in your LaTeX template and then do `header = list(intropage = "D:/ExameAEI_R/Intro.pdf")`. – Achim Zeileis Jul 03 '21 at 23:34
  • Alternatively, you can also use the quick & dirty solution that you tried above and generate the code directly with `header = list("includepdf[pages=-]" = "D:/ExameAEI_R/Intro.pdf"))`. This generates the right LaTeX code for your specific use case but is a bit hacky. Personally, I would go for the "clean" solution mentioned above. – Achim Zeileis Jul 03 '21 at 23:37
  • Thanks @Achim. Actually, I think solution 2 ("dirty") is simpler as it does not have to tweak with the latex templates, and it will work out of the box with the custom exams templates. Problem solved! Thank you once again. – JPMD Jul 05 '21 at 09:31
  • As usual: Please remember to accept the answer so that it is flagged correspondingly on SO. – Achim Zeileis Jul 05 '21 at 15:17
  • I am trying to use tghe \intropage{} clean format. I got this far `\newcommand{\myIntro}[1]{\includepdf[pages=-] = #1}` in the solution.tex template. Then, I use `header=list(myIntro = "D:/OneDrive - ISPA/AEII/2021-2022/Exames/ExEpEspTE/Intro.pdf",...)` in the exam2pdf arguments... But when I run the exams2pdf I got this " Undefined control sequence. \myIntro #1->\includepdf [pages=-] = #1 l.77 ...AEII/2021-2022/Exames/ExEpEspTE/Intro.pdf}".... So my latex function is ill defined... can you help? Thanks! – JPMD Aug 24 '22 at 17:59
  • I got it... the float package was missing in the preambule of the template!... – JPMD Aug 24 '22 at 18:33
1

Have you considered combining the pdf afterwards?

library(qpdf)
pdf_combine(input = c("Intro.pdf", quests), output = quests)

See documentation of qpdf for more details: https://cran.r-project.org/web/packages/qpdf/qpdf.pdf

MrH
  • 101
  • 6