How to make a pipe-divided tuple?Emulate a “tuple” data structure when the syntax for creating a tuple is...
Are there mathematical concepts that exist in the fourth dimension, but not in the third dimension?
How do I make my fill-in-the-blank exercise more obvious?
Could this estimate of the size and mass of the Chicxulub Impactor be accurate?
Looking for the comic book where Spider-Man was [mistakenly] addressed as Super-Man
Male viewpoint in an erotic novel
What's in a druid's grove?
How to make a pipe-divided tuple?
How do I write a vertically-stacked definition of a sequence?
GFI outlets tripped after power outage
Add builder hat to other people with tikzpeople
Professor refuses to write a recommendation letter to students who haven't written a research paper with him
How to measure the statistical "distance" between two frequency distributions?
How could a planet have one hemisphere way warmer than the other without the planet being tidally locked?
Meaning of "Right Out" in Context
Why there is no wireless switch?
Entering the US with dual citizenship but US passport is long expired?
These roommates throw strange parties
What's this constructed number's starter?
How do German speakers decide what should be on the left side of the verb?
Notation: grace note played on the beat with a chord
Why did Tony's Arc Reactor do this?
Examples where "thin + thin = nice and thick"
What quests do you need to stop at before you make an enemy of a faction for each faction?
1kV DC Circuit - Insulation on ground wire?
How to make a pipe-divided tuple?
Emulate a “tuple” data structure when the syntax for creating a tuple is predefined and cannot be alteredHow to use a Pipe (|) with indices like an IntegralHow to make pipe to grow with both sides?Underbraces in Matrix Divided in BlocksDesigning a new tuple of Algebra OperatorsDefine macro for sequence/list/tuple macrosDivided differences tableEmbedding a simple tikz diagram within a tuple equation
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I'm trying to write a macro, tup
that will effectively expand this:
tup{a | b | ... | z}
to this:
leftlangle a ~middle|~ b ~middle|~ ... ~middle|~ z rightrangle
I tried the following, but it only puts down the first separator and then stops.
documentclass[preview]{standalone}
newcommand{brak}[1]{ensuremath{leftlangle#1rightrangle}}
makeatletter
newcommand{tup}[1]{%
begingroup%
@tempswafalse%
def@sep{~middle|~}%
edef@tempa{#1|}%
expandafterbrakexpandafter{expandafter@tuploop@temparelax}%
endgroup%
}
def@tuploop#1|#2relax{%
if@tempswa@sepelse@tempswatruefi{#1}
begingroup
ifxrelax#2relax
renewcommand{next}{endgroup}%
else
renewcommand{next}{endgroup@tuploop#2relax}%
fi
next
}
makeatother
begin{document}
$tup{a|b|c}$
end{document}
However, if I change the definition of @sep
to def@sep{~|~}
then it "works" and puts the pipe characters in between
I believe middle
might be expanding too early, but I'm not sure what to do. How can I get this to work?
math-mode macros
New contributor
add a comment |
I'm trying to write a macro, tup
that will effectively expand this:
tup{a | b | ... | z}
to this:
leftlangle a ~middle|~ b ~middle|~ ... ~middle|~ z rightrangle
I tried the following, but it only puts down the first separator and then stops.
documentclass[preview]{standalone}
newcommand{brak}[1]{ensuremath{leftlangle#1rightrangle}}
makeatletter
newcommand{tup}[1]{%
begingroup%
@tempswafalse%
def@sep{~middle|~}%
edef@tempa{#1|}%
expandafterbrakexpandafter{expandafter@tuploop@temparelax}%
endgroup%
}
def@tuploop#1|#2relax{%
if@tempswa@sepelse@tempswatruefi{#1}
begingroup
ifxrelax#2relax
renewcommand{next}{endgroup}%
else
renewcommand{next}{endgroup@tuploop#2relax}%
fi
next
}
makeatother
begin{document}
$tup{a|b|c}$
end{document}
However, if I change the definition of @sep
to def@sep{~|~}
then it "works" and puts the pipe characters in between
I believe middle
might be expanding too early, but I'm not sure what to do. How can I get this to work?
math-mode macros
New contributor
add a comment |
I'm trying to write a macro, tup
that will effectively expand this:
tup{a | b | ... | z}
to this:
leftlangle a ~middle|~ b ~middle|~ ... ~middle|~ z rightrangle
I tried the following, but it only puts down the first separator and then stops.
documentclass[preview]{standalone}
newcommand{brak}[1]{ensuremath{leftlangle#1rightrangle}}
makeatletter
newcommand{tup}[1]{%
begingroup%
@tempswafalse%
def@sep{~middle|~}%
edef@tempa{#1|}%
expandafterbrakexpandafter{expandafter@tuploop@temparelax}%
endgroup%
}
def@tuploop#1|#2relax{%
if@tempswa@sepelse@tempswatruefi{#1}
begingroup
ifxrelax#2relax
renewcommand{next}{endgroup}%
else
renewcommand{next}{endgroup@tuploop#2relax}%
fi
next
}
makeatother
begin{document}
$tup{a|b|c}$
end{document}
However, if I change the definition of @sep
to def@sep{~|~}
then it "works" and puts the pipe characters in between
I believe middle
might be expanding too early, but I'm not sure what to do. How can I get this to work?
math-mode macros
New contributor
I'm trying to write a macro, tup
that will effectively expand this:
tup{a | b | ... | z}
to this:
leftlangle a ~middle|~ b ~middle|~ ... ~middle|~ z rightrangle
I tried the following, but it only puts down the first separator and then stops.
documentclass[preview]{standalone}
newcommand{brak}[1]{ensuremath{leftlangle#1rightrangle}}
makeatletter
newcommand{tup}[1]{%
begingroup%
@tempswafalse%
def@sep{~middle|~}%
edef@tempa{#1|}%
expandafterbrakexpandafter{expandafter@tuploop@temparelax}%
endgroup%
}
def@tuploop#1|#2relax{%
if@tempswa@sepelse@tempswatruefi{#1}
begingroup
ifxrelax#2relax
renewcommand{next}{endgroup}%
else
renewcommand{next}{endgroup@tuploop#2relax}%
fi
next
}
makeatother
begin{document}
$tup{a|b|c}$
end{document}
However, if I change the definition of @sep
to def@sep{~|~}
then it "works" and puts the pipe characters in between
I believe middle
might be expanding too early, but I'm not sure what to do. How can I get this to work?
math-mode macros
math-mode macros
New contributor
New contributor
edited 8 hours ago
Alex Reinking
New contributor
asked 8 hours ago
Alex ReinkingAlex Reinking
1185 bronze badges
1185 bronze badges
New contributor
New contributor
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
I don't think you need a loop:
documentclass{article}
begingroup
lccode`~=`|
lowercase{endgroup
deftup#1{{def~{;middlevert;}mathcode`|="8000leftlangle#1rightrangle}}}
begin{document}
[
tup{a | b | c | z} + tup{a |frac{A}{B} | c | z}
]
end{document}
Do you mind explaining how this works a little bit? It seems very cool, but I don't know how I would have ever come up with it.
– Alex Reinking
6 hours ago
@AlexReinking it just gives|
an active definition that expands to;middlevert;
so you don't need any looping. The definition uses mathcode"8000
(math active) so it is only locally special within that argument and other uses of|
not inside the argument are not affected.
– David Carlisle
6 hours ago
add a comment |
As a general rule, I recommend to avoid left
and right
whenever possible.
Here's an implementation with a syntax similar to commands defined with DeclarePairedDelimiter
from mathtools
.
The unadorned command uses normal size; in the optional argument big
, Big
, bigg
or Bigg
can appear. The *
means using automatic sizing.
Spaces in the mandatory argument are ignored, so tup{a|b|c}
is the same as tup{a | b | c}
.
documentclass{article}
usepackage{amsmath}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{tup}{som}
{
IfBooleanTF{#1}
{
tl_set:Nn l__reinking_tup_open_tl { leftlangle }
tl_set:Nn l__reinking_tup_middle_tl { ;middle|; }
tl_set:Nn l__reinking_tup_close_tl { rightrangle }
}
{
IfNoValueTF{#2}
{
tl_set:Nn l__reinking_tup_open_tl { langle }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{|} }
tl_set:Nn l__reinking_tup_close_tl { rangle }
}
{
tl_set:Nn l__reinking_tup_open_tl { mathopen{#2langle} }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{#2|} }
tl_set:Nn l__reinking_tup_close_tl { mathclose{#2rangle} }
}
}
__reinking_tup:n { #3 }
}
tl_new:N l__reinking_tup_open_tl
tl_new:N l__reinking_tup_middle_tl
tl_new:N l__reinking_tup_close_tl
seq_new:N l__reinking_tup_items_seq
cs_new_protected:Nn __reinking_tup:n
{
seq_set_split:Nnn l__reinking_tup_items_seq { | } { #1 }
l__reinking_tup_open_tl
seq_use:NV l__reinking_tup_items_seq l__reinking_tup_middle_tl
l__reinking_tup_close_tl
}
cs_generate_variant:Nn seq_use:Nn { NV }
ExplSyntaxOff
begin{document}
$tup{a|b|dots|z}$
quad
$tup[big]{a|b|dots|z}$
quad
$tup[Big]{a|b|dots|z}$
quad
$tup*{dfrac{a}{2}|b|dots|z}$
end{document}
A package-free version, which indiscriminately uses left
, middle
and right
. The items are absorbed one at a time and appended to a list, then the list is executed.
documentclass{article}
usepackage{amsmath}
makeatletter
newcommand{tup}[1]{%
def@tup@list{}%
@tup#1|@tup|
}
def@tup#1|{%
ifx@tup#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
{leftlangle@tup@listrightrangle}%
{ifx@tup@list@empty@tup@append{#1}else@tup@append{;middle|;#1}fi@tup}%
}
def@tup@append#1{%
expandafterdefexpandafter@tup@listexpandafter{@tup@list#1}%
}
makeatother
begin{document}
$tup{a}$
$tup{a|b|dots|z}$
end{document}
This way there's no problem with groups implicitly formed by left
, middle
and right
.
Unfortunately, the publishing system I'm submitting to does not have xparse whitelisted.
– Alex Reinking
7 hours ago
@AlexReinking Too bad. Do you really needleft
,middle
andright
?
– egreg
7 hours ago
@AlexReinking I added a package-free version.
– egreg
7 hours ago
Perhapsifx@tup#1
in case one usestup{aa}
– Phelype Oleinik
6 hours ago
1
@PhelypeOleinik You're right. I opted forifx@tup#1relax
which also copes with an empty argument.
– egreg
6 hours ago
|
show 4 more comments
Your command was almost working. If you put more items in tup
, like $tup{a|b|c|d|e|f}$
you'd see this:
it's not the first item which is left out, rather they are grouped in pairs. Your command starts with a leftlangle
, which starts a "math left group" and with the @tempswa
switch set to false. In the first iteration of @tuploop
you do if@tempswa@sepelse@tempswatruefi
which expands to @tempswatrue
, meaning that no @sep
is inserted now, and @tempswa
is now true. The command then inserts the first item (a
) and proceeds to the next iteration.
On the second iteration, @tempswa
is true and if@tempswa@sepelse@tempswatruefi
expands to @sep
, which inserts a middle|
(the separator after a
). The middle
primitive will end the "math left group" started by leftlangle
and will start another "math left group". However, when the first group ended, your @tempswa
switch was restored to its value outside the current group, which is false! And now the command inserts b
and goes to the third iteration, but now with @tempswa
set to false once again.
So your command was actually inserting <token>|<token>
and then <token>|<token>
, and so on, due to the end of group triggered by middle
. You could circumvent this with some aftergroup
magic, or simply making a gloabl assignment to the switch:
if@tempswa@sepelseglobal@tempswatruefi
However, as egreg said in the comment, @tempswa
is supposed to be set locally only, so it would be better if you created a newififg@insertsep
and then always used globalg@insertseptrue
and globalg@insertsepfalse
.
And since meanwhile egreg posted a much more elaborate expl3
answer than the one-liner I would post, I'll stop here ;-)
if@tempswa
should only be set locally. A different conditional to be always used globally would be preferable.
– egreg
7 hours ago
@egreg How would I know that withoutg_...
? ;-) Thanks for pointing it out, I added a note in the answer.
– Phelype Oleinik
7 hours ago
add a comment |
There seems to be space for one more answer, in which one could mention that all this seems to have been done some long time ago.
documentclass{article}
usepackage{braket}
begin{document}
[
Braket{a | b | c | z} + Braket{a |frac{A}{B} | c | z}
]
end{document}
Back then they also needed no loop. ;-)
Well, this is definitely how I will do this whenever I don't have a strict whitelist of packages to adhere to.
– Alex Reinking
5 hours ago
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/4.0/"u003ecc by-sa 4.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
});
}
});
Alex Reinking is a new contributor. Be nice, and check out our Code of Conduct.
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%2f507195%2fhow-to-make-a-pipe-divided-tuple%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't think you need a loop:
documentclass{article}
begingroup
lccode`~=`|
lowercase{endgroup
deftup#1{{def~{;middlevert;}mathcode`|="8000leftlangle#1rightrangle}}}
begin{document}
[
tup{a | b | c | z} + tup{a |frac{A}{B} | c | z}
]
end{document}
Do you mind explaining how this works a little bit? It seems very cool, but I don't know how I would have ever come up with it.
– Alex Reinking
6 hours ago
@AlexReinking it just gives|
an active definition that expands to;middlevert;
so you don't need any looping. The definition uses mathcode"8000
(math active) so it is only locally special within that argument and other uses of|
not inside the argument are not affected.
– David Carlisle
6 hours ago
add a comment |
I don't think you need a loop:
documentclass{article}
begingroup
lccode`~=`|
lowercase{endgroup
deftup#1{{def~{;middlevert;}mathcode`|="8000leftlangle#1rightrangle}}}
begin{document}
[
tup{a | b | c | z} + tup{a |frac{A}{B} | c | z}
]
end{document}
Do you mind explaining how this works a little bit? It seems very cool, but I don't know how I would have ever come up with it.
– Alex Reinking
6 hours ago
@AlexReinking it just gives|
an active definition that expands to;middlevert;
so you don't need any looping. The definition uses mathcode"8000
(math active) so it is only locally special within that argument and other uses of|
not inside the argument are not affected.
– David Carlisle
6 hours ago
add a comment |
I don't think you need a loop:
documentclass{article}
begingroup
lccode`~=`|
lowercase{endgroup
deftup#1{{def~{;middlevert;}mathcode`|="8000leftlangle#1rightrangle}}}
begin{document}
[
tup{a | b | c | z} + tup{a |frac{A}{B} | c | z}
]
end{document}
I don't think you need a loop:
documentclass{article}
begingroup
lccode`~=`|
lowercase{endgroup
deftup#1{{def~{;middlevert;}mathcode`|="8000leftlangle#1rightrangle}}}
begin{document}
[
tup{a | b | c | z} + tup{a |frac{A}{B} | c | z}
]
end{document}
answered 7 hours ago
David CarlisleDavid Carlisle
521k44 gold badges1182 silver badges1952 bronze badges
521k44 gold badges1182 silver badges1952 bronze badges
Do you mind explaining how this works a little bit? It seems very cool, but I don't know how I would have ever come up with it.
– Alex Reinking
6 hours ago
@AlexReinking it just gives|
an active definition that expands to;middlevert;
so you don't need any looping. The definition uses mathcode"8000
(math active) so it is only locally special within that argument and other uses of|
not inside the argument are not affected.
– David Carlisle
6 hours ago
add a comment |
Do you mind explaining how this works a little bit? It seems very cool, but I don't know how I would have ever come up with it.
– Alex Reinking
6 hours ago
@AlexReinking it just gives|
an active definition that expands to;middlevert;
so you don't need any looping. The definition uses mathcode"8000
(math active) so it is only locally special within that argument and other uses of|
not inside the argument are not affected.
– David Carlisle
6 hours ago
Do you mind explaining how this works a little bit? It seems very cool, but I don't know how I would have ever come up with it.
– Alex Reinking
6 hours ago
Do you mind explaining how this works a little bit? It seems very cool, but I don't know how I would have ever come up with it.
– Alex Reinking
6 hours ago
@AlexReinking it just gives
|
an active definition that expands to ;middlevert;
so you don't need any looping. The definition uses mathcode "8000
(math active) so it is only locally special within that argument and other uses of |
not inside the argument are not affected.– David Carlisle
6 hours ago
@AlexReinking it just gives
|
an active definition that expands to ;middlevert;
so you don't need any looping. The definition uses mathcode "8000
(math active) so it is only locally special within that argument and other uses of |
not inside the argument are not affected.– David Carlisle
6 hours ago
add a comment |
As a general rule, I recommend to avoid left
and right
whenever possible.
Here's an implementation with a syntax similar to commands defined with DeclarePairedDelimiter
from mathtools
.
The unadorned command uses normal size; in the optional argument big
, Big
, bigg
or Bigg
can appear. The *
means using automatic sizing.
Spaces in the mandatory argument are ignored, so tup{a|b|c}
is the same as tup{a | b | c}
.
documentclass{article}
usepackage{amsmath}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{tup}{som}
{
IfBooleanTF{#1}
{
tl_set:Nn l__reinking_tup_open_tl { leftlangle }
tl_set:Nn l__reinking_tup_middle_tl { ;middle|; }
tl_set:Nn l__reinking_tup_close_tl { rightrangle }
}
{
IfNoValueTF{#2}
{
tl_set:Nn l__reinking_tup_open_tl { langle }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{|} }
tl_set:Nn l__reinking_tup_close_tl { rangle }
}
{
tl_set:Nn l__reinking_tup_open_tl { mathopen{#2langle} }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{#2|} }
tl_set:Nn l__reinking_tup_close_tl { mathclose{#2rangle} }
}
}
__reinking_tup:n { #3 }
}
tl_new:N l__reinking_tup_open_tl
tl_new:N l__reinking_tup_middle_tl
tl_new:N l__reinking_tup_close_tl
seq_new:N l__reinking_tup_items_seq
cs_new_protected:Nn __reinking_tup:n
{
seq_set_split:Nnn l__reinking_tup_items_seq { | } { #1 }
l__reinking_tup_open_tl
seq_use:NV l__reinking_tup_items_seq l__reinking_tup_middle_tl
l__reinking_tup_close_tl
}
cs_generate_variant:Nn seq_use:Nn { NV }
ExplSyntaxOff
begin{document}
$tup{a|b|dots|z}$
quad
$tup[big]{a|b|dots|z}$
quad
$tup[Big]{a|b|dots|z}$
quad
$tup*{dfrac{a}{2}|b|dots|z}$
end{document}
A package-free version, which indiscriminately uses left
, middle
and right
. The items are absorbed one at a time and appended to a list, then the list is executed.
documentclass{article}
usepackage{amsmath}
makeatletter
newcommand{tup}[1]{%
def@tup@list{}%
@tup#1|@tup|
}
def@tup#1|{%
ifx@tup#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
{leftlangle@tup@listrightrangle}%
{ifx@tup@list@empty@tup@append{#1}else@tup@append{;middle|;#1}fi@tup}%
}
def@tup@append#1{%
expandafterdefexpandafter@tup@listexpandafter{@tup@list#1}%
}
makeatother
begin{document}
$tup{a}$
$tup{a|b|dots|z}$
end{document}
This way there's no problem with groups implicitly formed by left
, middle
and right
.
Unfortunately, the publishing system I'm submitting to does not have xparse whitelisted.
– Alex Reinking
7 hours ago
@AlexReinking Too bad. Do you really needleft
,middle
andright
?
– egreg
7 hours ago
@AlexReinking I added a package-free version.
– egreg
7 hours ago
Perhapsifx@tup#1
in case one usestup{aa}
– Phelype Oleinik
6 hours ago
1
@PhelypeOleinik You're right. I opted forifx@tup#1relax
which also copes with an empty argument.
– egreg
6 hours ago
|
show 4 more comments
As a general rule, I recommend to avoid left
and right
whenever possible.
Here's an implementation with a syntax similar to commands defined with DeclarePairedDelimiter
from mathtools
.
The unadorned command uses normal size; in the optional argument big
, Big
, bigg
or Bigg
can appear. The *
means using automatic sizing.
Spaces in the mandatory argument are ignored, so tup{a|b|c}
is the same as tup{a | b | c}
.
documentclass{article}
usepackage{amsmath}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{tup}{som}
{
IfBooleanTF{#1}
{
tl_set:Nn l__reinking_tup_open_tl { leftlangle }
tl_set:Nn l__reinking_tup_middle_tl { ;middle|; }
tl_set:Nn l__reinking_tup_close_tl { rightrangle }
}
{
IfNoValueTF{#2}
{
tl_set:Nn l__reinking_tup_open_tl { langle }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{|} }
tl_set:Nn l__reinking_tup_close_tl { rangle }
}
{
tl_set:Nn l__reinking_tup_open_tl { mathopen{#2langle} }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{#2|} }
tl_set:Nn l__reinking_tup_close_tl { mathclose{#2rangle} }
}
}
__reinking_tup:n { #3 }
}
tl_new:N l__reinking_tup_open_tl
tl_new:N l__reinking_tup_middle_tl
tl_new:N l__reinking_tup_close_tl
seq_new:N l__reinking_tup_items_seq
cs_new_protected:Nn __reinking_tup:n
{
seq_set_split:Nnn l__reinking_tup_items_seq { | } { #1 }
l__reinking_tup_open_tl
seq_use:NV l__reinking_tup_items_seq l__reinking_tup_middle_tl
l__reinking_tup_close_tl
}
cs_generate_variant:Nn seq_use:Nn { NV }
ExplSyntaxOff
begin{document}
$tup{a|b|dots|z}$
quad
$tup[big]{a|b|dots|z}$
quad
$tup[Big]{a|b|dots|z}$
quad
$tup*{dfrac{a}{2}|b|dots|z}$
end{document}
A package-free version, which indiscriminately uses left
, middle
and right
. The items are absorbed one at a time and appended to a list, then the list is executed.
documentclass{article}
usepackage{amsmath}
makeatletter
newcommand{tup}[1]{%
def@tup@list{}%
@tup#1|@tup|
}
def@tup#1|{%
ifx@tup#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
{leftlangle@tup@listrightrangle}%
{ifx@tup@list@empty@tup@append{#1}else@tup@append{;middle|;#1}fi@tup}%
}
def@tup@append#1{%
expandafterdefexpandafter@tup@listexpandafter{@tup@list#1}%
}
makeatother
begin{document}
$tup{a}$
$tup{a|b|dots|z}$
end{document}
This way there's no problem with groups implicitly formed by left
, middle
and right
.
Unfortunately, the publishing system I'm submitting to does not have xparse whitelisted.
– Alex Reinking
7 hours ago
@AlexReinking Too bad. Do you really needleft
,middle
andright
?
– egreg
7 hours ago
@AlexReinking I added a package-free version.
– egreg
7 hours ago
Perhapsifx@tup#1
in case one usestup{aa}
– Phelype Oleinik
6 hours ago
1
@PhelypeOleinik You're right. I opted forifx@tup#1relax
which also copes with an empty argument.
– egreg
6 hours ago
|
show 4 more comments
As a general rule, I recommend to avoid left
and right
whenever possible.
Here's an implementation with a syntax similar to commands defined with DeclarePairedDelimiter
from mathtools
.
The unadorned command uses normal size; in the optional argument big
, Big
, bigg
or Bigg
can appear. The *
means using automatic sizing.
Spaces in the mandatory argument are ignored, so tup{a|b|c}
is the same as tup{a | b | c}
.
documentclass{article}
usepackage{amsmath}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{tup}{som}
{
IfBooleanTF{#1}
{
tl_set:Nn l__reinking_tup_open_tl { leftlangle }
tl_set:Nn l__reinking_tup_middle_tl { ;middle|; }
tl_set:Nn l__reinking_tup_close_tl { rightrangle }
}
{
IfNoValueTF{#2}
{
tl_set:Nn l__reinking_tup_open_tl { langle }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{|} }
tl_set:Nn l__reinking_tup_close_tl { rangle }
}
{
tl_set:Nn l__reinking_tup_open_tl { mathopen{#2langle} }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{#2|} }
tl_set:Nn l__reinking_tup_close_tl { mathclose{#2rangle} }
}
}
__reinking_tup:n { #3 }
}
tl_new:N l__reinking_tup_open_tl
tl_new:N l__reinking_tup_middle_tl
tl_new:N l__reinking_tup_close_tl
seq_new:N l__reinking_tup_items_seq
cs_new_protected:Nn __reinking_tup:n
{
seq_set_split:Nnn l__reinking_tup_items_seq { | } { #1 }
l__reinking_tup_open_tl
seq_use:NV l__reinking_tup_items_seq l__reinking_tup_middle_tl
l__reinking_tup_close_tl
}
cs_generate_variant:Nn seq_use:Nn { NV }
ExplSyntaxOff
begin{document}
$tup{a|b|dots|z}$
quad
$tup[big]{a|b|dots|z}$
quad
$tup[Big]{a|b|dots|z}$
quad
$tup*{dfrac{a}{2}|b|dots|z}$
end{document}
A package-free version, which indiscriminately uses left
, middle
and right
. The items are absorbed one at a time and appended to a list, then the list is executed.
documentclass{article}
usepackage{amsmath}
makeatletter
newcommand{tup}[1]{%
def@tup@list{}%
@tup#1|@tup|
}
def@tup#1|{%
ifx@tup#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
{leftlangle@tup@listrightrangle}%
{ifx@tup@list@empty@tup@append{#1}else@tup@append{;middle|;#1}fi@tup}%
}
def@tup@append#1{%
expandafterdefexpandafter@tup@listexpandafter{@tup@list#1}%
}
makeatother
begin{document}
$tup{a}$
$tup{a|b|dots|z}$
end{document}
This way there's no problem with groups implicitly formed by left
, middle
and right
.
As a general rule, I recommend to avoid left
and right
whenever possible.
Here's an implementation with a syntax similar to commands defined with DeclarePairedDelimiter
from mathtools
.
The unadorned command uses normal size; in the optional argument big
, Big
, bigg
or Bigg
can appear. The *
means using automatic sizing.
Spaces in the mandatory argument are ignored, so tup{a|b|c}
is the same as tup{a | b | c}
.
documentclass{article}
usepackage{amsmath}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{tup}{som}
{
IfBooleanTF{#1}
{
tl_set:Nn l__reinking_tup_open_tl { leftlangle }
tl_set:Nn l__reinking_tup_middle_tl { ;middle|; }
tl_set:Nn l__reinking_tup_close_tl { rightrangle }
}
{
IfNoValueTF{#2}
{
tl_set:Nn l__reinking_tup_open_tl { langle }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{|} }
tl_set:Nn l__reinking_tup_close_tl { rangle }
}
{
tl_set:Nn l__reinking_tup_open_tl { mathopen{#2langle} }
tl_set:Nn l__reinking_tup_middle_tl { mathrel{#2|} }
tl_set:Nn l__reinking_tup_close_tl { mathclose{#2rangle} }
}
}
__reinking_tup:n { #3 }
}
tl_new:N l__reinking_tup_open_tl
tl_new:N l__reinking_tup_middle_tl
tl_new:N l__reinking_tup_close_tl
seq_new:N l__reinking_tup_items_seq
cs_new_protected:Nn __reinking_tup:n
{
seq_set_split:Nnn l__reinking_tup_items_seq { | } { #1 }
l__reinking_tup_open_tl
seq_use:NV l__reinking_tup_items_seq l__reinking_tup_middle_tl
l__reinking_tup_close_tl
}
cs_generate_variant:Nn seq_use:Nn { NV }
ExplSyntaxOff
begin{document}
$tup{a|b|dots|z}$
quad
$tup[big]{a|b|dots|z}$
quad
$tup[Big]{a|b|dots|z}$
quad
$tup*{dfrac{a}{2}|b|dots|z}$
end{document}
A package-free version, which indiscriminately uses left
, middle
and right
. The items are absorbed one at a time and appended to a list, then the list is executed.
documentclass{article}
usepackage{amsmath}
makeatletter
newcommand{tup}[1]{%
def@tup@list{}%
@tup#1|@tup|
}
def@tup#1|{%
ifx@tup#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
{leftlangle@tup@listrightrangle}%
{ifx@tup@list@empty@tup@append{#1}else@tup@append{;middle|;#1}fi@tup}%
}
def@tup@append#1{%
expandafterdefexpandafter@tup@listexpandafter{@tup@list#1}%
}
makeatother
begin{document}
$tup{a}$
$tup{a|b|dots|z}$
end{document}
This way there's no problem with groups implicitly formed by left
, middle
and right
.
edited 6 hours ago
answered 8 hours ago
egregegreg
767k90 gold badges2001 silver badges3353 bronze badges
767k90 gold badges2001 silver badges3353 bronze badges
Unfortunately, the publishing system I'm submitting to does not have xparse whitelisted.
– Alex Reinking
7 hours ago
@AlexReinking Too bad. Do you really needleft
,middle
andright
?
– egreg
7 hours ago
@AlexReinking I added a package-free version.
– egreg
7 hours ago
Perhapsifx@tup#1
in case one usestup{aa}
– Phelype Oleinik
6 hours ago
1
@PhelypeOleinik You're right. I opted forifx@tup#1relax
which also copes with an empty argument.
– egreg
6 hours ago
|
show 4 more comments
Unfortunately, the publishing system I'm submitting to does not have xparse whitelisted.
– Alex Reinking
7 hours ago
@AlexReinking Too bad. Do you really needleft
,middle
andright
?
– egreg
7 hours ago
@AlexReinking I added a package-free version.
– egreg
7 hours ago
Perhapsifx@tup#1
in case one usestup{aa}
– Phelype Oleinik
6 hours ago
1
@PhelypeOleinik You're right. I opted forifx@tup#1relax
which also copes with an empty argument.
– egreg
6 hours ago
Unfortunately, the publishing system I'm submitting to does not have xparse whitelisted.
– Alex Reinking
7 hours ago
Unfortunately, the publishing system I'm submitting to does not have xparse whitelisted.
– Alex Reinking
7 hours ago
@AlexReinking Too bad. Do you really need
left
, middle
and right
?– egreg
7 hours ago
@AlexReinking Too bad. Do you really need
left
, middle
and right
?– egreg
7 hours ago
@AlexReinking I added a package-free version.
– egreg
7 hours ago
@AlexReinking I added a package-free version.
– egreg
7 hours ago
Perhaps
ifx@tup#1
in case one uses tup{aa}
– Phelype Oleinik
6 hours ago
Perhaps
ifx@tup#1
in case one uses tup{aa}
– Phelype Oleinik
6 hours ago
1
1
@PhelypeOleinik You're right. I opted for
ifx@tup#1relax
which also copes with an empty argument.– egreg
6 hours ago
@PhelypeOleinik You're right. I opted for
ifx@tup#1relax
which also copes with an empty argument.– egreg
6 hours ago
|
show 4 more comments
Your command was almost working. If you put more items in tup
, like $tup{a|b|c|d|e|f}$
you'd see this:
it's not the first item which is left out, rather they are grouped in pairs. Your command starts with a leftlangle
, which starts a "math left group" and with the @tempswa
switch set to false. In the first iteration of @tuploop
you do if@tempswa@sepelse@tempswatruefi
which expands to @tempswatrue
, meaning that no @sep
is inserted now, and @tempswa
is now true. The command then inserts the first item (a
) and proceeds to the next iteration.
On the second iteration, @tempswa
is true and if@tempswa@sepelse@tempswatruefi
expands to @sep
, which inserts a middle|
(the separator after a
). The middle
primitive will end the "math left group" started by leftlangle
and will start another "math left group". However, when the first group ended, your @tempswa
switch was restored to its value outside the current group, which is false! And now the command inserts b
and goes to the third iteration, but now with @tempswa
set to false once again.
So your command was actually inserting <token>|<token>
and then <token>|<token>
, and so on, due to the end of group triggered by middle
. You could circumvent this with some aftergroup
magic, or simply making a gloabl assignment to the switch:
if@tempswa@sepelseglobal@tempswatruefi
However, as egreg said in the comment, @tempswa
is supposed to be set locally only, so it would be better if you created a newififg@insertsep
and then always used globalg@insertseptrue
and globalg@insertsepfalse
.
And since meanwhile egreg posted a much more elaborate expl3
answer than the one-liner I would post, I'll stop here ;-)
if@tempswa
should only be set locally. A different conditional to be always used globally would be preferable.
– egreg
7 hours ago
@egreg How would I know that withoutg_...
? ;-) Thanks for pointing it out, I added a note in the answer.
– Phelype Oleinik
7 hours ago
add a comment |
Your command was almost working. If you put more items in tup
, like $tup{a|b|c|d|e|f}$
you'd see this:
it's not the first item which is left out, rather they are grouped in pairs. Your command starts with a leftlangle
, which starts a "math left group" and with the @tempswa
switch set to false. In the first iteration of @tuploop
you do if@tempswa@sepelse@tempswatruefi
which expands to @tempswatrue
, meaning that no @sep
is inserted now, and @tempswa
is now true. The command then inserts the first item (a
) and proceeds to the next iteration.
On the second iteration, @tempswa
is true and if@tempswa@sepelse@tempswatruefi
expands to @sep
, which inserts a middle|
(the separator after a
). The middle
primitive will end the "math left group" started by leftlangle
and will start another "math left group". However, when the first group ended, your @tempswa
switch was restored to its value outside the current group, which is false! And now the command inserts b
and goes to the third iteration, but now with @tempswa
set to false once again.
So your command was actually inserting <token>|<token>
and then <token>|<token>
, and so on, due to the end of group triggered by middle
. You could circumvent this with some aftergroup
magic, or simply making a gloabl assignment to the switch:
if@tempswa@sepelseglobal@tempswatruefi
However, as egreg said in the comment, @tempswa
is supposed to be set locally only, so it would be better if you created a newififg@insertsep
and then always used globalg@insertseptrue
and globalg@insertsepfalse
.
And since meanwhile egreg posted a much more elaborate expl3
answer than the one-liner I would post, I'll stop here ;-)
if@tempswa
should only be set locally. A different conditional to be always used globally would be preferable.
– egreg
7 hours ago
@egreg How would I know that withoutg_...
? ;-) Thanks for pointing it out, I added a note in the answer.
– Phelype Oleinik
7 hours ago
add a comment |
Your command was almost working. If you put more items in tup
, like $tup{a|b|c|d|e|f}$
you'd see this:
it's not the first item which is left out, rather they are grouped in pairs. Your command starts with a leftlangle
, which starts a "math left group" and with the @tempswa
switch set to false. In the first iteration of @tuploop
you do if@tempswa@sepelse@tempswatruefi
which expands to @tempswatrue
, meaning that no @sep
is inserted now, and @tempswa
is now true. The command then inserts the first item (a
) and proceeds to the next iteration.
On the second iteration, @tempswa
is true and if@tempswa@sepelse@tempswatruefi
expands to @sep
, which inserts a middle|
(the separator after a
). The middle
primitive will end the "math left group" started by leftlangle
and will start another "math left group". However, when the first group ended, your @tempswa
switch was restored to its value outside the current group, which is false! And now the command inserts b
and goes to the third iteration, but now with @tempswa
set to false once again.
So your command was actually inserting <token>|<token>
and then <token>|<token>
, and so on, due to the end of group triggered by middle
. You could circumvent this with some aftergroup
magic, or simply making a gloabl assignment to the switch:
if@tempswa@sepelseglobal@tempswatruefi
However, as egreg said in the comment, @tempswa
is supposed to be set locally only, so it would be better if you created a newififg@insertsep
and then always used globalg@insertseptrue
and globalg@insertsepfalse
.
And since meanwhile egreg posted a much more elaborate expl3
answer than the one-liner I would post, I'll stop here ;-)
Your command was almost working. If you put more items in tup
, like $tup{a|b|c|d|e|f}$
you'd see this:
it's not the first item which is left out, rather they are grouped in pairs. Your command starts with a leftlangle
, which starts a "math left group" and with the @tempswa
switch set to false. In the first iteration of @tuploop
you do if@tempswa@sepelse@tempswatruefi
which expands to @tempswatrue
, meaning that no @sep
is inserted now, and @tempswa
is now true. The command then inserts the first item (a
) and proceeds to the next iteration.
On the second iteration, @tempswa
is true and if@tempswa@sepelse@tempswatruefi
expands to @sep
, which inserts a middle|
(the separator after a
). The middle
primitive will end the "math left group" started by leftlangle
and will start another "math left group". However, when the first group ended, your @tempswa
switch was restored to its value outside the current group, which is false! And now the command inserts b
and goes to the third iteration, but now with @tempswa
set to false once again.
So your command was actually inserting <token>|<token>
and then <token>|<token>
, and so on, due to the end of group triggered by middle
. You could circumvent this with some aftergroup
magic, or simply making a gloabl assignment to the switch:
if@tempswa@sepelseglobal@tempswatruefi
However, as egreg said in the comment, @tempswa
is supposed to be set locally only, so it would be better if you created a newififg@insertsep
and then always used globalg@insertseptrue
and globalg@insertsepfalse
.
And since meanwhile egreg posted a much more elaborate expl3
answer than the one-liner I would post, I'll stop here ;-)
edited 7 hours ago
answered 7 hours ago
Phelype OleinikPhelype Oleinik
33.5k7 gold badges56 silver badges112 bronze badges
33.5k7 gold badges56 silver badges112 bronze badges
if@tempswa
should only be set locally. A different conditional to be always used globally would be preferable.
– egreg
7 hours ago
@egreg How would I know that withoutg_...
? ;-) Thanks for pointing it out, I added a note in the answer.
– Phelype Oleinik
7 hours ago
add a comment |
if@tempswa
should only be set locally. A different conditional to be always used globally would be preferable.
– egreg
7 hours ago
@egreg How would I know that withoutg_...
? ;-) Thanks for pointing it out, I added a note in the answer.
– Phelype Oleinik
7 hours ago
if@tempswa
should only be set locally. A different conditional to be always used globally would be preferable.– egreg
7 hours ago
if@tempswa
should only be set locally. A different conditional to be always used globally would be preferable.– egreg
7 hours ago
@egreg How would I know that without
g_...
? ;-) Thanks for pointing it out, I added a note in the answer.– Phelype Oleinik
7 hours ago
@egreg How would I know that without
g_...
? ;-) Thanks for pointing it out, I added a note in the answer.– Phelype Oleinik
7 hours ago
add a comment |
There seems to be space for one more answer, in which one could mention that all this seems to have been done some long time ago.
documentclass{article}
usepackage{braket}
begin{document}
[
Braket{a | b | c | z} + Braket{a |frac{A}{B} | c | z}
]
end{document}
Back then they also needed no loop. ;-)
Well, this is definitely how I will do this whenever I don't have a strict whitelist of packages to adhere to.
– Alex Reinking
5 hours ago
add a comment |
There seems to be space for one more answer, in which one could mention that all this seems to have been done some long time ago.
documentclass{article}
usepackage{braket}
begin{document}
[
Braket{a | b | c | z} + Braket{a |frac{A}{B} | c | z}
]
end{document}
Back then they also needed no loop. ;-)
Well, this is definitely how I will do this whenever I don't have a strict whitelist of packages to adhere to.
– Alex Reinking
5 hours ago
add a comment |
There seems to be space for one more answer, in which one could mention that all this seems to have been done some long time ago.
documentclass{article}
usepackage{braket}
begin{document}
[
Braket{a | b | c | z} + Braket{a |frac{A}{B} | c | z}
]
end{document}
Back then they also needed no loop. ;-)
There seems to be space for one more answer, in which one could mention that all this seems to have been done some long time ago.
documentclass{article}
usepackage{braket}
begin{document}
[
Braket{a | b | c | z} + Braket{a |frac{A}{B} | c | z}
]
end{document}
Back then they also needed no loop. ;-)
answered 5 hours ago
Schrödinger's catSchrödinger's cat
4,8517 silver badges16 bronze badges
4,8517 silver badges16 bronze badges
Well, this is definitely how I will do this whenever I don't have a strict whitelist of packages to adhere to.
– Alex Reinking
5 hours ago
add a comment |
Well, this is definitely how I will do this whenever I don't have a strict whitelist of packages to adhere to.
– Alex Reinking
5 hours ago
Well, this is definitely how I will do this whenever I don't have a strict whitelist of packages to adhere to.
– Alex Reinking
5 hours ago
Well, this is definitely how I will do this whenever I don't have a strict whitelist of packages to adhere to.
– Alex Reinking
5 hours ago
add a comment |
Alex Reinking is a new contributor. Be nice, and check out our Code of Conduct.
Alex Reinking is a new contributor. Be nice, and check out our Code of Conduct.
Alex Reinking is a new contributor. Be nice, and check out our Code of Conduct.
Alex Reinking is a new contributor. Be nice, and check out our Code of Conduct.
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.
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%2f507195%2fhow-to-make-a-pipe-divided-tuple%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