diff options
author | Karl Berry <karl@freefriends.org> | 2005-12-29 00:31:48 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2005-12-29 00:31:48 +0000 |
commit | a9d59a2d83b345581f2eb0c6b7f08c091f5622f0 (patch) | |
tree | 00fba7fbf3f00c16be8699398c6e4c2a256c68ac /Master/texmf-doc/doc/english/FAQ-en/html/FAQ-csname.html | |
parent | 89caab08d62b22519b44acf582a5fc3d302cbd60 (diff) |
doc/english/F-ca
git-svn-id: svn://tug.org/texlive/trunk@19 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-doc/doc/english/FAQ-en/html/FAQ-csname.html')
-rw-r--r-- | Master/texmf-doc/doc/english/FAQ-en/html/FAQ-csname.html | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Master/texmf-doc/doc/english/FAQ-en/html/FAQ-csname.html b/Master/texmf-doc/doc/english/FAQ-en/html/FAQ-csname.html new file mode 100644 index 00000000000..0c314c6cab9 --- /dev/null +++ b/Master/texmf-doc/doc/english/FAQ-en/html/FAQ-csname.html @@ -0,0 +1,57 @@ +<head> +<title>UK TeX FAQ -- question label csname</title> +</head><body> +<h3>Defining a macro from an argument</h3> +<p>It's common to want a command to create another command: often one +wants the new command's name to derive from an argument. LaTeX +does this all the time: for example, <code>\</code><code>newenvironment</code> creates +start- and end-environment commands whose names are derived from the +name of the environment command. +<p>The (seemingly) obvious approach: +<blockquote> +<pre> +\def\relay#1#2{\def\#1{#2}} +</pre> +</blockquote> +doesn't work (the TeX engine interprets it +as a rather strange redefinition of <code>#</code>). The trick is to use +<code>\</code><code>csname</code>, which is a TeX primitive for generating command names +from random text, together with <code>\</code><code>expandafter</code>. The definition +above should read: +<blockquote> +<pre> +\def\relay#1#2{% + \expandafter\def\csname #1\endcsname{#2}% +} +</pre> +</blockquote> +With this definition, <code>\</code><code>relay{blah}{bleah}</code> is equivalent to +<code>\</code><code>def</code><code>\</code><code>blah{bleah}</code>. +<p>Note that the definition of <code>\</code><code>relay</code> omits the braces round the +'command name' in the <code>\</code><code>newcommand</code> it executes. This is +because they're not necessary (in fact they seldom are), and in this +circumstance they make the macro code slightly more tedious. +<p>The name created need not (of course) be <em>just</em> the argument: +<blockquote> + +<pre> +\def\newrace#1#2#3{% + \expandafter\def\csname start#1\endcsname{% + #2% + }% + \expandafter\def\csname finish#1\endcsname{% + #3% + }% +} +</pre> +</blockquote> +With commands +<blockquote> +<pre> +\def\start#1{\csname start#1\endcsname} +\def\finish#1{\csname finish#1\endcsname} +</pre> +</blockquote> +these 'races' could behave a bit like LaTeX environments. +<p><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=csname">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=csname</a> +</body> |