4

I'm working on a figure of a graph with nodes. I put a white transparent box behind the node label using fill=white and opacity=0.8 and then I tried using text opacity=1.0 so that the text is still readable. The problem is that the node labels, created in a for-loop, end up having 1.0 instead of the correct number. The graph I get and the graph I want are pictured below:

Incorrect graph Correct graph

I haven't found any posts relating text opacity to pgfmathresult. Here is a MWE of the code I have:

\usepackage{xifthen}
\usetikzlibrary{backgrounds}
\begin{document}
    \begin{tikzpicture}
        [
          every label/.style={inner sep=0pt, opacity=0.8, text opacity=1.0, fill=white},
          cnode/.style={draw=black,fill=#1,minimum width=3mm,circle},
        ]
        \node[cnode=red,label=0:{$\hat y_1$}] (s1) at (6,-1) {};
        \node[cnode=red,label=0:{$\hat y_2$}] (s2) at (6,-2) {};
        \node at (6,-3) {$\vdots$};
        \node[cnode=red,label=0:{$\hat y_K$}] (sK) at (6,-4) {};

        \foreach \x in {0,...,4}
        {   
            \pgfmathparse{\x<4 ? \x : "q-1"}
            \ifthenelse{\x>0}
            {
                \node[cnode=gray,label={90:$z_{\pgfmathresult}$}] (h-\x) at (3,{-\x-div(\x,4)+.5}) {};
            }
            {
                \node[cnode=gray,label=90:$1$] (h-0) at (3,0.5) {};
            }
            \begin{scope}[on background layer]
              \draw (h-\x) --  (s1);
            \draw (h-\x) -- (s2);
            \draw (h-\x) --  (sK);
            \end{scope}
        }

        \node at (3,-3.5) {$\vdots$};

        \node[cnode=gray,label=90:$z_{M}$] (h-q) at (3,-5.5) {};
        \begin{scope}[on background layer]
        \draw (h-q) --  (s1);
        \draw (h-q) -- (s2);
        \draw (h-q) --  (sK);
        \end{scope}
    \end{tikzpicture}
\end{document}

Thanks in advanced.

4

The issue is that \pgfmathresult gets overwritten, so you may want to use \pgfmathsetmacro instead to store the result of the computation in a macro. (I also got rid of xifthen because it is not needed for integer comparisons, for which you can use a simple \ifnum.)

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
    \begin{tikzpicture}
        [
          every label/.style={inner sep=0pt, opacity=0.8, text opacity=1.0, fill=white},
          cnode/.style={draw=black,fill=#1,minimum width=3mm,circle},
        ]
        \node[cnode=red,label=0:{$\hat y_1$}] (s1) at (6,-1) {};
        \node[cnode=red,label=0:{$\hat y_2$}] (s2) at (6,-2) {};
        \node at (6,-3) {$\vdots$};
        \node[cnode=red,label=0:{$\hat y_K$}] (sK) at (6,-4) {};

        \foreach \x in {0,...,4}
        {   
            \pgfmathsetmacro{\myindex}{\x<4 ? \x : "q-1"}
            \ifnum\x>0
                \node[cnode=gray,label={90:$z_{\myindex}$}] (h-\x) at (3,{-\x-div(\x,4)+.5}) {};
            \else
                 \node[cnode=gray,label=90:$1$] (h-0) at (3,0.5) {};
            \fi
            \begin{scope}[on background layer]
              \draw (h-\x) --  (s1);
            \draw (h-\x) -- (s2);
            \draw (h-\x) --  (sK);
            \end{scope}
        }

        \node at (3,-3.5) {$\vdots$};

        \node[cnode=gray,label=90:$z_{M}$] (h-q) at (3,-5.5) {};
        \begin{scope}[on background layer]
        \draw (h-q) --  (s1);
        \draw (h-q) -- (s2);
        \draw (h-q) --  (sK);
        \end{scope}
    \end{tikzpicture}
\end{document}

enter image description here

5
  • Thank you for your quick response! Why does pgfmathresult get overwritten? – pwesterbaan Oct 2 '19 at 13:36
  • 1
    @pwesterbaan Because it gets used in basically all mathematical functions.When you say label=90:... TikZ computes the cosine and the sine of 90, and you get to see the result of sin(90). – user194703 Oct 2 '19 at 13:40
  • That is maybe a long-term thing to fix in the core. It's quite annoying and there are always people running into that. – Henri Menke Nov 23 '19 at 20:32
  • @HenriMenke Personally I feel that, if you are really to start modifying the core functions, there are much more urgent issues. For me personally this would be the possibility to switch locally to a slower fpu mode which avoids the dimension too large errors. But of course I do know that this is a gigantic task. – user194703 Nov 23 '19 at 23:49
  • @Schrödinger'scat You're right, that's an important issue as well, that the fpu is incompatible with drawing code. But the code for the fpu isn't so nice, so I haven't gotten around to looking at that yet. Maybe after I hand in my thesis. – Henri Menke Nov 24 '19 at 0:46
1

An another approach which not solve your problem with pgfmathparse. With slightly different nodes' positioning calculation with \pgfmathparse for nodes positions is omitted. For positioning are used chains, fit and positioning libraries only:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds,
                chains,
                fit}

\begin{document}
    \begin{tikzpicture}[
node distance = 6mm and 30mm,
  start chain = going below,
every label/.style = {label distance=2pt, inner sep=0.5pt, 
                      fill=white, fill opacity=0.8, text opacity=1},
      cnode/.style = {minimum size=3mm, on chain,
                      append after command = {\pgfextra{% this is used for correct positioning of nodes
                            \node[circle, draw, fill=#1, inner sep=0pt,
                                  fit=(\tikzlastnode)] {};}
                                              }% end of append after command
                     }
                        ]
\node (s1) [cnode=red,label=0:$\hat{y}_1$]  {}; 
\node (s2) [cnode=red,label=0:$\hat{y}_2$]  {};
\node (s3) [inner ysep=0pt, on chain]       {$\vdots$};
\node (s4) [cnode=red,label=0:$\hat{y}_K$]  {}; 
%
\node (h1) [cnode=green,label=$1$,                  
      above left = of  s1]    {};
\foreach \i in {1,2,4}
{
\scoped[on background layer]
\draw (h1) -- (s\i);
}
\foreach \i [count=\ii from 2] in {2,3,4,q-1,M}
{
\ifnum\ii=4 
    \node (h\ii) [inner ysep=0pt,on chain]   {$\vdots$};
\else
    \node (h\ii) [cnode=green,label=$z_{\i}$]    {};
\begin{scope}[on background layer]
    \draw (h\ii) -- (s1);
    \draw (h\ii) -- (s2);
    \draw (h\ii) -- (s4);
            \end{scope}
\fi
}
     \end{tikzpicture}
\end{document}

enter image description here

1
  • @cfr, thank you very much! – Zarko Nov 23 '19 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.