\ifthenelse
is "normal" LaTeX code. Therefore you can not use this command inside a TikZ path specification. But since the node text is put in a normal TeX box you can use \ifthenelse
inside the node text. So you can try
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{graphs,graphs.standard,quotes}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}[
vertex/.style={circle,fill=blue!15,draw,minimum size=17pt,inner sep=0pt}
]
%See TikZ documentation! (Section 19, graphs)
\graph[circular placement, radius=4cm, group polar shift=(360/5:0),
nodes={circle,draw,vertex}] {
\foreach \x in {0,...,4} {
\foreach \y in {\x,...,4} {
\x --["\ifthenelse{\x=3 \OR \y=3 \OR \x=\y}{}{\x\y}",sloped] \y;
}}};
\end{tikzpicture}
\end{document}
![enter image description here](https://i.stack.imgur.com/j71qs.png)
Or you can position the nodes inside the \graph
and connect them outside. Then you can use \ifthenelse
outside of a path specification. Note that the complete \draw
paths are in the arguments of \ifthenelse
.
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{graphs,graphs.standard,quotes}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}[
vertex/.style={circle,fill=blue!15,draw,minimum size=17pt,inner sep=0pt}
]
%See TikZ documentation! (Section 19, graphs)
\graph[circular placement, radius=4cm, group polar shift=(360/5:0),
nodes={circle,draw,vertex}] {\foreach \x in {0,...,4} \x;
};
\foreach \x in {0,...,4} {
\foreach \y in {\x,...,4} {
\ifthenelse{\x=3 \OR \y=3 \OR \x=\y}{\draw(\x)--(\y);}{\draw(\x)--node[auto,sloped]{\x\y}(\y);}
}}
\end{tikzpicture}
\end{document}
The result is the same as above.
In the pgfmanual you can also find \pgfextra
as a possibilty to suspend temporarly the path construction and do some other things like \ifthenelse
. Here is a simple example where the filling color depends on the counter:
\documentclass[tikz,margin=5mm]{standalone}
\newcommand\mycolor{blue!15}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}[
vertex/.style={circle,draw,minimum size=17pt,inner sep=0pt}
]
\foreach \i in {0,...,4}{
\path(0,0)\pgfextra{\ifthenelse{\i=2}{\def\mycolor{red!15}}{}}
(90+\i*72:1)node[vertex,fill=\mycolor]{\i};}
\end{tikzpicture}
\end{document}
This results in
![enter image description here](https://i.stack.imgur.com/iEUcb.png)
But AFAIK this does not work inside a \graph
command.
Update:
Solutions without package \ifthen
based on a suggestion of @marmot in a comment:
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{graphs,graphs.standard,quotes}
\begin{document}
\begin{tikzpicture}[
vertex/.style={circle,fill=blue!15,draw,minimum size=17pt,inner sep=0pt}
]
%See TikZ documentation! (Section 19, graphs)
\graph[circular placement, radius=4cm, group polar shift=(360/5:0),
nodes={circle,draw,vertex}] {
\foreach \x in {0,...,4} {
\foreach \y
[evaluate=\y as \z using {int(ifthenelse(\x==3 || \y==3 || \x==\y,1,0))}]
in {\x,...,4} {
\x --["\ifnum \z=0 {\x\y}\fi",sloped] \y;
}}};
\end{tikzpicture}
\end{document}
or
\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{graphs,graphs.standard,quotes}
\begin{document}
\begin{tikzpicture}[
vertex/.style={circle,fill=blue!15,draw,minimum size=17pt,inner sep=0pt}
]
%See TikZ documentation! (Section 19, graphs)
\graph[circular placement, radius=4cm, group polar shift=(360/5:0),
nodes={circle,draw,vertex}] {\foreach \x in {0,...,4} \x;
};
\foreach \x in {0,...,4} {
\foreach \y
[evaluate=\y as \z using {int(ifthenelse(\x==3 || \y==3 || \x==\y,1,0))}]
in {\x,...,4} {
\ifnum \z=1
\draw(\x)--(\y);
\else
\draw(\x)--node[auto,sloped]{\x\y}(\y);
\fi
}}
\end{tikzpicture}
\end{document}
The result is the same as in the first picture.
Best Answer
You could easily use ifthenelse in the tikz node:
This should create the third grid you wanted.
Also see the answer to the following question for more examples with ifthenelse and loops: If-then-else inside TikZ graph?