3

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.

Qrrbrbirlbel
  • 119,821
pluton
  • 16,421
  • I really don't understand this question. Could you add a little more context? – Seamus Sep 07 '12 at 11:30
  • 2
    \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 07 '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 07 '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 07 '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 07 '12 at 16:32
  • @David: yes, Ulrike's comment helps but I edited my question to bring more insight on the "why of my question". – pluton Sep 07 '12 at 17:21
  • tex does not have strings its macros defined by def, newcommand etc store sequences of tokens, that may be characters, as in your example or may include other command tokens. as Ulrike said, you coul make users define the token directly but often it is nicer to use the format that you show where you give a more declaritive interface in which the macro holding the author name is hidden. – David Carlisle Sep 07 '12 at 17:50

1 Answers1

4

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.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • Maybe to make things clear, just a remark that @ is treated as a letter in some contexts, see http://tex.stackexchange.com/q/8351/11002 – yo' Sep 08 '12 at 09:02