TransWikia.com

Calling a Global Variable using Expl3

TeX - LaTeX Asked on April 9, 2021

I’ve written a macro for my document that producing a random discontinuous but otherwise smooth curve, given a few randomly assigned points. I then ask what the output is for particular (not random) values of the input, this is using expl3 and TikZ. Now I’m trying to take one of the randomly generated outputs using in the macro and reference it in the document but I can’t seem to figure out how to do it, and haven’t found anything explicit enough for me to understand. This is what I’ve written:

documentclass[12pt]{article}
usepackage[utf8]{inputenc}
usepackage[margin=0.75in]{geometry}
usepackage[utf8]{inputenc}
usepackage{graphicx}


usepackage{amsmath,amssymb,enumitem}
usepackage[first=-20, last=20]{lcg}
usepackage{tikz}
usepackage{tkz-euclide}

usepackage{calculator}
usepackage{calculus}
usepackage{ifthen}
usepackage{xfp}

usepackage{pgfplots}
pgfplotsset{ every non boxed x axis/.append style={x axis line style=-}, every non boxed y axis/.append style={y axis line style=-}}

newcommand{randi}{randarabic{rand}}

ExplSyntaxOnnewcommand{intR}[2]{chgrand[first=#1, last=#2] randi}ExplSyntaxOff

usepackage{pgffor}

newcommand{addsub}{pgfmathrandomitem{choice}{choices1}choice}
pgfmathdeclarerandomlist{choices1}{{+}{-}}

newcommand{subnone}{pgfmathrandomitem{choice}{choices3}choice}
pgfmathdeclarerandomlist{choices3}{{}{-}}

ExplSyntaxOn

int_new:N l_LeftEnd_int
int_new:N l_LeftMid_int
int_new:N g_hole_int
int_new:N l_RightEnd_int
NewDocumentCommand{smoothplot}{}
{
int_set:Nn l_LeftEnd_int { int_rand:nn { 1 } { 5 } }
int_set:Nn l_LeftMid_int { int_rand:nn { -2 } { 2 } }
int_set:Nn l_RightEnd_int { int_rand:nn { -4 } { 5 } }
int_gset:N g_hole_int { l_LeftMid_int }
begin{tikzpicture}
begin{axis}[
        width=1textwidth,
        height=0.5textwidth,
        grid=major,
        xmax=4.5,
        xmin=-4.5,
        ymin=-4.5,
        ymax=5.5,
        xtick={-4,-3,-2,-1,0,1,2,3,4},
        ytick={-4,-3,-2,-1,0,1,2,3,4,5},
        ylabel={$g(x)$},
        xlabel={$x$}
          ]
addplot [smooth, red, mark=none, thick] coordinates
    {(-4, l_LeftEnd_int) (-3,2*rand-2) (-2,2*floor{rand}) (-1,3*rand+1) (0,3*floor{rand}) (1, l_LeftMid_int+2) (2,l_LeftMid_int)};
    
addplot [red, only~marks] coordinates {(-4, l_LeftEnd_int) (2, l_LeftMid_int) (4, l_RightEnd_int)};

addplot [red, mark=o] coordinates {(2, l_LeftMid_int+2)};

addplot [smooth, red, mark=none, thick] coordinates {(2, l_LeftMid_int+2) (2.7, rand+3) (3.3, 2*rand+1.5) (4, l_RightEnd_int)};

end{axis}
end{tikzpicture}
}

NewDocumentCommand{hole}{} % This is the part that I'm trying to get to reference the variable
{
g_hole_int
}

ExplSyntaxOff

begin{document}

section*{Problem 3}

smoothplot 
Find the values for $x  = -4, -2, 0, 2, 4$.
Find the domain and range of $g(x)$.
% ADD{hole}{2}{temp}sol % This line doesn't work, but I'll revisit this
% hole
For what value(s) of $x$ does $g(x) =$ hole + 2
end{document}

Output

Sorry for the blurriness

One Answer

I added int_use:N infrom of my global variable for the NewDocumentCommand{hole}, also I changed the variable names:

ExplSyntaxOn

int_new:N l_point_LeftEnd_int
int_new:N l_point_LeftMid_int
int_new:N g_point_LeftMid_int
int_new:N l_point_RightEnd_int
NewDocumentCommand{smoothplot}{}
{
int_set:Nn l_point_LeftEnd_int { int_rand:nn { 1 } { 5 } }
int_set:Nn l_point_LeftMid_int { int_rand:nn { -2 } { 2 } }
int_set:Nn l_point_RightEnd_int { int_rand:nn { -4 } { 5 } }
int_gset:Nn g_point_LeftMid_int { l_point_LeftMid_int }
begin{tikzpicture}
begin{axis}[
        width=1textwidth,
        height=0.5textwidth,
        grid=major,
        xmax=4.5,
        xmin=-4.5,
        ymin=-4.5,
        ymax=5.5,
        xtick={-4,-3,-2,-1,0,1,2,3,4},
        ytick={-4,-3,-2,-1,0,1,2,3,4,5},
        ylabel={$g(x)$},
        xlabel={$x$}
          ]
addplot [smooth, red, mark=none, thick] coordinates
    {(-4, l_point_LeftEnd_int) (-3,2*rand-2) (-2,2*floor{rand}) (-1,3*rand+1) (0,3*floor{rand}) (1, l_point_LeftMid_int+2) (2,l_point_LeftMid_int)};
    
addplot [red, only~marks] coordinates {(-4, l_point_LeftEnd_int) (2, l_point_LeftMid_int) (4, l_point_RightEnd_int)};

addplot [red, mark=o] coordinates {(2, l_point_LeftMid_int+2)};

addplot [smooth, red, mark=none, thick] coordinates {(2, l_point_LeftMid_int+2) (2.7, rand+3) (3.3, 2*rand+1.5) (4, l_point_RightEnd_int)};

end{axis}
end{tikzpicture}
}

NewDocumentCommand{hole}{}
{
ADD{ int_use:N g_point_LeftMid_int }{ 2 }sol~
sol
}

ExplSyntaxOff

Could someone comment on why I needed to include int_use:N for the hole command but not for when calling the variables in my smoothplot command?

Answered by David G. on April 9, 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