Collision of etoolbox and LuaLatex
up vote
3
down vote
favorite
I use a macro mychoice{item a, item b, items c,…}
that returns one of the items of the comma-separated-list determined by an external counter based on the docsvlist
macro from etoolbox
. In simple text, this macro works fine, but when I try to use it in a directlua{}
from Lua(La)TeX, it raises strange (at least for me) errors.
MWE:
documentclass{article}
usepackage{luacode}
usepackage{etoolbox}
begin{luacode*}
function dummy(argument)
end
end{luacode*}
newcounter{choicecounter}
newcounter{argcounter}
newcommand{mychoice}[1]{
setcounter{argcounter}{0}
renewcommand*{do}[1]{
stepcounter{argcounter}
csedef{argcounterarabic{argcounter}}{##1}}
docsvlist{#1}
csuse{argcounterarabic{choicecounter}}
}
newcommand{main}[1]{%
setcounter{choicecounter}{#1}
hello world mychoice{one,{two,three}}
directlua{dummy(luastring{mychoice{foo,bar}})}
}
begin{document}
main{1}
main{2}
end{document}
So this version raises the error. If you comment out the directlua
it compiles fine.
What am I doing wrong? Or is there a bug somewhere?
PS: Replacing directlua
with luadirect
or luaexec
from luacode
-package or leaving out luastring
-command doesn't change anything.
luatex etoolbox luacode
add a comment |
up vote
3
down vote
favorite
I use a macro mychoice{item a, item b, items c,…}
that returns one of the items of the comma-separated-list determined by an external counter based on the docsvlist
macro from etoolbox
. In simple text, this macro works fine, but when I try to use it in a directlua{}
from Lua(La)TeX, it raises strange (at least for me) errors.
MWE:
documentclass{article}
usepackage{luacode}
usepackage{etoolbox}
begin{luacode*}
function dummy(argument)
end
end{luacode*}
newcounter{choicecounter}
newcounter{argcounter}
newcommand{mychoice}[1]{
setcounter{argcounter}{0}
renewcommand*{do}[1]{
stepcounter{argcounter}
csedef{argcounterarabic{argcounter}}{##1}}
docsvlist{#1}
csuse{argcounterarabic{choicecounter}}
}
newcommand{main}[1]{%
setcounter{choicecounter}{#1}
hello world mychoice{one,{two,three}}
directlua{dummy(luastring{mychoice{foo,bar}})}
}
begin{document}
main{1}
main{2}
end{document}
So this version raises the error. If you comment out the directlua
it compiles fine.
What am I doing wrong? Or is there a bug somewhere?
PS: Replacing directlua
with luadirect
or luaexec
from luacode
-package or leaving out luastring
-command doesn't change anything.
luatex etoolbox luacode
2
your command is not expandable. You can't fed abitrary stuff to directlua, it must expand to something lua can handle.
– Ulrike Fischer
Dec 7 at 11:41
And how can I make it expandable? Or rather: What is preventing it from being expanded to something, lua can handle?
– Toscho
Dec 7 at 13:59
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I use a macro mychoice{item a, item b, items c,…}
that returns one of the items of the comma-separated-list determined by an external counter based on the docsvlist
macro from etoolbox
. In simple text, this macro works fine, but when I try to use it in a directlua{}
from Lua(La)TeX, it raises strange (at least for me) errors.
MWE:
documentclass{article}
usepackage{luacode}
usepackage{etoolbox}
begin{luacode*}
function dummy(argument)
end
end{luacode*}
newcounter{choicecounter}
newcounter{argcounter}
newcommand{mychoice}[1]{
setcounter{argcounter}{0}
renewcommand*{do}[1]{
stepcounter{argcounter}
csedef{argcounterarabic{argcounter}}{##1}}
docsvlist{#1}
csuse{argcounterarabic{choicecounter}}
}
newcommand{main}[1]{%
setcounter{choicecounter}{#1}
hello world mychoice{one,{two,three}}
directlua{dummy(luastring{mychoice{foo,bar}})}
}
begin{document}
main{1}
main{2}
end{document}
So this version raises the error. If you comment out the directlua
it compiles fine.
What am I doing wrong? Or is there a bug somewhere?
PS: Replacing directlua
with luadirect
or luaexec
from luacode
-package or leaving out luastring
-command doesn't change anything.
luatex etoolbox luacode
I use a macro mychoice{item a, item b, items c,…}
that returns one of the items of the comma-separated-list determined by an external counter based on the docsvlist
macro from etoolbox
. In simple text, this macro works fine, but when I try to use it in a directlua{}
from Lua(La)TeX, it raises strange (at least for me) errors.
MWE:
documentclass{article}
usepackage{luacode}
usepackage{etoolbox}
begin{luacode*}
function dummy(argument)
end
end{luacode*}
newcounter{choicecounter}
newcounter{argcounter}
newcommand{mychoice}[1]{
setcounter{argcounter}{0}
renewcommand*{do}[1]{
stepcounter{argcounter}
csedef{argcounterarabic{argcounter}}{##1}}
docsvlist{#1}
csuse{argcounterarabic{choicecounter}}
}
newcommand{main}[1]{%
setcounter{choicecounter}{#1}
hello world mychoice{one,{two,three}}
directlua{dummy(luastring{mychoice{foo,bar}})}
}
begin{document}
main{1}
main{2}
end{document}
So this version raises the error. If you comment out the directlua
it compiles fine.
What am I doing wrong? Or is there a bug somewhere?
PS: Replacing directlua
with luadirect
or luaexec
from luacode
-package or leaving out luastring
-command doesn't change anything.
luatex etoolbox luacode
luatex etoolbox luacode
edited Dec 7 at 23:09
Mico
272k30369756
272k30369756
asked Dec 7 at 11:31
Toscho
3,7501118
3,7501118
2
your command is not expandable. You can't fed abitrary stuff to directlua, it must expand to something lua can handle.
– Ulrike Fischer
Dec 7 at 11:41
And how can I make it expandable? Or rather: What is preventing it from being expanded to something, lua can handle?
– Toscho
Dec 7 at 13:59
add a comment |
2
your command is not expandable. You can't fed abitrary stuff to directlua, it must expand to something lua can handle.
– Ulrike Fischer
Dec 7 at 11:41
And how can I make it expandable? Or rather: What is preventing it from being expanded to something, lua can handle?
– Toscho
Dec 7 at 13:59
2
2
your command is not expandable. You can't fed abitrary stuff to directlua, it must expand to something lua can handle.
– Ulrike Fischer
Dec 7 at 11:41
your command is not expandable. You can't fed abitrary stuff to directlua, it must expand to something lua can handle.
– Ulrike Fischer
Dec 7 at 11:41
And how can I make it expandable? Or rather: What is preventing it from being expanded to something, lua can handle?
– Toscho
Dec 7 at 13:59
And how can I make it expandable? Or rather: What is preventing it from being expanded to something, lua can handle?
– Toscho
Dec 7 at 13:59
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
You can use expl3 and clist_item:nn
to select an item in a list. This is expandable:
documentclass{article}
usepackage{luacode}
usepackage{expl3}
begin{luacode*}
function dummy(argument)
tex.sprint("lua: ".. argument)
end
end{luacode*}
ExplSyntaxOn
newcommand{main}[1]{%
hello~world~clist_item:nn{one,{two,three}}{#1}par
directlua{dummy(luastring{clist_item:nn{foo,bar}{#1}})}
}
ExplSyntaxOff
begin{document}
main{1}
main{2}
end{document}
As egreg suggested in the comments an real expl3 version would use lua_now:e instead of directlua:
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
– egreg
Dec 7 at 23:03
@egreg yes, but perhaps this looks to frightening for someone not used to expl3 ;-) (but I added it).
– Ulrike Fischer
Dec 7 at 23:07
1
That's not such a good idea.expl3
takes ages to load in LuaTeX because of the huge Unicode tables.
– Henri Menke
Dec 8 at 6:09
Comments are not for extended discussion; this conversation has been moved to chat.
– Joseph Wright♦
Dec 8 at 7:51
Thank you @UlrikeFischer . This answer is doing exactly what I want. I didn't think, thatexpl3
has so easy solutions for problems, where I only find highly complex solutions inLaTeX2e
based onkeyval
-package or definition of useless commands as in my try.
– Toscho
Dec 8 at 11:49
add a comment |
up vote
2
down vote
The most important thing you need to do is replace luastring
with luastringN
-- the N
stands for "perform NO expansion" -- in the definition of the main
macro. In addition, you need to disallow all those gratuitous whitespaces, by placing %
at various line ends inside the definitions of the main
and mychoice
macros.
For the following MWE, I borrowed the form of the dummy
function from Ulrike's answer.
documentclass{article}
usepackage{etoolbox}
usepackage{luacode} % for 'luacode' environment and 'luastringN' macro
begin{luacode}
function dummy ( argument )
tex.sprint ( "lua: " .. argument )
end
end{luacode}
newcounter{choicecounter}
newcounter{argcounter}
newcommand{mychoice}[1]{%
setcounter{argcounter}{0}%
renewcommand*{do}[1]{%
stepcounter{argcounter}%
csedef{argcounterarabic{argcounter}}{##1}}%
docsvlist{#1}%
csuse{argcounterarabic{choicecounter}}%
}
newcommand{main}[1]{%
setcounter{choicecounter}{#1}%
mychoice{one,{two,three}}
directlua{dummy(luastringN{mychoice{one,{two,three}}})}
}
begin{document}
1 --- main{1}
medskip
2 --- main{2}
end{document}
I'm pretty sure that Lua can extract an item from a comma separated list of items.
– egreg
Dec 7 at 23:40
@egreg - It sure can. My answer was meant to address the OP's opening remark, viz., "In simple text, [themychoice
] macro works fine, but when I try to use it in adirectlua{}
from Lua(La)TeX, it raises strange ... errors." The point of my answer is that all the OP really has to do is change the way themychoice
macro is called from inside thedirectlua
directive: One has to make sure thatmychoice{one,{two,three}}}
doesn't get expanded prematurely, by replacingluastring
withluastringN
. (Getting rid of the gratuitous whitespaces is nice, but not crucial.)
– Mico
Dec 7 at 23:48
Thank you, @Mico for your answer, but it doesn't fit my needs. The contents ofluastring
have to be expanded in my real problem, becausedummy
will store these contents and some later function will use these contents. By then, thechoice
-counter will have changed. I will see if storing the value of thechoice
-counter separately will help.
– Toscho
Dec 8 at 11:38
@Toscho - Ah, I was definitely quite unaware of your objectives. I was mistakenly under the impression that you simply wanted to get the existingmychoice
macro to work inside adirectlua
directive.
– Mico
Dec 8 at 15:03
@Mico And I didn't state my objective in that detail.
– Toscho
Dec 9 at 10:54
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',
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%2f463674%2fcollision-of-etoolbox-and-lualatex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
You can use expl3 and clist_item:nn
to select an item in a list. This is expandable:
documentclass{article}
usepackage{luacode}
usepackage{expl3}
begin{luacode*}
function dummy(argument)
tex.sprint("lua: ".. argument)
end
end{luacode*}
ExplSyntaxOn
newcommand{main}[1]{%
hello~world~clist_item:nn{one,{two,three}}{#1}par
directlua{dummy(luastring{clist_item:nn{foo,bar}{#1}})}
}
ExplSyntaxOff
begin{document}
main{1}
main{2}
end{document}
As egreg suggested in the comments an real expl3 version would use lua_now:e instead of directlua:
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
– egreg
Dec 7 at 23:03
@egreg yes, but perhaps this looks to frightening for someone not used to expl3 ;-) (but I added it).
– Ulrike Fischer
Dec 7 at 23:07
1
That's not such a good idea.expl3
takes ages to load in LuaTeX because of the huge Unicode tables.
– Henri Menke
Dec 8 at 6:09
Comments are not for extended discussion; this conversation has been moved to chat.
– Joseph Wright♦
Dec 8 at 7:51
Thank you @UlrikeFischer . This answer is doing exactly what I want. I didn't think, thatexpl3
has so easy solutions for problems, where I only find highly complex solutions inLaTeX2e
based onkeyval
-package or definition of useless commands as in my try.
– Toscho
Dec 8 at 11:49
add a comment |
up vote
5
down vote
accepted
You can use expl3 and clist_item:nn
to select an item in a list. This is expandable:
documentclass{article}
usepackage{luacode}
usepackage{expl3}
begin{luacode*}
function dummy(argument)
tex.sprint("lua: ".. argument)
end
end{luacode*}
ExplSyntaxOn
newcommand{main}[1]{%
hello~world~clist_item:nn{one,{two,three}}{#1}par
directlua{dummy(luastring{clist_item:nn{foo,bar}{#1}})}
}
ExplSyntaxOff
begin{document}
main{1}
main{2}
end{document}
As egreg suggested in the comments an real expl3 version would use lua_now:e instead of directlua:
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
– egreg
Dec 7 at 23:03
@egreg yes, but perhaps this looks to frightening for someone not used to expl3 ;-) (but I added it).
– Ulrike Fischer
Dec 7 at 23:07
1
That's not such a good idea.expl3
takes ages to load in LuaTeX because of the huge Unicode tables.
– Henri Menke
Dec 8 at 6:09
Comments are not for extended discussion; this conversation has been moved to chat.
– Joseph Wright♦
Dec 8 at 7:51
Thank you @UlrikeFischer . This answer is doing exactly what I want. I didn't think, thatexpl3
has so easy solutions for problems, where I only find highly complex solutions inLaTeX2e
based onkeyval
-package or definition of useless commands as in my try.
– Toscho
Dec 8 at 11:49
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
You can use expl3 and clist_item:nn
to select an item in a list. This is expandable:
documentclass{article}
usepackage{luacode}
usepackage{expl3}
begin{luacode*}
function dummy(argument)
tex.sprint("lua: ".. argument)
end
end{luacode*}
ExplSyntaxOn
newcommand{main}[1]{%
hello~world~clist_item:nn{one,{two,three}}{#1}par
directlua{dummy(luastring{clist_item:nn{foo,bar}{#1}})}
}
ExplSyntaxOff
begin{document}
main{1}
main{2}
end{document}
As egreg suggested in the comments an real expl3 version would use lua_now:e instead of directlua:
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
You can use expl3 and clist_item:nn
to select an item in a list. This is expandable:
documentclass{article}
usepackage{luacode}
usepackage{expl3}
begin{luacode*}
function dummy(argument)
tex.sprint("lua: ".. argument)
end
end{luacode*}
ExplSyntaxOn
newcommand{main}[1]{%
hello~world~clist_item:nn{one,{two,three}}{#1}par
directlua{dummy(luastring{clist_item:nn{foo,bar}{#1}})}
}
ExplSyntaxOff
begin{document}
main{1}
main{2}
end{document}
As egreg suggested in the comments an real expl3 version would use lua_now:e instead of directlua:
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
edited Dec 7 at 23:09
answered Dec 7 at 22:36
Ulrike Fischer
184k7289666
184k7289666
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
– egreg
Dec 7 at 23:03
@egreg yes, but perhaps this looks to frightening for someone not used to expl3 ;-) (but I added it).
– Ulrike Fischer
Dec 7 at 23:07
1
That's not such a good idea.expl3
takes ages to load in LuaTeX because of the huge Unicode tables.
– Henri Menke
Dec 8 at 6:09
Comments are not for extended discussion; this conversation has been moved to chat.
– Joseph Wright♦
Dec 8 at 7:51
Thank you @UlrikeFischer . This answer is doing exactly what I want. I didn't think, thatexpl3
has so easy solutions for problems, where I only find highly complex solutions inLaTeX2e
based onkeyval
-package or definition of useless commands as in my try.
– Toscho
Dec 8 at 11:49
add a comment |
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
– egreg
Dec 7 at 23:03
@egreg yes, but perhaps this looks to frightening for someone not used to expl3 ;-) (but I added it).
– Ulrike Fischer
Dec 7 at 23:07
1
That's not such a good idea.expl3
takes ages to load in LuaTeX because of the huge Unicode tables.
– Henri Menke
Dec 8 at 6:09
Comments are not for extended discussion; this conversation has been moved to chat.
– Joseph Wright♦
Dec 8 at 7:51
Thank you @UlrikeFischer . This answer is doing exactly what I want. I didn't think, thatexpl3
has so easy solutions for problems, where I only find highly complex solutions inLaTeX2e
based onkeyval
-package or definition of useless commands as in my try.
– Toscho
Dec 8 at 11:49
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
– egreg
Dec 7 at 23:03
lua_now:e {dummy("lua_escape:e{clist_item:nn{foo,bar}{#1}}")}
– egreg
Dec 7 at 23:03
@egreg yes, but perhaps this looks to frightening for someone not used to expl3 ;-) (but I added it).
– Ulrike Fischer
Dec 7 at 23:07
@egreg yes, but perhaps this looks to frightening for someone not used to expl3 ;-) (but I added it).
– Ulrike Fischer
Dec 7 at 23:07
1
1
That's not such a good idea.
expl3
takes ages to load in LuaTeX because of the huge Unicode tables.– Henri Menke
Dec 8 at 6:09
That's not such a good idea.
expl3
takes ages to load in LuaTeX because of the huge Unicode tables.– Henri Menke
Dec 8 at 6:09
Comments are not for extended discussion; this conversation has been moved to chat.
– Joseph Wright♦
Dec 8 at 7:51
Comments are not for extended discussion; this conversation has been moved to chat.
– Joseph Wright♦
Dec 8 at 7:51
Thank you @UlrikeFischer . This answer is doing exactly what I want. I didn't think, that
expl3
has so easy solutions for problems, where I only find highly complex solutions in LaTeX2e
based on keyval
-package or definition of useless commands as in my try.– Toscho
Dec 8 at 11:49
Thank you @UlrikeFischer . This answer is doing exactly what I want. I didn't think, that
expl3
has so easy solutions for problems, where I only find highly complex solutions in LaTeX2e
based on keyval
-package or definition of useless commands as in my try.– Toscho
Dec 8 at 11:49
add a comment |
up vote
2
down vote
The most important thing you need to do is replace luastring
with luastringN
-- the N
stands for "perform NO expansion" -- in the definition of the main
macro. In addition, you need to disallow all those gratuitous whitespaces, by placing %
at various line ends inside the definitions of the main
and mychoice
macros.
For the following MWE, I borrowed the form of the dummy
function from Ulrike's answer.
documentclass{article}
usepackage{etoolbox}
usepackage{luacode} % for 'luacode' environment and 'luastringN' macro
begin{luacode}
function dummy ( argument )
tex.sprint ( "lua: " .. argument )
end
end{luacode}
newcounter{choicecounter}
newcounter{argcounter}
newcommand{mychoice}[1]{%
setcounter{argcounter}{0}%
renewcommand*{do}[1]{%
stepcounter{argcounter}%
csedef{argcounterarabic{argcounter}}{##1}}%
docsvlist{#1}%
csuse{argcounterarabic{choicecounter}}%
}
newcommand{main}[1]{%
setcounter{choicecounter}{#1}%
mychoice{one,{two,three}}
directlua{dummy(luastringN{mychoice{one,{two,three}}})}
}
begin{document}
1 --- main{1}
medskip
2 --- main{2}
end{document}
I'm pretty sure that Lua can extract an item from a comma separated list of items.
– egreg
Dec 7 at 23:40
@egreg - It sure can. My answer was meant to address the OP's opening remark, viz., "In simple text, [themychoice
] macro works fine, but when I try to use it in adirectlua{}
from Lua(La)TeX, it raises strange ... errors." The point of my answer is that all the OP really has to do is change the way themychoice
macro is called from inside thedirectlua
directive: One has to make sure thatmychoice{one,{two,three}}}
doesn't get expanded prematurely, by replacingluastring
withluastringN
. (Getting rid of the gratuitous whitespaces is nice, but not crucial.)
– Mico
Dec 7 at 23:48
Thank you, @Mico for your answer, but it doesn't fit my needs. The contents ofluastring
have to be expanded in my real problem, becausedummy
will store these contents and some later function will use these contents. By then, thechoice
-counter will have changed. I will see if storing the value of thechoice
-counter separately will help.
– Toscho
Dec 8 at 11:38
@Toscho - Ah, I was definitely quite unaware of your objectives. I was mistakenly under the impression that you simply wanted to get the existingmychoice
macro to work inside adirectlua
directive.
– Mico
Dec 8 at 15:03
@Mico And I didn't state my objective in that detail.
– Toscho
Dec 9 at 10:54
add a comment |
up vote
2
down vote
The most important thing you need to do is replace luastring
with luastringN
-- the N
stands for "perform NO expansion" -- in the definition of the main
macro. In addition, you need to disallow all those gratuitous whitespaces, by placing %
at various line ends inside the definitions of the main
and mychoice
macros.
For the following MWE, I borrowed the form of the dummy
function from Ulrike's answer.
documentclass{article}
usepackage{etoolbox}
usepackage{luacode} % for 'luacode' environment and 'luastringN' macro
begin{luacode}
function dummy ( argument )
tex.sprint ( "lua: " .. argument )
end
end{luacode}
newcounter{choicecounter}
newcounter{argcounter}
newcommand{mychoice}[1]{%
setcounter{argcounter}{0}%
renewcommand*{do}[1]{%
stepcounter{argcounter}%
csedef{argcounterarabic{argcounter}}{##1}}%
docsvlist{#1}%
csuse{argcounterarabic{choicecounter}}%
}
newcommand{main}[1]{%
setcounter{choicecounter}{#1}%
mychoice{one,{two,three}}
directlua{dummy(luastringN{mychoice{one,{two,three}}})}
}
begin{document}
1 --- main{1}
medskip
2 --- main{2}
end{document}
I'm pretty sure that Lua can extract an item from a comma separated list of items.
– egreg
Dec 7 at 23:40
@egreg - It sure can. My answer was meant to address the OP's opening remark, viz., "In simple text, [themychoice
] macro works fine, but when I try to use it in adirectlua{}
from Lua(La)TeX, it raises strange ... errors." The point of my answer is that all the OP really has to do is change the way themychoice
macro is called from inside thedirectlua
directive: One has to make sure thatmychoice{one,{two,three}}}
doesn't get expanded prematurely, by replacingluastring
withluastringN
. (Getting rid of the gratuitous whitespaces is nice, but not crucial.)
– Mico
Dec 7 at 23:48
Thank you, @Mico for your answer, but it doesn't fit my needs. The contents ofluastring
have to be expanded in my real problem, becausedummy
will store these contents and some later function will use these contents. By then, thechoice
-counter will have changed. I will see if storing the value of thechoice
-counter separately will help.
– Toscho
Dec 8 at 11:38
@Toscho - Ah, I was definitely quite unaware of your objectives. I was mistakenly under the impression that you simply wanted to get the existingmychoice
macro to work inside adirectlua
directive.
– Mico
Dec 8 at 15:03
@Mico And I didn't state my objective in that detail.
– Toscho
Dec 9 at 10:54
add a comment |
up vote
2
down vote
up vote
2
down vote
The most important thing you need to do is replace luastring
with luastringN
-- the N
stands for "perform NO expansion" -- in the definition of the main
macro. In addition, you need to disallow all those gratuitous whitespaces, by placing %
at various line ends inside the definitions of the main
and mychoice
macros.
For the following MWE, I borrowed the form of the dummy
function from Ulrike's answer.
documentclass{article}
usepackage{etoolbox}
usepackage{luacode} % for 'luacode' environment and 'luastringN' macro
begin{luacode}
function dummy ( argument )
tex.sprint ( "lua: " .. argument )
end
end{luacode}
newcounter{choicecounter}
newcounter{argcounter}
newcommand{mychoice}[1]{%
setcounter{argcounter}{0}%
renewcommand*{do}[1]{%
stepcounter{argcounter}%
csedef{argcounterarabic{argcounter}}{##1}}%
docsvlist{#1}%
csuse{argcounterarabic{choicecounter}}%
}
newcommand{main}[1]{%
setcounter{choicecounter}{#1}%
mychoice{one,{two,three}}
directlua{dummy(luastringN{mychoice{one,{two,three}}})}
}
begin{document}
1 --- main{1}
medskip
2 --- main{2}
end{document}
The most important thing you need to do is replace luastring
with luastringN
-- the N
stands for "perform NO expansion" -- in the definition of the main
macro. In addition, you need to disallow all those gratuitous whitespaces, by placing %
at various line ends inside the definitions of the main
and mychoice
macros.
For the following MWE, I borrowed the form of the dummy
function from Ulrike's answer.
documentclass{article}
usepackage{etoolbox}
usepackage{luacode} % for 'luacode' environment and 'luastringN' macro
begin{luacode}
function dummy ( argument )
tex.sprint ( "lua: " .. argument )
end
end{luacode}
newcounter{choicecounter}
newcounter{argcounter}
newcommand{mychoice}[1]{%
setcounter{argcounter}{0}%
renewcommand*{do}[1]{%
stepcounter{argcounter}%
csedef{argcounterarabic{argcounter}}{##1}}%
docsvlist{#1}%
csuse{argcounterarabic{choicecounter}}%
}
newcommand{main}[1]{%
setcounter{choicecounter}{#1}%
mychoice{one,{two,three}}
directlua{dummy(luastringN{mychoice{one,{two,three}}})}
}
begin{document}
1 --- main{1}
medskip
2 --- main{2}
end{document}
answered Dec 7 at 23:16
Mico
272k30369756
272k30369756
I'm pretty sure that Lua can extract an item from a comma separated list of items.
– egreg
Dec 7 at 23:40
@egreg - It sure can. My answer was meant to address the OP's opening remark, viz., "In simple text, [themychoice
] macro works fine, but when I try to use it in adirectlua{}
from Lua(La)TeX, it raises strange ... errors." The point of my answer is that all the OP really has to do is change the way themychoice
macro is called from inside thedirectlua
directive: One has to make sure thatmychoice{one,{two,three}}}
doesn't get expanded prematurely, by replacingluastring
withluastringN
. (Getting rid of the gratuitous whitespaces is nice, but not crucial.)
– Mico
Dec 7 at 23:48
Thank you, @Mico for your answer, but it doesn't fit my needs. The contents ofluastring
have to be expanded in my real problem, becausedummy
will store these contents and some later function will use these contents. By then, thechoice
-counter will have changed. I will see if storing the value of thechoice
-counter separately will help.
– Toscho
Dec 8 at 11:38
@Toscho - Ah, I was definitely quite unaware of your objectives. I was mistakenly under the impression that you simply wanted to get the existingmychoice
macro to work inside adirectlua
directive.
– Mico
Dec 8 at 15:03
@Mico And I didn't state my objective in that detail.
– Toscho
Dec 9 at 10:54
add a comment |
I'm pretty sure that Lua can extract an item from a comma separated list of items.
– egreg
Dec 7 at 23:40
@egreg - It sure can. My answer was meant to address the OP's opening remark, viz., "In simple text, [themychoice
] macro works fine, but when I try to use it in adirectlua{}
from Lua(La)TeX, it raises strange ... errors." The point of my answer is that all the OP really has to do is change the way themychoice
macro is called from inside thedirectlua
directive: One has to make sure thatmychoice{one,{two,three}}}
doesn't get expanded prematurely, by replacingluastring
withluastringN
. (Getting rid of the gratuitous whitespaces is nice, but not crucial.)
– Mico
Dec 7 at 23:48
Thank you, @Mico for your answer, but it doesn't fit my needs. The contents ofluastring
have to be expanded in my real problem, becausedummy
will store these contents and some later function will use these contents. By then, thechoice
-counter will have changed. I will see if storing the value of thechoice
-counter separately will help.
– Toscho
Dec 8 at 11:38
@Toscho - Ah, I was definitely quite unaware of your objectives. I was mistakenly under the impression that you simply wanted to get the existingmychoice
macro to work inside adirectlua
directive.
– Mico
Dec 8 at 15:03
@Mico And I didn't state my objective in that detail.
– Toscho
Dec 9 at 10:54
I'm pretty sure that Lua can extract an item from a comma separated list of items.
– egreg
Dec 7 at 23:40
I'm pretty sure that Lua can extract an item from a comma separated list of items.
– egreg
Dec 7 at 23:40
@egreg - It sure can. My answer was meant to address the OP's opening remark, viz., "In simple text, [the
mychoice
] macro works fine, but when I try to use it in a directlua{}
from Lua(La)TeX, it raises strange ... errors." The point of my answer is that all the OP really has to do is change the way the mychoice
macro is called from inside the directlua
directive: One has to make sure that mychoice{one,{two,three}}}
doesn't get expanded prematurely, by replacing luastring
with luastringN
. (Getting rid of the gratuitous whitespaces is nice, but not crucial.)– Mico
Dec 7 at 23:48
@egreg - It sure can. My answer was meant to address the OP's opening remark, viz., "In simple text, [the
mychoice
] macro works fine, but when I try to use it in a directlua{}
from Lua(La)TeX, it raises strange ... errors." The point of my answer is that all the OP really has to do is change the way the mychoice
macro is called from inside the directlua
directive: One has to make sure that mychoice{one,{two,three}}}
doesn't get expanded prematurely, by replacing luastring
with luastringN
. (Getting rid of the gratuitous whitespaces is nice, but not crucial.)– Mico
Dec 7 at 23:48
Thank you, @Mico for your answer, but it doesn't fit my needs. The contents of
luastring
have to be expanded in my real problem, because dummy
will store these contents and some later function will use these contents. By then, the choice
-counter will have changed. I will see if storing the value of the choice
-counter separately will help.– Toscho
Dec 8 at 11:38
Thank you, @Mico for your answer, but it doesn't fit my needs. The contents of
luastring
have to be expanded in my real problem, because dummy
will store these contents and some later function will use these contents. By then, the choice
-counter will have changed. I will see if storing the value of the choice
-counter separately will help.– Toscho
Dec 8 at 11:38
@Toscho - Ah, I was definitely quite unaware of your objectives. I was mistakenly under the impression that you simply wanted to get the existing
mychoice
macro to work inside a directlua
directive.– Mico
Dec 8 at 15:03
@Toscho - Ah, I was definitely quite unaware of your objectives. I was mistakenly under the impression that you simply wanted to get the existing
mychoice
macro to work inside a directlua
directive.– Mico
Dec 8 at 15:03
@Mico And I didn't state my objective in that detail.
– Toscho
Dec 9 at 10:54
@Mico And I didn't state my objective in that detail.
– Toscho
Dec 9 at 10:54
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%2f463674%2fcollision-of-etoolbox-and-lualatex%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
2
your command is not expandable. You can't fed abitrary stuff to directlua, it must expand to something lua can handle.
– Ulrike Fischer
Dec 7 at 11:41
And how can I make it expandable? Or rather: What is preventing it from being expanded to something, lua can handle?
– Toscho
Dec 7 at 13:59