What exactly do csname and endcsname do?
up vote
143
down vote
favorite
What exactly do csname
and endcsname
do? What are their job?
I have glanced at the Texbook and some other books, but none of them was clear enough to me.
Can anyone please give a simple example to clarify this issue?
macros tex-core
add a comment |
up vote
143
down vote
favorite
What exactly do csname
and endcsname
do? What are their job?
I have glanced at the Texbook and some other books, but none of them was clear enough to me.
Can anyone please give a simple example to clarify this issue?
macros tex-core
8
See TeX by Topic, section 11.6
– Martin Schröder
Dec 26 '11 at 22:26
add a comment |
up vote
143
down vote
favorite
up vote
143
down vote
favorite
What exactly do csname
and endcsname
do? What are their job?
I have glanced at the Texbook and some other books, but none of them was clear enough to me.
Can anyone please give a simple example to clarify this issue?
macros tex-core
What exactly do csname
and endcsname
do? What are their job?
I have glanced at the Texbook and some other books, but none of them was clear enough to me.
Can anyone please give a simple example to clarify this issue?
macros tex-core
macros tex-core
edited Dec 26 '11 at 23:19
lockstep
189k52585719
189k52585719
asked Dec 26 '11 at 22:04
Vahid Damanafshan
3,15332652
3,15332652
8
See TeX by Topic, section 11.6
– Martin Schröder
Dec 26 '11 at 22:26
add a comment |
8
See TeX by Topic, section 11.6
– Martin Schröder
Dec 26 '11 at 22:26
8
8
See TeX by Topic, section 11.6
– Martin Schröder
Dec 26 '11 at 22:26
See TeX by Topic, section 11.6
– Martin Schröder
Dec 26 '11 at 22:26
add a comment |
6 Answers
6
active
oldest
votes
up vote
133
down vote
accepted
Normally, control sequence names are made only of letters or of one non-letter character.
A letter is, more precisely, a character having category code 11 at the moment the control sequence name is read. So, any character can become part of a control sequence name, provided we change its catcode before the definition and each usage.
With csname...endcsname
we are freed from this limitation and every character can go inside them to form a control sequence name (of course, %
is excluded because it disappears together what remains on the line before TeX is doing its work on characters).
However, this is not the main purpose of csname...endcsname
. This construction is used to build commands from "variable parts". Think, for instance to LaTeX's newcounter
: after newcounter{foo}
, TeX knows thefoo
that is built precisely in this way. Roughly, what LaTeX does is
newcommand{newcounter}[1]{%
expandafternewcountcsname c@#1endcsname
expandafterdefcsname the#1endcsname{arabic{#1}}%
}
so that newcounter{foo}
does the right job. It's more complicated than this, of course, but the main things are here; newcount
is the low-level command to allocate a counter. The expandafter
is just to build the control sequence before newcount
and def
see the token.
Inside csname...endcsname
, category codes don't matter (with one main exception: active characters will be expanded if not preceded by string
, see final note). LaTeX exploits this in order to build control sequence names that users won't be able to access (easily). For example, the control sequence to choose the default ten point font is OT1/cmr/m/n/10
, which can be easily split internally (by the "reverse" operation that is string
) and is not available to the casual user.
Another important use is in environments: when you say newenvironment{foo}
, LaTeX really defines foo
and endfoo
. Upon finding begin{foo}
, LaTeX does some bookkeeping and then executes csname fooendcsname
(that's why one can say also newenvironment{foo*}
); similarly, at end{foo}
LaTeX executes csname endfooendcsname
and after this it does some bookkeeping again.
Other uses: label{foo}
will define control sequences based on foo
via csname...endcsname
that can be used by ref
.
When one says csname fooendcsname
, LaTeX will look whether foo
is defined; if not, it will execute relax
and from then on (respecting grouping), foo
will be interpreted as relax
. An interesting usage for this feature is that one can say
chapter*{Introduction}
csname phantomsectionendcsname
addcontentsline{toc}{chapter}{Introduction}
and keep hyperref
happy if it's loaded, while doing nothing if the package is not loaded.
It's possible to give many other interesting uses of this trick. But one should always keep in mind that TeX does complete expansion of what it finds in that context and that only characters must remain. So
csname abcrelax defendcsname
is forbidden. But, after defxyz{abc}
,
csname xyz defendcsname
will be legal and equivalent to saying csname abcdefendcsname
or abcdef
.
Final note
It's better to add something about category codes. An active character in csname...endcsname
will be expanded, so to get a literal ~
one has to write string~
. Comment (category 14), ignored (category 9) and invalid (category 15) characters will remain such. So
csname %endcsname
will give an error (Missing endcsname
); in csname ^^@endcsname
there will be no character and csname ^^?endcsname
will raise an error.
2
Braces don't need to be balanced.expandaftershowcsname{endcsname
works fine with usual catcodes. Otherwise, very good response.
– Bruno Le Floch
Dec 27 '11 at 2:03
1
@BrunoLeFloch Right; they have to be balanced only if used to surround arguments to macros, of course.
– egreg
Dec 27 '11 at 10:37
add a comment |
up vote
32
down vote
For reference, from the TeX Book (with slight formatting changes), Chapter 7: How TeX Reads What You Type (p 40):
...you can go from a list of character tokens to a control sequence by
sayingcsname<tokens>endcsname
. The tokens that appear in this
construction betweencsname
andendcsname
may include other
control sequences, as long as those control sequences ultimately
expand into characters instead of TeX primitives; the final characters
can be of any category, not necessarily letters. For example,csname
is essentially the same as
TeXendcsnameTeX
; but
csnameTeXendcsname
is illegal, becauseTeX
expands into tokens
containing thekern
primitive. Furthermore,
csnamestringTeXendcsname
will produce the unusual control
sequence\TeX
, i.e., the token<TeX>
, which you can't ordinarily
write.
I have used this indirectly by using the label
-ref
system and defining labels based on counters:
newcounter{mycount}
%...
newcommand{mycmd}{%
stepcounter{mycount}%
label{abcthemycount}%
%...
}
This creates a "successive label abc1
, abc2
, ... for every call to mycmd
, in order to avoid creating multiply defined labels with the same name. Indirectly, label{abcthemycount}
calls @namedef{r@abcthemycount}
, which calls
expandafterdefcsname r@abcthemycountendcsname
thereby expanding r@abcthemycount
to r@abc1
and defining r@abc1
for the first label, r@abc2
for the second label, etc. Yes, labels in LaTeX are actually control sequences prepended with r@
and is constructed using csname ... endcsname
which then allows numerals.
add a comment |
up vote
23
down vote
csname
/endcsname
allows you to build commands whose names contains 1. non-letters (e.g. dots or colons or numbers) and - more importantly - 2. commands which are expanded when you define or use the command. Both is useful if you want to construct a command name from various pieces of informations.
As an example: The xskak-Package loops through the notation of a chess game and stores a lot of informations of every move in commands. Its code contains a lot of definition of this type:
expandafterxdef
csname Xskak.xskak@val@gameid.thec@move.WhiteToMove{w}{b}.pieceendcsname{%
....
}
where xskak@val@gameid
is the id of the current game, thec@move
gives the current move number, WhiteToMove{w}{b}
gives w or b depending on which player currently moves. So in the 10th move of black in the game with id "mygame" this xdef
defines a "command" Xskak.mygame.10.b.piece
which contains the name of the piece which has been moved by black in the tenth move.
add a comment |
up vote
22
down vote
Suppose you want to define a command foo2
. You cannot do this because 2 is not a letter. However, this construction works: csname foo2endcsname
. Sometimes this is useful, e.g. when you need a series of commands, foo1
, foo2
, etc (another way is to use roman numerals). Another example, suppose you want to define a series of commands like endsection
, endsubsection
, etc. Then you can use a loop with expandafterdefcsname end#1csname...
Thanks for your example, but can you tell me what generally their job are? When and where do I need to use them?
– Vahid Damanafshan
Dec 26 '11 at 22:35
4
Well, @egreg beat me to it. Basically a TeX command is either (1) a sequence of letters starting from`, e.g.
parskip, or (2) a special symbol optionally preceded by a backslash, e.g.
@, and (3) any sequence of symbols between
csname` andendcsname
. So the job of these commands is to introduce a way to produce TeX commands. You do not need to use them unless you do TeX programming. In the latter case they are handy.
– Boris
Dec 26 '11 at 23:34
add a comment |
up vote
18
down vote
Short answer: csname
and endcsname
are a "macro environment" whose contents, if they evaluate (after expanding macros) to "ordinary text", are converted into the name of a macro (or control sequence, hence "csname").
Actually, it is a perhaps strange joke that by the rules of LaTeX macros, you can actually write begin{csname}...end{csname}
and it will act as you expect, to a certain extent. For example:
defmacro{text}
defo{o}
begin{csname}%
macro
end{csname}
% Same as csname macro endcsname
(when run with latex
rather than tex
) will produce the word "text" in the output. The %
sign is there so that unnecessary spaces don't creep into the name of the macro we are constructing.
add a comment |
up vote
0
down vote
As a very beginner for this antique problem, I just provide a simple example that may tell future novice something about csname
. Though I do not fully understand I have tried it and successfully solved one problem of mine.
My problem was to test different effect of different font command, like:
textit{textit} textbf{textbf} {bfseries bfseries} {small small}
and I tried to create a new command newcommand{FontTest}[1]{#1 #1}
, I had quite a struggle wondering how to turn the first #1 to a command. And after many trials I used csname
and it worked, so it was:
newcommand{FontTest}[1]{{ csname #1endcsname{#1}}}
and I can freely test different type of font families and shapes like: FontTest{textit} FontTest{sffamily}
etc.
And its effect seems like turning a string into command I guess.
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f39380%2fwhat-exactly-do-csname-and-endcsname-do%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
133
down vote
accepted
Normally, control sequence names are made only of letters or of one non-letter character.
A letter is, more precisely, a character having category code 11 at the moment the control sequence name is read. So, any character can become part of a control sequence name, provided we change its catcode before the definition and each usage.
With csname...endcsname
we are freed from this limitation and every character can go inside them to form a control sequence name (of course, %
is excluded because it disappears together what remains on the line before TeX is doing its work on characters).
However, this is not the main purpose of csname...endcsname
. This construction is used to build commands from "variable parts". Think, for instance to LaTeX's newcounter
: after newcounter{foo}
, TeX knows thefoo
that is built precisely in this way. Roughly, what LaTeX does is
newcommand{newcounter}[1]{%
expandafternewcountcsname c@#1endcsname
expandafterdefcsname the#1endcsname{arabic{#1}}%
}
so that newcounter{foo}
does the right job. It's more complicated than this, of course, but the main things are here; newcount
is the low-level command to allocate a counter. The expandafter
is just to build the control sequence before newcount
and def
see the token.
Inside csname...endcsname
, category codes don't matter (with one main exception: active characters will be expanded if not preceded by string
, see final note). LaTeX exploits this in order to build control sequence names that users won't be able to access (easily). For example, the control sequence to choose the default ten point font is OT1/cmr/m/n/10
, which can be easily split internally (by the "reverse" operation that is string
) and is not available to the casual user.
Another important use is in environments: when you say newenvironment{foo}
, LaTeX really defines foo
and endfoo
. Upon finding begin{foo}
, LaTeX does some bookkeeping and then executes csname fooendcsname
(that's why one can say also newenvironment{foo*}
); similarly, at end{foo}
LaTeX executes csname endfooendcsname
and after this it does some bookkeeping again.
Other uses: label{foo}
will define control sequences based on foo
via csname...endcsname
that can be used by ref
.
When one says csname fooendcsname
, LaTeX will look whether foo
is defined; if not, it will execute relax
and from then on (respecting grouping), foo
will be interpreted as relax
. An interesting usage for this feature is that one can say
chapter*{Introduction}
csname phantomsectionendcsname
addcontentsline{toc}{chapter}{Introduction}
and keep hyperref
happy if it's loaded, while doing nothing if the package is not loaded.
It's possible to give many other interesting uses of this trick. But one should always keep in mind that TeX does complete expansion of what it finds in that context and that only characters must remain. So
csname abcrelax defendcsname
is forbidden. But, after defxyz{abc}
,
csname xyz defendcsname
will be legal and equivalent to saying csname abcdefendcsname
or abcdef
.
Final note
It's better to add something about category codes. An active character in csname...endcsname
will be expanded, so to get a literal ~
one has to write string~
. Comment (category 14), ignored (category 9) and invalid (category 15) characters will remain such. So
csname %endcsname
will give an error (Missing endcsname
); in csname ^^@endcsname
there will be no character and csname ^^?endcsname
will raise an error.
2
Braces don't need to be balanced.expandaftershowcsname{endcsname
works fine with usual catcodes. Otherwise, very good response.
– Bruno Le Floch
Dec 27 '11 at 2:03
1
@BrunoLeFloch Right; they have to be balanced only if used to surround arguments to macros, of course.
– egreg
Dec 27 '11 at 10:37
add a comment |
up vote
133
down vote
accepted
Normally, control sequence names are made only of letters or of one non-letter character.
A letter is, more precisely, a character having category code 11 at the moment the control sequence name is read. So, any character can become part of a control sequence name, provided we change its catcode before the definition and each usage.
With csname...endcsname
we are freed from this limitation and every character can go inside them to form a control sequence name (of course, %
is excluded because it disappears together what remains on the line before TeX is doing its work on characters).
However, this is not the main purpose of csname...endcsname
. This construction is used to build commands from "variable parts". Think, for instance to LaTeX's newcounter
: after newcounter{foo}
, TeX knows thefoo
that is built precisely in this way. Roughly, what LaTeX does is
newcommand{newcounter}[1]{%
expandafternewcountcsname c@#1endcsname
expandafterdefcsname the#1endcsname{arabic{#1}}%
}
so that newcounter{foo}
does the right job. It's more complicated than this, of course, but the main things are here; newcount
is the low-level command to allocate a counter. The expandafter
is just to build the control sequence before newcount
and def
see the token.
Inside csname...endcsname
, category codes don't matter (with one main exception: active characters will be expanded if not preceded by string
, see final note). LaTeX exploits this in order to build control sequence names that users won't be able to access (easily). For example, the control sequence to choose the default ten point font is OT1/cmr/m/n/10
, which can be easily split internally (by the "reverse" operation that is string
) and is not available to the casual user.
Another important use is in environments: when you say newenvironment{foo}
, LaTeX really defines foo
and endfoo
. Upon finding begin{foo}
, LaTeX does some bookkeeping and then executes csname fooendcsname
(that's why one can say also newenvironment{foo*}
); similarly, at end{foo}
LaTeX executes csname endfooendcsname
and after this it does some bookkeeping again.
Other uses: label{foo}
will define control sequences based on foo
via csname...endcsname
that can be used by ref
.
When one says csname fooendcsname
, LaTeX will look whether foo
is defined; if not, it will execute relax
and from then on (respecting grouping), foo
will be interpreted as relax
. An interesting usage for this feature is that one can say
chapter*{Introduction}
csname phantomsectionendcsname
addcontentsline{toc}{chapter}{Introduction}
and keep hyperref
happy if it's loaded, while doing nothing if the package is not loaded.
It's possible to give many other interesting uses of this trick. But one should always keep in mind that TeX does complete expansion of what it finds in that context and that only characters must remain. So
csname abcrelax defendcsname
is forbidden. But, after defxyz{abc}
,
csname xyz defendcsname
will be legal and equivalent to saying csname abcdefendcsname
or abcdef
.
Final note
It's better to add something about category codes. An active character in csname...endcsname
will be expanded, so to get a literal ~
one has to write string~
. Comment (category 14), ignored (category 9) and invalid (category 15) characters will remain such. So
csname %endcsname
will give an error (Missing endcsname
); in csname ^^@endcsname
there will be no character and csname ^^?endcsname
will raise an error.
2
Braces don't need to be balanced.expandaftershowcsname{endcsname
works fine with usual catcodes. Otherwise, very good response.
– Bruno Le Floch
Dec 27 '11 at 2:03
1
@BrunoLeFloch Right; they have to be balanced only if used to surround arguments to macros, of course.
– egreg
Dec 27 '11 at 10:37
add a comment |
up vote
133
down vote
accepted
up vote
133
down vote
accepted
Normally, control sequence names are made only of letters or of one non-letter character.
A letter is, more precisely, a character having category code 11 at the moment the control sequence name is read. So, any character can become part of a control sequence name, provided we change its catcode before the definition and each usage.
With csname...endcsname
we are freed from this limitation and every character can go inside them to form a control sequence name (of course, %
is excluded because it disappears together what remains on the line before TeX is doing its work on characters).
However, this is not the main purpose of csname...endcsname
. This construction is used to build commands from "variable parts". Think, for instance to LaTeX's newcounter
: after newcounter{foo}
, TeX knows thefoo
that is built precisely in this way. Roughly, what LaTeX does is
newcommand{newcounter}[1]{%
expandafternewcountcsname c@#1endcsname
expandafterdefcsname the#1endcsname{arabic{#1}}%
}
so that newcounter{foo}
does the right job. It's more complicated than this, of course, but the main things are here; newcount
is the low-level command to allocate a counter. The expandafter
is just to build the control sequence before newcount
and def
see the token.
Inside csname...endcsname
, category codes don't matter (with one main exception: active characters will be expanded if not preceded by string
, see final note). LaTeX exploits this in order to build control sequence names that users won't be able to access (easily). For example, the control sequence to choose the default ten point font is OT1/cmr/m/n/10
, which can be easily split internally (by the "reverse" operation that is string
) and is not available to the casual user.
Another important use is in environments: when you say newenvironment{foo}
, LaTeX really defines foo
and endfoo
. Upon finding begin{foo}
, LaTeX does some bookkeeping and then executes csname fooendcsname
(that's why one can say also newenvironment{foo*}
); similarly, at end{foo}
LaTeX executes csname endfooendcsname
and after this it does some bookkeeping again.
Other uses: label{foo}
will define control sequences based on foo
via csname...endcsname
that can be used by ref
.
When one says csname fooendcsname
, LaTeX will look whether foo
is defined; if not, it will execute relax
and from then on (respecting grouping), foo
will be interpreted as relax
. An interesting usage for this feature is that one can say
chapter*{Introduction}
csname phantomsectionendcsname
addcontentsline{toc}{chapter}{Introduction}
and keep hyperref
happy if it's loaded, while doing nothing if the package is not loaded.
It's possible to give many other interesting uses of this trick. But one should always keep in mind that TeX does complete expansion of what it finds in that context and that only characters must remain. So
csname abcrelax defendcsname
is forbidden. But, after defxyz{abc}
,
csname xyz defendcsname
will be legal and equivalent to saying csname abcdefendcsname
or abcdef
.
Final note
It's better to add something about category codes. An active character in csname...endcsname
will be expanded, so to get a literal ~
one has to write string~
. Comment (category 14), ignored (category 9) and invalid (category 15) characters will remain such. So
csname %endcsname
will give an error (Missing endcsname
); in csname ^^@endcsname
there will be no character and csname ^^?endcsname
will raise an error.
Normally, control sequence names are made only of letters or of one non-letter character.
A letter is, more precisely, a character having category code 11 at the moment the control sequence name is read. So, any character can become part of a control sequence name, provided we change its catcode before the definition and each usage.
With csname...endcsname
we are freed from this limitation and every character can go inside them to form a control sequence name (of course, %
is excluded because it disappears together what remains on the line before TeX is doing its work on characters).
However, this is not the main purpose of csname...endcsname
. This construction is used to build commands from "variable parts". Think, for instance to LaTeX's newcounter
: after newcounter{foo}
, TeX knows thefoo
that is built precisely in this way. Roughly, what LaTeX does is
newcommand{newcounter}[1]{%
expandafternewcountcsname c@#1endcsname
expandafterdefcsname the#1endcsname{arabic{#1}}%
}
so that newcounter{foo}
does the right job. It's more complicated than this, of course, but the main things are here; newcount
is the low-level command to allocate a counter. The expandafter
is just to build the control sequence before newcount
and def
see the token.
Inside csname...endcsname
, category codes don't matter (with one main exception: active characters will be expanded if not preceded by string
, see final note). LaTeX exploits this in order to build control sequence names that users won't be able to access (easily). For example, the control sequence to choose the default ten point font is OT1/cmr/m/n/10
, which can be easily split internally (by the "reverse" operation that is string
) and is not available to the casual user.
Another important use is in environments: when you say newenvironment{foo}
, LaTeX really defines foo
and endfoo
. Upon finding begin{foo}
, LaTeX does some bookkeeping and then executes csname fooendcsname
(that's why one can say also newenvironment{foo*}
); similarly, at end{foo}
LaTeX executes csname endfooendcsname
and after this it does some bookkeeping again.
Other uses: label{foo}
will define control sequences based on foo
via csname...endcsname
that can be used by ref
.
When one says csname fooendcsname
, LaTeX will look whether foo
is defined; if not, it will execute relax
and from then on (respecting grouping), foo
will be interpreted as relax
. An interesting usage for this feature is that one can say
chapter*{Introduction}
csname phantomsectionendcsname
addcontentsline{toc}{chapter}{Introduction}
and keep hyperref
happy if it's loaded, while doing nothing if the package is not loaded.
It's possible to give many other interesting uses of this trick. But one should always keep in mind that TeX does complete expansion of what it finds in that context and that only characters must remain. So
csname abcrelax defendcsname
is forbidden. But, after defxyz{abc}
,
csname xyz defendcsname
will be legal and equivalent to saying csname abcdefendcsname
or abcdef
.
Final note
It's better to add something about category codes. An active character in csname...endcsname
will be expanded, so to get a literal ~
one has to write string~
. Comment (category 14), ignored (category 9) and invalid (category 15) characters will remain such. So
csname %endcsname
will give an error (Missing endcsname
); in csname ^^@endcsname
there will be no character and csname ^^?endcsname
will raise an error.
edited Sep 14 at 14:26
answered Dec 26 '11 at 22:38
egreg
706k8618773157
706k8618773157
2
Braces don't need to be balanced.expandaftershowcsname{endcsname
works fine with usual catcodes. Otherwise, very good response.
– Bruno Le Floch
Dec 27 '11 at 2:03
1
@BrunoLeFloch Right; they have to be balanced only if used to surround arguments to macros, of course.
– egreg
Dec 27 '11 at 10:37
add a comment |
2
Braces don't need to be balanced.expandaftershowcsname{endcsname
works fine with usual catcodes. Otherwise, very good response.
– Bruno Le Floch
Dec 27 '11 at 2:03
1
@BrunoLeFloch Right; they have to be balanced only if used to surround arguments to macros, of course.
– egreg
Dec 27 '11 at 10:37
2
2
Braces don't need to be balanced.
expandaftershowcsname{endcsname
works fine with usual catcodes. Otherwise, very good response.– Bruno Le Floch
Dec 27 '11 at 2:03
Braces don't need to be balanced.
expandaftershowcsname{endcsname
works fine with usual catcodes. Otherwise, very good response.– Bruno Le Floch
Dec 27 '11 at 2:03
1
1
@BrunoLeFloch Right; they have to be balanced only if used to surround arguments to macros, of course.
– egreg
Dec 27 '11 at 10:37
@BrunoLeFloch Right; they have to be balanced only if used to surround arguments to macros, of course.
– egreg
Dec 27 '11 at 10:37
add a comment |
up vote
32
down vote
For reference, from the TeX Book (with slight formatting changes), Chapter 7: How TeX Reads What You Type (p 40):
...you can go from a list of character tokens to a control sequence by
sayingcsname<tokens>endcsname
. The tokens that appear in this
construction betweencsname
andendcsname
may include other
control sequences, as long as those control sequences ultimately
expand into characters instead of TeX primitives; the final characters
can be of any category, not necessarily letters. For example,csname
is essentially the same as
TeXendcsnameTeX
; but
csnameTeXendcsname
is illegal, becauseTeX
expands into tokens
containing thekern
primitive. Furthermore,
csnamestringTeXendcsname
will produce the unusual control
sequence\TeX
, i.e., the token<TeX>
, which you can't ordinarily
write.
I have used this indirectly by using the label
-ref
system and defining labels based on counters:
newcounter{mycount}
%...
newcommand{mycmd}{%
stepcounter{mycount}%
label{abcthemycount}%
%...
}
This creates a "successive label abc1
, abc2
, ... for every call to mycmd
, in order to avoid creating multiply defined labels with the same name. Indirectly, label{abcthemycount}
calls @namedef{r@abcthemycount}
, which calls
expandafterdefcsname r@abcthemycountendcsname
thereby expanding r@abcthemycount
to r@abc1
and defining r@abc1
for the first label, r@abc2
for the second label, etc. Yes, labels in LaTeX are actually control sequences prepended with r@
and is constructed using csname ... endcsname
which then allows numerals.
add a comment |
up vote
32
down vote
For reference, from the TeX Book (with slight formatting changes), Chapter 7: How TeX Reads What You Type (p 40):
...you can go from a list of character tokens to a control sequence by
sayingcsname<tokens>endcsname
. The tokens that appear in this
construction betweencsname
andendcsname
may include other
control sequences, as long as those control sequences ultimately
expand into characters instead of TeX primitives; the final characters
can be of any category, not necessarily letters. For example,csname
is essentially the same as
TeXendcsnameTeX
; but
csnameTeXendcsname
is illegal, becauseTeX
expands into tokens
containing thekern
primitive. Furthermore,
csnamestringTeXendcsname
will produce the unusual control
sequence\TeX
, i.e., the token<TeX>
, which you can't ordinarily
write.
I have used this indirectly by using the label
-ref
system and defining labels based on counters:
newcounter{mycount}
%...
newcommand{mycmd}{%
stepcounter{mycount}%
label{abcthemycount}%
%...
}
This creates a "successive label abc1
, abc2
, ... for every call to mycmd
, in order to avoid creating multiply defined labels with the same name. Indirectly, label{abcthemycount}
calls @namedef{r@abcthemycount}
, which calls
expandafterdefcsname r@abcthemycountendcsname
thereby expanding r@abcthemycount
to r@abc1
and defining r@abc1
for the first label, r@abc2
for the second label, etc. Yes, labels in LaTeX are actually control sequences prepended with r@
and is constructed using csname ... endcsname
which then allows numerals.
add a comment |
up vote
32
down vote
up vote
32
down vote
For reference, from the TeX Book (with slight formatting changes), Chapter 7: How TeX Reads What You Type (p 40):
...you can go from a list of character tokens to a control sequence by
sayingcsname<tokens>endcsname
. The tokens that appear in this
construction betweencsname
andendcsname
may include other
control sequences, as long as those control sequences ultimately
expand into characters instead of TeX primitives; the final characters
can be of any category, not necessarily letters. For example,csname
is essentially the same as
TeXendcsnameTeX
; but
csnameTeXendcsname
is illegal, becauseTeX
expands into tokens
containing thekern
primitive. Furthermore,
csnamestringTeXendcsname
will produce the unusual control
sequence\TeX
, i.e., the token<TeX>
, which you can't ordinarily
write.
I have used this indirectly by using the label
-ref
system and defining labels based on counters:
newcounter{mycount}
%...
newcommand{mycmd}{%
stepcounter{mycount}%
label{abcthemycount}%
%...
}
This creates a "successive label abc1
, abc2
, ... for every call to mycmd
, in order to avoid creating multiply defined labels with the same name. Indirectly, label{abcthemycount}
calls @namedef{r@abcthemycount}
, which calls
expandafterdefcsname r@abcthemycountendcsname
thereby expanding r@abcthemycount
to r@abc1
and defining r@abc1
for the first label, r@abc2
for the second label, etc. Yes, labels in LaTeX are actually control sequences prepended with r@
and is constructed using csname ... endcsname
which then allows numerals.
For reference, from the TeX Book (with slight formatting changes), Chapter 7: How TeX Reads What You Type (p 40):
...you can go from a list of character tokens to a control sequence by
sayingcsname<tokens>endcsname
. The tokens that appear in this
construction betweencsname
andendcsname
may include other
control sequences, as long as those control sequences ultimately
expand into characters instead of TeX primitives; the final characters
can be of any category, not necessarily letters. For example,csname
is essentially the same as
TeXendcsnameTeX
; but
csnameTeXendcsname
is illegal, becauseTeX
expands into tokens
containing thekern
primitive. Furthermore,
csnamestringTeXendcsname
will produce the unusual control
sequence\TeX
, i.e., the token<TeX>
, which you can't ordinarily
write.
I have used this indirectly by using the label
-ref
system and defining labels based on counters:
newcounter{mycount}
%...
newcommand{mycmd}{%
stepcounter{mycount}%
label{abcthemycount}%
%...
}
This creates a "successive label abc1
, abc2
, ... for every call to mycmd
, in order to avoid creating multiply defined labels with the same name. Indirectly, label{abcthemycount}
calls @namedef{r@abcthemycount}
, which calls
expandafterdefcsname r@abcthemycountendcsname
thereby expanding r@abcthemycount
to r@abc1
and defining r@abc1
for the first label, r@abc2
for the second label, etc. Yes, labels in LaTeX are actually control sequences prepended with r@
and is constructed using csname ... endcsname
which then allows numerals.
answered Dec 27 '11 at 3:23
Werner
435k629571645
435k629571645
add a comment |
add a comment |
up vote
23
down vote
csname
/endcsname
allows you to build commands whose names contains 1. non-letters (e.g. dots or colons or numbers) and - more importantly - 2. commands which are expanded when you define or use the command. Both is useful if you want to construct a command name from various pieces of informations.
As an example: The xskak-Package loops through the notation of a chess game and stores a lot of informations of every move in commands. Its code contains a lot of definition of this type:
expandafterxdef
csname Xskak.xskak@val@gameid.thec@move.WhiteToMove{w}{b}.pieceendcsname{%
....
}
where xskak@val@gameid
is the id of the current game, thec@move
gives the current move number, WhiteToMove{w}{b}
gives w or b depending on which player currently moves. So in the 10th move of black in the game with id "mygame" this xdef
defines a "command" Xskak.mygame.10.b.piece
which contains the name of the piece which has been moved by black in the tenth move.
add a comment |
up vote
23
down vote
csname
/endcsname
allows you to build commands whose names contains 1. non-letters (e.g. dots or colons or numbers) and - more importantly - 2. commands which are expanded when you define or use the command. Both is useful if you want to construct a command name from various pieces of informations.
As an example: The xskak-Package loops through the notation of a chess game and stores a lot of informations of every move in commands. Its code contains a lot of definition of this type:
expandafterxdef
csname Xskak.xskak@val@gameid.thec@move.WhiteToMove{w}{b}.pieceendcsname{%
....
}
where xskak@val@gameid
is the id of the current game, thec@move
gives the current move number, WhiteToMove{w}{b}
gives w or b depending on which player currently moves. So in the 10th move of black in the game with id "mygame" this xdef
defines a "command" Xskak.mygame.10.b.piece
which contains the name of the piece which has been moved by black in the tenth move.
add a comment |
up vote
23
down vote
up vote
23
down vote
csname
/endcsname
allows you to build commands whose names contains 1. non-letters (e.g. dots or colons or numbers) and - more importantly - 2. commands which are expanded when you define or use the command. Both is useful if you want to construct a command name from various pieces of informations.
As an example: The xskak-Package loops through the notation of a chess game and stores a lot of informations of every move in commands. Its code contains a lot of definition of this type:
expandafterxdef
csname Xskak.xskak@val@gameid.thec@move.WhiteToMove{w}{b}.pieceendcsname{%
....
}
where xskak@val@gameid
is the id of the current game, thec@move
gives the current move number, WhiteToMove{w}{b}
gives w or b depending on which player currently moves. So in the 10th move of black in the game with id "mygame" this xdef
defines a "command" Xskak.mygame.10.b.piece
which contains the name of the piece which has been moved by black in the tenth move.
csname
/endcsname
allows you to build commands whose names contains 1. non-letters (e.g. dots or colons or numbers) and - more importantly - 2. commands which are expanded when you define or use the command. Both is useful if you want to construct a command name from various pieces of informations.
As an example: The xskak-Package loops through the notation of a chess game and stores a lot of informations of every move in commands. Its code contains a lot of definition of this type:
expandafterxdef
csname Xskak.xskak@val@gameid.thec@move.WhiteToMove{w}{b}.pieceendcsname{%
....
}
where xskak@val@gameid
is the id of the current game, thec@move
gives the current move number, WhiteToMove{w}{b}
gives w or b depending on which player currently moves. So in the 10th move of black in the game with id "mygame" this xdef
defines a "command" Xskak.mygame.10.b.piece
which contains the name of the piece which has been moved by black in the tenth move.
answered Dec 27 '11 at 9:16
Ulrike Fischer
185k7289666
185k7289666
add a comment |
add a comment |
up vote
22
down vote
Suppose you want to define a command foo2
. You cannot do this because 2 is not a letter. However, this construction works: csname foo2endcsname
. Sometimes this is useful, e.g. when you need a series of commands, foo1
, foo2
, etc (another way is to use roman numerals). Another example, suppose you want to define a series of commands like endsection
, endsubsection
, etc. Then you can use a loop with expandafterdefcsname end#1csname...
Thanks for your example, but can you tell me what generally their job are? When and where do I need to use them?
– Vahid Damanafshan
Dec 26 '11 at 22:35
4
Well, @egreg beat me to it. Basically a TeX command is either (1) a sequence of letters starting from`, e.g.
parskip, or (2) a special symbol optionally preceded by a backslash, e.g.
@, and (3) any sequence of symbols between
csname` andendcsname
. So the job of these commands is to introduce a way to produce TeX commands. You do not need to use them unless you do TeX programming. In the latter case they are handy.
– Boris
Dec 26 '11 at 23:34
add a comment |
up vote
22
down vote
Suppose you want to define a command foo2
. You cannot do this because 2 is not a letter. However, this construction works: csname foo2endcsname
. Sometimes this is useful, e.g. when you need a series of commands, foo1
, foo2
, etc (another way is to use roman numerals). Another example, suppose you want to define a series of commands like endsection
, endsubsection
, etc. Then you can use a loop with expandafterdefcsname end#1csname...
Thanks for your example, but can you tell me what generally their job are? When and where do I need to use them?
– Vahid Damanafshan
Dec 26 '11 at 22:35
4
Well, @egreg beat me to it. Basically a TeX command is either (1) a sequence of letters starting from`, e.g.
parskip, or (2) a special symbol optionally preceded by a backslash, e.g.
@, and (3) any sequence of symbols between
csname` andendcsname
. So the job of these commands is to introduce a way to produce TeX commands. You do not need to use them unless you do TeX programming. In the latter case they are handy.
– Boris
Dec 26 '11 at 23:34
add a comment |
up vote
22
down vote
up vote
22
down vote
Suppose you want to define a command foo2
. You cannot do this because 2 is not a letter. However, this construction works: csname foo2endcsname
. Sometimes this is useful, e.g. when you need a series of commands, foo1
, foo2
, etc (another way is to use roman numerals). Another example, suppose you want to define a series of commands like endsection
, endsubsection
, etc. Then you can use a loop with expandafterdefcsname end#1csname...
Suppose you want to define a command foo2
. You cannot do this because 2 is not a letter. However, this construction works: csname foo2endcsname
. Sometimes this is useful, e.g. when you need a series of commands, foo1
, foo2
, etc (another way is to use roman numerals). Another example, suppose you want to define a series of commands like endsection
, endsubsection
, etc. Then you can use a loop with expandafterdefcsname end#1csname...
answered Dec 26 '11 at 22:21
Boris
29.9k262106
29.9k262106
Thanks for your example, but can you tell me what generally their job are? When and where do I need to use them?
– Vahid Damanafshan
Dec 26 '11 at 22:35
4
Well, @egreg beat me to it. Basically a TeX command is either (1) a sequence of letters starting from`, e.g.
parskip, or (2) a special symbol optionally preceded by a backslash, e.g.
@, and (3) any sequence of symbols between
csname` andendcsname
. So the job of these commands is to introduce a way to produce TeX commands. You do not need to use them unless you do TeX programming. In the latter case they are handy.
– Boris
Dec 26 '11 at 23:34
add a comment |
Thanks for your example, but can you tell me what generally their job are? When and where do I need to use them?
– Vahid Damanafshan
Dec 26 '11 at 22:35
4
Well, @egreg beat me to it. Basically a TeX command is either (1) a sequence of letters starting from`, e.g.
parskip, or (2) a special symbol optionally preceded by a backslash, e.g.
@, and (3) any sequence of symbols between
csname` andendcsname
. So the job of these commands is to introduce a way to produce TeX commands. You do not need to use them unless you do TeX programming. In the latter case they are handy.
– Boris
Dec 26 '11 at 23:34
Thanks for your example, but can you tell me what generally their job are? When and where do I need to use them?
– Vahid Damanafshan
Dec 26 '11 at 22:35
Thanks for your example, but can you tell me what generally their job are? When and where do I need to use them?
– Vahid Damanafshan
Dec 26 '11 at 22:35
4
4
Well, @egreg beat me to it. Basically a TeX command is either (1) a sequence of letters starting from
`, e.g.
parskip, or (2) a special symbol optionally preceded by a backslash, e.g.
@, and (3) any sequence of symbols between
csname` and endcsname
. So the job of these commands is to introduce a way to produce TeX commands. You do not need to use them unless you do TeX programming. In the latter case they are handy.– Boris
Dec 26 '11 at 23:34
Well, @egreg beat me to it. Basically a TeX command is either (1) a sequence of letters starting from
`, e.g.
parskip, or (2) a special symbol optionally preceded by a backslash, e.g.
@, and (3) any sequence of symbols between
csname` and endcsname
. So the job of these commands is to introduce a way to produce TeX commands. You do not need to use them unless you do TeX programming. In the latter case they are handy.– Boris
Dec 26 '11 at 23:34
add a comment |
up vote
18
down vote
Short answer: csname
and endcsname
are a "macro environment" whose contents, if they evaluate (after expanding macros) to "ordinary text", are converted into the name of a macro (or control sequence, hence "csname").
Actually, it is a perhaps strange joke that by the rules of LaTeX macros, you can actually write begin{csname}...end{csname}
and it will act as you expect, to a certain extent. For example:
defmacro{text}
defo{o}
begin{csname}%
macro
end{csname}
% Same as csname macro endcsname
(when run with latex
rather than tex
) will produce the word "text" in the output. The %
sign is there so that unnecessary spaces don't creep into the name of the macro we are constructing.
add a comment |
up vote
18
down vote
Short answer: csname
and endcsname
are a "macro environment" whose contents, if they evaluate (after expanding macros) to "ordinary text", are converted into the name of a macro (or control sequence, hence "csname").
Actually, it is a perhaps strange joke that by the rules of LaTeX macros, you can actually write begin{csname}...end{csname}
and it will act as you expect, to a certain extent. For example:
defmacro{text}
defo{o}
begin{csname}%
macro
end{csname}
% Same as csname macro endcsname
(when run with latex
rather than tex
) will produce the word "text" in the output. The %
sign is there so that unnecessary spaces don't creep into the name of the macro we are constructing.
add a comment |
up vote
18
down vote
up vote
18
down vote
Short answer: csname
and endcsname
are a "macro environment" whose contents, if they evaluate (after expanding macros) to "ordinary text", are converted into the name of a macro (or control sequence, hence "csname").
Actually, it is a perhaps strange joke that by the rules of LaTeX macros, you can actually write begin{csname}...end{csname}
and it will act as you expect, to a certain extent. For example:
defmacro{text}
defo{o}
begin{csname}%
macro
end{csname}
% Same as csname macro endcsname
(when run with latex
rather than tex
) will produce the word "text" in the output. The %
sign is there so that unnecessary spaces don't creep into the name of the macro we are constructing.
Short answer: csname
and endcsname
are a "macro environment" whose contents, if they evaluate (after expanding macros) to "ordinary text", are converted into the name of a macro (or control sequence, hence "csname").
Actually, it is a perhaps strange joke that by the rules of LaTeX macros, you can actually write begin{csname}...end{csname}
and it will act as you expect, to a certain extent. For example:
defmacro{text}
defo{o}
begin{csname}%
macro
end{csname}
% Same as csname macro endcsname
(when run with latex
rather than tex
) will produce the word "text" in the output. The %
sign is there so that unnecessary spaces don't creep into the name of the macro we are constructing.
edited Dec 27 '11 at 9:09
answered Dec 27 '11 at 4:38
Ryan Reich
31.1k799158
31.1k799158
add a comment |
add a comment |
up vote
0
down vote
As a very beginner for this antique problem, I just provide a simple example that may tell future novice something about csname
. Though I do not fully understand I have tried it and successfully solved one problem of mine.
My problem was to test different effect of different font command, like:
textit{textit} textbf{textbf} {bfseries bfseries} {small small}
and I tried to create a new command newcommand{FontTest}[1]{#1 #1}
, I had quite a struggle wondering how to turn the first #1 to a command. And after many trials I used csname
and it worked, so it was:
newcommand{FontTest}[1]{{ csname #1endcsname{#1}}}
and I can freely test different type of font families and shapes like: FontTest{textit} FontTest{sffamily}
etc.
And its effect seems like turning a string into command I guess.
New contributor
add a comment |
up vote
0
down vote
As a very beginner for this antique problem, I just provide a simple example that may tell future novice something about csname
. Though I do not fully understand I have tried it and successfully solved one problem of mine.
My problem was to test different effect of different font command, like:
textit{textit} textbf{textbf} {bfseries bfseries} {small small}
and I tried to create a new command newcommand{FontTest}[1]{#1 #1}
, I had quite a struggle wondering how to turn the first #1 to a command. And after many trials I used csname
and it worked, so it was:
newcommand{FontTest}[1]{{ csname #1endcsname{#1}}}
and I can freely test different type of font families and shapes like: FontTest{textit} FontTest{sffamily}
etc.
And its effect seems like turning a string into command I guess.
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
As a very beginner for this antique problem, I just provide a simple example that may tell future novice something about csname
. Though I do not fully understand I have tried it and successfully solved one problem of mine.
My problem was to test different effect of different font command, like:
textit{textit} textbf{textbf} {bfseries bfseries} {small small}
and I tried to create a new command newcommand{FontTest}[1]{#1 #1}
, I had quite a struggle wondering how to turn the first #1 to a command. And after many trials I used csname
and it worked, so it was:
newcommand{FontTest}[1]{{ csname #1endcsname{#1}}}
and I can freely test different type of font families and shapes like: FontTest{textit} FontTest{sffamily}
etc.
And its effect seems like turning a string into command I guess.
New contributor
As a very beginner for this antique problem, I just provide a simple example that may tell future novice something about csname
. Though I do not fully understand I have tried it and successfully solved one problem of mine.
My problem was to test different effect of different font command, like:
textit{textit} textbf{textbf} {bfseries bfseries} {small small}
and I tried to create a new command newcommand{FontTest}[1]{#1 #1}
, I had quite a struggle wondering how to turn the first #1 to a command. And after many trials I used csname
and it worked, so it was:
newcommand{FontTest}[1]{{ csname #1endcsname{#1}}}
and I can freely test different type of font families and shapes like: FontTest{textit} FontTest{sffamily}
etc.
And its effect seems like turning a string into command I guess.
New contributor
New contributor
answered 12 mins ago
Pistachio Guoguo
1
1
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f39380%2fwhat-exactly-do-csname-and-endcsname-do%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
8
See TeX by Topic, section 11.6
– Martin Schröder
Dec 26 '11 at 22:26