12

I'm relatively new to TikZ and I drew the following graph. However, there is some space around the filled black node. Is there a way to have the lines actually connecting with the node?

EDIT: I'm not sure why it doesn't compile and show the graph in here? Maybe someone could please help me with this?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.8]
\node (1) at (0.4, 2.2) [circle,draw] {1};
\node (4) at (0.4, 0.6) [circle,draw] {4};
\node (3) at (2.6, 0.6) [circle,draw] {3};
\node (7) at (1.5,1.5) {};
\node (2) at (2.6, 2.2) [circle,draw] {2};

\fill (7) circle (2.5pt);

\draw[-] (1) to (7);
\draw[-] (4) to (7);
\draw[-] (3) to (7);
\draw[-] (2) to (7);

\end{tikzpicture}
\end{document}

enter image description here

  • 2
    Welcome to TeX.SX! The code you upload here doesn't get compiled automatically. What you can do is upload a picture of the results yourself. You'll have to convert the pdf to something else (png or jpeg should be fine). – Skillmon Nov 16 '18 at 8:55
  • Thank you! I've added a picture for the sake of completeness. – SallyOwens Nov 16 '18 at 9:14
12

You can control the space added with the inner sep key:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.8]
\node (1) at (0.4, 2.2) [circle,draw] {1};
\node (4) at (0.4, 0.6) [circle,draw] {4};
\node (3) at (2.6, 0.6) [circle,draw] {3};
\node[inner sep=0pt] (7) at (1.5,1.5) {};
\node (2) at (2.6, 2.2) [circle,draw] {2};

\fill (7) circle (2.5pt);

\draw[-] (1) to (7);
\draw[-] (4) to (7);
\draw[-] (3) to (7);
\draw[-] (2) to (7);

\end{tikzpicture}
\end{document}

enter image description here

  • Thank you. I also have another graph as the one above only that there are two black nodes and a line in the middle. If I add [inner sep=0pt] as you suggested to both nodes, the middle line however appears to be darker than the rest - is there a way to prevent this? – SallyOwens Nov 16 '18 at 9:26
  • 1
    @SallyOwens that might be a problem of the rendering of your PDF viewer. You could try to zoom in more on that line to really check that. Or use another viewer (xpdf is known to be very precise in rendering). – Skillmon Nov 16 '18 at 9:28
  • Thank you - this migth be the problem. When zooming in, they all appear equally dark/bold :) I just wanted to check that it is no a problem caused by my code. – SallyOwens Nov 16 '18 at 9:29
7

You are already using circular node shapes, so why not here? And you can put the filled circle in the middle with barycentric cs: (but since you are naming the ones 1, 2, 3, 4, the syntax is here a bit amusing, which is the main reason why I am posting this ;-). And \foreach can make your life easier.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.8]
\node (1) at (0.4, 2.2) [circle,draw] {1};
\node (4) at (0.4, 0.6) [circle,draw] {4};
\node (3) at (2.6, 0.6) [circle,draw] {3};
\node (2) at (2.6, 2.2) [circle,draw] {2};
\node[inner sep=2.5pt,circle,fill] (7) at (barycentric cs:1=1,2=1,3=1,4=1) {};

\foreach \X in {1,...,4}
{\draw[-] (\X) to (7);}

\end{tikzpicture}
\end{document}

enter image description here

  • 1
    +1 for barycentric coordinates. it is very, very rare visible here :-) – Zarko Nov 16 '18 at 15:23
  • @Zarko Thanks! (I actually posted it because of 2=1. If one would use \node[inner sep=2.5pt,circle,fill] (7) at (barycentric cs:1=1,2=2,3=3,4=4) {}; instead, it wouldn't work. ;-) – marmot Nov 16 '18 at 15:25
  • 1
    This question draws the attention of all TikZ association stakeholders. – Diaa Nov 16 '18 at 18:04
5
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.8]
\node (1) at (0.4, 2.2) [circle,draw] {1};
\node (4) at (0.4, 0.6) [circle,draw] {4};
\node (3) at (2.6, 0.6) [circle,draw] {3};
\node (7) at (1.5,1.5) {};
\node (2) at (2.6, 2.2) [circle,draw] {2};

