1

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".

1 Answers1

3

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

  • For whatever reason, when I tried something like this earlier the expressions weren't evaluated properly; I had decided that the issue was using pgf in the context was the issue, hence the weird approach. In any case, this works! Thanks for the help. – Ben Grossmann Dec 18 '20 at 03:46