Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)  

    \item\label{itm:01} \def\AA{Heading text.} \rAA\

\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?

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

1 Answer 1

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}
\item\label{itm:01} \definestring\rAA{Heading text.} \rAA
\end{enumerate}

Now we use \rAA

\end{document}

enter image description here

share|improve this answer
    
This worked perfectly. You saved a great deal of work, thank you. –  user60435 Aug 8 at 21:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.