# tkz-berge, renumbering vertices

This TikZ code for drawing an icosahedron and labeling gives only a partial answer to my question.

With the following code

\documentclass{article}

\usepackage{tkz-berge}

\begin{document}

\begin{tikzpicture}

\grPath[Math,prefix=v,RA=-1,RS=0]{9}
\begin{scope}[xshift=0.5 cm]
\grPath[Math,prefix=y,RA=-1,RS=1]{10}
\end{scope}
\begin{scope}[xshift=1.5 cm]
\grPath[Math,prefix=y,RA=-1,RS=2]{10}
\end{scope}

\end{tikzpicture}

\end{document}


we have vertices numbered from $x_0$, $y_o$, $z_0$. Is it possible to number them from $x_1$ and so on, without manual labelling about 30 positions?

Here's a more dynamic approach in which you do not have to write out each of the vertices' names.

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tkz-berge}

\def\myInstance#1#2{$#1_{#2}$}
\newcommand\aeAssignLabels{%%
\let\myexecutable\relax
\foreach \myn in {#3}
{%%
\ifx\myexecutable\relax
\xdef\myexecutable{\myInstance{#2}{\myn}}%%
\else
\xdef\myexecutable{\expandonce\myexecutable,\myInstance{#2}{\myn}}%%
\fi
}%%
\edef\myexecutable{\noexpand#1{#2}{\expandonce\myexecutable}}%%
\myexecutable
}

\begin{document}

\begin{tikzpicture}
\SetVertexNoLabel
\grPath[Math,prefix=x,RA=-1,RS=0]{9}
\aeAssignLabels\AssignVertexLabel{x}{9,...,1}%%
\begin{scope}[xshift=0.5 cm]
\grPath[Math,prefix=y,RA=-1,RS=1]{10}
\aeAssignLabels\AssignVertexLabel{y}{10,...,1}%%
\end{scope}
\begin{scope}[xshift=1.5 cm]
\grPath[Math,prefix=z,RA=-1,RS=2]{10}
\aeAssignLabels\AssignVertexLabel{z}{10,...,1}%%
\end{scope}
\end{tikzpicture}

\end{document}


The idea here is to use \foreach, which comes for free when using just about anything from TikZ. It is possible to write this without using the etoolbox package, but it's much loopier and harder to follow. Nevertheless, using the etoolbox package tools, you can control when expansion of the various tokens occurs and build up the assignment macro and then execute it.

The macro I define has the following syntax:

 \aeAssignLabels<control sequence>{<variable name>}{<range of numbers>}


Using the \foreach loop to cycle through the range, you can build up the macro to be executed. Since each cycle of pgf's \foreach loop occurs within a group, you need to globally make the assignments. This is done via \xdef which is equivalent to \global\edef which expands the token in its definition at the time of the definition.

And, while this may seem just as much work as hand writing in each of the vertex names, hopefully this code illustrates a method you can then easily duplicate elsewhere and truly cut some corners.

The answer that you linked to does tell you how to add the labels. You first need to stop the labels being printed using \SetVertexNoLabel and then you need to manually specify the labels that you want using \AssignVertexLabel: Here's the full code to manually change the labels:

\documentclass{article}
\usepackage{tkz-berge}

\begin{document}

\begin{tikzpicture}
\SetVertexNoLabel
\grPath[Math,prefix=x,RA=-1,RS=0]{9}
\AssignVertexLabel{x}{$x_9$,$x_8$,$x_7$,$x_6$,$x_5$,$x_4$,$x_3$,$x_2$,$x_1$};
\begin{scope}[xshift=0.5 cm]
\grPath[Math,prefix=y,RA=-1,RS=1]{10}
\AssignVertexLabel{y}{$y_{10}$,$y_9$,$y_8$,$y_7$,$y_6$,$y_5$,$y_4$,$x_3$,$y_2$,$y_1$};
\end{scope}
\begin{scope}[xshift=1.5 cm]
\grPath[Math,prefix=z,RA=-1,RS=2]{10}
\AssignVertexLabel{z}{$z_{10}$,$z_9$,$z_8$,$z_7$,$z_6$,$z_5$,$z_4$,$x_3$,$z_2$,$z_1$};
\end{scope}
\end{tikzpicture}

\end{document}


Edit

When I first answered this question I didn't see the request to be able to do this without manually inserting all of the labels. Then I tried to work out how to do this "properly". I was able to construct the list of labels easily enough but I couldn't work out how to pass this list to \AssignVertexLabel until the solution of @A.Ellett was posted. Using the expansion trick from @A.Ellett's post here is an automated version that produces the same output as above.

  \documentclass{article}
\usepackage{tikz}
\usepackage{tkz-berge}
\usepackage{etoolbox}
\newcommand\AssignMyLabels{% \AssignMyLabels{symbol}{number}
\def\mylabels{$#1_{1}$}
\foreach\c in {2,...,#2} {
\xdef\mylabels{\mylabels, $#1_{\c}$}
}
\edef\doThemLabels{\noexpand\AssignVertexLabel{#1}{\expandonce{\mylabels}}}
\doThemLabels
}
\begin{document}

\begin{tikzpicture}
\SetVertexNoLabel
\grPath[Math,prefix=x,RA=-1,RS=0]{9}
\AssignMyLabels{x}{9}
\begin{scope}[xshift=0.5 cm]
\grPath[Math,prefix=y,RA=-1,RS=1]{10}
\AssignMyLabels{y}{10}
\end{scope}
\begin{scope}[xshift=1.5 cm]
\grPath[Math,prefix=z,RA=-1,RS=2]{10}
\AssignMyLabels{z}{10}
\end{scope}
\end{tikzpicture}

\end{document}

• It seems this is exactly what the user didn't want to have to do May 3 '15 at 2:50
• You can automate this using clever tricks with pgf's \foreach , \xdef, and the expansion tools from etoolbox. May 3 '15 at 2:51