The following TikZ code attempts to make a graph by drawing vertices 1 through 6, then drawing edges from the list. The edges are formatted as 12
, 23
, etc., so the command StrChar
from package xstring
is used to extract the first and second characters.
\documentclass{minimal}
\usepackage{tikz}
\usepackage{xstring}
\begin{document}
\begin{tikzpicture}
\foreach \index in {1, ..., 6}{%
\node[draw,thick,circle,fill=blue!20,inner sep=1pt,minimum size=7pt] (\index) at (\index*360/6:1) {} ;
}
\foreach \edgetodraw in {12,13,14,15,16,34}{%
\draw[thick] (\StrChar{\edgetodraw}{1}) -- (\StrChar{\edgetodraw}{2}) ;
}
\end{tikzpicture}
\end{document}
When compiled with pdflatex this gives the unhelpful error
! Undefined control sequence.
\@xs@StrChar@@ ...\@xs@arg@ii {#2}\edef \@xs@call
{\noexpand \@testopt {\noe...
l.13 }
An even smaller MWE is below:
\documentclass{minimal}
\usepackage{tikz}
\usepackage{xstring}
\begin{document}
\begin{tikzpicture}
\draw[thick] (\StrChar{34}{1}) -- (\StrChar{34}{2}) ;
\end{tikzpicture}
\end{document}
giving the same error. How can I get StrChar
to work inside tikzpicture
?
More Details:
StrChar
works outside of tikzpicture just fine, even when used inside a\foreach
loop.I am writing a macro to be able to draw many graphs very quickly and systematically. I would really like it to be able to work with the format I am using for edges (
{12, 13, 14, 15, 16, 23}
). So in the interest of my question being as simple and specific as possible, I am asking about how to useStrChar
insidetikz.
But if it is considered a bad idea to to use such heavily programmed macros in vanilla LaTeX, I'm interested in alternative approaches as well.Maybe this question about xstring and includegraphics is related?