2

I am having an issue I have never had before where code I wrote and works fine on my PC using Texmaker (MiKTeX) throws errors when copy-pasted into Overleaf.

This is the code:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\def\chair#1#2#3#4{
\begin{scope}[shift={#1},rotate={#2-90},scale={0.5*#3}]
\draw (-1.1,1) to[out=30,in=150] (1.1,1);
\draw (-1,0.75) to[out=30,in=150] (1,0.75) -- (1,-0.75) to[out=-150,in=-30] (-1,-0.75) to cycle;
\draw (0,0) node {#4};
\end{scope}
}

\pagenumbering{gobble}
\centering
\begin{tikzpicture}[line width=0.5mm,scale=1.1]
\def\a{12}
\def\s{2}
\foreach \i in {0,...,\a-1}
    \chair{({\i*180/(\a-1)}:{1.5+\s})}{\i*180/(\a-1)}{0.75}{}; % (this is line 20)
\end{tikzpicture}
\end{document}

In Texmaker it renders exactly as I would like:

enter image description here

But when that exact code is copy-pasted into Overleaf the \foreach statement does not iterate properly as shown below.

enter image description here

(Note that the messed up anti-aliasing indicates that the 12 figures were stacked on top of each other.)

And I am given the following two error messages:

enter image description here

Based on this post I suspect this has something to do with the version used by Overleaf, but I am unsure how to change this. If it is not possible, how can I amend my code to render properly in the version Overleaf uses?

PGmath
  • 310

2 Answers2

4

Here is a fix:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\def\chair#1#2#3#4{
\begin{scope}[shift={#1},rotate={#2-90},scale={0.5*#3}]
\draw (-1.1,1) to[out=30,in=150] (1.1,1);
\draw (-1,0.75) to[out=30,in=150] (1,0.75) -- (1,-0.75) to[out=-150,in=-30] (-1,-0.75) to cycle;
\draw (0,0) node {#4};
\end{scope}
}

\pagenumbering{gobble}
\centering
\begin{tikzpicture}[line width=0.5mm,scale=1.1]
\def\a{12}
\def\s{2}
\foreach \i in {0,...,{\numexpr\a-1}}
    \chair{({\i*180/(\a-1)}:{1.5+\s})}{\i*180/(\a-1)}{0.75}{}; % (this is line 20)
\end{tikzpicture}
\end{document}

I just added the \numexpr command for the \a-1 that could not be read directly. Possibly in newer editions of tikz, could, but is also suggested (in my opinion) to give it there as an already calculated parameter or at least inside {}.

Output: Your output.

koleygr
  • 20,105
  • I also suggest to place the content of your \foreach command inside curly braces ({}). It is just one command but sometime later you could possibly try to add more commands and this could create problems. Even if not it is more readable inside of them. – koleygr Mar 25 '19 at 00:18
3

One nice thing about TikZ is that it has pics for precisely this purpose. The origin of the error has already hinted at by koleygr: yes, indeed you cannot have \a-1 in \foreach. (AFAIK, however, the \numexpr trick goes back to Symbol 1, at least I learned it from this answer of his.) But there is no need for this here.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\pagenumbering{gobble}
\centering
\begin{tikzpicture}[line width=0.5mm,scale=1.1,
pics/chair/.style={code={%
\draw (-1.1,1) to[out=30,in=150] (1.1,1);
\draw (-1,0.75) to[out=30,in=150] (1,0.75) -- (1,-0.75) to[out=-150,in=-30] (-1,-0.75) to cycle;
\draw (0,0) node {#1};
}}]
\def\a{12}
\def\s{2}
\foreach \i in {1,...,\a}
{\path ({(\i-1)*180/(\a-1)}:{1.5+\s}) pic[scale=0.4,rotate={-90+(\i-1)*180/(\a-1)}]{chair};}
\end{tikzpicture}
\end{document}

enter image description here