# Concatenate macro, string and counter

I'm trying the following:

\documentclass{article}
\newcounter{testcounter}
\def\teststring{begin}

\begin{document}
\stepcounter{testcounter}

\def\newstring{\expandafter\string\csname\teststring-middle-\thetestcounter\endcsname}

\thetestcounter

\teststring

\newstring

\end{document}


And the result is the following:

So, I don't know where the " comes from. The above example is just a demo. The concatenated string \newstring´´ will later be used inside a \label command.

Any hints are welcome!

The " is just the character \ printed using OT1 encoding, try with

\usepackage[T1]{fontenc}


To see a \

So it really is \ and can be used as such in internal command names.

Note however it would generally not be safe to use such a string as the argument to \label as while the \ is inert while being written out to the aux file it will be interpreted as the escape character while the aux file is read.

• Sorry David, I forgot to mention that I just want the string concatenated and no \ at the begin. You're right: I don't want the backslash inside the \label. – aronadaal Aug 1 '16 at 11:14
• @aronadaal in that case just remove the \expandafter\string\csname and \endcsname and possibly use \edef instead of \def, depending on what you want. – David Carlisle Aug 1 '16 at 11:17

When you do

\string\foo


the control sequence name is transformed into a list of characters (all having character code 12, except for possible spaces that keep category code 10) preceded by the character having the same ASCII code as the current value of \escapechar (usually 92, that is, the backslash).

When you do \expandafter\string\csname<code>\endcsname, you first make a token out of the full expansion of <code> and then represent it as before; in your example you get

\begin-middle-1


as a string of characters.

What you probably want is

\edef\newstring{\teststring-middle-\thetestcounter}


that does the full expansion as well, but doesn't make a control sequence name out of it.