# Use LaTeX3 variables within TikZ picture

I’m new to LaTeX3 and don’t understand why the following doesn’t work

\documentclass{article}

\usepackage{xparse}
\usepackage{tikz}

\ExplSyntaxOn

\tl_new:N { \g_tobi_color_tl }
\tl_set:Nn { \g_tobi_color_tl } { red }

\tl_set:Nn { \g_tobi_radius_tl } { 1em }

\NewDocumentCommand { \usevar } { m }
{
\tl_use:c { g_tobi_#1_tl }
}

\ExplSyntaxOff

\begin{document}
Color: \usevar{color}

\end{document}


Is it a conflict between L3 and TikZ? And is it possible to fix this?

The “classical“ way works fine:

\documentclass{article}

\usepackage{tikz}
\usepackage{etoolbox}

\makeatletter

\def\var@color{red}

\newcommand{\usevar}[1]{%
\csuse{var@#1}%
}

\begin{document}
Color: \usevar{color}

\end{document}

• Not that you ask for it but \makeatletter \def\var@color{red} \def\var@radius{1em} \def\usevar#1{\csname var@#1\endcsname } \makeatother looks a little easier. So you don't need to expand it additionally once more. – percusse Jul 18 '13 at 19:10
• @percusse: Isn’t that the same as my “classical” version, except that I use etoolbox\csdef, which is a little shorter?! – Tobi Jul 18 '13 at 19:30
• Yes indeed but you skip a package :P – percusse Jul 18 '13 at 19:39
• That’s an argument … :-) – Tobi Jul 18 '13 at 21:45

Two hints:

1. You need an expandable version of \usevar. This can be achieved by \DeclareExpandableDocumentCommand

2. The specifier N requires an unbraced token.

Here the example:

\documentclass{article}

\usepackage{xparse}
\usepackage{tikz}

\ExplSyntaxOn

\tl_new:N  \g_tobi_color_tl
\tl_set:Nn  \g_tobi_color_tl  { red }

\DeclareExpandableDocumentCommand { \usevar } { m }
{
\tl_use:c { g_tobi_#1_tl }
}

\ExplSyntaxOff

\begin{document}
Color: \usevar{color}


• Works fine. Thank you! But why does it work with braced arguments even there is the upper case N. I didn’t see any warning … – Tobi Jul 18 '13 at 18:58
• @Tobi, because there is no reasonably efficient way for LaTeX3 to enforce that there be no braces (given the tools TeX provides). Most of the time, N versus n is just a convention which helps know which arguments should be single tokens versus multiple (braced) tokens. But every here and there, braces are mandatory, or forbidden (e.g., with \cs_set_eq:NN the second N argument mustn't be braced), so you're safer sticking with conventions. – Bruno Le Floch Jul 18 '13 at 22:24