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.

What exactly happens in:

\newcommand{\mystring}[1]{\gdef\@mystring{#1}}

when \mystring{mystring} is used later in the document? It is known that mystring will be put in \mystring but I would like to know how in a Latex sense.

Edit: More information is given below. My programming skills in general and especially my Latex programming skills are decently low. I want to prepare a template for conference proceedings where specific information should be stored in appropriate strings that can be used later. Typically, authors' names could be stored in a variable that we will call \AuthorNames so that \AuthorNames (or more exaclty \@AuthorNames as indicated by Ulrike) can be indicated in the right footer of the paper for instance. Since I do not know much, I decided to mimic existing Latex classes (probably the article class). By travelling through the class examples, I figured out that:

\newcommand{\AuthorNames}[1]{\gdef\@AuthorNames{#1}}

was doing was I needed. I can know reformulate my question through a comparison with FORTRAN. What is the LaTeX version of:

 CHARACTER :: AuthorNames

Or in other terms, is there a LaTeX guide somewhere saying: "if you want to declare a string of characters in LaTeX", use this: [blablabla]. I should probably read "texbytopic", that's all.

share|improve this question
    
I really don't understand this question. Could you add a little more context? –  Seamus Sep 7 '12 at 11:30
1  
\gdef is like \newcommand except it does not complain of the command is already defined and the definition is global so is not lost at the end of the next { } group or environment. So basically \mystring{mystring} does \newcommand\@mystring{mystring} –  David Carlisle Sep 7 '12 at 11:36
    
yes but the question behind is: why the command \@mystring contains the string mystring. Is this just the default functioning of such command? (in other words, is this the only way to assign a string of characters to a command in Latex) –  pluton Sep 7 '12 at 12:17
1  
"mystring" is not put in \mystring but in \@mystring. That's a different command. The purpose of \mystring is 1. to give a better interface. users can store a string with \mystring{string} instead of having to do \gdef\@mystring.... and 2. in this way you can store the value in a command with @ in its name which can't be changed easily by a user and so it is (a bit) protected. –  Ulrike Fischer Sep 7 '12 at 13:57
1  
@pluton your question even after the comment above is not at all clear. But perhaps Ulrike's comment answered it. If not I suggest you edit your question to be more explicit, and we can try again. –  David Carlisle Sep 7 '12 at 16:32
show 2 more comments

1 Answer 1

up vote 3 down vote accepted

TeX is a macro replacement language. That means that when we do

\def\foo{bar}

TeX simply stores bar as the replacement text for \foo. Thus when we use \foo, TeX replaces it exactly with bar, and carries on working. The content of a macro is not a string: it's a token list: thus we can do

\def\fooa{\foob}
\def\foob{bar}

and see the same outcome (TeX replaces \fooa with \foob,then \foob with bar). LaTeX's \newcommand is a wrapper around \def, so the principal is identical (although \newcommand will complain if \foo already exists, which \def will not).

The \gdef primitive works in the same way as \def except it applies globally (\def is local, obeying TeX's grouping system). Thus

\newcommand{\AuthorNames}[1]{\gdef\@AuthorNames{#1}}

works by defining \@AuthorNames as a macro to be replaced by whatever is given as the argument to \AuthorNames. As mentioned in the comments, this approach means that the user has a simple interface

\AuthorNames{whatever}

rather than needing to know \gdef and why it should be used here.

TeX also has another form of token storage: the token register or 'toks'. There are reasons for picking a toks over a macro for storage in some cases. However, most of the time you can happily use a macro: they are in many ways more convenient.

share|improve this answer
    
Maybe to make things clear, just a remark that @ is treated as a letter in some contexts, see tex.stackexchange.com/q/8351/11002 –  tohecz Sep 8 '12 at 9:02
add comment

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.