Graphics, Figures & Tables ⇒ tikz algebraic expressions
tikz algebraic expressions
\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?
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
tikz algebraic expressions
\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.Code: Select all
\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}
\pgfmathsetlengthmacro
command.Code: Select all
\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}
\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.