TransWikia.com

expl3 get expresion evaluation afterward

TeX - LaTeX Asked by Enderdead on June 3, 2021

I’m working on a latex exam class using expl3 language.
Each section is representing an exercise with several questions in it.
An exercise is defined with a given number of point.

From this structure, I want to automatically, display the total number of point for the given exercise.

For instance, the result would be :

structure example

I want to provide a latex interface like this one :

excercise

question{2}
...

question{2}
...

In order to implement this interface, I need to find out a way to perform an expression evaluation postponed to the very last moment of the compilation process. This solution would enable us to get all question value defined later in the source code.

The following source code is my attempt to implement this idea.

ExplSyntaxOn
seq_new:N g_points_per_question_seq % Store the point of each section
int_zero_new:N g_current_sec_int % Counter of the current exercise




DeclareDocumentCommand{addpoint} {m} % Add the arg to the current exercise total point 
{
    pt_add_point:Nn g_points_per_question_seq {#1} 
}

  
cs_new_protected:Npn pt_add_point:Nn #1 #2 { %  intern method
    group_begin:
        tl_set_eq:NN l_foo_tl c_empty_tl %  create a temp token variable

        seq_gpop_right:NN g_points_per_question_seq l_foo_tl  % get the current exercise total point
        
        fp_set:Nn l_tmpk_fp  {tl_head:N l_foo_tl  } % Create a local float variable for addition purpose
        fp_add:Nn l_tmpk_fp  { #2 } % Add the argument value to the local variable
        
        seq_gput_right:Nx g_points_per_question_seq { fp_to_tl:N l_tmpk_fp } % Push back the new total point of the current exercise
    group_end:
}



DeclareDocumentCommand{flush} {} % Declare a new exercise 
{
    seq_gput_right:Nn g_points_per_question_seq {0.0} % Set the total point of the new exercise
    int_gadd:Nn g_current_sec_int 1 % Incr the exercise number
}


DeclareDocumentCommand{displaytotalpoint} {} % Display the total point of the current exercise.
{
     seq_item:Nn  g_points_per_question_seq  {int_use:N g_current_sec_int}  % Get the associeted seq element 
}

ExplSyntaxOff


newcommand{exercise}{
flush
section{Exercise : displaytotalpoint~points}
}
newcommand{question}[1]{
addpoint{#1}
subsection{Question : #1 points}
}



begin{document}

maketitle


exercise

question{2.0}
.....
question{2.0}
.....

The result is the following one :

bad result

As you can see, the total point is not good since it has to be equals to 4. However, when I plot the sequence state at the end, it successfully contains the number 4 at the first index.

So I guess that I need to postpone the seq_item instruction after the evaluation of all questions that belongs to the current exercise.

How can I avoid as long as possible the expression expand of this statement in order to get what I want?

 seq_item:Nn  g_points_per_question_seq  {int_use:N g_current_sec_int} 

Have you an idea of how to implement this structure?

Huge thank’s to those who will read and answer me!

One Answer

This sort of things are normally done by writing to the aux-file. A simple variant would be

documentclass{article}
begin{document}
Exercise ref{ex:points}pt

Questions 

Questions 
makeatletter 
def@currentlabel{4}% or for example your `int` variable. 
label{ex:points}%label  
makeatother

end{document}

More sophisticated variants are possible, e.g. with the zref-package, but the principle stays the same.

Answered by Ulrike Fischer on June 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP