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}
  • You should try without numbers in your csname. – TeXnician Feb 20 '17 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". – Martin Rattigan Feb 20 '17 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 Feb 20 '17 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 Feb 20 '17 at 18:16
10

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}
| improve this answer | |
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
| improve this answer | |

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.