## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ tikz algebraic expressions

Information and discussion about graphics, figures & tables in LaTeX documents.
mtanner
Posts: 9
Joined: Sat Jul 22, 2017 9:55 am

### tikz algebraic expressions

I am trying to do something which I think is very elementary, but cannot seem to figure out the correct syntax. Basically I want to define some variables that specify the dimensions of a simple item. I want to draw several instances of that item, So I am using a \foreach and within the \foreach I want to do some simple coordinate calculations. So I am trying the following.

\begin{tikzpicture}
\def \CUSTWDTH {0.15cm}; % customer width
\def \CUSTHGHT {0.3cm}; % customer height
\def \CUSTSPCE {0.1cm}; % customer spacing
\def \YC {0cm}; % vertical centre of diagram
\def \N {6};
\foreach \I in {1,...,\N}
{
\def \X {(\I-1)*(\CUSTWDTH+\CUSTSPCE)+\CUSTWDTH)}}
\draw[fill=gray!80] (\X,0) ellipse (\CUSTWDTH and \CUSTHGHT);
\draw[fill=gray!80] (\X,0) circle (\CUSTWDTH);
}
\end{tikzpicture}

The \def within the \foreeach seems cause errro messages. I have tried various things, including the following

\newcommand{\set}[2]{\pgfmathsetmacro{#1}{#2}};
\set {\X} {(\I-1)*(\CUSTWDTH+\CUSTSPCE)+\CUSTWDTH)}

but nothing seems to work. On the ellipse statement I get a message saying the "and" is not recognised.

I have trawled the web, and there are plenty of sophisticated entries on doing clever things within a \foreach, but nothing that addresses just doing some simple arithmetic.

One thing I am confused about is the \def statement. Is it a variable declaration, or can it be used as an assignment statement? And then how do I do a simple assignment statment such as \X+\Y+\z?

LaTeXguide.org • LaTeX-Cookbook.net

Bartman
Posts: 303
Joined: Fri Jan 03, 2020 2:39 pm

### tikz algebraic expressions

In the first variant with \def you need a second pair of braces so that the parenthesis around the components of the coordinates can be distinguished from those for the factors in a mathematical expression.

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\def \CUSTWDTH {0.15cm}; % customer width
\def \CUSTHGHT {0.3cm}; % customer height
\def \CUSTSPCE {0.1cm}; % customer spacing
\def \YC {0cm}; % vertical centre of diagram
\def \N {6};
% The command doesn't have to be in the loop
%\def \X {{(\I-1)*(\CUSTWDTH+\CUSTSPCE)+\CUSTWDTH}}

\foreach \I in {1,...,\N}{
\def \X {{(\I-1)*(\CUSTWDTH+\CUSTSPCE)+\CUSTWDTH}}
\draw[fill=gray!80]
(\X,0) ellipse ({\CUSTWDTH} and \CUSTHGHT)
(\X,0) circle (\CUSTWDTH)
;
}
\end{tikzpicture}
\end{document}

With the second variant you would have to either omit the units of the previously determined commands for the evaluation of the expression or use the \pgfmathsetlengthmacro command.

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\def \CUSTWDTH {0.15cm}; % customer width
\def \CUSTHGHT {0.3cm}; % customer height
\def \CUSTSPCE {0.1cm}; % customer spacing
\def \YC {0cm}; % vertical centre of diagram
\def \N {6};

\foreach \I in {1,...,\N}{
\pgfmathsetlengthmacro{\X}{(\I-1)*(\CUSTWDTH+\CUSTSPCE)+\CUSTWDTH}
\draw[fill=gray!80]
(\X,0) ellipse ({\CUSTWDTH} and \CUSTHGHT)
(\X,0) circle (\CUSTWDTH)
;
}
\end{tikzpicture}
\end{document}

No space is output between a command and the following string, i.e. \CUSTWDTH should be placed in braces to avoid an error message.

You can skip this measure if you replace the outdated specification of the values in parenthesis by the recommended one with brackets and options. Read section 14.6 "The Circle and Ellipse Operations" to find out more.