I am creating a CV under Latex (based on the altacv template but it does not have such importance) and in the preamble of my .tex document, I am trying to find a solution to be able to quickly test a configuration of color shades.

Since I discovered the LaTeX3 interface and its functions, I tried to used the \str_case:nn function to realize a kind of switch case.

But I am not getting the desired result and after compilation, the default colors from the class are used and not the new ones I wished from my configuration in the .tex document.

I fear that it is because I'm trying to use it in the preamble without really "calling it" and not after my \begin{document}, am I right ? But I don't really see how to realize such call.

Here, is my code. As you can see, I made some variables in order to test if a case was really considered during compilation. Also, I am not sure if the commands used for my grey hue color selection are correct. I am using the xcolor package.

% Available options
    \str_const:Nn \l_greyColorHue_str {greyHue}
    \str_const:Nn \l_blueColorHue_str {blueHue}
    % To modify to choose the switch-case option
    \str_set_eq:NN \l_colorHueSelection_str \l_greyColorHue_str
    % String to verify if the switch case worked
    \str_new:N \l_effectiveColorHue_str
    %% Color fields definition
    \str_case:nn {\l_colorHueSelection_str}
        {\l_greyColorHue_str} {
            \colorlet{name}{\color[Gray]{15}} % Used for : \name
            \colorlet{tagline}{\color[Gray]{13}} % Used for : \tagline
            \colorlet{heading}{\color[Gray]{13}} % Used for : \cvsection
            \colorlet{headingrule}{\color[Gray]{15}} % Used for : \rule
            \colorlet{subheading}{\color[Gray]{13}} % Used for : \cvsubsection, \cvsubsubsection
            \colorlet{accent}{\color[Gray]{10}} % Used for : \cvref(#1), \cvskill(#1), \skillfive, \cvachievment(#1), \cvevent(#1), \quote, \printinfo(#1)
            \colorlet{emphasis}{\color[Gray]{9}} % Used for : \cvref(#1), \cvskill(#1) \cvachievment(#2), \cvevent(#1)
            \colorlet{body}{\color[Gray]{15}} % Used for : all text color
            \str_set_eq:NN \l_effectiveColorHue_str \l_greyColorHue_str
        {\l_blueColorHue_str} {
            \colorlet{name}{black} % Used for : \name
            \colorlet{tagline}{RoyalBlue} % Used for : \tagline
            \colorlet{heading}{RoyalBlue} % Used for : \cvsection
            \colorlet{headingrule}{black} % Used for : \rule
            \colorlet{subheading}{Navy} % Used for : \cvsubsection, \cvsubsubsection
            \colorlet{accent}{MediumBlue} % Used for : \cvref(#2), \cvskill(#2), \skillfive, \cvachievment(#1), \cvevent(#2), \quote, \printinfo(#1)
            \colorlet{emphasis}{MidnightBlue} % Used for : \cvref(#1), \cvskill(#1) \cvachievment(#2), \cvevent(#1)
            \colorlet{body}{black} % Used for : all text color

            \str_set_eq:NN \l_effectiveColorHue_str \l_blueColorHue_str

Then, I begin my document and return my variable \l_effectiveColorHue_str to see its content in the generated document.




    Selected~coloration~: \\
    (~ \str_use:N \l_effectiveColorHue_str ~)

Do you have any idea to make this work or even another solution with "classic" Latex code in order that I can easily switch between different shades of colors for test without overwritting my commands or commenting/Uncommenting my colorlet selections ?

  • 1
    Try \str_case_e:nn instead. As it is, it compares the string \l_colorHueSelection_str with \l_greyColorHue_str, rather than the value of these variables – Phelype Oleinik Mar 3 at 19:05
  • Oh godness, it works ! I am so ashamed I didn't read just a little further in the interface3 documentation to see it... Thank you so much ! – impishwhite Mar 3 at 19:18
  • No reason to be ashamed at all! We're all here to learn. (also interface3 is huge :) I will write an answer with a bit more detail for you – Phelype Oleinik Mar 3 at 20:09

Functions in the str module take their arguments as strings always, so

\str_case:nn { \l_colorHueSelection_str }
    { \l_greyColorHue_str } { ... }
    { \l_blueColorHue_str } { ... }

compares the strings \ l _ c o l o r H u e S e l e c t i o n _ s t r (24 characters) and \ l _ g r e y C o l o r H u e _ s t r (19 characters) rather than the contents of the str variables \l_colorHueSelection_str and \l_greyColorHue_str. The strings are obviously different, but their contents might not be, which is what you want to compare.

To compare the contents, you can use \str_case_e:nn. The e stands for expanded, which means it will first expand the strings being compared, and then compare them.

A few nitpicks on your coding style:

  • You do \str_const:Nn \l_greyColorHue_str {greyHue}, but the name \l_greyColorHue_str indicates a local variable. Constants should be named \c_....

  • More generally, variables in expl3 should be named \<scope>_<module>_<name>_<type>, where:

    • <scope> is either local, global or constant;
    • <module> is like a name space (for example the name of your package);
    • <name> describes the variable. This part may contain one or more _ for clarity;
    • <type> is the type of variable, for example tl or str or int...

    so your variable names would be better as \c_impish_grey_color_hue_str and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.