0

I have a following graph constructed with tkz-berge library. In the current settings the nodes/vertices are numbered automatically started with index 0. I wonder how to reset the counter in order to start with 1?

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{tkz-berge}
\begin{document}
\begin{tikzpicture}
\begin{tikzpicture}
\GraphInit[vstyle=Normal]
\SetUpVertex[Math,Lpos=-180,LabelOut]
\SetVertexNormal[FillColor=red,OuterSep=0pt]
\grEmptyPath[form=2,x=0,y=0,RA=2,rotation=90,prefix=u,Math]{5}
\SetUpVertex[Lpos=0]
\SetVertexNormal[FillColor=blue]
\grEmptyPath[form=2,x=6,y=0,RA=2,rotation=90,prefix=v,Math]{4}
\Edges(u4,v3,u3,v2,u1)
\Edges(u3,v3,u0,v0,u2,v1)
\end{tikzpicture}
\end{tikzpicture}
\end{document}

Figure-1

2

The node counter is used throughout the code of tkz-berge to calculate positions and to generate internal node names, so when you change the counter all kinds of things break down. However, you can renew (i.e., modify) the lower level node printing command \write@math which calls \Vertex (defined by the package tkz-graph, which is loaded by tkz-berge) with the node label as one of the arguments. Using \pgfmathtruncatemacro you can add 1 to the node counter, store the result in a macro, and print that macro in the label instead of the counter itself. Note that \pgfmathtruncatemacro should be used instead of \pgfmathsetmacro to avoid adding .0 in the label.

MWE:

\documentclass{article}
\usepackage{tkz-berge}
\makeatletter
\renewcommand*{\write@math}[3]{%
            \pgfmathtruncatemacro{\printindex}{#3+1}
            \Vertex[x = #1,y = #2,%
                    L = \cmdGR@cl@prefix\grMathSep{\printindex}]{\cmdGR@cl@prefix#3}}
\makeatother
\begin{document}
\begin{tikzpicture}
\GraphInit[vstyle=Normal]
\SetUpVertex[Math,Lpos=-180,LabelOut]
\SetVertexNormal[FillColor=red,OuterSep=0pt]
\grEmptyPath[form=2,x=0,y=0,RA=2,rotation=90,prefix=u,Math]{5}
\SetUpVertex[Lpos=0]
\SetVertexNormal[FillColor=blue]
\grEmptyPath[form=2,x=6,y=0,RA=2,rotation=90,prefix=v,Math]{4}
\Edges(u4,v3,u3,v2,u1)
\Edges(u3,v3,u0,v0,u2,v1)
\end{tikzpicture}
\end{document}

Result:

enter image description here

  • Works like a charm! – Andrej Mar 8 at 12:21

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.