1

I am generating a tikz necklace graph and I want to label the edges, and for the life of me I can't figure out how from the manual.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{circular}

\begin{document}

\begin{tikzpicture} [baseline=-6mm, field/.style={font=\footnotesize,circle,fill=black, minimum size=4mm, inner sep=0mm}, wall/.style={font=\footnotesize,circle,draw=black, fill=white, minimum size=4mm, inner sep=0mm}] \graph [simple necklace layout, node sep=2mm, node distance=0mm, nodes={field,as=},horizontal=C to B] { A[wall, label=above:1] -- B[label=below:2] -- C[label=below:3] -- A, C -- D[label=above:4], }; \end{tikzpicture}

\end{document}

I would like for the labels to appear next to the edge on the outside of the graph. Any suggestions would be greatly appreciated.

  • please upload code which can compile – js bibra Mar 05 '21 at 23:46
  • This compiles fine for me. – scmartin Mar 06 '21 at 02:43
  • 1
    Please add a sketch of the expected output and probably a screenshot of your current output, as well. A clarification of the description "appear next to the edge on the outside of the graph" would also be helpful. If I compile your document, I get the following: https://i.stack.imgur.com/J9gpH.png. From the description in your question, I guess, you want something like this: https://i.stack.imgur.com/v5A0y.png If this assumption correct? – leandriis Mar 06 '21 at 09:13
  • @leandriis, not quite. I can manipulate the node labels fine, I need to add labels to the edges. My answer below shows a solution I came up with, but I'm not sure how to customize it (e.g. changing the distance of the label or its position relative to the edge). – scmartin Mar 06 '21 at 16:13
  • What does the :1, :2, etc. notation mean? – Bill Nace Apr 01 '22 at 03:18
  • @BillNace Those are the labels on the nodes of the graph. The "above", "below", etc. that comes before is the position of the node label. – scmartin Apr 02 '22 at 01:19

2 Answers2

1

I think graphdrawing may be overkill for this kind of example. Here is a solution with tikz-cd:

enter image description here

\documentclass{article}

\usepackage{tikz-cd} \tikzset{field/.style={circle,fill=black, minimum size=4mm, inner sep=0mm}, wall/.style={circle,draw=black, fill=white, minimum size=4mm, inner sep=0mm}}

\begin{document} \begin{tikzcd}[cells={nodes={}},row sep=1cm, column sep=1cm] |[field,label=above left:4]|\arrow[d,-,"f_{34}"'] & |[wall,label=above right:1]|\arrow[d,-,"f_{12}"]\arrow[dl,-]\ |[field,label=below left:3]|\arrow[r,-,"f_{23}"'] & |[field,label=below right:2]| \end{tikzcd} \end{document}

You can add many options to the line labels by including {<stuff>} after the quotes "<label>". For example, modifying the f_{34} label:

\arrow[d,-,"f_{34}"'{pos=.7,xshift=-8pt,red}]

enter image description here

Sandy G
  • 42,558
0

After reading this question, I found that using the tikz quotes library \usetikzlibrary{quotes} gives me the basic functionality I need, as in the example below, but I don't really understand what the quotes library is doing, or how to customize position, etc. using this library. Any other methods to accomplish this, or explanations of how to manipulate the labels on the edges would be appreciated.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing,quotes}
\usegdlibrary{circular}

\begin{document}

\begin{tikzpicture} [baseline=-6mm, font=\small, label distance=-1mm, field/.style={circle,fill=black, minimum size=4mm, inner sep=0mm}, wall/.style={circle,draw=black, fill=white, minimum size=4mm, inner sep=0mm}] \graph [simple necklace layout, node sep=3mm, node distance=0mm, nodes={field,as=},horizontal=C to B] { A[wall, label=above right:1] --["( f_{12} )"] B[label=below right:2] --["( f_{23} )"] C[label=below left:3] -- A, C --["( f_{34} )"] D[label=above left:4], }; \end{tikzpicture}

\end{document}