18

I wish to add a label above a node that already contains a label.

\begin{tikzpicture}[scale=0.8,every node/.style={draw=black,circle}]
    \node (a) at (0,0) {a};
    \node (b) at (2,0) {b};
    \draw[->] (a) to (b);
\end{tikzpicture}

The markup above will give me this:

tikzpicture1

But I want something like this:

tikz2

Is there any easy way to solve this? I am not looking for solutions using \tikzlibrary{background}

23

Actually, tikz nodes have text inside and labels outside:

label

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[scale=0.8,every node/.style={draw=black,circle}]
    \node[label={\small 1/16}] (a) at (0,0) {a};
    \node (b) at (2,0) {b};
    \draw[->] (a) to (b);
\end{tikzpicture}

\end{document}
  • Let me noted: since label is node, its features is influenced by options determined in every node style. To see this, change every node/.style={draw=black,circle} to every node/.style={draw, red,circle} ... Therefore it is sensible in similar cases to define labels' style with something as every label/.append style={rectangle, font=\small}. – Zarko Apr 20 '17 at 20:40
3

A PSTricks solution using the pst-node (for the drawing itself) and xfp packages (for the floating point calculations):

\documentclass{article}

\usepackage{pst-node}
\usepackage{xfp}

\def\labelSep{\fpeval{28.75*\radius+3.375}} % found by experimenting

% parameters
\def\arrowLength{1.8}
\def\radius{0.3}

\begin{document}

\begin{pspicture}(\fpeval{4*\radius+\arrowLength},\fpeval{2*\radius+0.35})
% change `0.35` in the calculation of the height of the bounding box
% according to the contents on top of the node
  \cnode(\radius,\radius){\radius}{A}
  \rput(A){a}
  \cnode(\fpeval{3*\radius+\arrowLength},\radius){\radius}{B}
  \rput(B){b}
  \ncline{->}{A}{B}
  \uput{\labelSep pt}[90](A){\small\texttt{1/16}}
\end{pspicture}

\end{document}

output

Notice that all you have to do is choose the values of the parameters and the drawing (including the bounding box) will be adjusted accordingly.

  • 1
    \ncline{->}{A}{B} – user2478 Dec 11 '14 at 21:38

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.