String variable with multiple valuesifthenelse equal string comparison failsVariable-name newcommand with...

Is using an 'empty' metaphor considered bad style?

What are "industrial chops"?

SET NOCOUNT Error in handling SQL call after upgrade

Can a Spectator be a bodyguard? So, can its treasure/item to guard be a person/person's item?

Play Zip, Zap, Zop

Dilemma of explaining to interviewer that he is the reason for declining second interview

Why did the villain in the first Men in Black movie care about Earth's Cockroaches?

Mathematics and the art of linearizing the circle

Why is working on the same position for more than 15 years not a red flag?

What is the most fuel efficient way out of the Solar System?

Numbers with a minus sign in a matrix not aligned with the numbers wihtout minus sign

Why did Luke use his left hand to shoot?

Cat is tipping over bed-side lamps during the night

Why do cars have plastic shrouds over the engine?

How should I handle players who ignore the session zero agreement?

Why would space fleets be aligned?

Has any human ever had the choice to leave Earth permanently?

How to play electric guitar and bass as a duet

Early credit roll before the end of the film

How to deal with an incendiary email that was recalled

Why am I able to open Wireshark in macOS without root privileges?

Why wasn't TEventArgs made contravariant in the standard event pattern in the .NET ecosystem?

Absorbing damage with Planeswalker

Is there any risk in sharing info about technologies and products we use with a supplier?



String variable with multiple values


ifthenelse equal string comparison failsVariable-name newcommand with parameters within another newcommandDefine listings language using variableRegarding variable names and values of variables inside macrosWhat exactly is a command in Latex?How to define a newcommand that expands into another newcommandDefine a new command with parameters inside newcommandDefine macro for sequence/list/tuple macrosNewcommand with PythonTexDefine variable without newcommandSuggested method of creating an end user editable variable with newcommand













2















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.










share|improve this question









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
    4 hours ago











  • @WesH Thanks, for the link. I'll investigate. Seems to apply quite well.

    – Felix
    4 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
    4 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
    3 hours ago
















2















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.










share|improve this question









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
    4 hours ago











  • @WesH Thanks, for the link. I'll investigate. Seems to apply quite well.

    – Felix
    4 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
    4 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
    3 hours ago














2












2








2








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.










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








edited 4 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 4 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
    4 hours ago











  • @WesH Thanks, for the link. I'll investigate. Seems to apply quite well.

    – Felix
    4 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
    4 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
    3 hours ago



















  • Have you seen this question? It addresses a method for parsing a simple string for matches.

    – WesH
    4 hours ago











  • @WesH Thanks, for the link. I'll investigate. Seems to apply quite well.

    – Felix
    4 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
    4 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
    3 hours ago

















Have you seen this question? It addresses a method for parsing a simple string for matches.

– WesH
4 hours ago





Have you seen this question? It addresses a method for parsing a simple string for matches.

– WesH
4 hours ago













@WesH Thanks, for the link. I'll investigate. Seems to apply quite well.

– Felix
4 hours ago





@WesH Thanks, for the link. I'll investigate. Seems to apply quite well.

– Felix
4 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
4 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
4 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
3 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
3 hours ago










2 Answers
2






active

oldest

votes


















3














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.






share|improve this answer


























  • Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?

    – Felix
    2 hours ago











  • @Felix I've added a general constructor in my answer.

    – zetaeffe
    1 hour ago











  • Man. You've made my next few months a lot easier <3

    – Felix
    1 hour ago



















1














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}


enter image description here






share|improve this answer























    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.










    draft saved

    draft discarded


















    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









    3














    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.






    share|improve this answer


























    • Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?

      – Felix
      2 hours ago











    • @Felix I've added a general constructor in my answer.

      – zetaeffe
      1 hour ago











    • Man. You've made my next few months a lot easier <3

      – Felix
      1 hour ago
















    3














    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.






    share|improve this answer


























    • Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?

      – Felix
      2 hours ago











    • @Felix I've added a general constructor in my answer.

      – zetaeffe
      1 hour ago











    • Man. You've made my next few months a lot easier <3

      – Felix
      1 hour ago














    3












    3








    3







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 21 mins ago









    Felix

    1175




    1175










    answered 3 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
      2 hours ago











    • @Felix I've added a general constructor in my answer.

      – zetaeffe
      1 hour ago











    • Man. You've made my next few months a lot easier <3

      – Felix
      1 hour ago



















    • Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?

      – Felix
      2 hours ago











    • @Felix I've added a general constructor in my answer.

      – zetaeffe
      1 hour ago











    • Man. You've made my next few months a lot easier <3

      – Felix
      1 hour ago

















    Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?

    – Felix
    2 hours ago





    Is there an equally simple way of defining the macro "factory" so to speak, as in the example in my question?

    – Felix
    2 hours ago













    @Felix I've added a general constructor in my answer.

    – zetaeffe
    1 hour ago





    @Felix I've added a general constructor in my answer.

    – zetaeffe
    1 hour ago













    Man. You've made my next few months a lot easier <3

    – Felix
    1 hour ago





    Man. You've made my next few months a lot easier <3

    – Felix
    1 hour ago











    1














    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}


    enter image description here






    share|improve this answer




























      1














      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}


      enter image description here






      share|improve this answer


























        1












        1








        1







        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}


        enter image description here






        share|improve this answer













        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}


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 35 mins ago









        egregegreg

        723k8719163219




        723k8719163219






















            Felix is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

            How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

            Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...