6

I'm very new to this site and just about as new to LaTeX. The question is simple: how do I get some text to appear above a node rather than inside of it? For example, I used this tikz stuff to make a cyclic graph:

\begin{figure}
\centering
\begin{tikzpicture}[scale=.7, every node/.style={circle,fill=blue!20}]
\node (n1) at (0,0) {1};
\node (n2) at (3,0) {2};
\node (n3) at (1.5,3) {3};
\draw (n1) -- (n2) -- (n3) -- (n1);
\end{tikzpicture}
\end{figure}

My attempts thus far have brought me to this:

\begin{figure}[h]
\centering
\begin{tikzpicture}
\filldraw [blue!20] (.5,0) circle (8pt);
\node (n1) at (.2,.5) {$x$};
\filldraw [blue!20] (3.5,0) circle (8pt);
\node (n2) at (3.75,.4) {$x-1$};
\filldraw [blue!20] (1.75,3) circle (8pt);
\node (n3) at (1.75,3.5) {$x-2$};
\draw (.5,0) -- (3.5,0) -- (1.75,3) -- (.5,0);
\end{tikzpicture}
\end{figure}

However, I don't like the use of \filldraw directive for the "nodes" as much as the \node directive. I copy/pasted from some website the stuff I started using tikz and so I'm not sure how to take the "globally applied" properties/settings and make them applicable to just a single node. I tried a few things, but this didn't help.

What I'm after is, basically, the effect of the second drawing. However, I'd like it to be better polished.

Thanks,
Andy

1
  • 1
    Welcome to TeX.SE. In general, you make your question easier to answer if you create a Minimal Working Example (MWE), which can directly be compiled without having to add \documentclass{...} and so on. My answer shows how such an MWE can look.
    – hbaderts
    Oct 8 '16 at 22:58
12

First, you can define styles for only specific nodes by

\begin{tikzpicture}[scale=.7,
    mycirc/.style={circle,fill=blue!20, minimum size=0.5cm}
    ]

and apply this style to a node by create it with

\node[mycirc] at (0,0) {};

Then, you can create labels for a node with

\node[mycirc, label={$x-1$}] at (0,0) {};

and you can define where this label is printed with

\node[mycirc, label=right:{$x-1$}] at (0,0) {};
\node[mycirc, label=left:{$x-1$}] at (0,0) {};
\node[mycirc, label=above:{$x-1$}] at (0,0) {};
\node[mycirc, label=below:{$x-1$}] at (0,0) {};

or even in any arbitrary angle with

\node[mycirc, label=165:{$x-1$}] at (0,0) {};

where 165 is the angle in degrees. 0 corresponds to right, 90 to above and so on.

So, with these changes, a minimal working example (MWE) of your graph looks like this:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=.7,
    mycirc/.style={circle,fill=blue!20, minimum size=0.5cm}
    ]
    \node[mycirc,label=left:{$x$}] (n1) at (0,0) {};
    \node[mycirc,label=311:{$x-1$}] (n2) at (3,0) {};
    \node[mycirc,label=above:{$x-2$}] (n3) at (1.5,3) {};
    \draw (n1) -- (n2) -- (n3) -- (n1);

\end{tikzpicture}
\end{document}

result

2
  • 1
    That is beautiful. Thank you: precisely what I was looking for. In the future I'll post with MWE. BTW, I didn't see how it's rendered in the markup help. Do folks just post a screen capture of their tex? Oct 9 '16 at 16:44
  • 1
    Yes, the website can not render LaTeX code. I just compiled it in my LaTeX editor, and uploaded a print-screen of the result as an image. If you post an MWE, we can just copy-paste your code to our editor and start working, without having to think what packages are needed and so on.
    – hbaderts
    Oct 9 '16 at 17:36

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.