I need help with these macros. What I'm trying to achieve is to loop over all the letters in the title of a custom environment. Then colorize the first three letters of that title with a different color each. The colors used should be stored in a list.
So for every call of the environment, I'm increasing a counter that counts the number of environments, I then call a function that fetches (or rather should fetch) the correct color.
That function should now iterate through the list of colors and return the color that has the same key as the environment: For the first environment, the first color should be used, for the second environment, the second one and so on...
A feature that is not implemented yet in my (not working) code is to restart with the first color of the list if there are more environments than colors.
There are also some bugs in this:
- The color is only returned for the first environment
- The letter loop deletes my spaces
If you have any idea how to solve one of these problems, I'll be very glad for your answer. This also applies if you think this should be handled in a different way.
M(N)WE
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox, xcolor, environ, parskip}
% Title Color List
\listadd{\titleColors}{red}
\listadd{\titleColors}{green}
\listadd{\titleColors}{blue}
\listadd{\titleColors}{yellow}
% Colorize the first three letters
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{letterCounter}%
\makeatletter
\newcommand{\titleColorize}[2]{%
\setcounter{letterCounter}{1}%
\@tfor\next:=#1\do{%
\ifnumcomp{\the\value{letterCounter}}{<}{4}%
{\textcolor{#2}{\next}}%
{\next}%
\refstepcounter{letterCounter}%
}%
}%
\makeatother
% Get the corresponding color
% %%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{colourCounter}
\setcounter{colourCounter}{1}
\newcommand{\getTitleColor}[1]%
{%
\renewcommand*{\do}[1]%
{%
\ifnumcomp{\thecolourCounter}{=}{#1}%
{%
##1%
}{}%
\refstepcounter{colourCounter}%
}%
\dolistloop{\titleColors}%
}
% Define the environment
% %%%%%%%%%%%%%%%%%%%%%%
\newcounter{titleCounter}
\setcounter{titleCounter}{1}
\NewEnviron{entrylist}[1]{
\vskip 1em
{%
\LARGE\bfseries\lowercase{%
\titleColorize{#1}{red}
\getTitleColor{\thetitleCounter} % Returns the correct color
% \titleColorize{#1}{\getTitleColor{\thetitleCounter}}
}%
}\par
\refstepcounter{titleCounter}
\BODY
}
\begin{document}
\begin{entrylist}{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}{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}{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}{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}{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}
Edit
The answers provided by Werner
and egreg
are both fully functional, however, due to the very complex syntax of the expl3
"language", I prefer Werners approach.
After implementing the code provided by the latter into my project, I realized the need of translations in my project. So I imported the translations
package and defined translations for all the strings in the document. Also for the titles of my custom entrylist
environment which were subject of this question. After replacing the strings by a \GetTranslation{}
command, I recompiled the document and noticed that the output does not show the correct result, even if LaTeX does not throw an error message. As this issue is clearly caused by the code, looping through the string passed and setting a color to the first three items, I think that it is better to keep this in the same thread instead of opening a new one. If anyone knows what causes the false output and how this could be fixed, feel free to answer. At best this should work with the more comprehensible code provided by Werner
.
Here is what LaTeX outputs: Babel on ngerman
and \GetTranslation{interests}
results in the output
terestsngerman
If selecting english
as language, it changes to
terestsenglish
So apparently the \GetTranslation
command is not executed at all as terests
is clearly the key of the translatable string (except the two first letters).
MWE by Werner with added translations
\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{} 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}