Search code examples
rpandocr-exams

R/exams use styles in reference.docx


With vanilla pandoc, when converting to docx, it is possible to adapt the paragraph styles in a reference.docx that is passed to pandoc via --reference-doc=reference.docx. However, in r-exams this is somewhat limited, as all elements in a converted docx file seem to have the style "Normal". Also custom styles (https://pandoc.org/MANUAL.html#custom-styles) do not seem to work because of this. Is there any fix/hack to use this pandoc feature?


Solution

  • Options in exams2pandoc

    When using exams2pandoc() you can supply any options to be used when calling pandoc. So you can set a reference-doc but you need to supply the full path to it because the exercises will be processed in a temporary directory and not the current working directory. Thus, you can do:

    exams2pandoc("capitals.Rmd", options = "--reference-doc=/path/to/reference.docx")
    

    The option is passed on in this case but I'm not sure what we would need to do to leverage the styles defined in this reference document. I tried setting the default paragraph style (e.g., using a different font) but this doesn't seem to have an effect. But this may well be due to my lacking skills in Word formatting!

    Details

    If you want to have a closer look at how this could be tweaked, the following may be helpful. The exams2pandoc() function internally first creates a .tex file (by default). When using the capitals template as above you will get a file like:

    \documentclass[a4paper]{article}
    
    \usepackage[utf8]{inputenc}
    \usepackage{a4wide,color,Sweave,url,amsmath,booktabs,longtable}
    
    \begin{document}
    
    %% Exam 1
    %% 2022-12-01
    
    \begin{enumerate}
    
      \item
      \textbf{Question}\\
      Which of the following cities are the capital of the corresponding country?
      \begin{enumerate}[(a)]
        \item São Paulo (Brazil)
        \item Riyadh (Saudi Arabia)
        \item Warsaw (Poland)
        \item Tokyo (Japan)
        \item Istanbul (Turkey)
      \end{enumerate}
    
      \textbf{Solution}\\
      
      \begin{enumerate}[(a)]
        \item False. The capital of Brazil is Brasilia.
        \item True. Riyadh is the capital of Saudi Arabia.
        \item True. Warsaw is the capital of Poland.
        \item True. Tokyo is the capital of Japan.
        \item False. The capital of Turkey is Ankara.
      \end{enumerate}
    \end{enumerate}
    
    \end{document}
    

    Then subsequently this is converted to .docx via

    pandoc -o capitals.docx capitals.tex
    

    where you can additionally insert further options as described above.

    Maybe you can provide some guidance on what would be necessary to leverage other styles from a reference docx?