\node[circle, fill=black, draw=black, minimum size=2.5pt] (7) at (1.5, 1.4) {};

\draw[-] (1) to (7);
\draw[-] (4) to (7);
\draw[-] (3) to (7);
\draw[-] (2) to (7);

\end{tikzpicture}
\end{document}

You forgot \begin{document} and you should use a node for the circle as well, where you specify the size and color in the node properties.

  • For an equivalent to circle(2.5pt), minimum size should be 5pt. And you need to fix inner sep=0pt otherwise default inner sep makes a node larger than 5pt. Finally, outer sep=0pt is needed to avoid a little gap between lines and central node. \node[circle, fill=black, minimum size=5pt, inner sep=0pt, outer sep=0pt] (7) at (1.5,1.5) {}; – Ignasi Nov 16 '18 at 10:59
  • Ups sorry, if you also draw the node, outer sep=0pt is not needed. \node[circle, draw, fill=black, minimum size=5pt, inner sep=0pt] (7) at (1.5,1.5) {}; – Ignasi Nov 16 '18 at 11:01
5

You can define (7) as coordinate instead of node to solve this.

\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
\node (1) at (0.4, 2.2) [circle,draw] {1};
\node (4) at (0.4, 0.6) [circle,draw] {4};
\node (3) at (2.6, 0.6) [circle,draw] {3};
\coordinate (7) at (1.5,1.5) {};
\node (2) at (2.6, 2.2) [circle,draw] {2};

\fill (7) circle (2.5pt);

\draw (1) to (7);
\draw (4) to (7);
\draw (3) to (7);
\draw (2) to (7);
\end{tikzpicture}
\end{document}

enter image description here

4

using polar coordinates and \foreach loop make your code simpler and shorter ...

\documentclass[tikz, margin=3.141592mm]{standalone}

\begin{document}
    \begin{tikzpicture}[scale=0.8,
circ/.style = {circle, draw},
 dot/.style = {circle, fill, inner sep=2.5pt}
                        ]
\node (n7) [dot]    {};
\foreach \i [count=\ii] in {135, 45, 315, 225}
\node (\ii) [circ] at (\i:16mm) {\ii};
\draw (1) -- (3)    (2) -- (4);
\end{tikzpicture}
\end{document}

enter image description here

4

I would like to join the celebration by offering an additional solution!

The middle node (7) is defined when drawing the first diagonal (1)--(3).

\draw (1) -- (3) node [midway,fill] (7){};

I also factorized the style applied to the nodes with the key every node/.style={circle,draw}

capture

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.8,every node/.style={circle,draw}]]
\node (1) at (0.4, 2.2) {1};
\node (4) at (0.4, 0.6) {4};
\node (3) at (2.6, 0.6) {3};
\node (2) at (2.6, 2.2) {2};
\draw (1) -- (3) node [midway,fill] (7){};
\draw (2) to (7)to (4);
\end{tikzpicture}
\end{document}
  • 1
    +1 since the 4 edges are aligned, that is, 3 nodes are colinear. – Sigur Nov 16 '18 at 17:49
2

You can write \draw (1)--(7.center). enter image description here

\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
\node (1) at (0.4, 2.2) [circle,draw] {1};
\node (4) at (0.4, 0.6) [circle,draw] {4};
\node (3) at (2.6, 0.6) [circle,draw] {3};
\node (7) at (1.5,1.5) {};
\node (2) at (2.6, 2.2) [circle,draw] {2};

\fill (7) circle (2.5pt);

\draw (1) to (7.center);
\draw (4) to (7.center);
\draw (3) to (7.center);
\draw (2) to (7.center);
\end{tikzpicture}
\end{document}

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.