2

I'm trying to draw the graph of a function, which I'm interested in, and for this I need to recalculate the value of a variable in a for-loop. Following the suggestion from this thread, I use the for-loop which comes with the math library. For my computations I also need the binary expansion of the counter of the loop and for this I use the bitset package. However, I cannot make it work and I do not get what's wrong. Here is a MWE:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{math}
\usepackage{pgfmath,pgffor}
\usepackage{bitset}

\begin{document}
\begin{tikzpicture}
\foreach \i in {1, ..., 10}{%
    \bitsetSetDec{mybitset}{\i};

    \tikzmath{
        \x = 0; 
        \y = 0;

        for \j in {0,...,3}{
             \b = \bitsetGet{mybitset}{\j};
             \x = \x + \b;
             \y = \y +2* \b;
       };
    };
    \draw[red,thick] (\x, \y) -- ({\x +0.25}, \y);
}
\end{tikzpicture}
\end{document}

It seems to me that the problem is in this line:

\b = \bitsetGet{mybitset}{\j};

but I don't see how to fix it. Any suggestions? Thanks already in advance!

1

Welcome! There are basically two issues:

  1. You cannot just expect to be able to assign the value of a macro like this.
  2. It seems that you cannot go with \j larger than \i.

To solve the first problem, you can declare a function that returns the value. This function is called bitsetGet and it expects the first argument to be a string delimited by "...". Restricting the loop variable such that the second problem does not arise one gets

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{math}
\usepackage{bitset}
\pgfmathdeclarefunction{bitsetGet}{2}{\begingroup
\edef\temp{\noexpand\edef\noexpand\pgfmathresult{\bitsetGet{#1}{#2}}}%
\temp
\pgfmathsmuggle\pgfmathresult
\endgroup}
\begin{document}
\begin{tikzpicture}
\foreach \i in {4, ..., 10}{%
    \bitsetSetDec{mybitset}{\i};
    \tikzmath{
        \x = 0; 
        \y = 0;     
         for \j in {0,1,2,3}{
               \b = bitsetGet("mybitset",\j); 
              \x = \x + \b;
              \y = \y +2* \b;
        };
        %print {$x=\x, y=\y$};
    };
    %\typeout{\x,\y}
    \draw[red,thick] (\x, \y) -- ({\x +0.25}, \y);
}
\end{tikzpicture}
\end{document}

enter image description here

I am assuming here that this is a tikzmath question. Obviously there are much simpler ways to get this output. However, I started to realize only recently how powerful \pgfmathdeclarefunction can be. (Here its powers are not used. It is really convenient when drawing some stuff and parsing the coordinates with new tricks.)

3
  • Isn't \pgfmathdeclarefunction{bitsetGet}{2}{\edef\pgfmathresult{\bitsetGet{#1}{#2}}} easier?
    – egreg
    Apr 16 '20 at 10:13
  • @egreg Yes, it is, thanks! I would still prefer to keep the \begingroup and \endgroup to be prepared for using this when the user also wants to use the fpu library. One can then locally switch on a non-fpu number format.
    – user194703
    Apr 16 '20 at 18:09
  • Great, thanks a lot!
    – matzas
    Apr 16 '20 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.