TransWikia.com

how to add two variables in latex

TeX - LaTeX Asked by Alan Jones on December 25, 2020

Is it possible to add two unrelated variables that hold an integer value?

For example, I have this MWE

documentclass[addpoints]{exam}
usepackage{enumitem}
begin{document}
    
begin{enumerate}[label=textbf{arabic*}hspace{4mm}]
    item this is one
    item this is two
    item this is three
    
label{lst:num} % this will give me the number of bullet points I have (in this case its 3)
end{enumerate}

begin{questions}

question[2] this is one
question[3] this is two 
question[4] this is three

end{questions}


Total = numpoints + ref{label}  % 9+3 (from our bullet point) = 12 (the result I want to obtain)
end{document}

In the above example, I would like to add numpoints and ref{label} to produce the result 12. Is it possible?

One Answer

Here is a robust one:

  • the value of label lst:num is retrieved using getrefnumber from refcount package and
  • the value of points is retrieved using numpoints@exp.

Some explanations:

  • The outer most logic is to add two integers, hence we have thenumexpr <num1>+<num2>relax, where both <num1> and <num2> should be fully-expandable and leave an integer in input stream.
  • Here <num1> is the value printed by numpoints, which is defined as
    defnumpoints{@ifundefined{exam@numpoints}%
      {mbox{normalfontbfseries ??}}%
      exam@numpoints
    } 
    
    and is not expandable when exam@numpoints is undefined or equal to relax. Hence an expandable variant numpoints@exp is provided and it leaves 0 when exam@numpoints is undefined.
  • Here <num2> is the value stored in label lst:num. Similarly, ref{lst:num} is not expandable when label lst:num is not defined (for example in first run). getrefnumber{<label>} is the expandable variant of ref and it also returns 0 when <label> is not defined.

Update:

To use enumitem option label, two things are added:

  • enumitem option ref is used, which restores the plain format of the arabic "value" stored in an item label
  • a wrapper macro getrefnumber@wrap{<label>} is provided, which strips one level of braces (added by enumitem) around the "returned value" of getrefnumber.
documentclass[addpoints]{exam}
usepackage{enumitem}
usepackage{refcount}

makeatletter
newcommandgetTotalPoints[1]{%
  thenumexprnumpoints@exp+getrefnumber@wrap{#1}relax
}

% expandable variant of numpoints
defnumpoints@exp{%
  ifcsname exam@numpointsendcsnameexam@numpointselse0fi
}

% strip pair of braces from result of getrefnumber{<label>}
defgetrefnumber@wrap#1{%
  expandaftergetrefnumber@wrap@iexpanded{getrefnumber{#1}}@nil
}
defgetrefnumber@wrap@i#1@nil{#1}

defenit@reflabel#1#2{%
  ifnumenit@depth=@neelse % no level 0
    advanceenit@depth@ne
    @namedef{p@@enumctr}{}% Don't accumulate labels
    advanceenit@depthm@ne
  fi
  ifcase#2%
    @namedef{the@enumctr}{#1}%
  else
    enit@normlabel{csname the@enumctrendcsname}{#1}%
  fi}
makeatother

%usepackage{unravel}
providecommandunravel[1]{#1}
providecommandunravelsetup[1]{}
unravelsetup{max-action=1000, max-input=1000, max-output=1000}


begin{document}
  
begin{enumerate}[label=textbf{arabic*}hspace{4mm}, ref=arabic*]
    item this is one
    item this is two
    item this is three
    
    label{lst:num} % this will give me the number of bullet points I have (in this case its 3)
end{enumerate}

begin{questions}
  question[2] this is one
  question[3] this is two 
  question[4] this is three
end{questions}

Total = getTotalPoints{lst:num}
end{document}

enter image description here

Correct answer by muzimuzhi Z on December 25, 2020

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