TransWikia.com

Tikz code that used to compile doesn't compile anymore after upgrade to Ubuntu 20.04 from Ubuntu 18.04

TeX - LaTeX Asked on November 28, 2020

I have a piece of code that compiles nicely in Ubuntu 18.04.
I upgraded to Ubuntu 20.04 and the very same code doesn’t compile anymore, while it still compiles in 18.04.
In both distribution I installed the official texlive and the same TeX-related packages.

Here is the code:

documentclass{standalone}
usepackage{tikz}           

newcommand{cycle}[1]{%
begin{tikzpicture}[baseline]
  foreach[count=n] v in {#1}; % count the number of elements
  pgfmathsetmacro{r}{n*0.2} % set the node distance from (0,0)
  pgfmathsetmacro{b}{120/n} % evaluate the bend angle
  foreach[count=i, evaluate=i as a using (i-1)*360/n ] v in {#1}
    node [circle, draw, font=scriptsize,inner sep=2pt] (i) at (a:r) {$v$};
    foreach i [remember=i as j (initially n)] in {1,...,n}
    draw[semithick,-stealth] (j) to [bend right=b] (i);
end{tikzpicture}
}

begin{document}
  cycle{1,2,3,4,5}
end{document}

The output (obtained in 18.04…) is the following:
enter image description here

The error in Ubuntu 20.04 is as follows:


! Undefined control sequence.
<argument> n 
              *0.2
l.18   cycle{1,2,3,4,5}
                        
? 

One Answer

The problem is with this line:

foreach[count=n] v in {#1}; % count the number of elements

I don't think that tikz ever promised to remember variables like this and I remember that there was a recent change that broke this behaviour. If you want to remember the value of n then you need to use something like:

foreach[count=c] v in {#1} {xdefn{c}}%

Note that you need to use xdef since foreach loops take place inside a TeX group.

Personally, I prefer putting in a few more braces so I would write your code as:

documentclass[tikz, border=2mm]{standalone}

newcommand{cycle}[1]{%
begin{tikzpicture}[baseline,scale=3]
    foreach[count=c] v in {#1} {xdefn{c}}% count the number of elements
    pgfmathsetmacro{r}{n*0.2} % set the node distance from (0,0)
    pgfmathsetmacro{b}{120/n} % evaluate the bend angle
    foreach[count=i, evaluate=i as a using (i-1)*360/n ] v in {#1} {
      node [circle, draw, font=scriptsize,inner sep=2pt] (i) at (a:r) {$v$};
    }
    foreach i [remember=i as j (initially n)] in {1,...,n}
    draw[semithick,-stealth] (j) to [bend right=b] (i);
end{tikzpicture}
}

begin{document}
  cycle{1,2,3,4,5}
end{document}

In any case, this produces your desired output:

enter image description here

Correct answer by user30471 on November 28, 2020

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