我希望我理解得expand
正确,如果没有,请随时纠正。
我正在为简历编写一个类。在其中,我有一个自定义环境,它接受将用作标题的强制参数。由于我需要多种语言的简历,所以我决定包含该translations
包。现在在我的类中,我想为翻译后的字符串设置样式并输出结果,但不幸的是,这不起作用。我想这是因为我的样式是针对关键字\GetTranslation{foo}
后面的字符串而不是翻译后的字符串进行的foo
。
那么我该如何告诉我的函数首先评估翻译,然后将样式应用到翻译的字符串?
平均能量损失
\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}
还有红色这理解代码。
答案1
您可以expl3
不做任何改变地使用该实现。
\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}