2

I am drawing a small graph using TikZ and would like to show the name of the node inside the node. Based on this post, I follow the same logic. Here is a minimal example.

\documentclass{beamer} %
\usetheme{CambridgeUS}
\usepackage[latin1]{inputenc}
\usepackage{tikz}

\usecolortheme{beaver}
\begin{document}
\begin{frame}
     \begin{tikzpicture}[scale=.5,minimum size=2mm ,every node/.style={circle,fill=black}, text=white, minimum size=0.45 cm]
  \node (n1)[color= red] at (0,-3) {};
  \node (n2) at (0,3) {};
  \node (n3) at (3,6) {};
  \foreach \from/\to in {n1/n2,n2/n3}
    \draw[thick](\from) -- (\to);  
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

Now, when I want to show the node labels, I place my text inside {} as \node (n1)[color= red] at (0,-3) {1};. This line makes my node larger rather than printing the node label. I was wondering what I might be doing wrong. Also, my intention is to show the labels within the node (i.e., inside the circle).

enter image description here

1

In order to see why your red node grows, let's make the text visible by adding text=black. (Recall that color=red also changes the text color.)

\documentclass{beamer} %
\usetheme{CambridgeUS}
\usepackage[latin1]{inputenc}
\usepackage{tikz}

\usecolortheme{beaver}
\begin{document}
\begin{frame}
     \begin{tikzpicture}[scale=.5,minimum size=2mm ,every
     node/.style={circle,fill=black},text=white,minimum size=0.45 cm]
  \node (n1)[color= red,text=black] at (0,-3) {1};
  \node (n2) at (0,3) {};
  \node (n3) at (3,6) {};
  \foreach \from/\to in {n1/n2,n2/n3}
    \draw[thick](\from) -- (\to);  
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

The circle has grown to host the 1.

If you want to label the node, use label.

\documentclass{beamer} %
\usetheme{CambridgeUS}
\usepackage[latin1]{inputenc}
\usepackage{tikz}

\usecolortheme{beaver}
\begin{document}
\begin{frame}
\begin{tikzpicture}[scale=.5, ,nodes={circle,fill=black,minimum size=3mm}, 
every label/.append style={rectangle,text=black},text=white]
  \node (n1)[color= red,label=right:1] at (0,-3) {};
  \node (n2) at (0,3) {};
  \node (n3) at (3,6) {};
  \foreach \from/\to in {n1/n2,n2/n3}
    \draw[thick](\from) -- (\to);  
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

If you want the nodes all the same size, add some text width and text height. Also use fill=red instead of color=red to only change the fill color.

\documentclass{beamer} %
\usetheme{CambridgeUS}
\usepackage[latin1]{inputenc}
\usepackage{tikz}

\usecolortheme{beaver}
\begin{document}
\begin{frame}
     \begin{tikzpicture}[scale=.5,minimum size=2mm ,every
     node/.style={circle,fill=black,text width={width("1")},
        text height={height("1")}},text=white]
  \node (n1)[fill= red] at (0,-3) {1};
  \node (n2) at (0,3) {};
  \node (n3) at (3,6) {};
  \foreach \from/\to in {n1/n2,n2/n3}
    \draw[thick](\from) -- (\to);  
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

5
  • Thanks for the recommendation, but I actually need to show the labels inside each node.
    – whitepanda
    Mar 4 '20 at 2:23
  • @whitepanda Then you need text=black (or any other color that is visible when surrounded by red).
    – user194703
    Mar 4 '20 at 2:25
  • Thanks! How can I define it globally? I will have several nodes.
    – whitepanda
    Mar 4 '20 at 2:26
  • 1
    @whitepanda Just use fill=red instead of color=red. Then you will not overrule the fill color. I added an example of that type, which also makes sure that all the nodes have the same size.
    – user194703
    Mar 4 '20 at 2:29
  • It should of course read "Then you will not overrule the text color."...
    – user194703
    Mar 4 '20 at 3:17

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.