1

I hope that I did understand expand correctly, if not, feel free to rectify.

I'm coding a class for a resume. In there, I have a custom environment accepting a mandatory argument that will be used as the title. As I need my resume in several languages I decided to include the translations package. Now in my class, I want to style the translated string and output the result, unfortunately, this does not work. I suppose that this is due to the fact, that my styling is done to the \GetTranslation{foo} instead of the translated string behind the foo keyword.

So how can I tell my function to first evaluate the translation and then apply the styles to the translated string?


MWE

\documentclass[ngerman]{article}



% %%%%%%%%%%%%%%%%
% Require Packages
% %%%%%%%%%%%%%%%%

\usepackage{%
    xcolor,%
    environ,%
    babel,%
    translations,%
}



% %%%%%%%%%%%%%%%
% Define Commands
% %%%%%%%%%%%%%%%

% Define the listcolor list
\newcounter{listcolor}
\makeatletter
    \newcommand{\addlistcolor}[1]{%
        \stepcounter{listcolor}%
        \@namedef{titlecolor@\thelistcolor}{#1}%
    }
\makeatother

% Choose the title color
\newcounter{titlecolor}
\newcommand{\colortitle}[3]{%
    \expandafter\textcolor\expandafter{\csname titlecolor@\thetitlecolor\endcsname}{#1#2#3}%
}



% %%%%%%%%%%%%%%%%%%%
% Define environments
% %%%%%%%%%%%%%%%%%%%

% Define the entrylist environment
\NewEnviron{entrylist}[1]{%
    \stepcounter{titlecolor}%
    \ifnum\value{titlecolor}>\value{listcolor}
        \setcounter{titlecolor}{1}%
    \fi
    \par\addvspace{1em}
    \begingroup
        \LARGE\bfseries\MakeLowercase{\expandafter\colortitle #1\relax}
        \par\nobreak
    \endgroup
    \BODY
}



% %%%%%%%%%%%%%%%%%
% Declare Variables
% %%%%%%%%%%%%%%%%%

% List colors
\addlistcolor{red}
\addlistcolor{green}
\addlistcolor{blue}
\addlistcolor{yellow}

% Translations
\DeclareTranslationFallback{test}{Test}
\DeclareTranslation{English}{test}{Test}
\DeclareTranslation{German}{test}{Test}



\begin{document}

\begin{entrylist}{\GetTranslation{test} 1}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}

\begin{entrylist}{\GetTranslation{test} 2}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}

\begin{entrylist}{\GetTranslation{test} 3}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}

\begin{entrylist}{\GetTranslation{test} 4}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}

\begin{entrylist}{\GetTranslation{test} 5}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}

\end{document}

Also red this to understand the code.

  • I see no use of the translations package in the MWE; it's just loaded, but none of its features is actually used. Please, remove the addition from the other question and move it to this one, showing where you would like to use \GetTranslation. – egreg Nov 22 '17 at 13:42
  • @egreg Whoops, I forgot to add the GetTranslation. Sorry about that. I added the MWE now. – Sam Nov 22 '17 at 13:44
2

You can use the expl3 implementation with no change at all.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,ngerman]{babel}

\usepackage{
  xcolor,
  environ,
  translations,
  xparse,
}

\ExplSyntaxOn

% entry list
\NewDocumentEnvironment{entrylist}{m}
 {
  % space the entries
  \par\addvspace{\topsep}
  % open a group to confine the \LARGE font
  \group_begin:
  \LARGE\bfseries\noindent
  % color the first three letters after lowercasing
  \sam_entries_color_argument:n { #1 }
  % end the line and allow no page break
  \par\nobreak
  % end the group
  \group_end:
  % typeset the entry
  \noindent\ignorespaces
 }
 {
  % end the paragraph and add the space
  \par\addvspace{\topsep}
  % increment the counter
  \int_gincr:N \g_sam_entries_color_int
 }

% two needed variables
\int_new:N \g_sam_entries_color_int
\tl_new:N \l__sam_entries_title_tl

% the main command
\cs_new_protected:Nn \sam_entries_color_argument:n
 {
  % lowercase the argument
  % the f-expansion means that \tl_lower_case:n is performed
  % and also the argument is expanded up to the first nonexpandable
  % token; if \GetTranslation appears first it will be expanded
  % correctly
  \tl_set:Nf \l__sam_entries_title_tl { \tl_lower_case:n { #1 } }
  % replace spaces with a macro for counting right
  % the counting can go wrong if there are spaces,
  % so we change explicit spaces into \c_space_tl
  \tl_replace_all:Nnn \l__sam_entries_title_tl { ~ } { \c_space_tl }
  \textcolor
   {% pick the color from the sequence, dividing modulo the number of colors
    \seq_item:Nn \g_sam_entries_colors_seq
     {
      \int_mod:nn { \g_sam_entries_color_int } { \__sam_entries_colors: } + 1
     }
   }
   {% just the first three items are colored
    % \tl_range:nnn { tokens } { 1 } { 3 }
    % returns the first three items; we use the V variant
    % so the tokens come from the variable we set up
    \tl_range:Vnn \l__sam_entries_title_tl { 1 } { 3 }
   }
   % the rest of the argument
   \tl_range:Vnn \l__sam_entries_title_tl { 4 } { \tl_count:V \l__sam_entries_title_tl }
 }
\cs_generate_variant:Nn \tl_range:nnn { V }

% color list
% add to the sequence holding the colors, see the example calls
% for the syntax
\NewDocumentCommand{\addtocolorlist}{m}
 {
  \clist_map_inline:nn { #1 } { \seq_gput_right:Nn \g_sam_entries_colors_seq { ##1 } }
 }
\seq_new:N \g_sam_entries_colors_seq
\cs_new:Nn \__sam_entries_colors:
 {
  \seq_count:N \g_sam_entries_colors_seq
 }

\ExplSyntaxOff

\addtocolorlist{red,green,blue}
\addtocolorlist{yellow}


% Translations
\DeclareTranslationFallback{test}{Test}
\DeclareTranslation{English}{test}{TestEnglish}
\DeclareTranslation{German}{test}{TestGermän}



\begin{document}

\begin{entrylist}{\GetTranslation{test} 1}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}

\begin{entrylist}{\GetTranslation{test} 2}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}

\begin{entrylist}{\GetTranslation{test} 3}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}

\begin{otherlanguage*}{english}
\begin{entrylist}{\GetTranslation{test} 4}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}
\end{otherlanguage*}

\begin{entrylist}{\GetTranslation{test} 5}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{entrylist}

\end{document}

enter image description here

  • Okay, well, as I don't like to add code to my class that I don't understand, is there a good resource that explains the expl3 "language"? Or would you be so nice to add some comments that explain what is going on? I find the syntax really hard to read and understand. :-( – Sam Nov 22 '17 at 13:54
  • @Sam I added some comments. Please, specify whether \GetTranslation is expected to be the first item in the argument. – egreg Nov 22 '17 at 14:05
  • Thank you very much, this helps a lot. It's still a bit hard because of the function names of expl3 that do not explain very much. In fact, in my resume, the \GetTranslation is the only item in the argument passed. I only added the numbers here. – Sam Nov 22 '17 at 14:09

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.