1

This is now part three of what will most likely be a several part questions, first of which was answered here, second of which was answered here. Now that I've begun to start using expl3 more extensively in my document, I'd like to know how to work with multiple variables within a new document command. The example I'm working with, for instance, is trying to write an expression that will ultimately go under a square root radical. Given by:

\documentclass[12pt]{article}
\usepackage{xfp}
\ExplSyntaxOn
\NewDocumentCommand{\underradical}{}
 {
  % Attempting: Setting a value to correspond to || or nothing
  \int_set:Nn \l_tmpa_int_abs { \int_rand:nn { 1 } { 2 } }
  % if odd indicator, use |, else do nothing
  \int_if_odd:nTF \l_tmpa_int_abs { | } {}
  % the variable
  x
  % Attempting: Setting a variable to correspond to an x shift or not
  \int_set:Nn \l_tmpa_int_val { \int_rand:nn { 1 } { 2 } }
  % Attempting: Setting a variable to correspond to a left or right shift
  \int_set:Nn \l_tmpa_int_dir { \int_rand:nn { 1 } { 2 } }
  % a random sign
  \int_if_odd:nTF \l_tmpa_int_val {
                                    \int_if_odd:nTF \l_tmpa_int_dir { + } { - }
                                    } {}
  % the absolute value of the summand
  \int_if_odd:nTF \l_tmpa_int_val { \int_rand:nn { 1 } { 3 } }
  % if odd exponent use |, else do nothing
  \int_if_odd:nTF \l_tmpa_int_abs { | } {}
 }

\ExplSyntaxOff

\begin{document} \underradical \end{document}

Where there are three distinct variables being used here, \l_tmpa_int_abs, \l_tmpa_int_val, and \l_tmpa_int_dir. However, this does not yield a desired output it gives an output of 2x22, or 1x22, or any combination as such where each 2 also has an equal likelihood of being a 1. I thought I was implementing the naming scheme correctly based off page 3 and 4 of the expl3 documentation, but obviously this is unlikely as my output is not correct. I can at least, get one of the 2s or 1s to actually being printed as the desired symbol or number, but only if I change their variable to be \l_tmpa_int. Naturally, I cannot have all three variables declared this to work as the compiler won't know which one I'm referring to. I would like to know how to work with multiple variables within a given new document command to allow me to fix my issue.

David G.
  • 205
  • 1
    Except for missing the declaration of the integer variables (\int_new:N <var>), it seems you are missing the false branch in the \int_if_odd:nTF below the absolute value of the summand – Phelype Oleinik Oct 15 '20 at 23:46
  • @David G. It appears that you have forgotten to accept the answer. – PatrickT Mar 26 '23 at 22:55

1 Answers1

5

Three observations:

  1. You have to declare all variables you are using with \..._new:N (In your case \int_new:N) or a similar function. (This isn't necessary with \l_tmpa_int because that one is predefined in expl3.)

  2. The naming scheme has the type of the variable at the end and the name in the middle. So e.g. \l_tmpa_int_abs has type int and therefore could be better named \l_underradical_abs_int. (Where underradical could be any name identifying you module.) There is no reason to name your variables tmpa if they have a defined function. (tmpa stands for temporary variable A, it's used to name prefedfined variables for temporary use if you don't want to declare "proper" names)

  3. In

    \int_if_odd:nTF \l_tmpa_int_val { \int_rand:nn { 1 } { 3 } }
    

    the false block is missing. You can fix this by adding {} (or some other code) at the end of the line or, if the block would be empty anyway, you can use \int_if_odd:nT instead. (For conditional macros ending with TF there are corresponding macros ending only with T or F for cases where the other branch should be empty.)

Applying all three points would result in

\documentclass[12pt]{article}
\usepackage{xfp}
\ExplSyntaxOn
\int_new:N \l_underradical_abs_int
\int_new:N \l_underradical_val_int
\int_new:N \l_underradical_dir_int
\NewDocumentCommand{\underradical}{}
 {
  % Attempting: Setting a value to correspond to || or nothing
  \int_set:Nn \l_underradical_abs_int { \int_rand:nn { 1 } { 2 } }
  % if odd indicator, use |, else do nothing. Yould use \int_if_odd:nT,
  % I left \int_if_odd:nTF to show that this works too
  \int_if_odd:nTF \l_underradical_abs_int { | } {}
  % the variable
  x
  % Attempting: Setting a variable to correspond to an x shift or not
  \int_set:Nn \l_underradical_val_int { \int_rand:nn { 1 } { 2 } }
  % Attempting: Setting a variable to correspond to a left or right shift
  \int_set:Nn \l_underradical_dir_int { \int_rand:nn { 1 } { 2 } }
  % a random sign
  \int_if_odd:nT \l_underradical_val_int {
    \int_if_odd:nTF \l_underradical_dir_int { + } { - }
  }
  % the absolute value of the summand
  \int_if_odd:nT \l_underradical_val_int { \int_rand:nn { 1 } { 3 } }
  % if odd exponent use |, else do nothing
  \int_if_odd:nT \l_underradical_abs_int { | }
 }

\ExplSyntaxOff

\begin{document} \underradical \end{document}