5

I am not very experienced in TIKZ but I have made a few graphs such as this.

enter image description here

When I try to change the names of the labels to greek letters I run into errors. Is there a way to modify my code where it'll change the labels to alpha, beta, etc?

Here is the code:

\documentclass{beamer}

\usepackage{tikz}

\usepackage{verbatim}
\usetikzlibrary{arrows,shapes}

\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\tikzstyle{vertex}=[circle,fill=black!25,minimum size=14pt,inner sep=0pt]
\tikzstyle{selected vertex} = [vertex, fill=red!24]
\tikzstyle{edge} = [draw,thick,-]
\tikzstyle{weight} = [font=\small]
\tikzstyle{selected edge} = [draw,line width=5pt,-,red!50]
\tikzstyle{ignored edge} = [draw,line width=5pt,-,black!20]

%Start of graph
\begin{tikzpicture}[scale=.7, auto,swap]
\foreach \pos/\name in {{(0,0)/a},{(2,0)/b},{(3,2)/c},{(2,4)/d},{(0,4)/e},
{(-1,2)/f},{(-1,-1)/1},{(-2,-2)/2},{(3,-1)/3},{(4,-2)/4},{(4,2)/5},
{(5,2)/6},{(-2,2)/7},{(-3,2)/8},{(-1,5)/9},{(-2,6)/10},{(3,5)/11},
{(4,6)/12}}
    \node[vertex] (\name) at \pos {$\name$}; 
% Connect vertices with edges
\foreach \source/ \dest /\weight in {a/b/, b/c/, c/d/, d/e/, 
e/f/,f/a/,a/1/,1/2/,b/3/,3/4/,c/5/,5/6/,f/7/,7/8/,e/9/,9/10/,d/11/,11/12/}
    \path[edge] (\source) -- node[weight] {$\weight$} (\dest);


\end{tikzpicture}
\end{document}
  • 2
    Welcome to TeX.SX! Please don't post code fragments. Instead, put your fragments into a complete compilable document that shows the problem. – Alan Munn Aug 16 '17 at 19:14
  • 2
    \foreach \pos/\name in {{(0,0)/\alpha},{(2,0)/\beta},{(3,2)/\gamma},...} \node[vertex] (\name) at \pos {$\name$}; ? – Zarko Aug 16 '17 at 19:17
4

You could split the first loop in two, introducing a third loop variable for the first part, representing the node text (\alpha, \beta, etc.).

enter image description here

\documentclass{beamer}

\usepackage{tikz}

\usepackage{verbatim}
\usetikzlibrary{arrows,shapes}

\begin{document}
\begin{frame}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\tikzset{
  vertex/.style={circle,fill=black!25,minimum size=14pt,inner sep=0pt},
  selected vertex/.style={vertex, fill=red!24},
  edge/.style={draw,thick,-},
  weight/.style={font=\small},
  selected edge/.style={draw,line width=5pt,-,red!50},
  ignored edge/.style={draw,line width=5pt,-,black!20}
}

%Start of graph
\begin{tikzpicture}[scale=.7, auto,swap]
\foreach \pos/\name/\txt in {{(0,0)/a/\alpha},{(2,0)/b/\beta},{(3,2)/c/\gamma},{(2,4)/d/\delta},{(0,4)/e/\epsilon},{(-1,2)/f/\zeta}}
    {
    \node[vertex] (\name) at \pos {$\txt$}; 
    }

\foreach \pos/\name in
{{(-1,-1)/1},{(-2,-2)/2},{(3,-1)/3},{(4,-2)/4},{(4,2)/5},
{(5,2)/6},{(-2,2)/7},{(-3,2)/8},{(-1,5)/9},{(-2,6)/10},{(3,5)/11},
{(4,6)/12}}
   \node[vertex] (\name) at \pos {$\name$}; 

% Connect vertices with edges
\foreach \source/ \dest /\weight in {a/b/, b/c/, c/d/, d/e/, 
e/f/,f/a/,a/1/,1/2/,b/3/,3/4/,c/5/,5/6/,f/7/,7/8/,e/9/,9/10/,d/11/,11/12/}
    \path[edge] (\source) -- node[weight] {$\weight$} (\dest);

\end{tikzpicture}
\end{frame}
\end{document}
  • 1
    @BrianNugent one way to appreciate the answer is to accept it by clicking on the checkmark on the left of the answer. The second way is to upvote it. – CroCo Aug 17 '17 at 1:24

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.