# Org mode exam template with question and results in combination with latex

I would like to use Org mode for creating exam questions and including the templates for the corresponding results for each question.

A general template is shown here

#+TITLE:     Lecture
#+AUTHOR:
#+EMAIL:
#+DATE:      Jan 2020
#+INSTITUTE: UoW
#+DESCRIPTION:
#+KEYWORDS:

* Fire Modeling
:PROPERTIES:
:andere_VL: Fire Modeling
:END:
** Calculation 1
Please add $1$ and $2$ together.

*** Results
#+BEGIN_SRC python :session results: value
res =  1 + 2
res
#+END_SRC

#+RESULTS:
: 3

The results is $$1 + 2 = 3$$

*** Results - Variables
#+BEGIN_SRC python :session results: value
res =  variable1 + variable2
res
#+END_SRC

#+RESULTS:
: 3

The results is $$variable1 + variable2 = variableResults$$

** Calculation 2


This looks as a screenshot like:

The above example includes two sections for "results". In the first one the plain numbers are displayed for clarity (I hope). In the second section "Results - Variables" I try to show how the variables need to be accessed.

The idea is now that I am able to set and use variables in the actual text, which is then used for the python calculation. This would be step one.

And the next step would be to include the python result inside the resulting latex equation.

### Update

Maybe a macro approach is suitable and convenient; a simple example is shown below:

** Calculation 2 - Macros
#+MACRO: var1 16
#+MACRO: var2 16
Please add {{{var1}}} and ${{{var2}}}$ together.
*** Results - Variables - MACROS
This works without {{{var1}}}, but only without math...!?

#+BEGIN_SRC python :session :results
{{{var1}}}
#+END_SRC


The corresponding screenshot is

This results as final tex in something like:

\subsection{Calculation 2 - Macros}
Please add 16 and $${{{var2}}}$$ together.
\subsubsection{Results - Variables - MACROS}
This works without 16, but only without math\ldots{}!?


So this could be a good approach, when I can use it for the latex math enviroment and also within python src region.

### Update -end

Does anyone have an idea how to achieve this? Maybe there exists already another add-on for this or similar task!? I would be happy about any kind of suggestion!

With jupyter there seems to be the option for the second part...

• I don't understand what you are trying to achieve. Is it that you want a template with some variables that get expanded by setting them somewhere? In your example, why are you putting numbers in math mode (e.g. between )? Is the eventual goal to export to pdf for the students, or will they be using org-files too? Commented Mar 4, 2021 at 13:52
• yes, I would like to export to pdf for students. I put the number into math mode for the example because we would have usually some like $\SI{20}{\kW}$ or similar... hope that clarifies it a bit, thank you! Commented Mar 4, 2021 at 13:57

I don't think you can use macros in a src-block, or in a latex snippet. You can do what you want with some preprocessing, and a special syntax for the expansions.

For example with this org file, and using the mustache template syntax (https://github.com/Wilfred/mustache.el/blob/master/mustache.el):

** Calculation 2 - Macros
#+var1: 16
#+var2: \SI{20}{\kW}

Please add {{var1}} and ${{var2}}$ together.

*** Results - Variables

#+BEGIN_SRC python :session
{{var1}}
#+END_SRC


You can set up this before parsing hook function to render the template

(require 'mustache)
(require 'ht)

(defun preprocess (_)
(let ((kws (eval (ht ,@(org-element-map (org-element-parse-buffer 'element)
'keyword
(lambda (elem)
;; org returns an upcased keyword, I downcase it here.
;; I don't know a way to reliably match case here
;; you need a convention to only downcase.
(list (downcase (org-element-property :key elem))
(org-element-property :value elem))))))))
(message "KWS: %S" kws)
(setf (buffer-string) (mustache-render (org-no-properties (buffer-string)) kws))))



Which for me generates this LaTeX:

\section{Calculation 2 - Macros}
\label{sec:org8139f6c}
Please add 16 and $$\SI{20}{\kW}$$ together.

\subsection{Results - Variables}
\label{sec:orge8e7364}

\begin{minted}[frame=lines,fontsize=\scriptsize,linenos]{python}
16
\end{minted}
\end{document}
`

You might be able to use other template solutions, e.g. s-format, etc, as long as it is compatible with the latex snippets.

One downside of this is you cannot execute the src block with the {{var}} syntax in it. I couldn't see an easy way to get the expansion in the text and in the src block though otherwise.

• Dear John, thank you very much, I will check your example and get back as soon as possible. Commented Mar 5, 2021 at 7:28
• Yes, it certainly works for me; I could test it now; unfortunately the downside of not being able to execute the src block would be quite helpful. I will further look and will post any update as soon as I find an option or a good work around. The execution of the python would help to quickly calculate the results based on new input numbers. Commented Mar 6, 2021 at 19:40
• you could add some code to the export hook that evaluates all the src blocks before you finish the export. Otherwise you would have to add some kind of advice in org-babel to expand the body using this template before running it, e.g. after org-babel-expand-src-block runs. Commented Mar 6, 2021 at 19:53