2

Here's a minimal graph drawn in tikz, placing edge nodes with auto=left:

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

\begin{tikzpicture}
    \node at (0,0) (A) {A}; 
    \node at (2,1) (B) {B}; 
    \node at (3,0) (C) {C}; 
    
    \path[->] (A) edge node[auto=left] {x} (B);
    \path[->] (B) edge node[auto=left] {y} (C);
    \path[->] (C) edge node[auto=left] {z} (A);

\end{tikzpicture}

\end{document}

enter image description here

However, the edge nodes (x, y, z) are further away from the arrows than I would like. How can I adjust this distance?

To be clear: I don't want to manually fine-tune the location of each edge label. (e.g. I don't want to use "above", "below" etc. to place them in relation to the midpoint of the line, because that would have to be specified separately for each one.) Instead I want to use auto=left / auto=right or equivalent functionality, but just have it place the labels a bit closer to the edges. This is because my plots are generated automatically and I don't have the ability to manually adjust each one.

  • It feels like a hack, and it won't adjust correctly to the size of the label, but I'm able to adjust it with \path[->] (A) edge node[auto=left, inner sep=0.5mm] {x} (B);, which will do for now. I tried searching the manual for "auto=" and didn't find any information about fine tuning the placement, but of course it might be somewhere else in that huge pdf. – Nathaniel Aug 11 at 2:39
  • If you don't want fine tuning of the nodes' positions, why don't you have a look at the graphs library? – Felix Emanuel Aug 11 at 11:08
  • @FelixEmanuel I will fix my question. By "nodes" I meant the nodes that form the edge labels. (x, y and z in my figure, rather than A, B and C.) If I correctly understand what the graphs library is responsible for, it doesn't deal with the placement of those nodes at all. – Nathaniel Aug 11 at 12:50
  • If I understand correctly, you want the distance between the text (the label) and the arrow to be always the same, right? – AndréC Aug 11 at 13:03
  • @AndréC in an ideal world, yes. But I realise that's impossible, because TeX only knows the bounding box, not the actual shape of the letter. The solution inner sep solution in both the answers is actually fine - I will accept yours now – Nathaniel Aug 11 at 13:16
7

With TikZ, a node is a box that contains text. This default box is rectangular. If it perfectly frames the text, the edges of the box touch the text, which is unattractive.

By default TikZ leaves a space between the text and the edges of the box which is defined by the length ìnner sep (initially is 0.3333em).

screenshot

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
%\tikzset{every node/.style={draw}}
\begin{document}

\begin{tikzpicture}
\begin{scope}
    \node at (0,0) (A) {A}; 
    \node at (2,1) (B) {B}; 
    \node at (3,0) (C) {C}; 
    
    \path[->] (A) edge node[auto=left,draw,inner sep=3em] {x} (B);
    \path[->] (B) edge node[auto=left,draw] {y} (C);
    \path[->] (C) edge node[auto=left,draw,inner sep=0em] {z} (A);
\end{scope}


\begin{scope}[xshift=6cm]
    \node at (0,0) (A) {A}; 
    \node at (2,1) (B) {B}; 
    \node at (3,0) (C) {C}; 
    
    \path[->] (A) edge node[blue,auto=left,draw,inner sep=3em,label={[blue,above]:$\text{inner sep}=3em$}](bigx) {x} (B);
    \node[inner sep=0pt](smallx) at (bigx){x};
    \draw[|<->,red] (smallx.west)--(bigx.west)node[midway,below,font=\footnotesize]{$3em$};
    \path[->] (B) edge node[red,auto=left,draw,label={[red,above right,align=center]:initially\\$\text{inner sep}=0.3333em$}] {y} (C);
    \path[->] (C) edge node[violet,auto=left,draw,inner sep=0em,label={[violet,below,yshift=-5pt]:$\text{inner sep}=0em$}] {z} (A);
\end{scope}


\end{tikzpicture}

\end{document}

When the node is a circle, this circle is circumscribed to the rectangular node which is drawn by default.

screenshot

\documentclass[border=5mm,tikz]{standalone}

\begin{document}
\begin{tikzpicture}[]
\node[draw=red] (a) at (0,0){A};
\node[draw=blue,circle] (a) at (0,0){A};
\node[align=center,font=\tiny] at (0,-.8) {same node with \textcolor{blue}{circle}\\ and \textcolor{red}{rectangular} shape};
\end{tikzpicture}

\end{document}
| improve this answer | |
  • 1
    +1 excellent -this further increase my education – js bibra Aug 11 at 6:20
  • Thank you very much. – AndréC Aug 11 at 6:25
3

I believe you are referring to the labels above the edges/arrows

Each label has been defined as a node -- a node always has some reserved space around it defined by the key inner sep

The key can be set globally at the beginning including the auto key so as to move the label and all nodes close to the edges/ or further

\begin{tikzpicture}[inner sep=1pt,auto=left, node distance=2cm,>=latex']
....
\end{tikzpicture}

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\begin{document}

\begin{tikzpicture}[inner sep=1pt,auto=left, node distance=2cm,>=latex']
    \node at (0,0) (A) {A}; 
    \node at (2,1) (B) {B}; 
    \node at (3,0) (C) {C}; 
    
    \path[->] (A) edge node {x} (B);
    \path[->] (B) edge node{y} (C);
    \path[->] (C) edge node {z} (A);

\end{tikzpicture}

\end{document}

An even better option is to define labels as labels rather than nodes -- please see my answer here --

https://tex.stackexchange.com/a/558016/197451

The same code modified (without inner sep invocation)

\begin{tikzpicture}[]
    \node at (0,0) (A) {A}; 
    \node at (2,1) (B) {B}; 
    \node at (3,0) (C) {C}; 
    
    \path[draw] (A) edge[->] node [pos=0.5, above,  sloped,](){x} (B);
    \path[draw] (B) edge[->] node [midway, label={[label distance=-13pt, 
    rotate=45]135:$y$}](){}(C);
    \path[draw] (C) edge[->] node [midway, label={[label distance=-6pt, 
    rotate=0]-90:z}](){} (A);

\end{tikzpicture}

enter image description here

| improve this answer | |

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.