# Defining a reusable string from within an environment

I write documents that rely heavily on the 'enumerate' environment. I'd like to turn any particular item from within that environment into a variable that I can use elsewhere. The idea is to ensure consistency with long headers I have to repeat later.

Ideally, the string store method wouldn't check for special characters like @ or _.
Better still if I could use numbers for the string name.

\begin{enumerate}

(piles of content)

\end{enumerate}

(piles of content)

\rAA


This throws an 'undefined' error. It's possible to define the command outside an environment and use it within one, but with extended lists and multiple headers, repeatedly going elsewhere to find out what section I'm in slows my workflow.

Am I overlooking a simple way (or even not-so-simple way) of doing this?

-
\gdef instead of \def? With the usual caveat that this doesn't check for already defined commands. – egreg Aug 8 '14 at 19:57

An environment forms a group, so a \def inside it will not survive the end of the envvironment. What you need is \gdef, but your \AA is a perfect example of something that shouldn't be done, as \AA is a font related command (it produces Å).

You can define your own check, using \@ifdefinable to check for availability of the command and \gdef to globally define the command, so transcending the group structure.

\makeatletter
\newcommand{\definestring}[2]{\@ifdefinable{#1}{\gdef#1{#2}}}
\makeatother


Complete example:

\documentclass{article}

\makeatletter
\newcommand{\definestring}[2]{\@ifdefinable{#1}{\gdef#1{#2}}}
\makeatother

\begin{document}

\begin{enumerate}