7

I was naively expecting the following code to produce the word "Jimmy" between dashes but it produces only the dashes (commented out version also). Could someone explain (preferably one syllable words) what I'm missing, please?

\documentclass[10pt]{article}

\usepackage[T1]{fontenc}

\setlength\parindent{0pt}

%

\begin{document}

\expandafter\def\csname v0 \endcsname{Jimmy}

%\expandafter\def\csname v0 \endcsname#1{Jimmy}

-\csname v0\endcsname-

\end{document}
4
  • You should try without numbers in your csname.
    – TeXnician
    Commented Feb 20, 2017 at 17:56
  • I actually want to define a sequence of colours in a tikz/pgf loop, e.g "\foreach\i in {1,...6}{\draw[color=\csname clr\i \endcsname]...}" or something similar. I thought characters 0-9 were allowed inside \csname ... \endcsname is that not the case? The same happens if I change "v0" to "va". Commented Feb 20, 2017 at 18:10
  • You're right, that was just a simple thought, because numbers in ordinary macro names are not really TeX's favourite. But as I posted, it works with it.
    – TeXnician
    Commented Feb 20, 2017 at 18:12
  • In \csname clr\i \endcsname there is no space after \i (spaces after control words are ignored during tokenization); but if you have done \expandafter\def\csname v1 \endcsname{red}, this has a space after 1. (Note: you may see this incorrectly, but the code has a space between 1 and \endcsname).
    – egreg
    Commented Feb 20, 2017 at 18:16

2 Answers 2

11

Your problem is the space between the name and the \endcsname.

jimmy

This works:

\documentclass[10pt]{article}

\usepackage[T1]{fontenc}

\setlength\parindent{0pt}

\begin{document}

\expandafter\def\csname v0\endcsname{Jimmy}

-\csname v0\endcsname-

\end{document}
8

There is a difference between \csname v0 \endcsname and \csname v0\endcsname. You defined the former, but used the latter:

-Jimmy-

\documentclass{article}

\begin{document}

\expandafter\def\csname v0 \endcsname{Jimmy}

-\csname v0 \endcsname-

\end{document}

Within \csname ... \endcsname even spaces are valid. So you'll have to match it exactly in your usage. Moreover, \csname ... \endcsname doesn't cause an error if the macro isn't defined. Instead, it defines it to \relax. Here's an example:

\csname v1\endcsname

\expandafter\show\csname v1\endcsname

The output in your .log/terminal is

\v1=\relax

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .