0

I'am using tikz to draw a simple threshold logic unit.

I have the following code:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{basic/.style={draw,fill=blue!20,text width=1em,text badly centered}}
\tikzset{functions/.style={basic,circle,fill=blue!10}}
%\tikzset{inputs/.style={basic,circle,fill=blue!20}}

\begin{document}
\begin{tikzpicture}


\node (center) {y};
\node[functions,left=3em of center, anchor=center] (left) {\scriptsize $\theta$=5};
\path[draw,->] (left) -- (center);
            
            
\node[left=3em of left] (l2) {};
  
        
\node[below of=l2] (n) {$x_2$};
\path[draw,->] (n) -- node[below, rotate=34] {\scriptsize $\omega_2=4$} (left);
  
            
\node[above of=l2] (1) {$x_1$};
\path[draw,->] (1) -- node[above, rotate=-34] {\scriptsize $\omega_1=3$} (left);
 

\end{tikzpicture}
\end{document}

This generates the following output:

enter image description here

As you can see the text in the node is not centered. I triedtext centered and align=center and I triedto adjust the size of the node by using minimum width=8mm, inner sep=0mm,.

How can I adjust the text to be exactly centered? I believe the math mode makes trouble.

2 Answers 2

2

The text is actually centered, so it's not a problem of alignment, but a problem of \textwidth, if you remove it, the circle node can adapt to the content and, there you go, it's centered.

My suggestion is to remove text width=1em or increase its value from the style.

Here you can see what I mean:

enter image description here

and this is the code:

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{basic/.style={draw,fill=blue!20,text width=1em}}
\tikzset{functions/.style={basic,circle,fill=blue!10}}
%\tikzset{inputs/.style={basic,circle,fill=blue!20}}

\begin{document}
\begin{tikzpicture}

\node[functions, anchor=center] (left) {\scriptsize $\theta$=5};

\node[functions,above=3em of left, anchor=center, text width=% resets the text width
] (left) {\scriptsize $\theta$=5};
            
\end{tikzpicture}
\end{document}
1
  • You'r right.. Thanks!
    – Pablo
    Nov 9, 2020 at 12:37
0

Problem is already described in @Alenanno answer, i.e. your text in node function is wider than prescribed text width - consequently it spill-out on the right side of node. Solution is (ii) prescribe wider text width in basic node or (ii) not use basic in definition of the function style, as is done in suggested MWE below. In it are proposed mostly off topic suggestions:

  • used are syntax defined in the library positioning
  • style function is defined independent from basic
  • for edge labels is used quotes library
  • for is drawn from node function
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,
                quotes}

\tikzset{basic/.style = {draw,fill=blue!20, inner sep=1pt,
                         minimum width=1em, align=center},
     functions/.style = {basic,circle,fill=blue!10, font=\scriptsize},
every edge/.append style = {->},
every edge quotes/.style = {auto, inner sep=1pt, font=\scriptsize, sloped}
}

\begin{document}
    \begin{tikzpicture}[
node distance =1.5em and 3em
                        ]
\node[functions] (f) {$\theta$=5};
\node[above left=of f] (x1) {$x_1$};
\node[below left=of f] (x2) {$x_2$};
\node[right=of f]       (y)     {$y$};
%
\path   (x1) edge ["$\omega_1=3$"]      (f) 
        (x2) edge ["$\omega_2=4$" ']    (f)
        (f)  edge   (y);
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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