10

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_new:N { \g_tobi_radius_tl }
\tl_set:Nn { \g_tobi_radius_tl } { 1em }

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

\ExplSyntaxOff

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

Radius: \usevar{radius}

\tikz \fill [\usevar{color}] circle [radius=\usevar{radius}] ;
\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}
\def\var@radius{1em}

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

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

Radius: \usevar{radius}

\tikz \fill [\usevar{color}] circle [radius=\usevar{radius}] ;
\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
9

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 }

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

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

\ExplSyntaxOff

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

Radius: \usevar{radius}

\tikz \fill [\usevar{color}] circle [radius=\usevar{radius}] ;
\end{document}
  • 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
  • 2
    @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
  • @BrunoLeFloch: That makes sense, thanks for your explanation! – Tobi Jul 18 '13 at 23:26

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.