String variable with multiple values
I'm after a definition of a command that could have multiple values to use in different contexts. The use case for this would be in a custom class that uses two languages simultaneously, or I guess any situation where a variable could have different values depending on the environment. After searching information on newcommand and things related to it, I'm still a bit puzzled about things.
As I understand it, a simple implementation would be:
newcommandvarname{text value}
However, having different commands for same things gets ugly in my eye. For example, I have in front of me an old class with commands like Doctype and Doctypefin for the Finnish version. Instead, I'd like to use the command like this:
% Definitions
title{en}{Title Of The Document}
title{fi}{Dokumentin otsikko}
% Use in class environments or tex files
title{en} % -> "Title Of The Document"
title{fi} % -> "Dokumentin otsikko"
Is there any way to construct such a macro or definition?
Ideally, specifying unseen categories (here languages) would not be a problem, but I can see it would be reasonable to require specifying the accepted values in the class file. Also, like in the example below, I think it would be acceptable to have a separate command for outputting the variable, if that's a problem.
I've tried to take note of the way other class commands are constructed. There's a handy MakeStringVar command that constructs a variable, and if not set, displays a default text. That default text functionality would be very useful for the new command, because loads of the environments in the class use the definitions to output text to title pages etc.
newcommandMakeStringVar[2][relax]{%
ifx#1relax%
expandafternewcommandcsname Emit#2endcsname{%
{scriptsize (Use {tttextbackslash #2} to replace this text.)}}%
else%
expandafternewcommandcsname Emit#2endcsname{#1}%
fi%
expandafternewcommandcsname #2endcsname[1]{%
expandafterrenewcommandcsname Emit#2endcsname{##1}%
}%
}
It is used like so:
MakeStringVar{Major} % Definition in class
Major{Major subject name} % Set value in pre-document
EmitMajor % Used in environments in class -> "Major subject name"
But I'm a newbie when it comes to LaTeX, so I'm not sure where to even begin. I have a feeling the above command could be expanded in some manner, but I really don't know the limitations of LaTeX macros.
macros variable
New contributor
Felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I'm after a definition of a command that could have multiple values to use in different contexts. The use case for this would be in a custom class that uses two languages simultaneously, or I guess any situation where a variable could have different values depending on the environment. After searching information on newcommand and things related to it, I'm still a bit puzzled about things.
As I understand it, a simple implementation would be:
newcommandvarname{text value}
However, having different commands for same things gets ugly in my eye. For example, I have in front of me an old class with commands like Doctype and Doctypefin for the Finnish version. Instead, I'd like to use the command like this:
% Definitions
title{en}{Title Of The Document}
title{fi}{Dokumentin otsikko}
% Use in class environments or tex files
title{en} % -> "Title Of The Document"
title{fi} % -> "Dokumentin otsikko"
Is there any way to construct such a macro or definition?
Ideally, specifying unseen categories (here languages) would not be a problem, but I can see it would be reasonable to require specifying the accepted values in the class file. Also, like in the example below, I think it would be acceptable to have a separate command for outputting the variable, if that's a problem.
I've tried to take note of the way other class commands are constructed. There's a handy MakeStringVar command that constructs a variable, and if not set, displays a default text. That default text functionality would be very useful for the new command, because loads of the environments in the class use the definitions to output text to title pages etc.
newcommandMakeStringVar[2][relax]{%
ifx#1relax%
expandafternewcommandcsname Emit#2endcsname{%
{scriptsize (Use {tttextbackslash #2} to replace this text.)}}%
else%
expandafternewcommandcsname Emit#2endcsname{#1}%
fi%
expandafternewcommandcsname #2endcsname[1]{%
expandafterrenewcommandcsname Emit#2endcsname{##1}%
}%
}
It is used like so:
MakeStringVar{Major} % Definition in class
Major{Major subject name} % Set value in pre-document
EmitMajor % Used in environments in class -> "Major subject name"
But I'm a newbie when it comes to LaTeX, so I'm not sure where to even begin. I have a feeling the above command could be expanded in some manner, but I really don't know the limitations of LaTeX macros.
macros variable
New contributor
Felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Have you seen this question? It addresses a method for parsing a simple string for matches.
– WesH
6 hours ago
@WesH Thanks, for the link. I'll investigate. Seems to apply quite well.
– Felix
6 hours ago
So you're comfortable with using different command for the different contexts? As in,title[en]{<English title>}andtitle[fi]{<Finnish title>}(say) together withthetitle[en](for<English title>) and/orthetitle[fi](for<Finnish title>). Note the use oftitlefor defining the title andthetitleto set the title in the document. Also, the language choice is presented as an optional argument, where you can specify some default (likeenfor English, say).
– Werner
6 hours ago
@Werner I am aware, that that's basically what's already happening :D but yes, I would. Even to have the argument. Call me crazy. And yeah, a default argument for the language would be a good addition!
– Felix
6 hours ago
add a comment |
I'm after a definition of a command that could have multiple values to use in different contexts. The use case for this would be in a custom class that uses two languages simultaneously, or I guess any situation where a variable could have different values depending on the environment. After searching information on newcommand and things related to it, I'm still a bit puzzled about things.
As I understand it, a simple implementation would be:
newcommandvarname{text value}
However, having different commands for same things gets ugly in my eye. For example, I have in front of me an old class with commands like Doctype and Doctypefin for the Finnish version. Instead, I'd like to use the command like this:
% Definitions
title{en}{Title Of The Document}
title{fi}{Dokumentin otsikko}
% Use in class environments or tex files
title{en} % -> "Title Of The Document"
title{fi} % -> "Dokumentin otsikko"
Is there any way to construct such a macro or definition?
Ideally, specifying unseen categories (here languages) would not be a problem, but I can see it would be reasonable to require specifying the accepted values in the class file. Also, like in the example below, I think it would be acceptable to have a separate command for outputting the variable, if that's a problem.
I've tried to take note of the way other class commands are constructed. There's a handy MakeStringVar command that constructs a variable, and if not set, displays a default text. That default text functionality would be very useful for the new command, because loads of the environments in the class use the definitions to output text to title pages etc.
newcommandMakeStringVar[2][relax]{%
ifx#1relax%
expandafternewcommandcsname Emit#2endcsname{%
{scriptsize (Use {tttextbackslash #2} to replace this text.)}}%
else%
expandafternewcommandcsname Emit#2endcsname{#1}%
fi%
expandafternewcommandcsname #2endcsname[1]{%
expandafterrenewcommandcsname Emit#2endcsname{##1}%
}%
}
It is used like so:
MakeStringVar{Major} % Definition in class
Major{Major subject name} % Set value in pre-document
EmitMajor % Used in environments in class -> "Major subject name"
But I'm a newbie when it comes to LaTeX, so I'm not sure where to even begin. I have a feeling the above command could be expanded in some manner, but I really don't know the limitations of LaTeX macros.
macros variable
New contributor
Felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'm after a definition of a command that could have multiple values to use in different contexts. The use case for this would be in a custom class that uses two languages simultaneously, or I guess any situation where a variable could have different values depending on the environment. After searching information on newcommand and things related to it, I'm still a bit puzzled about things.
As I understand it, a simple implementation would be:
newcommandvarname{text value}
However, having different commands for same things gets ugly in my eye. For example, I have in front of me an old class with commands like Doctype and Doctypefin for the Finnish version. Instead, I'd like to use the command like this:
% Definitions
title{en}{Title Of The Document}
title{fi}{Dokumentin otsikko}
% Use in class environments or tex files
title{en} % -> "Title Of The Document"
title{fi} % -> "Dokumentin otsikko"
Is there any way to construct such a macro or definition?
Ideally, specifying unseen categories (here languages) would not be a problem, but I can see it would be reasonable to require specifying the accepted values in the class file. Also, like in the example below, I think it would be acceptable to have a separate command for outputting the variable, if that's a problem.
I've tried to take note of the way other class commands are constructed. There's a handy MakeStringVar command that constructs a variable, and if not set, displays a default text. That default text functionality would be very useful for the new command, because loads of the environments in the class use the definitions to output text to title pages etc.
newcommandMakeStringVar[2][relax]{%
ifx#1relax%
expandafternewcommandcsname Emit#2endcsname{%
{scriptsize (Use {tttextbackslash #2} to replace this text.)}}%
else%
expandafternewcommandcsname Emit#2endcsname{#1}%
fi%
expandafternewcommandcsname #2endcsname[1]{%
expandafterrenewcommandcsname Emit#2endcsname{##1}%
}%
}
It is used like so:
MakeStringVar{Major} % Definition in class
Major{Major subject name} % Set value in pre-document
EmitMajor % Used in environments in class -> "Major subject name"
But I'm a newbie when it comes to LaTeX, so I'm not sure where to even begin. I have a feeling the above command could be expanded in some manner, but I really don't know the limitations of LaTeX macros.
macros variable
macros variable
New contributor
Felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 6 hours ago
Felix
New contributor
Felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 6 hours ago
FelixFelix
1175
1175
New contributor
Felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Felix is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Have you seen this question? It addresses a method for parsing a simple string for matches.
– WesH
6 hours ago
@WesH Thanks, for the link. I'll investigate. Seems to apply quite well.
– Felix
6 hours ago
So you're comfortable with using different command for the different contexts? As in,title[en]{<English title>}andtitle[fi]{<Finnish title>}(say) together withthetitle[en](for<English title>) and/orthetitle[fi](for<Finnish title>). Note the use oftitlefor defining the title andthetitleto set the title in the document. Also, the language choice is presented as an optional argument, where you can specify some default (likeenfor English, say).
– Werner
6 hours ago
@Werner I am aware, that that's basically what's already happening :D but yes, I would. Even to have the argument. Call me crazy. And yeah, a default argument for the language would be a good addition!
– Felix
6 hours ago
add a comment |
Have you seen this question? It addresses a method for parsing a simple string for matches.
– WesH
6 hours ago
@WesH Thanks, for the link. I'll investigate. Seems to apply quite well.
– Felix
6 hours ago
So you're comfortable with using different command for the different contexts? As in,title[en]{<English title>}andtitle[fi]{<Finnish title>}(say) together withthetitle[en](for<English title>) and/orthetitle[fi](for<Finnish title>). Note the use oftitlefor defining the title andthetitleto set the title in the document. Also, the language choice is presented as an optional argument, where you can specify some default (likeenfor English, say).
– Werner
6 hours ago
@Werner I am aware, that that's basically what's already happening :D but yes, I would. Even to have the argument. Call me crazy. And yeah, a default argument for the language would be a good addition!
– Felix
6 hours ago
Have you seen this question? It addresses a method for parsing a simple string for matches.
– WesH
6 hours ago
Have you seen this question? It addresses a method for parsing a simple string for matches.
– WesH
6 hours ago
@WesH Thanks, for the link. I'll investigate. Seems to apply quite well.
– Felix
6 hours ago
@WesH Thanks, for the link. I'll investigate. Seems to apply quite well.
– Felix
6 hours ago
So you're comfortable with using different command for the different contexts? As in,
title[en]{<English title>} and title[fi]{<Finnish title>} (say) together with thetitle[en] (for <English title>) and/or thetitle[fi] (for <Finnish title>). Note the use of title for defining the title and thetitle to set the title in the document. Also, the language choice is presented as an optional argument, where you can specify some default (like en for English, say).– Werner
6 hours ago
So you're comfortable with using different command for the different contexts? As in,
title[en]{<English title>} and title[fi]{<Finnish title>} (say) together with thetitle[en] (for <English title>) and/or thetitle[fi] (for <Finnish title>). Note the use of title for defining the title and thetitle to set the title in the document. Also, the language choice is presented as an optional argument, where you can specify some default (like en for English, say).– Werner
6 hours ago
@Werner I am aware, that that's basically what's already happening :D but yes, I would. Even to have the argument. Call me crazy. And yeah, a default argument for the language would be a good addition!
– Felix
6 hours ago
@Werner I am aware, that that's basically what's already happening :D but yes, I would. Even to have the argument. Call me crazy. And yeah, a default argument for the language would be a good addition!
– Felix
6 hours ago
add a comment |
2 Answers
2
active
oldest
votes
You may consider this approach which uses @namedef:
documentclass{article}
makeatletter
newcommanddeftitle[2][en]{%
global@namedef{title:#1}{#2}%
}
newcommandusetitle[1][en]{@nameuse{title:#1}}
makeatother
begin{document}
deftitle{Default Language (English) Title} % same as deftitle[en]{...}
deftitle[it]{Italian Title}
deftitle[fr]{French Title}
usetitle[it]
usetitle % same as usetitle[en]
usetitle[fr]
end{document}
When the user calls deftitle[en]{<content>}, a new macro title:en is defined and it expands to <content> when called via @nameuse.
EDIT: Here there's a general way to construct such macros:
makeatletter
newcommandnewconstructor[1]{%
expandafternewcommandcsname def#1endcsname[2][en]{%
global@namedef{#1:##1}{##2}%
}%
expandafternewcommandcsname use#1endcsname[1][en]{@nameuse{#1:##1}}%
}
makeatother
Now, for example, newconstructor{title} defines deftitle and usetitle, like before.
Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?
– Felix
4 hours ago
@Felix I've added a general constructor in my answer.
– zetaeffe
4 hours ago
Man. You've made my next few months a lot easier <3
– Felix
4 hours ago
add a comment |
In this implementation the various versions are input with a handy key-value interface (brace the value if it is to contain a comma).
Also aliases for keys can be defined. For languages, I think it's preferable to use long keys with the full language name, so languagename can be used to get the related string. However, aliases for keys can also be used in the document, provided they're defined beforehand.
You're not compelled to add all versions at the time of a variable definition as you can use addtovarstring later.
documentclass{article}
usepackage[english,finnish]{babel}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{definevarstring}{mO{}}
{
prop_new:c { g_felix_varstring_#1_prop }
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{addtovarstring}{mm}
{
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{definealias}{m}
{
prop_gset_from_keyval:Nn g_felix_varstring_alias_prop { #1 }
}
NewExpandableDocumentCommand{getvarstring}{mm}
{
prop_if_in:cfTF { g_felix_varstring_#1_prop } { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop } { #2 }
}
{
prop_if_in:NnT g_felix_varstring_alias_prop { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop }
{
prop_item:Nn g_felix_varstring_alias_prop { #2 }
}
}
}
}
cs_generate_variant:Nn prop_item:Nn { cf }
prg_generate_conditional_variant:Nnn prop_if_in:Nn { cf } { T,F,TF,p }
prop_new:N g_felix_varstring_alias_prop
cs_new_protected:Nn felix_varstring_add:nn
{
prop_gset_from_keyval:cn { g_felix_varstring_#1_prop } { #2 }
}
ExplSyntaxOff
definealias{fi=finnish,en=english}
definevarstring{title}[% long versions for languages
english=Title of the document,
finnish=Dokumentin otsikko,
]
begin{document}
author{A. Uthor}
title{getvarstring{title}{languagename}}
maketitle
selectlanguage{english}
getvarstring{title}{languagename}
getvarstring{title}{en}---getvarstring{title}{english}
getvarstring{title}{fi}---getvarstring{title}{finnish}
end{document}

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
});
}
});
Felix 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%2f476999%2fstring-variable-with-multiple-values%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
You may consider this approach which uses @namedef:
documentclass{article}
makeatletter
newcommanddeftitle[2][en]{%
global@namedef{title:#1}{#2}%
}
newcommandusetitle[1][en]{@nameuse{title:#1}}
makeatother
begin{document}
deftitle{Default Language (English) Title} % same as deftitle[en]{...}
deftitle[it]{Italian Title}
deftitle[fr]{French Title}
usetitle[it]
usetitle % same as usetitle[en]
usetitle[fr]
end{document}
When the user calls deftitle[en]{<content>}, a new macro title:en is defined and it expands to <content> when called via @nameuse.
EDIT: Here there's a general way to construct such macros:
makeatletter
newcommandnewconstructor[1]{%
expandafternewcommandcsname def#1endcsname[2][en]{%
global@namedef{#1:##1}{##2}%
}%
expandafternewcommandcsname use#1endcsname[1][en]{@nameuse{#1:##1}}%
}
makeatother
Now, for example, newconstructor{title} defines deftitle and usetitle, like before.
Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?
– Felix
4 hours ago
@Felix I've added a general constructor in my answer.
– zetaeffe
4 hours ago
Man. You've made my next few months a lot easier <3
– Felix
4 hours ago
add a comment |
You may consider this approach which uses @namedef:
documentclass{article}
makeatletter
newcommanddeftitle[2][en]{%
global@namedef{title:#1}{#2}%
}
newcommandusetitle[1][en]{@nameuse{title:#1}}
makeatother
begin{document}
deftitle{Default Language (English) Title} % same as deftitle[en]{...}
deftitle[it]{Italian Title}
deftitle[fr]{French Title}
usetitle[it]
usetitle % same as usetitle[en]
usetitle[fr]
end{document}
When the user calls deftitle[en]{<content>}, a new macro title:en is defined and it expands to <content> when called via @nameuse.
EDIT: Here there's a general way to construct such macros:
makeatletter
newcommandnewconstructor[1]{%
expandafternewcommandcsname def#1endcsname[2][en]{%
global@namedef{#1:##1}{##2}%
}%
expandafternewcommandcsname use#1endcsname[1][en]{@nameuse{#1:##1}}%
}
makeatother
Now, for example, newconstructor{title} defines deftitle and usetitle, like before.
Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?
– Felix
4 hours ago
@Felix I've added a general constructor in my answer.
– zetaeffe
4 hours ago
Man. You've made my next few months a lot easier <3
– Felix
4 hours ago
add a comment |
You may consider this approach which uses @namedef:
documentclass{article}
makeatletter
newcommanddeftitle[2][en]{%
global@namedef{title:#1}{#2}%
}
newcommandusetitle[1][en]{@nameuse{title:#1}}
makeatother
begin{document}
deftitle{Default Language (English) Title} % same as deftitle[en]{...}
deftitle[it]{Italian Title}
deftitle[fr]{French Title}
usetitle[it]
usetitle % same as usetitle[en]
usetitle[fr]
end{document}
When the user calls deftitle[en]{<content>}, a new macro title:en is defined and it expands to <content> when called via @nameuse.
EDIT: Here there's a general way to construct such macros:
makeatletter
newcommandnewconstructor[1]{%
expandafternewcommandcsname def#1endcsname[2][en]{%
global@namedef{#1:##1}{##2}%
}%
expandafternewcommandcsname use#1endcsname[1][en]{@nameuse{#1:##1}}%
}
makeatother
Now, for example, newconstructor{title} defines deftitle and usetitle, like before.
You may consider this approach which uses @namedef:
documentclass{article}
makeatletter
newcommanddeftitle[2][en]{%
global@namedef{title:#1}{#2}%
}
newcommandusetitle[1][en]{@nameuse{title:#1}}
makeatother
begin{document}
deftitle{Default Language (English) Title} % same as deftitle[en]{...}
deftitle[it]{Italian Title}
deftitle[fr]{French Title}
usetitle[it]
usetitle % same as usetitle[en]
usetitle[fr]
end{document}
When the user calls deftitle[en]{<content>}, a new macro title:en is defined and it expands to <content> when called via @nameuse.
EDIT: Here there's a general way to construct such macros:
makeatletter
newcommandnewconstructor[1]{%
expandafternewcommandcsname def#1endcsname[2][en]{%
global@namedef{#1:##1}{##2}%
}%
expandafternewcommandcsname use#1endcsname[1][en]{@nameuse{#1:##1}}%
}
makeatother
Now, for example, newconstructor{title} defines deftitle and usetitle, like before.
edited 2 hours ago
Felix
1175
1175
answered 6 hours ago
zetaeffezetaeffe
5467
5467
Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?
– Felix
4 hours ago
@Felix I've added a general constructor in my answer.
– zetaeffe
4 hours ago
Man. You've made my next few months a lot easier <3
– Felix
4 hours ago
add a comment |
Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?
– Felix
4 hours ago
@Felix I've added a general constructor in my answer.
– zetaeffe
4 hours ago
Man. You've made my next few months a lot easier <3
– Felix
4 hours ago
Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?
– Felix
4 hours ago
Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?
– Felix
4 hours ago
@Felix I've added a general constructor in my answer.
– zetaeffe
4 hours ago
@Felix I've added a general constructor in my answer.
– zetaeffe
4 hours ago
Man. You've made my next few months a lot easier <3
– Felix
4 hours ago
Man. You've made my next few months a lot easier <3
– Felix
4 hours ago
add a comment |
In this implementation the various versions are input with a handy key-value interface (brace the value if it is to contain a comma).
Also aliases for keys can be defined. For languages, I think it's preferable to use long keys with the full language name, so languagename can be used to get the related string. However, aliases for keys can also be used in the document, provided they're defined beforehand.
You're not compelled to add all versions at the time of a variable definition as you can use addtovarstring later.
documentclass{article}
usepackage[english,finnish]{babel}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{definevarstring}{mO{}}
{
prop_new:c { g_felix_varstring_#1_prop }
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{addtovarstring}{mm}
{
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{definealias}{m}
{
prop_gset_from_keyval:Nn g_felix_varstring_alias_prop { #1 }
}
NewExpandableDocumentCommand{getvarstring}{mm}
{
prop_if_in:cfTF { g_felix_varstring_#1_prop } { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop } { #2 }
}
{
prop_if_in:NnT g_felix_varstring_alias_prop { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop }
{
prop_item:Nn g_felix_varstring_alias_prop { #2 }
}
}
}
}
cs_generate_variant:Nn prop_item:Nn { cf }
prg_generate_conditional_variant:Nnn prop_if_in:Nn { cf } { T,F,TF,p }
prop_new:N g_felix_varstring_alias_prop
cs_new_protected:Nn felix_varstring_add:nn
{
prop_gset_from_keyval:cn { g_felix_varstring_#1_prop } { #2 }
}
ExplSyntaxOff
definealias{fi=finnish,en=english}
definevarstring{title}[% long versions for languages
english=Title of the document,
finnish=Dokumentin otsikko,
]
begin{document}
author{A. Uthor}
title{getvarstring{title}{languagename}}
maketitle
selectlanguage{english}
getvarstring{title}{languagename}
getvarstring{title}{en}---getvarstring{title}{english}
getvarstring{title}{fi}---getvarstring{title}{finnish}
end{document}

add a comment |
In this implementation the various versions are input with a handy key-value interface (brace the value if it is to contain a comma).
Also aliases for keys can be defined. For languages, I think it's preferable to use long keys with the full language name, so languagename can be used to get the related string. However, aliases for keys can also be used in the document, provided they're defined beforehand.
You're not compelled to add all versions at the time of a variable definition as you can use addtovarstring later.
documentclass{article}
usepackage[english,finnish]{babel}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{definevarstring}{mO{}}
{
prop_new:c { g_felix_varstring_#1_prop }
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{addtovarstring}{mm}
{
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{definealias}{m}
{
prop_gset_from_keyval:Nn g_felix_varstring_alias_prop { #1 }
}
NewExpandableDocumentCommand{getvarstring}{mm}
{
prop_if_in:cfTF { g_felix_varstring_#1_prop } { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop } { #2 }
}
{
prop_if_in:NnT g_felix_varstring_alias_prop { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop }
{
prop_item:Nn g_felix_varstring_alias_prop { #2 }
}
}
}
}
cs_generate_variant:Nn prop_item:Nn { cf }
prg_generate_conditional_variant:Nnn prop_if_in:Nn { cf } { T,F,TF,p }
prop_new:N g_felix_varstring_alias_prop
cs_new_protected:Nn felix_varstring_add:nn
{
prop_gset_from_keyval:cn { g_felix_varstring_#1_prop } { #2 }
}
ExplSyntaxOff
definealias{fi=finnish,en=english}
definevarstring{title}[% long versions for languages
english=Title of the document,
finnish=Dokumentin otsikko,
]
begin{document}
author{A. Uthor}
title{getvarstring{title}{languagename}}
maketitle
selectlanguage{english}
getvarstring{title}{languagename}
getvarstring{title}{en}---getvarstring{title}{english}
getvarstring{title}{fi}---getvarstring{title}{finnish}
end{document}

add a comment |
In this implementation the various versions are input with a handy key-value interface (brace the value if it is to contain a comma).
Also aliases for keys can be defined. For languages, I think it's preferable to use long keys with the full language name, so languagename can be used to get the related string. However, aliases for keys can also be used in the document, provided they're defined beforehand.
You're not compelled to add all versions at the time of a variable definition as you can use addtovarstring later.
documentclass{article}
usepackage[english,finnish]{babel}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{definevarstring}{mO{}}
{
prop_new:c { g_felix_varstring_#1_prop }
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{addtovarstring}{mm}
{
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{definealias}{m}
{
prop_gset_from_keyval:Nn g_felix_varstring_alias_prop { #1 }
}
NewExpandableDocumentCommand{getvarstring}{mm}
{
prop_if_in:cfTF { g_felix_varstring_#1_prop } { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop } { #2 }
}
{
prop_if_in:NnT g_felix_varstring_alias_prop { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop }
{
prop_item:Nn g_felix_varstring_alias_prop { #2 }
}
}
}
}
cs_generate_variant:Nn prop_item:Nn { cf }
prg_generate_conditional_variant:Nnn prop_if_in:Nn { cf } { T,F,TF,p }
prop_new:N g_felix_varstring_alias_prop
cs_new_protected:Nn felix_varstring_add:nn
{
prop_gset_from_keyval:cn { g_felix_varstring_#1_prop } { #2 }
}
ExplSyntaxOff
definealias{fi=finnish,en=english}
definevarstring{title}[% long versions for languages
english=Title of the document,
finnish=Dokumentin otsikko,
]
begin{document}
author{A. Uthor}
title{getvarstring{title}{languagename}}
maketitle
selectlanguage{english}
getvarstring{title}{languagename}
getvarstring{title}{en}---getvarstring{title}{english}
getvarstring{title}{fi}---getvarstring{title}{finnish}
end{document}

In this implementation the various versions are input with a handy key-value interface (brace the value if it is to contain a comma).
Also aliases for keys can be defined. For languages, I think it's preferable to use long keys with the full language name, so languagename can be used to get the related string. However, aliases for keys can also be used in the document, provided they're defined beforehand.
You're not compelled to add all versions at the time of a variable definition as you can use addtovarstring later.
documentclass{article}
usepackage[english,finnish]{babel}
usepackage{xparse}
ExplSyntaxOn
NewDocumentCommand{definevarstring}{mO{}}
{
prop_new:c { g_felix_varstring_#1_prop }
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{addtovarstring}{mm}
{
felix_varstring_add:nn { #1 } { #2 }
}
NewDocumentCommand{definealias}{m}
{
prop_gset_from_keyval:Nn g_felix_varstring_alias_prop { #1 }
}
NewExpandableDocumentCommand{getvarstring}{mm}
{
prop_if_in:cfTF { g_felix_varstring_#1_prop } { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop } { #2 }
}
{
prop_if_in:NnT g_felix_varstring_alias_prop { #2 }
{
prop_item:cf { g_felix_varstring_#1_prop }
{
prop_item:Nn g_felix_varstring_alias_prop { #2 }
}
}
}
}
cs_generate_variant:Nn prop_item:Nn { cf }
prg_generate_conditional_variant:Nnn prop_if_in:Nn { cf } { T,F,TF,p }
prop_new:N g_felix_varstring_alias_prop
cs_new_protected:Nn felix_varstring_add:nn
{
prop_gset_from_keyval:cn { g_felix_varstring_#1_prop } { #2 }
}
ExplSyntaxOff
definealias{fi=finnish,en=english}
definevarstring{title}[% long versions for languages
english=Title of the document,
finnish=Dokumentin otsikko,
]
begin{document}
author{A. Uthor}
title{getvarstring{title}{languagename}}
maketitle
selectlanguage{english}
getvarstring{title}{languagename}
getvarstring{title}{en}---getvarstring{title}{english}
getvarstring{title}{fi}---getvarstring{title}{finnish}
end{document}

answered 3 hours ago
egregegreg
723k8719163219
723k8719163219
add a comment |
add a comment |
Felix is a new contributor. Be nice, and check out our Code of Conduct.
Felix is a new contributor. Be nice, and check out our Code of Conduct.
Felix is a new contributor. Be nice, and check out our Code of Conduct.
Felix 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%2f476999%2fstring-variable-with-multiple-values%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
Have you seen this question? It addresses a method for parsing a simple string for matches.
– WesH
6 hours ago
@WesH Thanks, for the link. I'll investigate. Seems to apply quite well.
– Felix
6 hours ago
So you're comfortable with using different command for the different contexts? As in,
title[en]{<English title>}andtitle[fi]{<Finnish title>}(say) together withthetitle[en](for<English title>) and/orthetitle[fi](for<Finnish title>). Note the use oftitlefor defining the title andthetitleto set the title in the document. Also, the language choice is presented as an optional argument, where you can specify some default (likeenfor English, say).– Werner
6 hours ago
@Werner I am aware, that that's basically what's already happening :D but yes, I would. Even to have the argument. Call me crazy. And yeah, a default argument for the language would be a good addition!
– Felix
6 hours ago