2

I'm using tikz in LyX. I'm trying to build a basic graph. Is it possible to somehow add the path of the nodes without setting the location of each one?

For now I used:

\begin{tikzpicture}
  [scale=.8,auto=left,node/.style={circle,draw}]
  \node[circle,draw] (A) at (0,0) {$v_1$};
  \node[circle,draw] (B) at (4,0) {$v_2$};
 \draw[->] (A)--(B) node[midway,above]{$e_0$};
\end{tikzpicture}

Output:

enter image description here

But is it possible to remove the at (4,0) part so tikz will generator the graph alone? I don't really care where the nodes are located, I just care about the exitness of them and the paths between them. The desired code looks something like:

\begin{tikzpicture}
  [scale=.8,auto=left,node/.style={circle,draw}]
  \node[circle,draw] (A) {$v_1$};
  \node[circle,draw] (B) {$v_2$};
 \draw[->] (A)--(B) node[midway,above]{$e_0$};
\end{tikzpicture}

And the output should be the same (maybe 90 degree different but same logic).

The main goal is to just declare the path and tikz will do the hard work for me.

I was using the WebGraphviz (Graphviz in the Browser). There I could just declare the paths it would build the graph for me (without setting the locations). I understood that it is not possible to use Graphviz in LyX and everyone who uses it, uploads an image instead. I prefer to build the nodes so it would look better.

Is it possible to somehow create graph without declaring the coordinates for each node?

1
  • Yes, it seems to me you are looking for the chains library.
    – user121799
    Commented Jan 20, 2019 at 22:53

2 Answers 2

2

The graphs library allows you not to worry about the location of nodes (see page 408 et seq. of manual 3.1).

graph

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

\begin{document}

\begin{tikzpicture}
\graph[grow right=30mm,nodes={circle,draw}] {
1 ->[edge label={a}] 2   ->[edge label={b}] 3;
1 -- [bend right,edge label'={c}] 3;
};
\end{tikzpicture}

\end{document}
1

A very minimal realization of your suggestion might be

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[start chain,c/.style={circle,draw,join},every join/.style={-latex}]
\node [c,on chain] {A};
\node [c,on chain] {B};
\end{tikzpicture}
\end{document}

enter image description here

A slightly more sophisticated version could be

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{chains,decorations.markings}
\newcounter{eindex}
\begin{document}
\begin{tikzpicture}[node distance=2.4cm,start chain,%
midmark/.style={postaction={decorate,decoration={markings,
mark=at position 0.5 with {\stepcounter{eindex}
\node[anchor=south] at (0,0.1){$e_{\theeindex}$};}}}},
c/.style={circle,draw,join},every join/.style={-latex,midmark}]
\node [c,on chain] {A};
\node [c,on chain] {B};
\node [c,on chain] {C};
\end{tikzpicture}
\end{document}

enter image description here

Of course the chains can go in any direction and there are many more things, as you can see in section 48 of the pgfmanual.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .