TransWikia.com

Generating a Star Diagram: Tikz Evaluation in LuaLatex

TeX - LaTeX Asked by Ben Grossmann on January 26, 2021

I have run the following code in LuaLatex. I’m doing everything on overleaf, if that matters.

documentclass{article}


usepackage{tikz}
usetikzlibrary{graphs,graphdrawing,arrows.meta,graphs.standard}
usegdlibrary{circular}

begin{document}


begin{tikzpicture}
    graph [
    simple necklace layout, node distance=1.5cm,
    nodes={draw,circle}
    ]
    {
        subgraph C_n [n=8]; 
        foreach i in {1,...,5}{
            i -- directlua{tex.sprint(3+i)};
        }
    };
end{tikzpicture}

vspace{20 pt}

begin{tikzpicture}
    graph [
    simple necklace layout, node distance=1.5cm,
    nodes={draw,circle}
    ]
    {
        subgraph C_n [n=8]; 
        foreach i in {1,...,5}{
            i -- directlua{tex.sprint(mod({3+i},8))};
        }
    };
end{tikzpicture}


end{document}

The two Tikz pictures have the same code except that in the second loop, 3+i is replaced by mod({3+i},8)). Because 3 + i % 8 is equal to 3 + i for i = 1,…,4, I should have ended up with essentially the same picture both times. However, here is what I end up with:

enter image description here

How can I fix this code so that the modular arithmetic is evaluated correctly?

Once I have that working, the idea is to extend the loop to {1,…,8} in order to get the full "star".

One Answer

I would just use pgf to compute the mod. Note that mod returns numbers starting from 0 so you probably want int(1+mod(2+i,8)) instead of int(mod(3+i,8)).

documentclass{article}


usepackage{tikz}
usetikzlibrary{graphs,graphdrawing,arrows.meta,graphs.standard}
usegdlibrary{circular}

begin{document}


begin{tikzpicture}
    graph [
    simple necklace layout, node distance=1.5cm,
    nodes={draw,circle}
    ]
    {
        subgraph C_n [n=8]; 
        foreach i in {1,...,5}{
            i -- directlua{tex.sprint(3+i)};
        }
    };
end{tikzpicture}

vspace{20 pt}

begin{tikzpicture}
    graph [
    simple necklace layout, node distance=1.5cm,
    nodes={draw,circle}
    ]
    {
        subgraph C_n [n=8]; 
        foreach i [evaluate=i as j using {int(1+mod(2+i,8))}]in {1,...,5}{
            i -- j;
        }
    };
end{tikzpicture}


vspace{20 pt}

begin{tikzpicture}
    graph [
    simple necklace layout, node distance=1.5cm,
    nodes={draw,circle}
    ]
    {
        subgraph C_n [n=8]; 
        foreach i [evaluate=i as j using {int(1+mod(2+i,8))}]in {1,...,8}{
            i -- j;
        }
    };
end{tikzpicture}

end{document}

enter image description here

Correct answer by user231225 on January 26, 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