1

I have defined (with the help of egreg: What is wrong in this definition of a newcommand with variable number of parameters in its argument?) this newcommand:

\newcommand{\st}[3]{%
 \begin{center}
            \begin{tikzpicture}[scale=.5, line width=1pt]
                \draw (0 , 0) -- (0, 2*dim{{#3}}+2);
                \draw [fill=gray!25] (0 , 0) circle(1.6);
                \draw (0, 0) node {{\large \textbf{#2}}};
                \foreach \superscript [count=\ni from 2] in {#3} {%
                %\draw (-\ni, 2*\ni) -- (\ni, 2*\ni);
                \draw [->] (-\ni, 2*\ni) -- (dim{{#3}}+5, 2*\ni);
                \draw [fill=black] (0, 2*\ni) circle(.2);
                \draw [fill=white] (\ni , 2*\ni) circle(1);         
                \draw [fill=white] (-\ni, 2*\ni) circle(1);
                \draw (-\ni, 2*\ni) node {\superscript};
                \draw (\ni, 2*\ni) node {\the\numexpr#1/(2*#2)/\superscript};
                \pgfmathparse{#1/(2*#2)/\superscript}
                \draw ((dim{{#3}}+5, 2*\ni) node [draw, rectangle, fill = gray!10, right]{here the mathematical result};
                %\draw ((dim{{#3}}+5, 2*\ni) node {\the\numexpr#2*{{\pgfmathresult-\superscript^2}}};
                }
            \end{tikzpicture}
        \end{center}
    }

Running it as \st{252}{1}{9,7,6,3,2,1} we get the following diagram:

enter image description here

Im trying to write a mathematical result where I have written "here the mathematical result". The mathematical operations are the following: #2 * (square of number on the right node - square of number on the left node). #2 is the variable written inside the great circle (in gray) that in this example is #2 = 1. For example, for the pair of nodes in the top, we must calculate, 1 * (126² - 1²), for the second line, 1*(63² - 2²), ..., and so on.

I still don't know enough about these work environments and am always getting error messages on my attempts.

1 Answer 1

2

I would rather use nodes and a bit less of repetition, and xfp.

\documentclass{article}
\usepackage{tikz}
\usepackage{xfp}
\begin{document}
\newcommand{\st}[3]{%
\begin{center}
  \begin{tikzpicture}[scale=.5, line width=1pt,c/.style={circle,draw,minimum
  size=1cm},f/.style={circle,fill,minimum size=2mm}]
      \draw (0 , 0) -- (0, 2*dim{{#3}}+2);
      \draw [fill=gray!25] (0 , 0) circle(1.6);
      \draw (0, 0) node {{\large \textbf{#2}}};
      \pgfmathtruncatemacro{\mydim}{dim{{#3}}}
      \foreach \mysuperscript [count=\myni from 2] in {#3} {%
      \path (-\myni, 2*\myni) node[c](l-\myni) {$\mysuperscript$}
       -- node[f]{} (\myni, 2*\myni) node[c](r-\myni) {$\the\numexpr#1/(2*#2)/\mysuperscript$}
      (\mydim+5, 2*\myni) 
      node [draw, rectangle, fill = gray!10, right] (x-\myni)
      {$\fpeval{#2*((#1/(2*#2)/\mysuperscript)^2-1)}$}
      (r-\myni) edge (l-\myni) edge[-latex] (x-\myni) ;
      }
  \end{tikzpicture}
\end{center}}
\st{252}{1}{9,7,6,3,2,1}    
\end{document}

enter image description here

2
  • Thank you very much, user238301!. The formula was $(\fpeval{#2*((#1/(2*#2)/\mysuperscript)^2-\mysuperscript^2) and I had to modify the styles a bit so that the circles of the nodes have the same size and so that the circles filled in black are smaller: [scale=.5, line width=1pt,c/.style={circle,draw, minimum size=1cm, inner sep=2pt},f/.style={circle,fill,minimum size=2mm, inner sep=0pt}]. Commented Apr 1, 2021 at 20:22
  • However, although I still need a lot of studies to understand how Latex / Tikz works, your solution has been very useful to me, and although I find it very sophisticated, I have been able to make those small adjustments to obtain the desired result and even expand it a bit. Many thanks. Best regards from Spain. Commented Apr 1, 2021 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .